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

Concurrence容器

Description: 本教程教会你如何使用Concurrence容器

Tutorial Level: BEGINNER

Next Tutorial: Sequence容器

   1 from smach import Concurrence

指定concurrence输出

Concurrence输出图

SMACH并发的输出图指定了基于其子结果确定并发结果的策略。具体来说,map是一个字典,其中键是并发的潜在结果,而值是将子标签映射到子结果的词典。一旦并发的所有状态都终止了,如果满足了这些子结果映射的一个,并发将返回其关联的结果。如果没有映射得到满足,则并发将返回其默认结果。

   1 cc = Concurrence(outcomes = ['outcome1', 'outcome2'],
   2                  default_outcome = 'outcome1',
   3                  input_keys = ['sm_input'],
   4                  output_keys = ['sm_output'],
   5                  outcome_map = {'succeeded':{'FOO':'succeeded',
   6                                              'BAR':'outcome2'},
   7                                 'outcome3':{'FOO':'outcome2'}})
   8 with cc:
   9     Concurrence.add('FOO', Foo())
  10     Concurrence.add('BAR', Bar())

上面的示例指定了以下策略:

  • 当'FOO'有结果'成功','BAR'有结果'outcome2'时,状态机将退出,结果'成功'。
  • 当'FOO'有结果'outcome2', 状态机将以'outcome3'为结果退出, 独立于状态BAR的结果。

回调

如果你想要完整控制一整个concurrence状态机,你可以使用它提供的回调:child_termination_cb和outcome_cb:

   1 # gets called when ANY child state terminates
   2 def child_term_cb(outcome_map):
   3 
   4   # terminate all running states if FOO finished with outcome 'outcome3'
   5   if outcome_map['FOO'] == 'outcome3':
   6     return True
   7 
   8   # terminate all running states if BAR finished
   9   if outcome_map['BAR']:
  10     return True
  11 
  12   # in all other case, just keep running, don't terminate anything
  13   return False
  14 
  15 
  16 # gets called when ALL child states are terminated
  17 def out_cb(outcome_map):
  18    if outcome_map['FOO'] == 'succeeded':
  19       return 'outcome1'
  20    else:
  21       return 'outcome2'
  22 
  23 
  24 # creating the concurrence state machine
  25 sm = Concurrence(outcomes=['outcome1', 'outcome2'],
  26                  default_outcome='outcome1',
  27                  input_keys=['sm_input'],
  28                  output_keys=['sm_output'],
  29                  child_termination_cb = child_term_cb,
  30                  outcome_cb = out_cb)
  31                  
  32 with sm:
  33    Concurrence.add('FOO', Foo(),
  34                    remapping={'foo_in':'input'})
  35 
  36    Concurrence.add('BAR', Bar(),
  37                    remapping={'bar_out':'bar_out'})

  • 当一个子状态终止时,将调用child_termination_cb。在回调函数中,你可以决定状态机是否应该继续运行(返回False),或者是否应该抢占所有剩余的运行状态(返回True)。
  • 当最后一个子状态终止时,outcome_cb被调用一次。这个回调返回并发状态机的结果。

Wiki: cn/smach/Tutorials/Concurrence container (last edited 2018-03-10 06:50:11 by Playfish)