|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.|
CBStateDescription: 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
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 its 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.
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':