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

Generic State

Description: This tutorial show how to implement a generic SMACH State

Tutorial Level: BEGINNER

Next Tutorial: CB State

   1 from smach import State

The most general (but often least efficient) way to create a state is to derive from the 'State' base class. You can pretty much do anything you want in the execute method of a state. Know that SMACH comes with a library of useful states that you can use, without having to write a whole custom state.

   1   class Foo(smach.State):
   2      def __init__(self, outcomes=['outcome1', 'outcome2']):
   3        # Your state initialization goes here
   4      
   5      def execute(self, userdata):
   6         # Your state execution goes here
   7         if xxxx:
   8             return 'outcome1'
   9         else:
  10             return 'outcome2'

Some good code practices:

  • Do not block in your constructor. If your state needs to wait for another component to come up, do this in a separate thread

Wiki: smach/Tutorials/Generic State (last edited 2011-01-21 22:55:27 by MeloneeWise)