Nav2 Navigation Stack - kilted  kilted
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  unsigned skipped_count = 0;
39  for (std::size_t i = 0; i < children_nodes_.size(); ++i) {
40  auto status = children_nodes_[i]->executeTick();
41  switch (status) {
42  case BT::NodeStatus::FAILURE:
43  ControlNode::haltChildren();
44  last_child_ticked_ = 0; // reset
45  return status;
46  case BT::NodeStatus::SKIPPED:
47  skipped_count++;
48  // do nothing and continue on to the next child.
49  break;
50  case BT::NodeStatus::SUCCESS:
51  // do nothing and continue on to the next child. If it is the last child
52  // we'll exit the loop and hit the wrap-up code at the end of the method.
53  break;
54  case BT::NodeStatus::RUNNING:
55  if (i >= last_child_ticked_) {
56  last_child_ticked_ = i;
57  return status;
58  }
59  // else do nothing and continue on to the next child
60  break;
61  default:
62  std::stringstream error_msg;
63  error_msg << "Invalid node status. Received status " << status <<
64  "from child " << children_nodes_[i]->name();
65  throw std::runtime_error(error_msg.str());
66  }
67  }
68  // Wrap up.
69  ControlNode::haltChildren();
70  last_child_ticked_ = 0; // reset
71  if (skipped_count == children_nodes_.size()) {
72  // All the children were skipped
73  return BT::NodeStatus::SKIPPED;
74  }
75  return BT::NodeStatus::SUCCESS;
76 }
77 
79 {
80  BT::ControlNode::halt();
81  last_child_ticked_ = 0;
82 }
83 
84 } // namespace nav2_behavior_tree
85 
86 BT_REGISTER_NODES(factory)
87 {
88  factory.registerNodeType<nav2_behavior_tree::PipelineSequence>("PipelineSequence");
89 }
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.