Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
rate_controller.cpp
1 // Copyright (c) 2018 Intel Corporation
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/rate_controller.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_(false)
28 {
29 }
30 
32 {
33  double hz = 1.0;
34  getInput("hz", hz);
35  period_ = 1.0 / hz;
36 }
37 
38 BT::NodeStatus RateController::tick()
39 {
40  if (!BT::isStatusActive(status())) {
41  initialize();
42  // Reset the starting point since we're starting a new iteration of
43  // the rate controller (moving from IDLE to RUNNING)
44  start_ = std::chrono::high_resolution_clock::now();
45  first_time_ = true;
46  }
47 
48  setStatus(BT::NodeStatus::RUNNING);
49 
50  // Determine how long its been since we've started this iteration
51  auto now = std::chrono::high_resolution_clock::now();
52  auto elapsed = now - start_;
53 
54  // Now, get that in seconds
55  typedef std::chrono::duration<float> float_seconds;
56  auto seconds = std::chrono::duration_cast<float_seconds>(elapsed);
57 
58  // The child gets ticked the first time through and any time the period has
59  // expired. In addition, once the child begins to run, it is ticked each time
60  // 'til completion
61  if (first_time_ || (child_node_->status() == BT::NodeStatus::RUNNING) ||
62  seconds.count() >= period_)
63  {
64  first_time_ = false;
65  const BT::NodeStatus child_state = child_node_->executeTick();
66 
67  switch (child_state) {
68  case BT::NodeStatus::SKIPPED:
69  case BT::NodeStatus::RUNNING:
70  case BT::NodeStatus::FAILURE:
71  return child_state;
72 
73  case BT::NodeStatus::SUCCESS:
74  start_ = std::chrono::high_resolution_clock::now(); // Reset the timer
75  return BT::NodeStatus::SUCCESS;
76 
77  default:
78  return BT::NodeStatus::FAILURE;
79  }
80  }
81 
82  return status();
83 }
84 
85 } // namespace nav2_behavior_tree
86 
87 #include "behaviortree_cpp/bt_factory.h"
88 BT_REGISTER_NODES(factory)
89 {
90  factory.registerNodeType<nav2_behavior_tree::RateController>("RateController");
91 }
A BT::DecoratorNode that ticks its child at a specified rate.
void initialize()
Function to read parameters and initialize class variables.
RateController(const std::string &name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::RateController.