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

Revision history [back]

I can think of 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'])

        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
             current_data.append(do_just_a_bit_of_work())

             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.

I can think of 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
             current_data.append(do_just_a_bit_of_work())

             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'

             rate.sleep()

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.

I can think of two 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
             current_data.append(do_just_a_bit_of_work())

             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'

             rate.sleep()

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.

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
             current_data.append(do_just_a_bit_of_work())

             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'

             rate.sleep()

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.