|Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.|
StateMachine containerDescription: This tutorial teaches you how to use the StateMachine container.
Tutorial Level: BEGINNER
Next Tutorial: Concurrence container
Creating a State Machine
First import the state machine:
1 from smach import StateMachine
Since a SMACH StateMachine also provides a State interface, its outcomes and userdata interactions must be specified on construction.
Similarly to the SMACH State interface, input keys and output keys are optional. The constructor signature is:
1 __init__(self, outcomes, input_keys=, output_keys=)
When adding states to a state machine you first specify the state machine you want to add states to. This can be done by using Python's "with" statement. You can think of this like "opening" the container for construction. It creates a context in which all subsequent add* calls will apply to the open container.
For example, to add two states to a state machine called "sm", you could write the following:
The above example adds the two states labeled "FOO" and "BAR", of type "FooState" and "BarState", respectively. There are optional arguments to the static add method. The signature of the add method is:
1 add(label, state, transitions=None, remapping=None)
For more details on transitions, take a look at Getting Started
For more details on remapping, take a look at User Data