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

156


                  stop release;
                  OnFwd(OUT_A);
                  until (SENS  OR_3 == 100);
                  armLock = 1;
                  OnRev(OUT_A);
                  until (SENSOR_3 != 100);
                  armLock = 0;
                  Off(OUT_A);
              }

              task release() {
                  until (armLock == 0);
                  stop grab;
                  OnRev(OUT_A);
                  until (SENSOR_3 == 1    00);
                  armLock = 1;
                  OnFwd(OUT_A);
                  until (SENSOR_3 != 100);
                  armLock = 0;
                  Off(OUT_A);
              }

              task heartbeatWatcher () {
                  while (true) {
                      if (missedBeats > 1) {
                           Pla ySound (SOUND_DOWN);
                           stop messageWatcher;
                           Off(OUT_C);
                           until (armLock == 0);
                           Off(OUT_A);
                           until (Message () == HEARTBEAT_MESSAGE);
                           PlaySound(SOUND_UP);
                           missedBeats = 0;
                           start messageWat   cher;
                      }
                      missedBeats = missedBeats + 1;
                      Wait(HEARTBEAT_TIME);
                  }
              }

          Don't Break That Arm

          The grab and release tasks, as I said, have the ability to interrupt each other. They use a variable, armLock, to avoid
          potentially dangerous situations. For example, suppose that release is  running. It drives the arm motor in reverse until the
          arm is up and the limit touch sensor is pressed. Then the motor is run forward to release the touch sensor. Before the touch
          sensor is released, however, suppose grab starts up. It stops release and runs the motor forward until the touch sensor is
          pressed. It's already pressed, so grab starts running the motor in reverse, waiting for the touch sensor to be released. Since
          the arm is already at the top of its travel, running the arm motor in reverse creates pressure on the gears and will either break
          the arm apart or cause the gears to skip.
   162   163   164   165   166   167   168   169   170   171   172