- Mapping the Scenario
- Pseudocode and Flowcharting RSVP
- Statecharts for Robots and Objects
Statecharts for Robots and Objects
A statechart is one way to visualize a state machine.
For example, a “change of state” can be as simple as a change of location. When the robot travels from its initial location to the location next to the table, this is a change of the robot’s state. Another example is that the birthday candles change from an unlit state to a lit state. The state machine captures the events, transformations, and responses. A statechart is a diagram of these activities. The statechart is used to capture the possible situations for that object in that scenario. As you recall from Chapter 2, a situation is a snapshot of an event in the scenario. Possible situations for the BR-1 are
Situation 1: BR-1 waiting for signal to move to new location
Situation 2: BR-1 traveling to cake table
Situation 3: BR-1 next to cake on a table with candles that have not yet been lit
Situation 4: BR-1 positioning the lighter over the candles, and so on
All these situations represent changes in the state of the robot. Changes in the state of the robot or object take place when something happens, an event. That event can be a signal, the result of an operation, or just the passing of time. When an event happens, some activity happens, depending on the current state of the object. The current state determines what is possible.
The event works as a trigger or stimulus causing a condition in which a change of state can occur. This change from one state to another is called a transition. The object is transitioning from stateA, the source state, to stateB, the target state. Figure 3.12 shows a simple state machine for BR-1.
Figure 3.12 State machine for BR-1
Figure 3.12 shows two states for BR-1: Idle or Traveling. When BR-1 is in an Idle state, it is waiting for an event to occur. This event is a signal that contains the new location for the robot. Once the robot receives this signal, it transitions from Idle to Traveling state. BR-1 continues to travel until it reaches its target location. Once reached, the robot transitions from the Traveling state back to an Idle state. Signals, actions, and activities may be performed or controlled by the object or by outside forces. For example, the new location will not be generated by BR-1 but by another agent. BR-1 does have the capability to check its location while traveling.
Developing a Statechart
As discussed earlier, a state is condition or situation of an object that represents a transformation during the life of the object. A state machine shows states and transitions between states. There are many ways to represent a state machine. In this book, we represent a state machine as a UML (Unified Modeling Language) statechart. Statecharts have additional notations for events, actions, conditions, parts of transitions, and types and parts of states.
There are three types of states:
Initial: The default starting point for the state machine. It is a solid black dot with a transition to the first state of the machine.
Final: The ending state, meaning the object has reached the end of its lifetime. It is represented as a solid dot inside a circle.
Composite state and substate: A state contained inside another state. That state is called a superstate or composite state.
States have different parts. Table 3.5 lists the parts of states with a brief description. A state node displaying its name can also display the parts listed in this table. These parts can be used to represent processing that occurs when the object transitions to the new state. There may be actions to take as soon as the object enters and leaves the state. There may be actions that have to be taken while the object is in a particular state. All this can be noted in the statechart.
Table 3.5 Parts of a State
Part |
Description |
Name |
The unique name of the state distinguishes it from other states. |
Entry/exit actions |
Actions executed when entering the state (entry actions) or executed when exiting the state (exit actions). |
Composite/substates |
A nested state; the substates are the states that are activated inside the composite state. |
Internal transitions |
Transitions that occur within the state are handled without causing a change in the state but do not cause the entry and exit actions to execute. |
Self-transitions |
Transitions that occur within the state that are handled without causing a change in the state. They cause the exit and entry actions to execute when exiting and then reentering the state. |
Figure 3.13 shows a state node and format for actions, activities, and internal transition statements.
Figure 3.13 A state node and the format of statements
The entry and exit action statements have this format:
Entry/action or activity
Exit/action or activity
This is an example of an entry and exit action statement for a state called Validating:
entry action: entry / validate(data)
exit action: exit / send(data)
Upon entering the Validating state, the validate(data) function is called. Upon exiting this state, the exit action send(data) is called.
Internal transitions occur inside the state. They are events that take place after entry actions and before exit actions if there are any. Self-transitions are different from internal transitions. With a self-transition, the entry and exit actions are performed. The state is left; the exit action is performed.
Then the same state is reentered and the entry action is performed. The action of the self-transition is performed after the exit action and before the entry action. Self-transitions are represented as a directed line that loops and points back to the same state.
An internal or self-transition statement has this format:
Name/action or function
For example:
do / createChart(data)
“do” is the label for the activity, the function “createChart(data)” is executed.
There are several parts of a transition, the relationship between two states. We know that triggers cause transitions to occur, and actions can be coupled with triggers. A met condition can also cause a transition. Table 3.6 lists the parts of a transition.
Table 3.6 Parts of a Transition
Part |
Description |
Source state |
The original state of the object; when a transition occurs the object leaves the source state. |
Target state |
The state the object enters after the transition. |
Event trigger |
The event that causes the transition to occur. A transition may be triggerless, which means the transition occurs as soon as the object completes all activities in the source state. |
Guard condition |
A boolean expression associated with an event trigger, which when evaluated to TRUE, causes the transition to take place. |
Action |
An action executed by the object that takes place during a transition; may be associated with an event trigger or guard condition. |
An event trigger has a similar format as a state action statement:
Name/action or function
name [Guard] / action or function
For example, for the internal transition statement, a guarded condition can be added:
do [Validated] / createChart(data)
Figure 3.14 is the statechart for BR-1.
Figure 3.14 Statechart for BR-1
There are four states: Idle, Traveling, Lighting candles, Waiting and Removing dishes. When transitioning from Idle to Traveling, BR-1 gets the new location and knows its mission:
do [GetPosition] / setMission()
There are two transitions from the Traveling state:
Traveling to Lighting candles
Traveling to Removing dishes
Traveling transitions to LightingCandles when its target is reached and its Mission is candles. Traveling transitions to Removing dishes when its target is reached and its Mission is dishes. To transition from LightingCandles, “candles” mission must be complete. To transition from RemovingDishes to the final state, all missions must be completed.
LightingCandles is a composite state that contains two substates: LocatingWick and IgnitingWick. Upon entering the Lighting candles state, the boolean value Singing is evaluated. If there is singing, then the candles are to be lit. First the wick has to be located, then the arm is moved to that location, and finally the wick can be lit. In the LocatingWick state, the entry action evaluates the expression:
Candles > 0
If True, the state exits when the position of the first or next candle is retrieved and then the robot arm is moved to the position (Pos).
The position of the wick is retrieved, so BR-1 transitions to “IgnitingWick.” Upon entry, the lighter is checked to see if it is lit. If lit the candle wick is lit, (an internal state). To exit this state, Candles > 0, then “LocatingWick” state is reentered. If Candles = 0, then BR-1 transitions to “Waiting” state. BR-1 waits until the party is over. Then BR-1 can remove all the dishes. In the “Waiting” state, there is a self-transition “PartyNotOver.” Remember, with a self-transition, the exit and entry actions are performed as the state is exited and reentered. In this case, there are no exit actions, but there is an entry action “wait 5 minutes”. The guard condition is checked, “PartyNotOver.” If the party is not over, then the state is reentered and the entry action is executed; BR-1 waits for 5 minutes. Once the party is over, then BR-1 transitions to “RemovingDishes.” This is the last state. If boolean value AllMissionsCompleted is True, BR-1 transitions to the final state. But some objects may not have a final state and work continuously. Statecharts are good for working out the changing aspect of an object during the lifetime of the object. It shows the flow of control from one state of the object to another state.
What’s Ahead?
In Chapter 4, “Checking the Actual Capabilities of Your Robot,” we discuss what your robot is capable of doing. This means checking the capabilities and limits of the microcontroller, sensors, motors, and end-effectors of the robots.