(!) 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 container

Description: 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.

   1 sm = StateMachine(outcomes = ['outcome1', 'outcome2'],
   2                   input_keys = ['input1', 'input2'],
   3                   output_keys = ['output1', 'output2'])

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=[]) 

Adding States

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:

   1 with sm:
   2     StateMachine.add('FOO',
   3                      FooState(),
   4                      {'outcome2':'FOO',
   5                       'outcome3':'BAR'})
   6     StateMachine.add('BAR',
   7                      BarState(),
   8                      {'outcome3':'FOO',
   9                       'outcome4':'outcome2'})

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

Wiki: smach/Tutorials/StateMachine container (last edited 2010-08-09 22:16:30 by wim)