LEGO MINDSTORMS Robots Manual page 47

Unofficial guide
Table of Contents

Advertisement

The environment does provide a counter, which we'll use in lieu of a variable to hold the turning direction. Although you can't assign values directly to the counter, you can do two things: reset it
to zero and add one to it. Trusty will use just two values, 0 and 1, to mean turn left and turn right, respectively.
Coping with Failure
Our basic assumption about Trusty is that he will drive off the black line on alternating sides. But this probably won't really happen all the time, particularly if the black line has curves in it. What
Trusty needs is some way to figure out if he's turning the wrong way, away from the line instead of toward it. For this purpose, we'll use a timer. If Trusty doesn't find the line within a certain time
interval, we'll have him switch state and turn back the other way.
The real world is a very challenging place. You should always assume that bad things will happen to your robot and try to create a program that responds appropriately.
The Program
Figure 3-3 shows Trusty's basic program. It begins by setting the two motors to the forward direction at speed 4. The central decision point is the light sensor watcher. If the sensor sees the black
line, Trusty moves straight ahead. If the sensor sees the white background, then the program resets the timer and calls a subroutine called toggle. This subroutine turns the robot left or right,
alternating each time it is called.
Use your own values for the thresholds of the light sensor watcher. The values shown in Figure 3-3 are calibrated to my particular light sensor and may not work
with yours.
The timer is used in case Trusty happens to turn the wrong way. Suppose, for example, that he runs off the right side of the black line twice in a row. The first time, he would turn left to find the
line again. The second time, however, he would turn right, away from the line. The timer is used to limit this behavior. If Trusty is turning and the timer goes off, then Trusty automatically turns
back the other way. Figure 3-4 shows the timer watcher, which calls the same toggle subroutine if the robot is still off the line.
You might be wondering why the timer counts for one half second. Why not three quarters of a second, or a full second? Remember that the timer keeps Trusty from turning around completely.
The timer value is based on observation—if Trusty is turning toward the line, he will find it within a half second. If he is turning away from the line, he can be pretty sure he's missed it after a half
second. A line-follower with a different mechanical design might need a different timer value.
Figure 3-3.
A top-level view of Trusty's software
Page 46
Page 47

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the MINDSTORMS Robots and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

Table of Contents