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

181


















                                                        Figure 9-3.
                                                     RoboTag behaviors

          I'll start by examining the NQC code  for each behavior. Then I'll talk  about how a behavior is selected and how the motors are
          controlled. In the next section, I'll present the entire source code for RoboTag.

          The cruise behavior is simple:

              int cruiseCommand;
              task cruis  e() {
                  cru ise Command = COMMAND_FORWARD;
                  whi le (true) Wait(100);
              }

          cruise sets the cruiseCommand variable to the value COMMAND_FORWARD and then loops forever.∗ Each behavior
          (t ask) has an associated command  variable that  holds the desired motor  output.   cruise, for example, uses  the
          cruiseCommand variable to ho ld the desired motor output. In this simple case, cruise always wants the robot to move
          forward. Lat er on, I'll show you h ow this variable is used to determine what actually happens to the robot.

          The next behavior is tag. Like cruise , tag has its own motor output variable, tagCommand:

              int tagCommand;

              task tag() {
                  while(true) {
                      if (BUMP_SENSOR == 1) {
                           // Say tag!
                           SendMessage(MESSAGE_TAG);
                           // Coast to a stop.
                           tagCommand = COMMAND_FLOAT;
                           Wait(20);
                           // Check to see if we got an acknowledgement.
                           if (Message() == MESSAGE_ACKNOWLEDGE) {

          ∗ The endless loop isn't strictly necessary, but it makes cruise look more like the other behaviors.
   187   188   189   190   191   192   193   194   195   196   197