Propeller Manual

Table Of Contents
ABORT – Spin Language Reference
Page 50 · Propeller Manual v1.1
<more code>
The above example shows three methods of various logical levels, Main (“high-level”), Move
(“mid-level”) and
DriveMotors (“low-level”). The high-level method, Main, is the decision
maker of the application; deciding how to respond to events like sensor activations and motor
movements. The mid-level method,
Move, is responsible for moving the robot a short
distance. The low-level method,
DriveMotors, handles the details of driving the motors
properly and verifying that it is successful.
In an application like this, critical events could occur in low-level code that needs to be
addressed by high-level code. The
ABORT command can be instrumental in getting the
message to the high-level code without requiring complicated message-passing code for all
the mid-level code in-between. In this case, we have only one mid-level method but there
could be many nested mid-level methods between the high-level and the low-level.
The
Main method gets sensor inputs and decides what direction to move the robot via the CASE
statement. It then calls
Move in a special way, with the Abort Trap symbol, \ , preceding it.
The
Move method sets its RESULT to T TRUE and then calls DriveMotors in a finite loop. If it
successfully completes,
Move returns TRUE. The DriveMotors method handles the
complication of moving the robot’s motors to achieve the desired direction, but if it
determines the motors are stuck, it cannot move them further and it aborts with a
FALSE value.
Otherwise it simply returns normally.
If everything is fine, the
DriveMotors method returns normally, the Move method carries on
normally and eventually returns
TRUE, and the Main method continues on normally. If,
however,
DriveMotors finds a problem, it ABORTs which causes the Propeller to pop the call
stack all the way through the
Move method and up to the Main method where the Abort Trap
was found. The
Move method is completely oblivious to this and is now effectively
terminated. The
Main method checks the value returned by its call to Move (which is now the
FALSE value that was actually returned by the aborted DriveMotors method deep down the call
stack) and it decides to
Beep as a result of the detected failure.
If we had not put the Abort Trap, (
\ ), in front of the call to Move, when DriveMotors aborted,
the call stack would have been popped until it was empty and this application would have
terminated immediately.