Nav2 Navigation Stack - humble  humble
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 (status() == BT::NodeStatus::IDLE) {
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::RUNNING:
44  return BT::NodeStatus::RUNNING;
45 
46  case BT::NodeStatus::SUCCESS:
47  first_time_ = false;
48  return BT::NodeStatus::SUCCESS;
49 
50  case BT::NodeStatus::FAILURE:
51  first_time_ = false;
52  return BT::NodeStatus::FAILURE;
53 
54  default:
55  first_time_ = false;
56  return BT::NodeStatus::FAILURE;
57  }
58  }
59 
60  return BT::NodeStatus::FAILURE;
61 }
62 
63 } // namespace nav2_behavior_tree
64 
65 #include "behaviortree_cpp_v3/bt_factory.h"
66 BT_REGISTER_NODES(factory)
67 {
68  factory.registerNodeType<nav2_behavior_tree::SingleTrigger>("SingleTrigger");
69 }
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.