Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
nonblocking_sequence.cpp
1 // Copyright (c) 2025 Polymath Robotics
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 <stdexcept>
16 #include <sstream>
17 #include <string>
18 
19 #include "nav2_behavior_tree/plugins/control/nonblocking_sequence.hpp"
20 
21 namespace nav2_behavior_tree
22 {
23 
25 : BT::ControlNode(name, {})
26 {
27 }
28 
30  const std::string & name,
31  const BT::NodeConfiguration & conf)
32 : BT::ControlNode(name, conf)
33 {
34 }
35 
36 BT::NodeStatus NonblockingSequence::tick()
37 {
38  bool all_success = true;
39 
40  for (std::size_t i = 0; i < children_nodes_.size(); ++i) {
41  auto status = children_nodes_[i]->executeTick();
42  switch (status) {
43  case BT::NodeStatus::FAILURE:
44  ControlNode::haltChildren();
45  all_success = false; // probably not needed
46  return status;
47  case BT::NodeStatus::SUCCESS:
48  break;
49  case BT::NodeStatus::RUNNING:
50  all_success = false;
51  break;
52  default:
53  std::stringstream error_msg;
54  error_msg << "Invalid node status. Received status " << status <<
55  "from child " << children_nodes_[i]->name();
56  throw std::runtime_error(error_msg.str());
57  }
58  }
59 
60  // Wrap up.
61  if (all_success) {
62  ControlNode::haltChildren();
63  return BT::NodeStatus::SUCCESS;
64  }
65 
66  return BT::NodeStatus::RUNNING;
67 }
68 
69 } // namespace nav2_behavior_tree
70 
71 BT_REGISTER_NODES(factory)
72 {
73  factory.registerNodeType<nav2_behavior_tree::NonblockingSequence>("NonblockingSequence");
74 }
Type of sequence node that keeps tickinng through all the children until all children return SUCCESS.
NonblockingSequence(const std::string &name)
A constructor for nav2_behavior_tree::NonblockingSequence.
BT::NodeStatus tick() override
The main override required by a BT action.