Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
rate.hpp
1 // Copyright (c) 2026 Dexory
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 #ifndef NAV2_ROS_COMMON__RATE_HPP_
16 #define NAV2_ROS_COMMON__RATE_HPP_
17 
18 #include <chrono>
19 #include <functional>
20 #include <memory>
21 #include <utility>
22 
23 #include "rclcpp/rate.hpp"
24 #include "rclcpp/create_timer.hpp"
25 
26 namespace nav2
27 {
28 
39 template<typename NodeT>
40 rclcpp::Clock::SharedPtr selectSteadyOrSimClock(NodeT node)
41 {
42  bool use_sim_time = false;
43  auto params = node->get_node_parameters_interface();
44  if (params->has_parameter("use_sim_time")) {
45  use_sim_time = params->get_parameter("use_sim_time").as_bool();
46  }
47  if (use_sim_time) {
48  return node->get_clock();
49  }
50  return std::make_shared<rclcpp::Clock>(RCL_STEADY_TIME);
51 }
52 
60 class Rate : public rclcpp::Rate
61 {
62 public:
68  template<typename NodeT>
69  explicit Rate(NodeT node, double rate)
70  : rclcpp::Rate(rate, selectSteadyOrSimClock(node))
71  {}
72 };
73 
74 } // namespace nav2
75 
76 #endif // NAV2_ROS_COMMON__RATE_HPP_
A sim-time-aware rate for Nav2 loops.
Definition: rate.hpp:61
Rate(NodeT node, double rate)
Construct a Rate from a node and frequency.
Definition: rate.hpp:69