Nav2 Navigation Stack - lyrical  lyrical
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/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 }
33 
35 {
36  getInput("seconds", period_);
37  node_ = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
38  start_ = node_->now();
39 }
40 
42 {
43  if (!BT::isStatusActive(status())) {
44  initialize();
45  start_ = node_->now();
46  return BT::NodeStatus::FAILURE;
47  }
48 
49  // Determine how long it's been since we've started this iteration
50  auto elapsed = node_->now() - start_;
51 
52  // Now, get that in seconds
53  auto seconds = elapsed.seconds();
54 
55  if (seconds < period_) {
56  return BT::NodeStatus::FAILURE;
57  }
58 
59  start_ = node_->now(); // Reset the timer
60  return BT::NodeStatus::SUCCESS;
61 }
62 
63 } // namespace nav2_behavior_tree
64 
65 #include "behaviortree_cpp/bt_factory.h"
66 BT_REGISTER_NODES(factory)
67 {
68  factory.registerNodeType<nav2_behavior_tree::TimeExpiredCondition>("TimeExpired");
69 }
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.
void initialize()
Function to read parameters and initialize class variables.