A sequence is a well defined course of steps. These steps run in a strictly sequential order until the sequence has finished or some condition causes the sequence to stop. In such a case the sequence could be repeated or another sequence could be started.
A main feature of a sequence is whether it blocks the flow of control during running through its steps or whether the steps run in parallel to the main flow of control.
Sequence A runs. That is, all its steps run consecutively. After step 1 is done the sequence B is called. As B is blocking its two steps run while A is being blocked. Only after B finishes does the control return back to A where step 2 and step 3 are then executed. After this sequence C is called. As C is nonblocking its two steps run in parallel to the remaining steps of sequence A. A must wait for C to finish by calling wait().
A nonblocking sequence will run in its own thread of execution while a blocking sequence always runs in the same thread as the calling sequence.
The main sequence must always be defined as nonblocking. Otherwise, the flow of control would not return to the main program after calling start() of the main sequence.
Every step is itself basically a sequence. However, a step is always blocking. All the other features such as preconditions and exit conditions as well as monitors are identical, see below. A user has to define her own sequences or steps as described in Define your own Sequence or Step.
Each sequence or step runs as follows
class Move : public Step { public: Move(std::string name, Sequence* caller, Robot& r) : Step(name, this) { } int operator() (double x, double y) {xPos = x; yPos = y; return start();} int action() { robot.setValue(xPos, yPos); return 0; } bool checkExitCondition() { return (robot.getPosX() > xPos && robot.getPosY() > yPos); } private: double xPos, yPos; }; class MoveSequence : public Sequence { public: MoveSequence(std::string name, Sequencer& seq) : Sequence(name, seq), moveXY("step", this) { } int action() { robot.moveXY(10, 10) robot.moveXY(15, 25); robot.moveXY(22, 35); return 0; } private: Move moveXY; };
The sequence MoveSequence comprises three steps moveXY. Each of them moves a hypothetical robot a predefined robot in x,y direction.
Usually at some stage in your program you have to wait for a given sequence to finish until the program should continue. There are two methods to accomplish this, wait()
and waitAndTerminate()
.
The former waits for a given sequence to finish running. It returns as soon as all the steps defined in the sequence have completed running. However, it does not terminate the associated thread. This allows to restart the same sequence with start()
later on in the program.
On the other hand, waitAndTerminate()
does also wait for all the steps defined in the sequence to have completed. Further, it will terminate the associated thread and return only after successful elimination of the thread object. After this, the sequence cannot be restarted.
Never use wait()
or waitAndTerminate()
from within the safety system or the control system. The two methods should solely be used by other sequences or by the main program.