GOAL
The goal of this project is to create a functional line-following robot that can follow 6 different tracks. This robot must be able to follow those paths as quickly and (more importantly) consistently in order to win against other robots and the house robot in a competitive environment.
Our planning mainly consisted of a previous assignment we had before we started on this project. In the “GearBots completion and algorithm visualization” assignment, we were required to figure out the overall concept of a line following-robot. We had to code a double-sensor robot to follow a black line in the given simulation.
When the both sensors see white, the robot moves forward. This repeats until either the left or right sensor sees black. When the left sensor sees black, the right wheel goes forward and the front wheel goes backward to turn left, and the opposite goes when the right sensor sees black. We planned to use this logic for our line-following robot.
We were also tasked to make a flowchart that described this procedure, and this proved helpful to the current project at hand, as it uses the same logic that is used in the simulation.
import FA
fa = FA.Create()
fa.ComOpen(5)
fa.LCDClear()
fa.LCDBacklight(100)
fa.LCDPrint(0, 0, " Left Right ")
speed = 10
black = 0
white = 50
turns = ["forward"]
fa.LCDClear()
def turnRight():
fa.LCDPrint(0, 0, "left " + str(left) + " right " + str(right))
fa.SetMotors(0, 0)
turns.append("right")
fa.Right(5)
fa.LCDPrint(20, 20, "right")
def turnLeft():
fa.LCDPrint(0, 0, "left " + str(left) + " right " + str(right))
fa.SetMotors(0, 0)
turns.append("left")
fa.Left(5)
fa.LCDPrint(20, 20, "left")
while True:
fa.LCDClear()
count = 0
left = fa.ReadLine(0)
right = fa.ReadLine(1)
fa.LCDPrint(0, 0, "left " + str(left) + " right " + str(right))
if(left >= white and right < white):
fa.LCDPrint(20, 20, "left is white")
while left >= white and right < white:
turnRight()
left = fa.ReadLine(0)
right = fa.ReadLine(1)
fa.Forwards(10)
elif(left < white and right >= white):
fa.LCDPrint(20, 20, "right is white")
while right >= white and left < white:
turnLeft()
left = fa.ReadLine(0)
right = fa.ReadLine(1)
fa.Forwards(10)
elif(left >= white and right >= white):
fa.LCDPrint(20, 20, "both are white")
count = 0
while right >= white and count <= 18:
turnRight()
left = fa.ReadLine(0)
right = fa.ReadLine(1)
count += 1
if count > 18:
fa.Left(90)
count = 0
while left >= white and count <= 18:
turnLeft()
left = fa.ReadLine(0)
right = fa.ReadLine(1)
count += 1
if count > 18:
count = 0
fa.Right(90)
fa.Backwards(50)
elif (left < white and right < white):
fa.SetMotors(speed, speed)
else:
fa.Backwards(10)
The code shown above is used for our line-following robot. The two functions of turning left and turning right are initially defined. The code takes input from the two sensors and uses the pre-coded values of 0 and 100 to determine whether each sensor is looking at white or black. If the left sensor is looking at white and the right sensor is looking at black, then the code performs the function to turn right, and if the left sensor is looking at black and the right sensor is looking at white, then the code performs the function to turn left. If both sensors are looking at white, the robot stops, and then runs up to 18 iterations of 5 degree turns until one of the sensors sees black again. If 18 iterations are ran and neither sensors see black, the robot turns around and tries again. Finally, if none of these conditions are met, the robot moves forward. All of this code is put in a while loop to run the code forever (or at least until the robot reaches the end of the track).