Moving the car and having some fun with the lights.

2013-10-05 14.50.35Moving the car and having fun with the lights. 

The first part of the code is just setting up the GPIO and referencing the pins to make life a bit easier. After that we created a couple of function . Function allowed us to add features as we went along.

Function names and what they do. 

all_off  – This function sets all pins except the brake lights. This gives the effect of the parking lights being on when the car is at a stop.

cleanup – Just a standard GPIO cleanup.

BlinkRight / BlinkLeft- Uses a for loop to flash the yellow LED giving the impression of car indicators. It takes 2 variables which are set by the function fleft and fright . In my version it is set to blink 6 times . 1 blink every 0.5 seconds.

forward – Moves the car forward for 1 seconds . The time can be change by setting the value in the sleep function on line 88 . So sleep(5.0) would be forward for 5 seconds. This functions also turns the parking lights off.

backwards – It is the same as forwards but we go backwards. The time can be changed by changing line 98 . This function also turns off the parking lights and turns on the reverse lights too.

fright / fleft – This function moves the car forward and turns the steering in which ever direction you have chosen. It is one the most complex functions for this project. In it we have the car going forward , turn left/right , turn the parking led`s off and calling the BlinkRight / BlinkLeft function aswell.

allLights – this function was designed as a bit of a diagnoses tool but I kept it as it was fun.It flashes all the LED`s on and off 20 times.

The running code.

From line 147 we have the code the allows us to use the functions. First we do create a couple of variables.

Intro – Is just a quick message welcoming to the program and setting out the keys you can use and what they do.

running – This is a Boolean . It is set to True and allows the while loop on line 165 to run.

Now the fun bit 20 lines of code and endless amounts of fun.

First we Print the Intro variable and our while loop starts up because running == True . It sets the parking brakes to on and the car is ready. key is a raw input that asks the user which direction they want to go. I have used an if statement however a switch would work just as well and would have the benefit of being expand more easily than an if statement.

The options we have are :-

  • f    –    calls the forward function.
  • b   –    calls the backwards function.
  • fr  –    calls the fright function.
  • fl   –    calls the fleft function.
  • l    –    calls the allLights function . The time of flashes and frequency can be set here also. We have set to 20 times and the frequency every 0.5 second.
  • q    –   changes the Boolean value to False and kills the while loop.

After that a nice message is printed and GPIO.cleanup does it`s job.

#!/usr/bin/env python
# Pi car python code by julian and kyle milligan
# version 1.0 beta
""" Must be run as root - sudo python car.py """

import time, datetime, RPi.GPIO as GPIO , os

#referance name and gpio pin number
FORWARD = 11
BACK = 12
LEFT = 13
RIGHT = 15
BLED1 = 8
BLED2 = 10
SRLED1 = 3
SRLED2 = 5
SLLED1 = 19
SLLED2 = 21
BRAKE1 = 16
BRAKE2 = 18
HLED1  = 22
HLED2  = 24

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#Remember to setup the GPIO pin as input or output
GPIO.setup(FORWARD, GPIO.OUT)
GPIO.setup(BACK, GPIO.OUT)
GPIO.setup(LEFT, GPIO.OUT)
GPIO.setup(RIGHT, GPIO.OUT)
GPIO.setup(BLED1, GPIO.OUT)
GPIO.setup(BLED2, GPIO.OUT)
GPIO.setup(SRLED1, GPIO.OUT)
GPIO.setup(SRLED2, GPIO.OUT)
GPIO.setup(SLLED1, GPIO.OUT)
GPIO.setup(SLLED2, GPIO.OUT)
GPIO.setup(BRAKE1, GPIO.OUT)
GPIO.setup(BRAKE2, GPIO.OUT)
GPIO.setup(HLED1, GPIO.OUT)
GPIO.setup(HLED2, GPIO.OUT)

GPIO.setwarnings(False)
#functions

def all_off(): #turns all of the pins off - always call at the end to save battery
    GPIO.output(FORWARD,0)
    GPIO.output(BACK,0)
    GPIO.output(LEFT,0)
    GPIO.output(RIGHT,0)
    GPIO.output (BLED1,0)
    GPIO.output (BLED2,0)
    GPIO.output(SRLED1,0)
    GPIO.output(SRLED2,0)
    GPIO.output(BRAKE1,1)
    GPIO.output(BRAKE2,1)

def cleanup(): #cleans up the GPIO pins
    GPIO.cleanup()

def sleep(sleepTime=1):
    time.sleep(sleepTime)

def BlinkRight(numTimes,speed):
    for i in range(0,numTimes):## Run loop numTimes
        GPIO.output(SRLED1,1)
        GPIO.output(SRLED2,1)
        time.sleep(speed)
        GPIO.output(SRLED1,0)
        GPIO.output(SRLED2,0)
        time.sleep(speed)
    all_off()

def BlinkLeft(numTimes,speed):
    for i in range(0,numTimes):## Run loop numTimes
        GPIO.output(SLLED1,1)
        GPIO.output(SLLED2,1)
        time.sleep(speed)
        GPIO.output(SLLED1,0)
        GPIO.output(SLLED2,0)
        time.sleep(speed)
    all_off()

def forward():
    print("Going forward")
    GPIO.output(BRAKE1,0)
    GPIO.output(BRAKE2,0)
    GPIO.output(FORWARD,1)
    sleep(1.0)
    all_off()

def backwards():
    print ("Going backwards")
    GPIO.output(BRAKE1,0)
    GPIO.output(BRAKE2,0)
    GPIO.output (BACK,1)
    GPIO.output (BLED1,1)
    GPIO.output (BLED2,1)
    sleep(1.0)
    all_off()

def fright():
    print ("Going forward and turning right")
    GPIO.output(BRAKE1,0)
    GPIO.output(BRAKE2,0)
    GPIO.output(FORWARD,1)
    GPIO.output(RIGHT,1)
    BlinkRight(6,0.5)
    sleep(2.0)
    all_off()

def fleft():
    print ("Going forward and turning left")
    GPIO.output(BRAKE1,0)
    GPIO.output(BRAKE2,0)
    GPIO.output(FORWARD,1)
    GPIO.output(LEFT,1)
    BlinkLeft(6,0.5)
    sleep(2.0)
    all_off()

def allLights (numTimes,speed):
    for i in range(0,numTimes):## Run loop numTimes
        GPIO.output(SLLED1,1)
        GPIO.output(SLLED2,1)
        GPIO.output(SRLED1,1)
        GPIO.output(SRLED2,1)
        GPIO.output(BLED1,1)
        GPIO.output(BLED2,1)
        GPIO.output(BRAKE1,0)
        GPIO.output(BRAKE2,0)
        GPIO.output(HLED1,0)
        GPIO.output(HLED2,0)
        time.sleep(speed)
        GPIO.output(SLLED1,0)
        GPIO.output(SLLED2,0)
        GPIO.output(SRLED1,0)
        GPIO.output(SRLED2,0)
        GPIO.output(BLED1,0)
        GPIO.output(BLED2,0)
        GPIO.output(BRAKE1,1)
        GPIO.output(BRAKE2,1)
        GPIO.output(HLED1,1)
        GPIO.output(HLED2,1)
        time.sleep(speed)
    all_off()

#varibles

Intro = "Welcome to Julian and Kyle`s Pi Car program\n\
Please use the following commands to move the car\n\
f = forwards\n\
b = backwards\n\
fr = forwards and turn right\n\
fl = forwards and turn left\n\
br = backwards and turn right\n\
bl = backwards and turn left\n\
l = all ligths on\n\
q = quit\n\
\n\
enjoy."

running = (True)
print (Intro)

while (running == True):

    GPIO.output(BRAKE1,1)
    GPIO.output(BRAKE2,1)
    key = raw_input("which way - ")

    if (key == "f"):
         forward()
    elif (key =="b"):
         backwards()
    elif (key =="fr"):
         fright()
    elif (key =="fl"):
         fleft()
    elif (key =="l"):
         allLights(20,0.5)
    elif (key =="q"):
         running = (False)

print (" Hope you had fun ")
GPIO.cleanup()

  
Tagged , . Bookmark the permalink.

Comments are closed.