Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
round_robin_node.cpp
1 // Copyright (c) 2019 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string>
16 
17 #include "nav2_behavior_tree/plugins/control/round_robin_node.hpp"
18 
19 namespace nav2_behavior_tree
20 {
21 
22 RoundRobinNode::RoundRobinNode(const std::string & name)
23 : BT::ControlNode::ControlNode(name, {})
24 {
25 }
26 
28  const std::string & name,
29  const BT::NodeConfiguration & config)
30 : BT::ControlNode(name, config)
31 {
32 }
33 
34 BT::NodeStatus RoundRobinNode::tick()
35 {
36  const auto num_children = children_nodes_.size();
37 
38  setStatus(BT::NodeStatus::RUNNING);
39 
40  while (num_failed_children_ < num_children) {
41  TreeNode * child_node = children_nodes_[current_child_idx_];
42  const BT::NodeStatus child_status = child_node->executeTick();
43 
44  switch (child_status) {
45  case BT::NodeStatus::SUCCESS:
46  {
47  // Wrap around to the first child
48  if (++current_child_idx_ >= num_children) {
49  current_child_idx_ = 0;
50  }
51  num_failed_children_ = 0;
52  ControlNode::haltChildren();
53  return BT::NodeStatus::SUCCESS;
54  }
55 
56  case BT::NodeStatus::FAILURE:
57  {
58  if (++current_child_idx_ >= num_children) {
59  current_child_idx_ = 0;
60  }
61  num_failed_children_++;
62  break;
63  }
64 
65  case BT::NodeStatus::RUNNING:
66  {
67  return BT::NodeStatus::RUNNING;
68  }
69 
70  default:
71  {
72  throw BT::LogicError("Invalid status return from BT node");
73  }
74  }
75  }
76 
77  halt();
78  return BT::NodeStatus::FAILURE;
79 }
80 
82 {
83  ControlNode::halt();
84  current_child_idx_ = 0;
85  num_failed_children_ = 0;
86 }
87 
88 } // namespace nav2_behavior_tree
89 
90 BT_REGISTER_NODES(factory)
91 {
92  factory.registerNodeType<nav2_behavior_tree::RoundRobinNode>("RoundRobin");
93 }
Type of sequence node that ticks children in a round-robin fashion.
void halt() override
The other (optional) override required by a BT action to reset node state.
BT::NodeStatus tick() override
The main override required by a BT action.
RoundRobinNode(const std::string &name)
A constructor for nav2_behavior_tree::RoundRobinNode.