This Code is for line following using Analog Distance Sensors (ADS)
What You Will Need To Run:
How the sensor works:
How to mount on the robot:
How to use the sensor:
The Code:
The Code: (This Code does not have the self correction yet)
#Important Importing
import RoboPiLib as RPL
import setup
import multiprocessing
import time
#Start of Commands
while 1:
#Setting A and B values from ADS
A = ((500 * RPL.analogRead(0))/1024)
B = ((500 * RPL.analogRead(1))/1024)
#Taking the True and False values recieved and giving the motor commands based on the voltage recieved from the sensor
#GOES FORWARD
if(A>=250 and B>=250): #go forward
print "IT GOES STRAIGHT!"
RPL.servoWrite(0,2000)
RPL.servoWrite(1,1000)
#GOES RIGHT
elif(A>=250 and B<=249): #turn right
print "IT TURNS RIGHT!"
RPL.servoWrite(0,0)
RPL.servoWrite(1,1000)
#GOES LEFT
elif(A<=249 and B>=250): #turn left
print "IT TURNS LEFT!"
RPL.servoWrite(0,2000)
RPL.servoWrite(1,0)
#STAYS STILL
else: #stay still
print "IT CAN STAND STILL!"
RPL.servoWrite(0,0)
RPL.servoWrite(1,0)
This is an Analog Distance Sensor (ADS)
Facing toward the front means that the cord will be going towards the back of the robot.
Graph of How The Sensor Works
Theory Code: (Self Correcting Code that needs fixing)
#Important Importing
import RoboPiLib as RPL
import setup
import multiprocessing
import time
import sys, tty, termios, signal
lastSighting = ""
readloop = 0
#Creating shortcuts for the commands
def turnRight():
RPL.servoWrite(0,0)
RPL.servoWrite(1,1000)
lastSighting = "R"
readloop = 0
def turnLeft():
RPL.servoWrite(0,2000)
RPL.servoWrite(1,0)
lastSighting = "L"
readloop = 0
def goForward():
RPL.servoWrite(0,2000)
RPL.servoWrite(1,1000)
def stop():
RPL.servoWrite(0,0)
RPL.servoWrite(1,0)
def jiggle():
turnRight()
time.sleep(.5)
turnLeft()
time.sleep(.5)
#Start of Commands
while 1:
#Setting A and B values from ADS
A = ((500 * RPL.analogRead(0))/1024)
B = ((500 * RPL.analogRead(1))/1024)
#Taking the True and False values recieved and giving the motor commands based on the voltage recieved from the sensor
#FORWARD
if(A>=250 and B>=250): #go forward
print "IT GOES STRAIGHT!"
goForward()
#Self Correction every 100 commands it recieves without turning
readloop = readloop + 1
if(readloop == 100):
if(lastSighting == "R"):
print "GO BACK RIGHT!"
turnRight()
elif(lastSighting == "L"):
print "GO BACK LEFT!"
turnLeft()
else:
goForward()
if(readloop > 50):
readloop = 0
#RIGHT
elif(A>=250 and B<=249): #turn right
print "IT TURNS RIGHT!"
turnRight()
#LEFT
elif(A<=249 and B>=250): #turn left
print "IT TURNS LEFT!"
turnLeft
#STILL
else: #stay still
print "IT CAN STAND STILL!"
stop()
time.sleep(2)
jiggle()
What Needs Work: