Page 153 - The Unofficial Guide to Lego Mindstorms Robots
P. 153
142
Finally, as an added bonus, the average value is shown on the RCX's display:
threshold @ showValue
The average value, thresh old, is used in the seek word to find a dark object to pick up. seek drives forward until the
li ght sensor returns a reading that is 3 less than the average: BEGIN 2 sensorValue threshold @ 3 - < UNTIL
D ebugging
When you're writing your own programs, there are two things you should keep in mind when you are debugging:
1. The interactive nature of the pbFORTH interpreter can really help. You can, for example, make changes in a word by
redefining it. With legOS (see Chapter 10), by contrast, you'd have to compile and download a whole new version of your
software. With pbFORTH, you can simply make new definitions for the words that aren't working.∗
Furthermore, you can interactively test every word in your program. This makes it easy to drill down from the higher
layers to the lower layers of your program to identify problems. Any word that is defined can be interactively tested. This
is powerful medicine indeed.
2. On the other hand, you need to be very careful about endless loops. If you do get stuck executing an endless loop, there's no
way to stop pbFORTH short of removing the batteries. In this case, you will have to use your firmware download utility to
reload pbFORTH.
If you entered all of the Minerva source code, you can interactively test any of its words. For example, to show a number on
the display, do something like this:
RCX_INIT
ok
42 showValue
ok
To pause for half a second, you could do this:
5 sleep
ok
To test out the calibrate word, try this:
initialize
ok
∗ Interestingly, new definitions don't replace the old ones, they just mask them out. If you want to return to an earlier
definition, You should use the MARKER word. See the pbFORTH web site for details.