def nested_sm(self): def execute(name, outcome): self._trace.append(name) return outcome resumable_inner_sm = ResumableStateMachine(outcomes=['ok']) inner_sm = StateMachine(outcomes=['ok']) outer_sm = ResumableStateMachine(outcomes=['ok']) with outer_sm: StateMachine.add('A', resumable_inner_sm, {'ok': 'B'}) StateMachine.add('B', inner_sm, {'ok': 'C'}) StateMachine.add( 'C', CBState(lambda ud: execute('C', 'ok'), outcomes=['ok'])) with resumable_inner_sm: StateMachine.add( 'A', CBState(lambda ud: execute('A.A', 'ok'), outcomes=['ok']), {'ok': 'B'}) StateMachine.add( 'B', CBState(lambda ud: execute('A.B', 'ok'), outcomes=['ok'])) with inner_sm: StateMachine.add( 'A', CBState(lambda ud: execute('B.A', 'ok'), outcomes=['ok']), {'ok': 'B'}) StateMachine.add( 'B', CBState(lambda ud: execute('B.B', 'ok'), outcomes=['ok'])) return outer_sm
def main(): rospy.init_node("smach_example_state_machine") # Create a SMACH state machine sm = StateMachine(outcomes=["outcome4", "outcome5"]) # Open the container with sm: # Add states to the container StateMachine.add("FOO", Foo(), transitions={ "outcome1": "BAR", "outcome2": "outcome4" }) StateMachine.add("BAR", Bar(), transitions={"outcome2": "FOO"}) # Create and start the introspection server sis = smach_ros.IntrospectionServer("my_smach_introspection_server", sm, "/SM_ROOT") sis.start() # Execute SMACH plan outcome = sm.execute() # Wait for ctrl-c to stop the application rospy.spin() sis.stop()