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/utilities.hpp"
33 #include "rclcpp/visibility_control.hpp"
34 #include "tracetools/tracetools.h"
35 #include "tracetools/utils.hpp"
37 #include "rcl/error_handling.h"
47 Time expected_call_time;
48 Time actual_call_time;
54 RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(
TimerBase)
68 Clock::SharedPtr clock,
69 std::chrono::nanoseconds period,
70 rclcpp::Context::SharedPtr context,
71 bool autostart =
true);
113 virtual std::shared_ptr<void>
125 std::shared_ptr<const rcl_timer_t>
135 std::chrono::nanoseconds
193 const Clock::SharedPtr &
get_clock()
const;
196 std::recursive_mutex callback_mutex_;
202 std::function<void(
size_t)> on_reset_callback_{
nullptr};
204 Clock::SharedPtr clock_;
205 std::shared_ptr<rcl_timer_t> timer_handle_;
207 std::atomic<bool> in_use_by_wait_set_{
false};
214 using VoidCallbackType = std::function<void ()>;
215 using TimerCallbackType = std::function<void (TimerBase &)>;
216 using TimerInfoCallbackType = std::function<void (
const TimerInfo &)>;
221 typename std::enable_if<
241 Clock::SharedPtr clock, std::chrono::nanoseconds period, FunctorT && callback,
244 :
TimerBase(clock, period, context, autostart), callback_(std::forward<FunctorT>(callback))
246 TRACETOOLS_TRACEPOINT(
247 rclcpp_timer_callback_added,
248 static_cast<const void *
>(get_timer_handle().get()),
249 reinterpret_cast<const void *
>(&callback_));
250 #ifndef TRACETOOLS_DISABLED
251 if (TRACETOOLS_TRACEPOINT_ENABLED(rclcpp_callback_register)) {
252 char * symbol = tracetools::get_symbol(callback_);
253 TRACETOOLS_DO_TRACEPOINT(
254 rclcpp_callback_register,
255 reinterpret_cast<const void *
>(&callback_),
273 std::shared_ptr<void>
276 auto timer_call_info_ = std::make_shared<rcl_timer_call_info_t>();
282 throw std::runtime_error(
"Failed to notify timer that callback occurred");
284 return timer_call_info_;
293 TRACETOOLS_TRACEPOINT(callback_start,
reinterpret_cast<const void *
>(&callback_),
false);
295 TRACETOOLS_TRACEPOINT(callback_end,
reinterpret_cast<const void *
>(&callback_));
300 typename CallbackT = FunctorT,
301 typename std::enable_if<
312 typename CallbackT = FunctorT,
313 typename std::enable_if<
325 typename CallbackT = FunctorT,
326 typename std::enable_if<
333 const TimerInfo info{Time{timer_call_info.expected_call_time, clock_->get_clock_type()},
334 Time{timer_call_info.actual_call_time, clock_->get_clock_type()}};
354 typename std::enable_if<
373 std::chrono::nanoseconds period,
374 FunctorT && callback,
376 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 const Clock::SharedPtr & get_clock() const
Returns the clock this timer uses.
RCLCPP_PUBLIC void set_on_reset_callback(const std::function< void(size_t)> &callback)
Set a callback to be called when the timer is reset.
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 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.