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

105


                  release();

                  // Turn around.
                  OnRev(OUT_C)  ;
                  Wait(TURNAROUND_TIME);
              }

          Let's look at the simple parts first. The grab() and release() inline subroutines take care of the grabber arm . All they
          d o is run the arm motor in one direction until the limit sensor is pressed. Running the motor forward causes the arm to descend,
                                                              r
          the grabber to close, and the arm to lift again. Running the moto in reverse makes the arm descend, the grabber open, and the
          ar m lift again. The mechanics of the arm take care of everything, as I'll explain later in the chapter. All we have to do is wait
          for th e arm to lift, which presses the switch when it's finished. You  might have noticed that the light sensor and the touch
          sensor are both attached to the same input. I'll talk about how this works later. For now, just  be aware that it's necessary for
                                                             o
          grab() and release() to move away from the touch sens r to use the light sensor.

          The  calibrate()  subroutine examines the values coming from  Minerva's light sensor.  It computes an average  value,
          which is stored in the vari able threshold. The retrieve() subroutine uses this value to figure out if it's looking at an
          o bject that should be picked up. Specifically, it tests if the light sensor value is a little less than the original average:

              until (SENSOR_3 < threshold - 3);

          C alibrating the sensor in this way frees us from hard-coding a light sensor threshold value into Minerva's program. It also
          makes Minerva bette r able to deal with different lighting conditions.

          The retrieve() subroutine is the heart of Minerva's program. It drives forward  (by turning output C on) until it finds
          so mething dark to pick up. While it's driving forward, timer 0 is ticking away, measuring how long it takes until something is
          found:

              OnFwd(OUT_C);
              ClearTimer(0);
              until (SENSOR_3 < threshold - 3);

          Once a  dark object is found, Minerva moves forward a little farther to position the grabber over the object. She records the
          forward movement time for the return trip. Finally, Minerva turns off output C to stop the robot's forward motion:

              Wait(20); // Move up on it a little.
              returnTime = Timer(0);
              Off(OUT_C);

          Having found something interesting, Minerva picks it up:

              grab();
   111   112   113   114   115   116   117   118   119   120   121