Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
time_expired_condition.cpp
1 // Copyright (c) 2019 Intel Corporation
2 // Copyright (c) 2020 Sarthak Mittal
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include <string>
17 #include <memory>
18 
19 #include "behaviortree_cpp_v3/condition_node.h"
20 
21 #include "nav2_behavior_tree/plugins/condition/time_expired_condition.hpp"
22 
23 namespace nav2_behavior_tree
24 {
25 
26 TimeExpiredCondition::TimeExpiredCondition(
27  const std::string & condition_name,
28  const BT::NodeConfiguration & conf)
29 : BT::ConditionNode(condition_name, conf),
30  period_(1.0)
31 {
32  getInput("seconds", period_);
33  node_ = config().blackboard->get<rclcpp::Node::SharedPtr>("node");
34  start_ = node_->now();
35 }
36 
38 {
39  if (status() == BT::NodeStatus::IDLE) {
40  start_ = node_->now();
41  return BT::NodeStatus::FAILURE;
42  }
43 
44  // Determine how long its been since we've started this iteration
45  auto elapsed = node_->now() - start_;
46 
47  // Now, get that in seconds
48  auto seconds = elapsed.seconds();
49 
50  if (seconds < period_) {
51  return BT::NodeStatus::FAILURE;
52  }
53 
54  start_ = node_->now(); // Reset the timer
55  return BT::NodeStatus::SUCCESS;
56 }
57 
58 } // namespace nav2_behavior_tree
59 
60 #include "behaviortree_cpp_v3/bt_factory.h"
61 BT_REGISTER_NODES(factory)
62 {
63  factory.registerNodeType<nav2_behavior_tree::TimeExpiredCondition>("TimeExpired");
64 }
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.