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

36


          Multitasking

          Don't be fooled by the simplicity of the RIS programming environment—it hides some pretty messy details. Hank's simple
          program demonstrates a powerful feature of the RCX software: multitasking. This is a term from the computer world—it just
          means that the RCX can do more than one thing at a time. Each of the two instruction sequences hanging off the touch sensor
          watchers is a separate task, and they can actually execute at the same time. To see this in action, touch one of Hank's bumpers
          to trigger the first task, then touch the other bumper shortly afterward. (To really see this effect, you could try putting in longer
          delay times in Hank's program.)

          The sensor  watchers in RCX Code exhibit another interesting property. If you trigger  a sensor  watcher, the code  for that
          watcher begins executing. If you trigger the same sensor watcher again, while the watcher code is still executing, the watcher
          code starts over again from the beginning.

          The relationship between the programs you create in RCX Code and the tasks that run on the RCX is not always clear.  Tasks
          an d subroutines are declared explicitly in NQC, one of the alternate programming environments for the RCX. See Chapter 4,
          Not Quite C, for details.

          Figure 2-15 shows how th e multitasking nature of the RCX can get you into trouble. The figure shows an alternate program for
          H ank. At first glance, it makes sense. The main program starts Hank moving forward. When one of the bumpers is touched, the
          robot backs up, waits, turns, waits, and starts going forward.



























                                                        Figure 2-15.
                                                 A slightly dangerous program
   42   43   44   45   46   47   48   49   50   51   52