Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
recovery_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 #include "nav2_behavior_tree/plugins/control/recovery_node.hpp"
17 
18 namespace nav2_behavior_tree
19 {
20 
22  const std::string & name,
23  const BT::NodeConfiguration & conf)
24 : BT::ControlNode::ControlNode(name, conf),
25  current_child_idx_(0),
26  number_of_retries_(1),
27  retry_count_(0)
28 {
29  getInput("number_of_retries", number_of_retries_);
30 }
31 
32 BT::NodeStatus RecoveryNode::tick()
33 {
34  const unsigned children_count = children_nodes_.size();
35 
36  if (children_count != 2) {
37  throw BT::BehaviorTreeException("Recovery Node '" + name() + "' must only have 2 children.");
38  }
39 
40  setStatus(BT::NodeStatus::RUNNING);
41 
42  while (current_child_idx_ < children_count && retry_count_ <= number_of_retries_) {
43  TreeNode * child_node = children_nodes_[current_child_idx_];
44  const BT::NodeStatus child_status = child_node->executeTick();
45 
46  if (current_child_idx_ == 0) {
47  switch (child_status) {
48  case BT::NodeStatus::SUCCESS:
49  {
50  // reset node and return success when first child returns success
51  halt();
52  return BT::NodeStatus::SUCCESS;
53  }
54 
55  case BT::NodeStatus::FAILURE:
56  {
57  if (retry_count_ < number_of_retries_) {
58  // halt first child and tick second child in next iteration
59  ControlNode::haltChild(0);
60  current_child_idx_++;
61  break;
62  } else {
63  // reset node and return failure when max retries has been exceeded
64  halt();
65  return BT::NodeStatus::FAILURE;
66  }
67  }
68 
69  case BT::NodeStatus::RUNNING:
70  {
71  return BT::NodeStatus::RUNNING;
72  }
73 
74  default:
75  {
76  throw BT::LogicError("A child node must never return IDLE");
77  }
78  } // end switch
79 
80  } else if (current_child_idx_ == 1) {
81  switch (child_status) {
82  case BT::NodeStatus::SUCCESS:
83  {
84  // halt second child, increment recovery count, and tick first child in next iteration
85  ControlNode::haltChild(1);
86  retry_count_++;
87  current_child_idx_--;
88  }
89  break;
90 
91  case BT::NodeStatus::FAILURE:
92  {
93  // reset node and return failure if second child fails
94  halt();
95  return BT::NodeStatus::FAILURE;
96  }
97 
98  case BT::NodeStatus::RUNNING:
99  {
100  return BT::NodeStatus::RUNNING;
101  }
102 
103  default:
104  {
105  throw BT::LogicError("A child node must never return IDLE");
106  }
107  } // end switch
108  }
109  } // end while loop
110 
111  // reset node and return failure
112  halt();
113  return BT::NodeStatus::FAILURE;
114 }
115 
116 void RecoveryNode::halt()
117 {
118  ControlNode::halt();
119  retry_count_ = 0;
120  current_child_idx_ = 0;
121 }
122 
123 } // namespace nav2_behavior_tree
124 
125 #include "behaviortree_cpp_v3/bt_factory.h"
126 BT_REGISTER_NODES(factory)
127 {
128  factory.registerNodeType<nav2_behavior_tree::RecoveryNode>("RecoveryNode");
129 }
The RecoveryNode has only two children and returns SUCCESS if and only if the first child returns SUC...
RecoveryNode(const std::string &name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::RecoveryNode.