本文主要是介绍smach(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://wiki.ros.org/smach/Tutorials/User%20Data
两个状态之间通过数据交流的方式进行状态转移。
#!/usr/bin/env pythonimport rospy
import smach
import smach_ros# define state Foo
class Foo(smach.State):def __init__(self):smach.State.__init__(self, outcomes=['outcome1','outcome2'],input_keys=['foo_counter_in'],output_keys=['foo_counter_out'])def execute(self, userdata):rospy.loginfo('Executing state FOO')if userdata.foo_counter_in < 3:userdata.foo_counter_out = userdata.foo_counter_in + 1return 'outcome1'else:return 'outcome2'# define state Bar
class Bar(smach.State):def __init__(self):smach.State.__init__(self, outcomes=['outcome1'],input_keys=['bar_counter_in'])def execute(self, userdata):rospy.loginfo('Executing state BAR')rospy.loginfo('Counter = %f'%userdata.bar_counter_in) return 'outcome1'def main():rospy.init_node('smach_example_state_machine')# Create a SMACH state machinesm = smach.StateMachine(outcomes=['outcome4'])sm.userdata.sm_counter = 0# Open the containerwith sm:# Add states to the containersmach.StateMachine.add('FOO', Foo(), transitions={'outcome1':'BAR', 'outcome2':'outcome4'},remapping={'foo_counter_in':'sm_counter', 'foo_counter_out':'sm_counter'})smach.StateMachine.add('BAR', Bar(), transitions={'outcome1':'FOO'},remapping={'bar_counter_in':'sm_counter'})# Execute SMACH planoutcome = sm.execute()if __name__ == '__main__':main()
这篇关于smach(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!