15 #ifndef RCLCPP__TIMER_HPP_
16 #define RCLCPP__TIMER_HPP_
25 #include <type_traits>
28 #include "rclcpp/clock.hpp"
29 #include "rclcpp/context.hpp"
30 #include "rclcpp/function_traits.hpp"
31 #include "rclcpp/macros.hpp"
32 #include "rclcpp/rate.hpp"
33 #include "rclcpp/utilities.hpp"
34 #include "rclcpp/visibility_control.hpp"
35 #include "tracetools/tracetools.h"
36 #include "tracetools/utils.hpp"
38 #include "rcl/error_handling.h"
41 #include "rmw/error_handling.h"
49 Time expected_call_time;
50 Time actual_call_time;
56 RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(
TimerBase)
70 Clock::SharedPtr clock,
71 std::chrono::nanoseconds period,
72 rclcpp::Context::SharedPtr context,
73 bool autostart =
true);
115 virtual std::shared_ptr<void>
127 std::shared_ptr<const rcl_timer_t>
137 std::chrono::nanoseconds
194 std::recursive_mutex callback_mutex_;
200 std::function<void(
size_t)> on_reset_callback_{
nullptr};
202 Clock::SharedPtr clock_;
203 std::shared_ptr<rcl_timer_t> timer_handle_;
205 std::atomic<bool> in_use_by_wait_set_{
false};
212 using VoidCallbackType = std::function<void ()>;
213 using TimerCallbackType = std::function<void (TimerBase &)>;
214 using TimerInfoCallbackType = std::function<void (
const TimerInfo &)>;
219 typename std::enable_if<
239 Clock::SharedPtr clock, std::chrono::nanoseconds period, FunctorT && callback,
242 :
TimerBase(clock, period, context, autostart), callback_(std::forward<FunctorT>(callback))
244 TRACETOOLS_TRACEPOINT(
245 rclcpp_timer_callback_added,
246 static_cast<const void *
>(get_timer_handle().get()),
247 reinterpret_cast<const void *
>(&callback_));
248 #ifndef TRACETOOLS_DISABLED
249 if (TRACETOOLS_TRACEPOINT_ENABLED(rclcpp_callback_register)) {
250 char * symbol = tracetools::get_symbol(callback_);
251 TRACETOOLS_DO_TRACEPOINT(
252 rclcpp_callback_register,
253 reinterpret_cast<const void *
>(&callback_),
271 std::shared_ptr<void>
274 auto timer_call_info_ = std::make_shared<rcl_timer_call_info_t>();
280 throw std::runtime_error(
"Failed to notify timer that callback occurred");
282 return timer_call_info_;
291 TRACETOOLS_TRACEPOINT(callback_start,
reinterpret_cast<const void *
>(&callback_),
false);
293 TRACETOOLS_TRACEPOINT(callback_end,
reinterpret_cast<const void *
>(&callback_));
298 typename CallbackT = FunctorT,
299 typename std::enable_if<
310 typename CallbackT = FunctorT,
311 typename std::enable_if<
323 typename CallbackT = FunctorT,
324 typename std::enable_if<
331 const TimerInfo info{Time{timer_call_info.expected_call_time, clock_->get_clock_type()},
332 Time{timer_call_info.actual_call_time, clock_->get_clock_type()}};
352 typename std::enable_if<
371 std::chrono::nanoseconds period,
372 FunctorT && callback,
374 bool autostart = true)
Context which encapsulates shared state between nodes and other similar entities.
Generic timer. Periodically executes a user-specified callback.
bool is_steady() override
Is the clock steady (i.e. is the time between ticks constant?)
void execute_callback(const std::shared_ptr< void > &data) override
virtual ~GenericTimer()
Default destructor.
std::shared_ptr< void > call() override
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 void clear_on_reset_callback()
Unset the callback registered for reset timer.
virtual RCLCPP_PUBLIC void execute_callback(const std::shared_ptr< void > &data)=0
Call the callback function when the timer signal is emitted.
virtual RCLCPP_PUBLIC std::shared_ptr< void > call()=0
Indicate that we're about to execute the callback.
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.
virtual bool is_steady()=0
Is the clock steady (i.e. is the time between ticks constant?)
RCLCPP_PUBLIC bool is_ready()
Check if the timer is ready to trigger the callback.
RCLCPP_PUBLIC TimerBase(Clock::SharedPtr clock, std::chrono::nanoseconds period, rclcpp::Context::SharedPtr context, bool autostart=true)
TimerBase constructor.
RCLCPP_PUBLIC void set_on_reset_callback(std::function< void(size_t)> callback)
Set a callback to be called when the timer is reset.
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.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Structure which encapsulates timer information when called.
@ RCL_STEADY_TIME
Use a steady clock time.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_call_with_info(rcl_timer_t *timer, rcl_timer_call_info_t *call_info)
Same as rcl_timer_call() except that it also retrieves the actual and expected call time.
#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.