Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
path_expiring_timer_condition.cpp
1 // Copyright (c) 2022 Joshua Wallace
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 <string>
16 #include <memory>
17 
18 #include "behaviortree_cpp/condition_node.h"
19 
20 #include "nav2_behavior_tree/plugins/condition/path_expiring_timer_condition.hpp"
21 
22 namespace nav2_behavior_tree
23 {
24 
25 PathExpiringTimerCondition::PathExpiringTimerCondition(
26  const std::string & condition_name,
27  const BT::NodeConfiguration & conf)
28 : BT::ConditionNode(condition_name, conf),
29  period_(1.0),
30  first_time_(true)
31 {
32  node_ = config().blackboard->get<rclcpp::Node::SharedPtr>("node");
33 }
34 
36 {
37  if (first_time_) {
38  getInput("seconds", period_);
39  getInput("path", prev_path_);
40  first_time_ = false;
41  start_ = node_->now();
42  return BT::NodeStatus::FAILURE;
43  }
44 
45  // Grab the new path
46  nav_msgs::msg::Path path;
47  getInput("path", path);
48 
49  // Reset timer if the path has been updated
50  if (prev_path_ != path) {
51  prev_path_ = path;
52  start_ = node_->now();
53  }
54 
55  // Determine how long its been since we've started this iteration
56  auto elapsed = node_->now() - start_;
57 
58  // Now, get that in seconds
59  auto seconds = elapsed.seconds();
60 
61  if (seconds < period_) {
62  return BT::NodeStatus::FAILURE;
63  }
64 
65  start_ = node_->now(); // Reset the timer
66  return BT::NodeStatus::SUCCESS;
67 }
68 
69 } // namespace nav2_behavior_tree
70 
71 #include "behaviortree_cpp/bt_factory.h"
72 BT_REGISTER_NODES(factory)
73 {
74  factory.registerNodeType<nav2_behavior_tree::PathExpiringTimerCondition>("PathExpiringTimer");
75 }
A BT::ConditionNode that returns SUCCESS every time a specified time period passes and FAILURE otherw...
BT::NodeStatus tick() override
The main override required by a BT action.