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

185


          task. However, if  you  do this, the  robot "jiggles" badly. The prob lem is that  arbitrate  changes  the value of
          motorCommand  several times each tim e it loops. As the value changes,  motorControl  responds  by changing the
          motors' directions.

          We're really inter ested  only in the value of  motorCommand   at the end of each loop in  arbitrate. Therefore,
          motorContr  ol  is implemented as a subroutine and is called once each time at the end of the arbitrate loop.

          The RoboTag Program

          On ce you get through the details  of implementing subsumption architecture, the rest of the programming is pretty simple. The
          Ro boTag program uses its main  task to start up all the behavior tasks and, of course, arbitrate. It also uses the light
          sensor initialization code from  M inerva (see Chapter 5, Minerva, a Robot with an Arm) to calculate a baseline value for the
          light sensor. When the light sensor reads lower than the average, the RoboTag robot can assume it's reached th e edge of the
          arena.

          Here is the entire code for RoboTag. You should download this program to both of the robots that will be playing.

              // Mot or commands.
              #de fin e COMMAND_NONE -1
              # de fine COMMAND_FORWARD 1
              #define COMMAND_REVERS    E 2
              #define COMMAND_LEFT 3
              #define COMMAND_RIGHT 4
              #define COMMAND_STOP 5
              #define COMMAND_FLOAT 6

              // IR messages.
              #define MESSAGE_TAG 33
              #define MESSAGE_ACKNOWLEDGE 6

              #define BUMP_SENSOR SE    NSOR_1
              #define LIGHT_SENSOR SENSOR_2

              int score;
              int averageLight;

              int cruiseCommand;

              task cruise() {
                  cruiseCommand =    COMMAND_FORWARD;
                  while (true) Wait(    100);
              }

              int tagCommand;

              task tag() {
   191   192   193   194   195   196   197   198   199   200   201