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

CBState

Description: This tutorial gives an example of how to use CBState, a state that simply executes a callback when it is active.

Tutorial Level: BEGINNER

Next Tutorial: Simple Action State

   1 from smach import CBState

Description

This state simply executes a single callback when the state is executed. This is useful for executing arbitrary code in a state, without having to declare a new state class. This class supports the use of the smach.cb_interface decorator, and it's API can be found here.

The CBState calls the callback with at least one argument: the container's userdata. Additional arguments and keyword arguments can be given to the CBState on construction. These args will be passed into the callback when the CBState is executed.

Example

The following example adds a CBState to a SMACH StateMachine that takes in some arguments and variables from userdata, and stores their sum in the userdata key 'xyz':

   1 @smach.cb_interface(input_keys=['q'],
   2                     output_keys=['xyz'],
   3                     outcomes=['foo'])
   4 def my_cb(ud, x, y, z):
   5     ud.xyz = ud.q + x + y + z
   6     return 'foo'
   7 ...
   8 with sm:
   9     ...
  10     StateMachine.add('MY_CB', CBState(my_cb,
  11                                       cb_args=[10],
  12                                       cb_kwargs={'z':2,'y':3}),
  13                               {'foo':'OTHER_STATE'})
  14     ...

Wiki: mysmach/Tutorials/CBState (last edited 2015-01-31 08:33:54 by hongming wang)