Light Sensors, Part 3
Improved Line Following

Improved Line Following

In the previous light sensor lesson we studied different approaches to following a line, all using a single light sensor. In this lesson we're going to see how adding an additional light sensor to our robot can improve its ability to follow a line.

Why two light sensors are better than one

So why are two light sensors better than one when it comes to following a line? The short answer is that using two light sensors gives you more information to work with so you can be more precise in telling your robot what to do. The reason you get more information with two light sensors is similar to why two eyes work better than one - you can examine and evaluate more of your environment at one time.

There are several different techniques you can use when writing a program that uses two light sensors. First, depending on the width of the line you want to follow you can position the light sensors close together and write your program to keep both light sensors on the line. Second, you can position the light sensors farther apart - again, the spacing depends on the width of the line you want to follow - and straddle the line. With this technique you would want to keep both of the light sensors off the line. Yet another approach, similar to the single light sensor design presented in the previous lesson, is to follow an edge of the line by positioning the light sensors so that one of them is on the line and the other is not. The third approach is the one presented in this lesson.

Line following program

Once you've decided on an overall design for your program you still need to figure out how it's going to work. Just like there are multiple ways to position the light sensors there are different program designs you can use to follow a line edge with two light sensors. We present one possible solution in this lesson. You may want to see if you can come up with a different and possibly better one.

Program description

This line following program will continually evaluate the light sensor readings, keeping track of the lightest and darkest values the robot sees. Using these values it can decide at any given time how close it is to the optimal position. The light sensor that is positioned over the line should return the lowest possible value and the other one should return the highest possible. To make the program easier to read and maintain we will divide it into three segments:

  • Main program
  • Set the min and max light sensor values
  • Adjust the robot's course

Pseudo-code

Main program
Read the current light sensor values
Move the robot straight ahead for a short while [1]
Loop
Set the min & max light sensor values
Check the current light sensor values and adjust the robot's course
Set both motors to move the robot straight ahead [2]
End of loop
End of Main program
 
Notes
[1] This assumes that the robot's environment is similar to the FLL Challenge, where the robot starts in base and doesn't encounter the line right away. Line up the robot so that after moving straight ahead for a short distance it will be positioned as close as possible to the line edge it should follow. It doesn't matter if the robot is off by a little bit, it will find the correct position along the line edge.
[2] As will be shown below one of the motors may be shut off after calling the AdjustCourse subroutine. We want to minimize the time the robot is steering so we set both motors to forward after adjusting the robot's course.
 
Subroutine to set the min and max light sensor values
Get the current value of the floor (bright) light sensor
If the current value is greater than the max value then
Set the max value to the current value
End if
Get the current value of the line (dark) light sensor
If the current value is less than (or equal to) the min value then
Set the min value to the current value
End if
End of subroutine
 
Subroutine to adjust the robot's course
Get the current line (min) and floor (max) values
Determine how far from the average min/max value the current values are
If we are farther from the line than we are from the floor then
Move towards the line
Else if we are farther from the floor than we are from the line then
Move towards the floor
End if
End of subroutine

Robolab code

The three main segments of the program are shown here as Robolab program diagrams.


Main program


Set Min and Max Subroutine


Adjust Course Subroutine

Copyright © 2003, Hacienda Robotics Program.