Propeller Manual

Table Of Contents
2: Spin Language Reference – ABORT
Propeller Manual v1.1 · Page 49
The type of exit that MayAbort actually used, ABORT or T RETURN, is not automatically known by
the trapping call; it may have just happened to be the destination of a
RETURN command.
Therefore, the code must be written in a way to detect which type was used. Some
possibilities are: 1) code may be designed such that a high-level method is the only place that
traps an abort and other mid-level code processes things normally without allowing
RETURNs
to propagate higher, or 2) aborting methods may return a special value that can not occur in
any normal circumstance, or 3) a global flag can be set by the aborting method prior to
aborting.
Example Use Of Abort
The following is an example of a simple-minded robot application in which the robot is
designed to move away from an object it senses with its four sensors (
Left, Right, Front and
Back). Assume that CheckSensors, Beep, and MotorStuck are methods defined elsewhere.
CON
#0, None, Left, Right, Front, Back 'Direction Enumerations
PUB Main | Direction
Direction := None
repeat
case CheckSensors 'Get active sensor
Left : Direction := Right 'Object on left? Let's go right
Right : Direction := Left 'Object on right? Let's go left
Front : Direction := Back 'Object in front? Let's go back
Back : Direction := Front 'Object in back? Let's go front
other : Direction := None 'Otherwise, stay still
if not \Move(Direction) 'Move robot
Beep 'We're stuck? Beep
PUB Move(Direction)
result := TRUE 'Assume success
if Direction == None
return 'Return if no direction
repeat 1000
DriveMotors(Direction) 'Drive motor 1000 times
PUB DriveMotors(Direction)
<code to drive motors>
if MotorStuck
abort FALSE 'If motor is stuck, abort