15 #ifndef RCLCPP__RATE_HPP_
16 #define RCLCPP__RATE_HPP_
22 #include "rclcpp/clock.hpp"
23 #include "rclcpp/duration.hpp"
24 #include "rclcpp/macros.hpp"
25 #include "rclcpp/utilities.hpp"
26 #include "rclcpp/visibility_control.hpp"
34 RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(
RateBase)
40 virtual bool sleep() = 0;
42 [[deprecated(
"use get_type() instead")]]
44 virtual bool is_steady()
const = 0;
50 virtual void reset() = 0;
53 using std::chrono::duration;
54 using std::chrono::duration_cast;
55 using std::chrono::nanoseconds;
57 template<
class Clock = std::chrono::high_resolution_clock>
64 : period_(duration_cast<nanoseconds>(duration<double>(1.0 / rate))), last_interval_(
Clock::now())
66 explicit GenericRate(std::chrono::nanoseconds period)
67 : period_(period), last_interval_(
Clock::now())
76 auto next_interval = last_interval_ + period_;
78 if (now < last_interval_) {
80 next_interval = now + period_;
83 auto time_to_sleep = next_interval - now;
85 last_interval_ += period_;
87 if (time_to_sleep <= std::chrono::seconds(0)) {
91 if (now > next_interval + period_) {
92 last_interval_ = now + period_;
102 [[deprecated(
"use get_type() instead")]]
106 return Clock::is_steady;
120 std::chrono::nanoseconds period()
const
128 std::chrono::nanoseconds period_;
129 using ClockDurationNano = std::chrono::duration<typename Clock::rep, std::nano>;
130 std::chrono::time_point<Clock, ClockDurationNano> last_interval_;
136 RCLCPP_SMART_PTR_DEFINITIONS(
Rate)
152 [[deprecated(
"use get_type() instead")]]
166 std::chrono::nanoseconds
170 RCLCPP_DISABLE_COPY(
Rate)
172 Clock::SharedPtr clock_;
181 explicit WallRate(
const double rate);
RCLCPP_PUBLIC Time now() const
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.
enum rcl_clock_type_e rcl_clock_type_t
Time source type, used to indicate the source of a time measurement.
@ RCL_SYSTEM_TIME
Use system time.
@ RCL_STEADY_TIME
Use a steady clock time.