亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在SMACH并發容器的不同狀態下使用相同的數據

在SMACH并發容器的不同狀態下使用相同的數據

米琪卡哇伊 2023-08-15 16:43:56
假設我們有一個并發 SMACH 容器sm_con,其中包括兩個狀態機SM1和SM2。我需要找到一種方法,讓SM1不斷更新一些數據,讓SM2訪問(并最終修改)相同的數據。我考慮通過將sm_con的用戶數據作為輸入和輸出鍵傳遞給SM1和SM2來解決這個問題,希望如果SM1修改數據,它會自動覆蓋sm_con的用戶數據(有點像在 C++ 中使用指針),但這并不'不工作。相應的代碼如下所示:import smachimport smach_rosimport rospyclass st1(smach.State):    def __init__(self, outcomes=['successful', 'preempted']):        smach.State.__init__(self, outcomes, input_keys=['in_test'], output_keys=['out_test'])    def execute(self, userdata):        if self.preempt_requested():            self.service_preempt()            return 'preempted'        rospy.logerr('test 1: '+str(userdata.in_test))        userdata.out_test=userdata.in_test+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))        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'], output_keys=['testdata'])        with sm_1:            smach.StateMachine.add('ST1', st1(),                                   remapping={'in_test': 'testdata', 'out_test': 'testdata'},                                   transitions={'successful': 'ST1'})運行此代碼,輸出“ test 1: ... ”顯示SM1內部用戶數據增加,而輸出“ test 2: ... ”顯示SM2不會訪問增加的數據,因為輸出仍為0。如何修改SM1中的某些數據并訪問SM2中修改后的數據?
查看完整描述

1 回答

?
慕無忌1623718

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()

由于這只是一種解決方法,請參閱我的相應問題帖子獲取更多信息。



查看完整回答
反對 回復 2023-08-15
  • 1 回答
  • 0 關注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號