smach_viewer引发KeyError

问题描述

我正在尝试使用SMACH在ROS上实现一个简单的状态机,并使用smach_viewer对其进行可视化。 我正在使用的系统:

  • Ubuntu 18.04
  • ROS Melodic(catkin_ws配置为使用python3!)

我的smach实现看起来像这样(test.py):

  #!/usr/bin/env python3

  import smach
  import smach_ros
  import rospy

  class t1(smach.State):
    def __init__(self,outcomes=['successful','Failed']):
        smach.State.__init__(self,outcomes)

    def execute(self,userdata):
        return 'successful'

  class t2(smach.State):
    def __init__(self,outcomes=['successful']):
        smach.State.__init__(self,userdata):
        #time.sleep(2)
        return 'successful'

  class t3(smach.State):
    def __init__(self,userdata):
        #time.sleep(2)
        return 'successful'


  if __name__=="__main__":
    rospy.init_node('test_state_machine')

    sm_top = smach.StateMachine(outcomes=['success'])
    with sm_top:
        smach.StateMachine.add('T1',t1(),transitions={'successful': 'T2','Failed': 'T3'})
        smach.StateMachine.add('T2',t2(),transitions={'successful': 'T1'})
        smach.StateMachine.add('T3',t3(),transitions={'successful': 'success'})

    # Create and start the introspection server
    sis = smach_ros.IntrospectionServer('introspection_server',sm_top,'/SM_ROOT')
    sis.start()
    # Execute SMACH plan
    outcome = sm_top.execute()
    # Wait for ctrl-c to stop the application
    rospy.spin()
    sis.stop()

根据我终端的输出,状态机运行得很好。但是,当我尝试使用smach_viewer可视化SM时,smach_viewer会引发以下错误

[ERROR] [1599821587.164191]: bad callback: <bound method SmachViewerFrame._status_msg_update of <__main__.SmachViewerFrame; proxy of <Swig Object of type 'wxFrame *' at 0x55616f7eb050> >>
Traceback (most recent call last):
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py",line 750,in _invoke_callback
    cb(msg)
  File "/home/faps/catkin_ws/src/executive_smach_visualization/smach_viewer/scripts/smach_viewer.py",line 844,in _status_msg_update
    if container.update_status(msg):
  File "/home/faps/catkin_ws/src/executive_smach_visualization/smach_viewer/scripts/smach_viewer.py",line 185,in update_status
    self._local_data._data = pickle.loads(msg.local_data)
  File "/usr/lib/python2.7/pickle.py",line 1388,in loads
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py",line 864,in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py",line 1157,in load_get
    self.append(self.memo[self.readline()[:-1]])
KeyError: 'AJ9cQA'

关于可能是什么问题以及如何解决的任何想法?

解决方法

我确保激活了正确的分支(在我的情况下,是旋律发展分支)并运行了

rosdep install smach_viewer

(我之前忘记了)。

这样做,smach_viewer至少可以正确显示状态机。尽管如此,在终端中错误仍然不断出现。

由于至少可视化现在可以正常工作,因此我将其标记为答案。有关更多信息,请查看问题线程here