Page 168 - The Unofficial Guide to Lego Mindstorms Robots
P. 168
157
Using the armLock variable enables grab and release to complete the press-release cycle of the arm limit switch
without being interrupted. Both grab and release wait for armLock to be 0 before interrupting the other task, like this:
until (armLock == 0);
T he grab and release tasks set armLock to 1 before doing the sensitive press-release cycle. This means Minerva
should never break her own arm.
S tayin' Alive
T he heartbeatWatcher task also deserves some mention. It adds one to the missedBeats variable just as often as it
expects the remote to send a h eartbeat. Remember that just as heartbeatWatcher is adding to missedBeats, the
m essageWatcher task is su btracting from missedBeats every time it hears the heartbeat message. If missedBeats
is ever greater than one, hea rtbeatWatcher shuts Minerva down. This shutdown is not as straightforward as you might
think, however. First, a sou nd is played (the descending arpeggio). This lets the human operator know that contact with the
remote has bee n lost. Then heartbeatWatcher shuts down the messageWatcher task and turns off Minerva's drive
m otor:
PlaySo und(SOUND_DOWN);
stop messageWatcher;
Of f(OUT_C);
h eartbeatWatcher also wants to shut down Minerva's arm motor, but it respects the armLock variable so the arm is
not left in an uncertain sta te:
until (armL ock == 0);
Off(OUT_A);
Now heartbeatWatcher waits to regain the heartbeat signal from the remote. If it is regained, heartbeatWatcher
plays another sound (the ascending arpeggio), resets missedBeats, and starts up messageWatcher again:
until (Message() == HEARTBEAT_MESSAGE);
PlaySound(SOUND_ UP);
missedBeats = 0;
start messageWatcher;