Skip to content

Smach: General Overview

Siddharth Kannan edited this page Jun 5, 2014 · 1 revision

Smach: General Overview

Installing Smach

sudo apt-get install ros-hydro-smach

Using Smach

As Smach is a Python Library, the files have an extension .py.

Running a python file which has code using the Smach library is simple. The steps are as so:

  1. Create a new package with smach and rospy as dependencies: roscreate-pkg test rospy smach
  2. Change into the src/ directory and create a .py file (say test.py)
  3. For running this file, simply use python test.py

If the code is properly written, you will see the desired output.

Suggestion: Start with this tutorial

About smach

  1. Smach is a Library for python, which is independent of ROS. (as per the official wiki)

  2. Smach is a library for creating state machines. But there are many more containers, apart from a finite state machine, that Smach supports. One of these containers, the Concurrence container is important, as it allows us to make decisions about the next state that state machine will transition to, on the basis of the outcomes of more than one state.

Viewing a State Machine built using Smach

There is an inbuilt tool called the smach-viewer which can be used to view the state diagram of a state machine that we have written.

Installing smach-viewer

sudo apt-get install ros-hydro-smach-viewer

Using smach-viewer

Running the command rosrun smach_viewer smach_viewer.py will open a window in which on setting the proper root, you will be able to see the state machine that is currently running.

Code to be added to the .py file.

Note that this code needs to be added to the .py file that contains the state machine before you can start using smach-viewer:

sis = smach_ros.IntrospectionServer('server_name', sm, '/SM_ROOT')
sis.start()
outcome = sm.execute()
rospy.spin()
sis.stop()

This code creates an IntrospectionServer and starts this server before executing the StateMachine code.