ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

smach: How to execute a new state and then continue with the old state

asked 2018-04-27 18:30:56 -0500

robot_commander gravatar image

updated 2018-05-18 08:16:24 -0500

Hi, I'm using SMACH and I have a situation where I'm executing say, state1. But I get a sensor input and I have to execute a new state now, state2. After execution of state2, I want to continue executing state1 where I left it off. Is this scenario possible? If yes, how do I make this happen?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-05-18 15:19:05 -0500

updated 2018-05-18 15:30:52 -0500

Two possibilities:

  1. Use instance variables inside your state1 Class
  2. Pass data between state instances with userdata

or some combination of the 2...

e.g. #1:

import rospy
from smach import State

class State1(State):
    def __init__(self):
        State.__init__(self, outcomes=['still_working', 'done'])

        rate = rospy.Rate(200)  # Loop at 200 Hz

        self._in_progress = False
        self._prev_data = None
        # etc. etc.

    def execute(self):
        # entry
        current_data = []
        if self._in_progress:
            current_data = self._prev_data

        sensor_input = False
        while not sensor_input and not rospy.is_shutdown():

             # do a bit of work here

             if check_if_work_done():
                 self._in_progress = False
                 self._prev_data = None
                 return 'done'

             if check_for_sensor_input():
                 self._in_progress = True
                 self._prev_data = current_data
                 return 'still_working'


For #2, state1 would pass out different userdata values based on whether or not it had completed before being interrupted. You'd probably want a state3 that would determine the next transition after state2 based on the userdata dictionary.

edit flag offensive delete link more


@josephcoombe Thank you for your input!

robot_commander gravatar image robot_commander  ( 2018-07-09 11:38:36 -0500 )edit

Question Tools

1 follower


Asked: 2018-04-27 18:30:56 -0500

Seen: 404 times

Last updated: May 18 '18