|
Computer Science 014 LEGO Robot Engineering Williams College Winter 2007
|
|
Lab 5: Line Following
Due: 10:00 AM, Thursday, January 11, 2007
Today, you will teach your robot how to follow a (possibly curved)
line. Using only light sensors, your robot will follow a path marked
out as a black line on a white surface. With minor modifications, it
should also follow a light-colored line on a dark surface.
In addition, your robot will need to follow the path politely. If it
bumps into anything along the way (another robot, for example), it
should wait a bit, allowing the other robot to move out of the way.
You will need one or two light sensors for this lab. (While either
option can work, robots using the two-sensor implementation usually
stay on track much better than those using the one-sensor
implementation.) The light sensors should be mounted on the front of
your robot, centered and pointing down toward the ground. You can put
them close together, or you may separate them a little bit (so that
they straddle the line, for example).
- A simple algorithm for two light sensors
The path your robot needs to follow will not be a straight line. Even
if it were a straight line, following it would be tricky. (Previous
labs probably convinced you of this.) You will need to monitor the
light sensor(s) constantly and make adjustments to the motors to keep
the robot on track.
If you have two sensors, for example, you can continuously compare
their readings. If one is sensing brightness while the other is
sensing darkness, you can adjust the motors to turn the robot in the
direction of the darkness (if the line is black). If the robot has
moved off the line to the right, the right light sensor will see a
brighter surface, and the adjustment should be made to the left; if
the robot has moved off the line to the left, the left light sensor
will see a brighter surface, and the adjustment should be made to the
right.
- Brightness and darkness are subjective
How can you measure brightness and darkness? What we humans see as
"bright" or "dark" can vary from person to person. Similarly, you
have discovered that your sensors don't all perceive light in exactly
the same way. Some sensors will provide light readings in the entire
range of 0 to 255; others will have a smaller range. You will want to
normalize the values produced, perhaps so they always fall within the
range 0-100. This should allow you to compare the relative
light/darkness detected by each of your sensors.
As described above, your robot should do line following, but it should
do so politely. That is, the robot should wait a bit if it bumps into
another robot, allowing the other one to move out of the way.
A nice way to go about implementing this is to think of each of these
two behaviors separately. First, write a function that implements the
line following behavior. Then write another function that implements
bumper sensing behavior. Once those work properly, you can make
modifications so that they work together.
- Bumper sensing
The bumper sensing function should continuously monitor the left and
right touch sensors on the front of your robot. If either of these is
pressed, the robot should stop, wait for some short amount of time (a
couple of seconds), perhaps back up just a bit, and then start to move
forward again.
- Coordinating two behaviors
We have seen how to start up functions as separate processes, thereby
allowing them to execute at the same time. Sometimes such separate
processes are truly independent. That is, they don't affect each
other. Here the behaviors aren't completely separate. If a bump is
detected by the bumper sensing process, line following should be
suspended temporarily.
The best way to handle this is through a shared (i.e., global)
variable. This variable should take on one of two values. One of
those values (say, 1) should signify that the robot is waiting; the
other value (say, 0) should signify that the robot is not waiting.
The bumper sensing function should be responsible for setting the
value of this variable (to 1 when it is time to wait; to 0 when it is
time to go). The line following function should continuously check
the value of this variable, doing light sensing and motor adjustment
only when the robot is not waiting.
In lab, you will find an oval track using black tape on a white
surface and a figure eight track using black tape on a white surface.
All robots should be tested on these tracks. In addition, with just a
minor modification to your program, your robot should be able to
follow a light-colored line on a dark surface. Make the necessary
changes to your program, and test the robot on the track set out on
the carpet in the lab. Demonstrate each of these behaviors and submit
a printout of your program.