15 #include "rclcpp/timer.hpp"
22 #include "rclcpp/contexts/default_context.hpp"
23 #include "rclcpp/exceptions.hpp"
25 #include "rcutils/logging_macros.h"
30 rclcpp::Clock::SharedPtr clock,
31 std::chrono::nanoseconds period,
32 rclcpp::Context::SharedPtr context)
33 : clock_(clock), timer_handle_(nullptr)
35 if (
nullptr == context) {
36 context = rclcpp::contexts::get_global_default_context();
39 auto rcl_context = context->get_rcl_context();
41 timer_handle_ = std::shared_ptr<rcl_timer_t>(
45 std::lock_guard<std::mutex> clock_guard(clock->get_clock_mutex());
47 RCUTILS_LOG_ERROR_NAMED(
49 "Failed to clean up rcl timer handle: %s", rcl_get_error_string().str);
61 rcl_clock_t * clock_handle = clock_->get_clock_handle();
63 std::lock_guard<std::mutex> clock_guard(clock_->get_clock_mutex());
65 timer_handle_.get(), clock_handle, rcl_context.get(), period.count(),
nullptr,
68 rclcpp::exceptions::throw_from_rcl_error(ret,
"Couldn't initialize rcl timer handle");
81 rclcpp::exceptions::throw_from_rcl_error(ret,
"Couldn't cancel timer");
91 rclcpp::exceptions::throw_from_rcl_error(ret,
"Couldn't get timer cancelled state");
101 rclcpp::exceptions::throw_from_rcl_error(ret,
"Couldn't reset timer");
111 rclcpp::exceptions::throw_from_rcl_error(ret,
"Failed to check timer");
116 std::chrono::nanoseconds
119 int64_t time_until_next_call = 0;
121 timer_handle_.get(), &time_until_next_call);
123 return std::chrono::nanoseconds::max();
125 rclcpp::exceptions::throw_from_rcl_error(ret,
"Timer could not get time until next call");
127 return std::chrono::nanoseconds(time_until_next_call);
130 std::shared_ptr<const rcl_timer_t>
131 TimerBase::get_timer_handle()
133 return timer_handle_;
139 return in_use_by_wait_set_.exchange(in_use_state);
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
virtual RCLCPP_PUBLIC ~TimerBase()
TimerBase destructor.
RCLCPP_PUBLIC bool is_canceled()
Return the timer cancellation state.
RCLCPP_PUBLIC void reset()
Reset the timer.
RCLCPP_PUBLIC bool exchange_in_use_by_wait_set_state(bool in_use_state)
Exchange the "in use by wait set" state for this timer.
RCLCPP_PUBLIC bool is_ready()
Check if the timer is ready to trigger the callback.
RCLCPP_PUBLIC std::chrono::nanoseconds time_until_trigger()
Check how long the timer has until its next scheduled callback.
RCLCPP_PUBLIC void cancel()
Cancel the timer.
Encapsulation of a time source.
Structure which encapsulates a ROS Timer.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_get_time_until_next_call(const rcl_timer_t *timer, int64_t *time_until_next_call)
Calculate and retrieve the time until the next call in nanoseconds.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_init(rcl_timer_t *timer, rcl_clock_t *clock, rcl_context_t *context, int64_t period, const rcl_timer_callback_t callback, rcl_allocator_t allocator)
Initialize a timer.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_reset(rcl_timer_t *timer)
Reset a timer.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_cancel(rcl_timer_t *timer)
Cancel a timer.
RCL_PUBLIC RCL_WARN_UNUSED rcl_timer_t rcl_get_zero_initialized_timer(void)
Return a zero initialized timer.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_fini(rcl_timer_t *timer)
Finalize a timer.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_is_canceled(const rcl_timer_t *timer, bool *is_canceled)
Retrieve the canceled state of a timer.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_is_ready(const rcl_timer_t *timer, bool *is_ready)
Calculates whether or not the timer should be called.
#define RCL_RET_OK
Success return code.
#define RCL_RET_TIMER_CANCELED
Given timer was canceled return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.