1 回答

TA貢獻1744條經驗 獲得超4個贊
我找到了一種使用可變對象的解決方法,它看起來像下面這樣:
import smach
import smach_ros
import rospy
class st1(smach.State):
? ? def __init__(self, outcomes=['successful', 'preempted']):
? ? ? ? smach.State.__init__(self, outcomes, input_keys=['in_test'])
? ? def execute(self, userdata):
? ? ? ? if self.preempt_requested():
? ? ? ? ? ? self.service_preempt()
? ? ? ? ? ? return 'preempted'
? ? ? ? rospy.logerr('test 1: '+str(userdata.in_test))
? ? ? ? userdata.in_test[0]=userdata.in_test[0]+1
? ? ? ? return 'successful'
class st2(smach.State):
? ? def __init__(self, outcomes=['successful', 'preempted']):
? ? ? ? smach.State.__init__(self, outcomes, input_keys=['in_test'])
? ? def execute(self, userdata):
? ? ? ? #time.sleep(2)
? ? ? ? if self.preempt_requested():
? ? ? ? ? ? self.service_preempt()
? ? ? ? ? ? return 'preempted'
? ? ? ? rospy.logerr('test 2: ' + str(userdata.in_test[0]))
? ? ? ? return 'successful'
if __name__=="__main__":
? ? rospy.init_node('test_state_machine')
? ? sm_con = smach.Concurrence(outcomes=['success'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?default_outcome='success'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?)
? ? sm_con.userdata.testdata = [0]
? ? with sm_con:
? ? ? ? sm_1 = smach.StateMachine(outcomes=['success', 'preempted'], input_keys=['testdata'])
? ? ? ? with sm_1:
? ? ? ? ? ? smach.StateMachine.add('ST1', st1(),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?remapping={'in_test': 'testdata'},
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?transitions={'successful': 'ST1'})
? ? ? ? sm_2 = smach.StateMachine(outcomes=['success', 'preempted'], input_keys=['testdata'])
? ? ? ? with sm_2:
? ? ? ? ? ? smach.StateMachine.add('ST2', st2(),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?remapping={'in_test':'testdata'},
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?transitions={'successful': 'ST2'})
? ? ? ? smach.Concurrence.add('SM1', sm_1)
? ? ? ? smach.Concurrence.add('SM2', sm_2)
? ? # Execute SMACH plan
? ? outcome = sm_con.execute()
? ? print('exit-outcome:' + outcome)
? ? # Wait for ctrl-c to stop the application
? ? rospy.spin()
由于這只是一種解決方法,請參閱我的相應問題帖子以獲取更多信息。
添加回答
舉報