(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

MonitorState (ROS)

Description: 监视状态

Tutorial Level: INTERMEDIATE

注 - 这就是我在试图了解MonitorState时编写的代码。请不要把它当作最佳实践或任何东西,直到JonBohren或其他有更多经验的人去看它。 -LauraLindzey

配置

这里有一个使用MonitorState的简单状态机的例子。

为了从状态Foo转换到状态BAR,发送一条消息到/sm_reset话题:

rostopic pub -1 /sm_reset std_msgs/Empty

代码

   1 #!/usr/bin/env python
   2 import roslib; roslib.load_manifest('smach_MonitorState_example')
   3 import rospy
   4 import smach
   5 import smach_ros
   6 
   7 from std_msgs.msg import Empty
   8 
   9 class bar(smach.State):
  10     def __init__(self):
  11         smach.State.__init__(self, outcomes=['bar_succeeded'])
  12     def execute(self, userdata):
  13         rospy.sleep(3.0)
  14         return 'bar_succeeded'
  15 
  16 def monitor_cb(ud, msg):
  17     return False
  18 
  19 def main():
  20     rospy.init_node("monitor_example")
  21 
  22     sm = smach.StateMachine(outcomes=['DONE'])
  23     with sm:
  24         smach.StateMachine.add('FOO', smach_ros.MonitorState("/sm_reset", Empty, monitor_cb), transitions={'invalid':'BAR', 'valid':'FOO', 'preempted':'FOO'})
  25         smach.StateMachine.add('BAR',bar(), transitions={'bar_succeeded':'FOO'})
  26 
  27     sis = smach_ros.IntrospectionServer('smach_server', sm, '/SM_ROOT')
  28     sis.start()
  29     sm.execute()
  30     rospy.spin()
  31     sis.stop()
  32 
  33 if __name__=="__main__":
  34     main()

关于代码

   1 def monitor_cb(ud, msg):
   2     return False

参数是用户数据和消息。

当我们希望监控状态终止时,这需要返回False。在本例中,monitor状态将返回'invalid'

   1     sm = smach.StateMachine(outcomes=['DONE'])
   2     with sm:
   3         smach.StateMachine.add('FOO', 
   4                                smach_ros.MonitorState("/sm_reset", 
   5                                                       Empty, 
   6                                                       monitor_cb), 
   7                                transitions={'invalid':'BAR', 
   8                                             'valid':'FOO', 
   9                                             'preempted':'FOO'})

请注意,您的MonitorState需要定义所有三个可能结果的转换:('valid', 'invalid', 'preempted').

来自MonitorState的源代码:

def __init__(self, topic, msg_type, cond_cb, max_checks=-1)

max_check参数是monitor_cb在MonitorState返回'valid'之前调用多少次的限制。

Wiki: cn/smach/Tutorials/MonitorState (last edited 2018-03-10 07:44:26 by Playfish)