15 #ifndef RCLCPP__RATE_HPP_ 16 #define RCLCPP__RATE_HPP_ 36 virtual bool sleep() = 0;
38 virtual void reset() = 0;
41 using std::chrono::duration;
42 using std::chrono::duration_cast;
43 using std::chrono::nanoseconds;
45 template<
class Clock = std::chrono::high_resolution_clock>
53 duration_cast<nanoseconds>(duration<
double>(1.0 / rate)))
56 : period_(period), last_interval_(Clock::now())
63 auto now = Clock::now();
65 auto next_interval = last_interval_ + period_;
67 if (now < last_interval_) {
69 next_interval = now + period_;
72 auto time_to_sleep = next_interval - now;
74 last_interval_ += period_;
76 if (time_to_sleep <= std::chrono::seconds(0)) {
80 if (now > next_interval + period_) {
81 last_interval_ = now + period_;
94 return Clock::is_steady;
100 last_interval_ = Clock::now();
111 std::chrono::nanoseconds period_;
112 using ClockDurationNano = std::chrono::duration<typename Clock::rep, std::nano>;
113 std::chrono::time_point<Clock, ClockDurationNano> last_interval_;
122 #endif // RCLCPP__RATE_HPP_ GenericRate(std::chrono::nanoseconds period)
Definition: rate.hpp:55
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
virtual bool is_steady() const =0
Definition: allocator_common.hpp:24
virtual bool sleep()
Definition: rate.hpp:60
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
bool sleep_for(const std::chrono::nanoseconds &nanoseconds)
Use the global condition variable to block for the specified amount of time.
virtual bool is_steady() const
Definition: rate.hpp:92
std::chrono::nanoseconds period() const
Definition: rate.hpp:103
virtual void reset()
Definition: rate.hpp:98