SMACH专题(二)----Concurrent状态机
Concurrent状态机是一种同时执行多个状态的状态机。如下图所示。状态FOO和BAR同时执行,当两个状态输出的结果同时满足一个组合条件时(FOO输出outcome2,BAR输出outcome1)才会映射到状态CON的输出结果outcome4。
1、简单例子
具体地,实现代码如下:
#!/usr/bin/env python import roslib; roslib.load_manifest('smach_example')
import time
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')
time.sleep(1)
if self.counter < 5:
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):
time.sleep(1)
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'}) # Create and start the introspection server
sis = smach_ros.IntrospectionServer('server_name', sm_top, '/SM_ROOT')
sis.start() # Execute SMACH plan
outcome = sm_top.execute()
rospy.spin()
sis.stop()
if __name__ == '__main__':
main()
2、复杂例子
定义复杂的输出结果条件判断,需要实现两个回调函数,如下代码的156行child_term_cb,178行的out_cb两个回调函数,具体说明,请参考代码处的文字解释。
实现代码如下:
#!/usr/bin/env python import roslib;
import time
import rospy
import smach
import smach_ros
from twisted.internet.defer import succeed # define state Bas
class Bas(smach.State):
def __init__(self):
smach.State.__init__(self, outcomes=['succeeded']) def execute(self, userdata):
time.sleep(4)
rospy.loginfo('Executing state BAS')
return 'succeeded' # define state Foo
class Foo(smach.State):
def __init__(self):
smach.State.__init__(self, outcomes=['succeeded','preempted','aborted'])
self.counter = 0 def execute(self, userdata):
rospy.loginfo('Executing state FOO')
time.sleep(1)
print "counter:%d"%(self.counter)
if self.counter < 5:
self.counter += 1
time.sleep(3)
return 'succeeded'
else:
return 'aborted' # define state Bar1
class Bar1(smach.State):
def __init__(self):
smach.State.__init__(self, outcomes=['succeeded','preempted','aborted'])
self.task_name = 'task_bar1'
def execute(self, userdata):
if self.preempt_requested():#如果暂停,则返回暂停状态
rospy.loginfo("Preempting %s"%(self.task_name))
self.recall_preempt()#唤醒,终止暂停
return 'preempted'
time.sleep(5)
rospy.loginfo('Executing state BAR1')
return 'succeeded' # define state Bar2
class Bar2(smach.State):
def __init__(self):
smach.State.__init__(self, outcomes=['succeeded','preempted','aborted'])
self.task_name ='bar2'
def execute(self, userdata):
if self.preempt_requested():#如果暂停,则返回暂停状态
rospy.loginfo("Preempting %s"%(self.task_name))
self.recall_preempt()#唤醒,终止暂停
return 'preempted'
time.sleep(5)
rospy.loginfo('Executing state BAR2')
return 'succeeded' # define state Bar3
class Bar3(smach.State):
def __init__(self):
smach.State.__init__(self, outcomes=['succeeded','preempted','aborted'])
self.task_name = 'task_bar3'
def execute(self, userdata):
if self.preempt_requested():#如果暂停,则返回暂停状态
rospy.loginfo("Preempting %s"%(self.task_name))
self.recall_preempt()#唤醒,终止暂停
return 'preempted'
time.sleep(5)
rospy.loginfo('Executing state BAR3')
return 'succeeded' # define state Charge
class Charge(smach.State):
def __init__(self):
smach.State.__init__(self, outcomes=['succeeded']) def execute(self, userdata):
time.sleep(5)
rospy.loginfo('Executing state BAS')
return 'succeeded' class ConcurrentExample:
def __init__(self):
rospy.init_node('smach_example_state_machine') self.last_bar_state = None
# Create the top level SMACH state machine
self.sm_top = smach.StateMachine(outcomes=['stop']) # Open the container
with self.sm_top: smach.StateMachine.add('BAS', Bas(),
transitions={'succeeded':'CON'}) # Create the sub SMACH state machine
self.sm_con = smach.Concurrence(outcomes=['succeeded','preempted','aborted'],
default_outcome='aborted',
#outcome_map = {'succeeded':{'FOO':'succeeded'},
# 'aborted':{'FOO':'aborted'}},
child_termination_cb = self.child_term_cb,
outcome_cb = self.out_cb
) # Open the container
with self.sm_con:
# Add states to the container
smach.Concurrence.add('FOO', Foo()) self.sm_bar = smach.StateMachine(outcomes=['succeeded','preempted','aborted'])
with self.sm_bar:
smach.StateMachine.add('BAR1',Bar1(),
transitions={'succeeded':'BAR2','preempted':'preempted'})
smach.StateMachine.add('BAR2',Bar2(),
transitions={'succeeded':'BAR3','preempted':'preempted'})
smach.StateMachine.add('BAR3',Bar3(),
transitions={'succeeded':'succeeded','preempted':'preempted'})
self.sm_bar.register_transition_cb(self.bar_transition_cb, cb_args=[])
smach.Concurrence.add('BAR', self.sm_bar) smach.StateMachine.add('CON', self.sm_con,
transitions={'succeeded':'stop',
'aborted':'stop',
'preempted':'CHARGE'}) smach.StateMachine.add('CHARGE', Charge(),
transitions={'succeeded':'CON'}) # Create and start the introspection server
sis = smach_ros.IntrospectionServer('server_name', self.sm_top, '/SM_ROOT')
sis.start() # Execute SMACH plan
outcome = self.sm_top.execute()
rospy.spin()
sis.stop() #状态之间转换的时候会调用该函数。比如BAR1转换到BAR2(或者BAR2转换到BAR3)后,执行该回调函数,
#那么活动的状态 active_states 是‘BAR2‘(‘BAR3‘)
def bar_transition_cb(self, userdata, active_states, *cb_args):
print active_states # 注意这里是字符串,活动状态的标识符例如‘BAR’
self.last_bar_state = active_states # gets called when ANY child state terminates,
# 只要Concurent下的其中一个状态完成,都会出发该回调函数
def child_term_cb(self, outcome_map): # terminate all running states if FOO preempted with outcome 'succeeded'
if outcome_map['FOO'] == 'succeeded':
print "child_term_cv:FOO finished"
if self.last_bar_state is not None: self.sm_bar.set_initial_state(self.last_bar_state, smach.UserData())
return True # terminate all running states if BAR preempted
if outcome_map['BAR']=='succeeded' or outcome_map['BAR']=='preempted':
print "child_term_cv:SM_BAR finished" return True # in all other case, just keep running, don't terminate anything
return False # gets called when ALL child states are terminated,只要Concurrent下的状态都结束了,
#调用该函数.注意不是BAR下面的BAR1,BAR2,BAR3的之一完成
def out_cb(self, outcome_map):
if outcome_map['FOO'] == 'aborted':
print "out_cb FOO aborted"
return 'aborted'
elif outcome_map['BAR'] == 'preempted': print "out_cb BAR preempted"
return 'preempted'
elif outcome_map['BAR'] == 'succeeded':
print "out_cb_BAR succeeded"
return 'succeeded'
if __name__ == '__main__':
ConcurrentExample()
状态机器效果图,CON下的FOO和BAR同时执行,如下所示:
CON进入暂停状态,切换到CHARGE状态下执行(绿色表示执行):
参考资料:
[1]. http://wiki.ros.org/smach/Tutorials/Concurrent%20States
[2]. ros_by_example_vol2_indigo.pdf
问题:只要BAR或FOO之一结束,就输出相应打结果,这个如何做到?还没找到方法
SMACH专题(二)----Concurrent状态机的更多相关文章
- 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...
- 「kuangbin带你飞」专题二十 斜率DP
layout: post title: 「kuangbin带你飞」专题二十 斜率DP author: "luowentaoaa" catalog: true tags: mathj ...
- 「kuangbin带你飞」专题二十二 区间DP
layout: post title: 「kuangbin带你飞」专题二十二 区间DP author: "luowentaoaa" catalog: true tags: - ku ...
- UI标签库专题二:JEECG智能开发平台Column(列) 子标签
UI标签库专题二:JEECG智能开发平台Column(列) 子标签 1.1. Column(列) 子标签 1.1.1. 演示样例 <t:dgCol title="年龄" ...
- 开发指南专题二:JEECG微云高速开发平台JEECG框架初探
开发指南专题二:JEECG微云高速开发平台JEECG框架初探 2.JEECG框架初探 2.1演示系统 打开浏览器输入JEECG演示环境界址:http://demo.jeecg.org:8090/能够看 ...
- SQL语句复习【专题二】
SQL语句复习[专题二] 单行函数(日期.数学.字符串.通用函数.转换函数)多行函数.分组函数.多行数据计算一个结果.一共5个.sum(),avg(),max(),min(),count()分组函数 ...
- SpringBoot之WEB开发-专题二
SpringBoot之WEB开发-专题二 三.Web开发 3.1.静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资 ...
- [.NET领域驱动设计实战系列]专题二:结合领域驱动设计的面向服务架构来搭建网上书店
一.前言 在前面专题一中,我已经介绍了我写这系列文章的初衷了.由于dax.net中的DDD框架和Byteart Retail案例并没有对其形成过程做一步步分析,而是把整个DDD的实现案例展现给我们,这 ...
- [你必须知道的NOSQL系列]专题二:Redis快速入门
一.前言 在前一篇博文介绍了MongoDB基本操作,本来打算这篇博文继续介绍MongoDB的相关内容的,例如索引,主从备份等内容的,但是发现这些内容都可以通过官方文档都可以看到,并且都非常详细,所以这 ...
随机推荐
- hibernate的枚举注解@Enumerated
@Enumerated(value=EnumType.ORDINAL)采用枚举类型的序号值与数据库进行交互, 此时数据库的数据类型需要是数值类型,例如在实际操作中 CatTest ct = new C ...
- 经典面试题:js继承方式下
上一篇讲解了构造函数的继承方式,今天来讲非构造函数的继承模式. 一.object()方法 json格式的发明人Douglas Crockford,提出了一个object()函数,可以做到这一点. fu ...
- git忽略特殊文件或文件夹
1.在项目目录中添加“.gitignore”文件,项目目录就是你存放git工程的目录就是有“.git”目录的目录 vi .gitignore 2.在文件中添加如下内容,其中“/runtime/”是忽略 ...
- python版本共存
要玩多版本最好使用虚拟环境,避免根python切换及包误安装的麻烦 1.直接安装实现 1.1 windows下 到官网(https://www.python.org/downloads/)下载,如py ...
- Tango ROS Streamer
谁想要在Android平台上编写机器人应用,或者谁希望扩展其与室内定位和3D感知新的传感器的机器人开发,Intermodalics创建的ROS Streamer应用的Tango. 这个Android应 ...
- day2编写购物商城(1)
作业:购物商城 商品展示,价格 买,加入购物车 付款,钱不够 具体实现了如下功能: 1.可购买的商品信息显示 2.显示购物车内的商品信息.数量.总金额 3.购物车内的商品数量进行增加.减少和商 ...
- Kibana安装及简单使用
Kibana安装 参照官方文档即可,这里只做相关操作记录: wget https://artifacts.elastic.co/downloads/kibana/kibana-5.5.0-linux- ...
- Gitlab-API各状态码解释
200 – OK : This means that the GET , PUT, or DELETE request was successful. When you request a resou ...
- hdoj1050 Moving Tables(贪心)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1050 题意 有一条走廊,走廊两边各有200个房间,一边的房间编号是奇数,另一边是偶数.现在有n个箱子需 ...
- 常见的mysql数据库sql语句的编写和运行结果
省份城市试题#省份表 -> select * from province;+----+----------+| id | province |+----+----------+| 1 | ...