Homework4: Rainbow Road




Goal:
Design and build a robot that can follow one or more colored lines using a camera and edge detection.
Solution:
I worked with Salvatore Novak to design a robot that uses one camera to follow a line using edge detection and path memorization/backtracking.
We used the same chassis for this robot that we used for our Homework 3 robot -- we just removed the color sensors and all of the related wiring and taped the pi to the front of the robot.
Before attaching the pi, we ran the base code provided by Professor Briana Bouchard and moved the camera around to find the optimal height at which the camera could perform the edge detection. Fortunately, that height was right around the height of the robot chassis so we were able to attach the camera directly to the top of the chassis without having to make any modifications to it.
The base code provided already did the edge detection using a series of filters and calculations:
- Gaussian blur
- Color thresholding
- Find the contours of the frame
- Find the biggest contour (if detected) -- this tells us where the line is
- Calculate the "center of mass" of the part of the line that the camera is seeing and use that data to determine whether to go left, right, or straight
We added code to turn the robot in the appropriate direction and voila...
In order to add an additional challenge, we implemented code that allows the robot to backtrack if it loses the line. The program keeps a list of the moves that it has performed, appending new moves to the front of the list. If the robot loses the line, it pops the command at the front of the list (the most recent move) and performs the opposite of that move, backtracking one move. The robot will continue to backtrack until it sees the line again.
One issue that we ran into when first implementing this algorithm was that the robot would suddenly backtrack when it still appeared to be on the line. We debugged by adding print statements and looking at the output of the code -- the issue was that in normal operation the robot every once in a while when it was right on the edge of the line would momentarily sense that it had lost the line and then see the line again and continue on its way. In order to avoid backtracking during these moments of indecision, we added a counter variable that begins to count when the robot goes off of the line. If the robot remains off of the line for 50 counts without moving off of the line then it is definitely off of the line and we need to backtrack. It worked!! :)