15 #ifndef RCLCPP__RATE_HPP_
16 #define RCLCPP__RATE_HPP_
22 #include "rclcpp/macros.hpp"
23 #include "rclcpp/utilities.hpp"
24 #include "rclcpp/visibility_control.hpp"
32 RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(
RateBase)
35 virtual bool sleep() = 0;
36 virtual bool is_steady()
const = 0;
37 virtual void reset() = 0;
40 using std::chrono::duration;
41 using std::chrono::duration_cast;
42 using std::chrono::nanoseconds;
44 template<
class Clock = std::chrono::high_resolution_clock>
52 duration_cast<nanoseconds>(duration<double>(1.0 / rate)))
54 explicit GenericRate(std::chrono::nanoseconds period)
55 : period_(period), last_interval_(
Clock::now())
64 auto next_interval = last_interval_ + period_;
66 if (now < last_interval_) {
68 next_interval = now + period_;
71 auto time_to_sleep = next_interval - now;
73 last_interval_ += period_;
75 if (time_to_sleep <= std::chrono::seconds(0)) {
79 if (now > next_interval + period_) {
80 last_interval_ = now + period_;
93 return Clock::is_steady;
102 std::chrono::nanoseconds period()
const
110 std::chrono::nanoseconds period_;
111 using ClockDurationNano = std::chrono::duration<typename Clock::rep, std::nano>;
112 std::chrono::time_point<Clock, ClockDurationNano> last_interval_;
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC bool sleep_for(const std::chrono::nanoseconds &nanoseconds, rclcpp::Context::SharedPtr context=nullptr)
Use the global condition variable to block for the specified amount of time.