Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
single_trigger_node.cpp
1 // Copyright (c) 2021 Samsung Research America
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 <chrono>
16 #include <string>
17 
18 #include "nav2_behavior_tree/plugins/decorator/single_trigger_node.hpp"
19 
20 namespace nav2_behavior_tree
21 {
22 
24  const std::string & name,
25  const BT::NodeConfiguration & conf)
26 : BT::DecoratorNode(name, conf),
27  first_time_(true)
28 {
29 }
30 
31 BT::NodeStatus SingleTrigger::tick()
32 {
33  if (!BT::isStatusActive(status())) {
34  first_time_ = true;
35  }
36 
37  setStatus(BT::NodeStatus::RUNNING);
38 
39  if (first_time_) {
40  const BT::NodeStatus child_state = child_node_->executeTick();
41 
42  switch (child_state) {
43  case BT::NodeStatus::SKIPPED:
44  case BT::NodeStatus::RUNNING:
45  return child_state;
46 
47  case BT::NodeStatus::FAILURE:
48  case BT::NodeStatus::SUCCESS:
49  first_time_ = false;
50  return child_state;
51 
52  default:
53  first_time_ = false;
54  return BT::NodeStatus::FAILURE;
55  }
56  }
57 
58  return BT::NodeStatus::FAILURE;
59 }
60 
61 } // namespace nav2_behavior_tree
62 
63 #include "behaviortree_cpp/bt_factory.h"
64 BT_REGISTER_NODES(factory)
65 {
66  factory.registerNodeType<nav2_behavior_tree::SingleTrigger>("SingleTrigger");
67 }
A BT::DecoratorNode that triggers its child only once and returns FAILURE for every succeeding tick (...
SingleTrigger(const std::string &name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::SingleTrigger.