ROS smach-----Concurrence state machine

1 Concurrence Outcome Map

smach Concurrence通过outcome map定义Concurrence的outcome,具体说就是基于concurrence状态机的孩子(即包含的状态)的outcome来确定concurrence的outcome。

基于outcome map如果concurrence状态机某个孩子的outcome满足,那么concurrence会有对应的outcome输出;如果没有一个孩子outcome满足map条件,则concurrence输出default outcome。

 

cc = Concurrence(outcomes = ['outcome1', 'outcome2'],
                 default_outcome = 'outcome1',
                 input_keys = ['sm_input'],
                 output_keys = ['sm_output'],
                 outcome_map = {'succeeded':{'FOO':'succeeded',
                                             'BAR':'outcome2'},
                                'outcome3':{'FOO':'outcome2'}})
with cc:
    Concurrence.add('FOO', Foo())
    Concurrence.add('BAR', Bar()

  • 当孩子'FOO' 输出'succeeded' 并且孩子 'BAR'输出'outcome2'时候, 状态机Concurrence将结束并且输出 'succeeded'
  • 当孩子 'FOO' 输出 'outcome2', 状态机Concurrence将结束并且输出 'outcome3',这时与孩子BAR输出无关 


2 Callbacks

如果要完全灵活掌控状态机Concurrence的outcome,可以使用提供的回调函数child_termination_cb和the outcome_cb。


# gets called when ANY child state terminates
def child_term_cb(outcome_map):

  # terminate all running states if FOO finished with outcome 'outcome3'
  if outcome_map['FOO'] == 'outcome3':
    return True

  # terminate all running states if BAR finished
  if outcome_map['BAR']:
    return True

  # in all other case, just keep running, don't terminate anything
  return False


# gets called when ALL child states are terminated
def out_cb(outcome_map):
   if outcome_map['FOO'] == 'succeeded':
      return 'outcome1'
   else:
      return 'outcome2'


# creating the concurrence state machine
sm = Concurrence(outcomes=['outcome1', 'outcome2'],
                 default_outcome='outcome1',
                 input_keys=['sm_input'],
                 output_keys=['sm_output'],
                 child_termination_cb = child_term_cb,
                 outcome_cb = out_cb)
                 
with sm:
   Concurrence.add('FOO', Foo(),
                   remapping={'foo_in':'input'})

   Concurrence.add('BAR', Bar(),
                   remapping={'bar_out':'bar_out'})

  • 只要有一个孩子状态完成child_termination_cb就会被调用,在回调函数中你可以决定状态机继续运行还是结束所有在运行状态
  • 当最后一个孩子状态完成时调用outcome_cb, 将返回concurrence的outcome。



3 example

#!/usr/bin/env python

import roslib; roslib.load_manifest('smach_tutorials')
import rospy
import smach
import smach_ros

# define state Foo
class Foo(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['outcome1','outcome2'])
        self.counter = 0

    def execute(self, userdata):
        rospy.loginfo('Executing state FOO')
        if self.counter < 3:
            self.counter += 1
            return 'outcome1'
        else:
            return 'outcome2'


# define state Bar
class Bar(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['outcome1'])

    def execute(self, userdata):
        rospy.loginfo('Executing state BAR')
        return 'outcome1'
        


# define state Bas
class Bas(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['outcome3'])

    def execute(self, userdata):
        rospy.loginfo('Executing state BAS')
        return 'outcome3'




def main():
    rospy.init_node('smach_example_state_machine')

    # Create the top level SMACH state machine
    sm_top = smach.StateMachine(outcomes=['outcome6'])
    
    # Open the container
    with sm_top:

        smach.StateMachine.add('BAS', Bas(),
                               transitions={'outcome3':'CON'})

        # Create the sub SMACH state machine
        sm_con = smach.Concurrence(outcomes=['outcome4','outcome5'],
                                   default_outcome='outcome4',
                                   outcome_map={'outcome5':
                                       { 'FOO':'outcome2',
                                         'BAR':'outcome1'}})

        # Open the container
        with sm_con:
            # Add states to the container
            smach.Concurrence.add('FOO', Foo())
            smach.Concurrence.add('BAR', Bar())

        smach.StateMachine.add('CON', sm_con,
                               transitions={'outcome4':'CON',
                                            'outcome5':'outcome6'})

    # Execute SMACH plan
    outcome = sm_top.execute()


if __name__ == '__main__':
    main()

运行结果:

[INFO] [WallTime: 1478584578.397681] State machine starting in initial state 'BAS' with userdata:
    []
[INFO] [WallTime: 1478584578.398319] Executing state BAS
[INFO] [WallTime: 1478584578.398788] State machine transitioning 'BAS':'outcome3'-->'CON'
[INFO] [WallTime: 1478584578.399252] Concurrence starting with userdata:
    []
[INFO] [WallTime: 1478584578.401027] Executing state FOO
[INFO] [WallTime: 1478584578.401614] Executing state BAR
[INFO] [WallTime: 1478584578.402149] Concurrent state 'FOO' returned outcome 'outcome1' on termination.
[INFO] [WallTime: 1478584578.402706] Concurrent state 'BAR' returned outcome 'outcome1' on termination.
[INFO] [WallTime: 1478584578.404882] Concurrent Outcomes: {'FOO': 'outcome1', 'BAR': 'outcome1'}
[INFO] [WallTime: 1478584578.405480] State machine transitioning 'CON':'outcome4'-->'CON'
[INFO] [WallTime: 1478584578.405959] Concurrence starting with userdata:
    []
[INFO] [WallTime: 1478584578.406966] Executing state FOO
[INFO] [WallTime: 1478584578.407653] Executing state BAR
[INFO] [WallTime: 1478584578.408183] Concurrent state 'FOO' returned outcome 'outcome1' on termination.
[INFO] [WallTime: 1478584578.408720] Concurrent state 'BAR' returned outcome 'outcome1' on termination.
[INFO] [WallTime: 1478584578.410798] Concurrent Outcomes: {'FOO': 'outcome1', 'BAR': 'outcome1'}
[INFO] [WallTime: 1478584578.411496] State machine transitioning 'CON':'outcome4'-->'CON'
[INFO] [WallTime: 1478584578.412002] Concurrence starting with userdata:
    []
[INFO] [WallTime: 1478584578.413100] Executing state FOO
[INFO] [WallTime: 1478584578.413924] Executing state BAR
[INFO] [WallTime: 1478584578.414754] Concurrent state 'FOO' returned outcome 'outcome1' on termination.
[INFO] [WallTime: 1478584578.415496] Concurrent state 'BAR' returned outcome 'outcome1' on termination.
[INFO] [WallTime: 1478584578.417591] Concurrent Outcomes: {'FOO': 'outcome1', 'BAR': 'outcome1'}
[INFO] [WallTime: 1478584578.418109] State machine transitioning 'CON':'outcome4'-->'CON'
[INFO] [WallTime: 1478584578.418569] Concurrence starting with userdata:
    []
[INFO] [WallTime: 1478584578.419639] Executing state FOO
[INFO] [WallTime: 1478584578.420264] Executing state BAR
[INFO] [WallTime: 1478584578.420775] Concurrent state 'FOO' returned outcome 'outcome2' on termination.
[INFO] [WallTime: 1478584578.421183] Concurrent state 'BAR' returned outcome 'outcome1' on termination.
[INFO] [WallTime: 1478584578.423097] Concurrent Outcomes: {'FOO': 'outcome2', 'BAR': 'outcome1'}
[INFO] [WallTime: 1478584578.423607] State machine terminating 'CON':'outcome5':'outcome6'




最后一次状态转变用图可以表示为:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ROS2 Web Bridge是一个开源的软件包,旨在使ROS2(Robot Operating System 2)与Web端进行通信和交互。它提供了一种简单而强大的方式,通过WebSocket协议将ROS2系统中的数据传输到Web浏览器。 ROS2 Web Bridge允许Web开发人员使用常见的Web技术(例如JavaScript)直接与ROS2系统进行交互。它提供了一个轻量级的接口,可以订阅和发布ROS2主题,并在Web浏览器中实时显示传感器数据、控制机器人等。 此外,ROS2 Web Bridge还支持ROS2服务和动作。它允许Web应用程序在Web端调用ROS2服务,从而实现与ROS2节点的双向通信。通过一个用户友好的Web界面,用户可以发送控制命令给机器人,执行任务并获得实时反馈。 ROS2 Web Bridge的主要优点之一是其跨平台性。它基于WebSocket协议,因此可以在不同的操作系统和设备上使用,无论是在PC端还是移动设备上。 此外,ROS2 Web Bridge还支持认证和授权机制,以确保通信安全。这对于确保只有被授权的用户可以访问和控制ROS2系统非常重要。 总的来说,ROS2 Web Bridge为ROS2系统提供了一种简便而强大的方式,使Web开发人员能够与ROS2系统进行交互。它扩展了ROS2的功能,使得机器人开发更加灵活和可视化。 ### 回答2: ros2-web-bridge是一种用于在ROS 2和Web应用程序之间进行通信的桥接工具。ROS 2是机器人操作系统的第二代版本,而Web应用程序是通过Web浏览器访问的应用程序。 ros2-web-bridge有几个主要功能。首先,它允许ROS 2中的节点直接与通过Web浏览器访问的Web应用程序进行通信。这使得在Web界面上实时监控和控制ROS 2系统变得更加容易。例如,可以使用ros2-web-bridge将传感器数据从ROS 2节点发送到Web应用程序,以便在Web界面上实时显示传感器数据。同时,还可以将来自Web界面的用户输入发送到ROS 2节点,以便远程控制机器人或系统。 其次,ros2-web-bridge还提供了一些工具和API,用于将ROS 2中的消息和服务转换为Web格式。这使得可以轻松地在ROS 2和Web应用程序之间进行数据传输和交互。它支持使用ROS 2的套接字和JSON进行通信,并提供了将消息和服务转换为JSON格式以及反向转换的功能。这样,ROS 2节点可以与通过Web浏览器访问的Web应用程序进行无缝通信。 总而言之,ros2-web-bridge为ROS 2和Web应用程序之间的通信提供了一个便捷的桥梁。它简化了ROS 2系统与Web界面之间的集成,并提供了实时数据传输和远程控制的能力。这对于构建基于ROS 2的机器人或系统的开发者和使用者来说是非常方便的工具。 ### 回答3: ros2-web-bridge是一个用于将ROS 2和Web技术进行连接的工具。它提供了一个桥接器,使得可以通过Web浏览器与ROS 2通信。这个工具是建立在ROS 2和Web Socket之间的通信基础上的。 通过ros2-web-bridge,我们可以在Web浏览器中实时地订阅和发布ROS 2的消息。这使得我们可以通过Web界面来控制ROS 2的机器人,或者将ROS 2的数据可视化展示出来。这对于远程监控、远程操作和数据可视化都非常有用。 ros2-web-bridge使用ROS 2提供的接口来与ROS 2系统进行通信。它将ROS 2的消息转换为适用于Web Socket的格式,并在浏览器和ROS 2之间建立起适配的连接。通过这种方式,Web界面就能够与ROS 2系统进行实时的双向通信。 ROS 2中的消息传递方式是异步的,而Web浏览器通常使用同步的方式进行通信。ros2-web-bridge通过在ROS 2和Web Socket之间进行适配和转换,使得二者能够协同工作。这意味着ROS 2中的数据可以通过ros2-web-bridge传输到Web浏览器,并在该浏览器中进行处理和展示。 总的来说,ros2-web-bridge是一个功能强大的工具,它架起了ROS 2和Web技术之间的桥梁。它使得我们可以通过Web浏览器来与ROS 2进行通信,进而实现远程操作和数据可视化的目的。通过ros2-web-bridge,我们可以更加灵活和方便地利用ROS 2的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值