Page 222 - The Unofficial Guide to Lego Mindstorms Robots
P. 222

211


          In either search, if a better value is found, it is used as the new baseline:

                  if (result != -1) baseline = result;
                  }
                  else baseline = result;
              }

          That's the basic algorithm. There is one extra feature that makes everything run a little smoother: every so often the baseline
          value is incremented. This means that if the robot is stuck in a dark corner, it will eventually get dissatisfied (because of the
          increasing baseline) and look for something better.

          To communicate with the outside world about what's going on, seek_enlightenment() shows the current light value as
          well as the current baseline on the display. The current value is shown in the first and second digits of the display, while the
          baseline is shown in the third and fourth digits. (Remember that the letter on the right side of the display tells you which
          behavior is currently active.) You can actually see the baseline increasing slowly as the program runs.

          I've implemented my own light sensor processing in process_light(). I didn't use legOS's LIGHT_2 macro because the
          values I was getting from it were not in the range from 0 to 100. Perhaps this is a bug that will be fixed in an upcoming release.
          At any rate, I implemented my own sensor data processing to produce a value from 0 to 100. You may need to adjust the
          RAW_DARK and RAW_LIGHT constants for your particular light sensor.

          The avoid() behavior is very simple. It just checks the touch sensors on inputs 1 and 3. If one of the touch sensors is
          pressed, avoid() backs the robot up and turns it a little.

          Development Tips

          legOS has serious programming power, but it has its rough spots, too. This section contains some helpful advice based on my
          own experience developing with legOS.

          Development Cycle

          legOS's development cycle is a little clumsy. You write a program, compile it with the legOS source code, then download the
          whole thing to the RCX. It's the downloading that takes a long time.
          Here are some tips to make things go smoother:

          1. Always include code that terminates your own program. If your program can stop itself, control returns to legOS. When
          legOS has control, you can turn the RCX on and off and even reinitialize the firmware, as described next.
   217   218   219   220   221   222   223   224   225   226   227