I spent today getting the code sorted to make the robot follow a line.
The actual code was a lot longer originally but it's nice and compact now.
I'm not sure at what point a piece of code becomes your own; so far I have read through other peoples code, cherry picked the bits I like and then changed them to suit my application. Apart from naming variables/commands etc and choice of GPIO pins there must be a finite amount of ways to control a line following robot using Python and I guess even if I think this piece of code is unique, it probably isn't.
Anyway, I have spent considerable time on it and it works so whether its truly mine or not I take pride in it.
Update: I put a line of insulation tape on the kitchen floor for the robot to follow and it didnt seem to pick it up as well as when I had the robot on the desk in my office. The floor is more reflective in the kitchen and the robot seemed to spend a lot of time with 3 lights on and getting confused. In addition to this I accidentally dropped a screwdriver onto some of the contacts on the line sensor; there was smoke and a smell of burning. It still seems to work ok...
I think I am going to do another test run but without the section of code where it has an action to take if 2 sensors pick up the line. And i'll have to do that another day as Jane has just got home and is wondering why there is a line all around the kitchen.
Here's todays code:
import RPi.GPIO as GPIO, sys, time
#use physical pin numbering
GPIO.setmode(GPIO.BOARD)
#pins: 7:left rev, 11:left for, 13:right for, 15:right rev.
GPIO.setup(7, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
def Stop():
GPIO.output(7, GPIO.LOW)
GPIO.output(11, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(15, GPIO.LOW)
def Forward():
GPIO.output(7, GPIO.LOW)
GPIO.output(11, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(15, GPIO.LOW)
def Reverse():
GPIO.output(7, GPIO.HIGH)
GPIO.output(11, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(15, GPIO.HIGH)
def Left():
GPIO.output(7, GPIO.LOW)
GPIO.output(11, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(15, GPIO.LOW)
def Right():
GPIO.output(7, GPIO.LOW)
GPIO.output(11, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(15, GPIO.LOW)
def Hardleft():
GPIO.output(7, GPIO.HIGH)
GPIO.output(11, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(15, GPIO.LOW)
def Hardright():
GPIO.output(7, GPIO.LOW)
GPIO.output(11, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(15, GPIO.HIGH)
#speed is 0.2 m/s
#1 second hardleft or hardright = 45 degrees
#left/right is slight change in direction
#line follower
#IRleft = 16
#IRcentre = 18
#IRright = 22
GPIO.setup(16, GPIO.IN)
GPIO.setup(18, GPIO.IN)
GPIO.setup(22, GPIO.IN)
try:
Stop()
raw_input("Say hello to my little friend.")
while True:
lineleft = GPIO.input(16)
lineright = GPIO.input(22)
linecentre = GPIO.input(18)
#if it picks up a line the input is 0
if linecentre == 0:
if lineleft == 0:
Left()
elif lineright == 0:
Right()
else:
Forward()
else:
if lineleft == 0:
Hardleft()
elif lineright == 0:
Hardright()
else: Stop()
except KeyboardInterrupt:
Stop()
GPIO.cleanup()