Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
pipeline_sequence.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 <stdexcept>
16 #include <sstream>
17 #include <string>
18 
19 #include "nav2_behavior_tree/plugins/control/pipeline_sequence.hpp"
20 
21 namespace nav2_behavior_tree
22 {
23 
24 PipelineSequence::PipelineSequence(const std::string & name)
25 : BT::ControlNode(name, {})
26 {
27 }
28 
30  const std::string & name,
31  const BT::NodeConfiguration & config)
32 : BT::ControlNode(name, config)
33 {
34 }
35 
36 BT::NodeStatus PipelineSequence::tick()
37 {
38  for (std::size_t i = 0; i < children_nodes_.size(); ++i) {
39  auto status = children_nodes_[i]->executeTick();
40  switch (status) {
41  case BT::NodeStatus::FAILURE:
42  ControlNode::haltChildren();
43  last_child_ticked_ = 0; // reset
44  return status;
45  case BT::NodeStatus::SUCCESS:
46  // do nothing and continue on to the next child. If it is the last child
47  // we'll exit the loop and hit the wrap-up code at the end of the method.
48  break;
49  case BT::NodeStatus::RUNNING:
50  if (i >= last_child_ticked_) {
51  last_child_ticked_ = i;
52  return status;
53  }
54  // else do nothing and continue on to the next child
55  break;
56  default:
57  std::stringstream error_msg;
58  error_msg << "Invalid node status. Received status " << status <<
59  "from child " << children_nodes_[i]->name();
60  throw std::runtime_error(error_msg.str());
61  }
62  }
63  // Wrap up.
64  ControlNode::haltChildren();
65  last_child_ticked_ = 0; // reset
66  return BT::NodeStatus::SUCCESS;
67 }
68 
70 {
71  BT::ControlNode::halt();
72  last_child_ticked_ = 0;
73 }
74 
75 } // namespace nav2_behavior_tree
76 
77 BT_REGISTER_NODES(factory)
78 {
79  factory.registerNodeType<nav2_behavior_tree::PipelineSequence>("PipelineSequence");
80 }
Type of sequence node that re-ticks previous children when a child returns running.
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.
PipelineSequence(const std::string &name)
A constructor for nav2_behavior_tree::PipelineSequence.