1818#include < atomic>
1919#include < chrono>
2020#include < functional>
21+ #include < optional>
2122#include < memory>
2223#include < sstream>
2324#include < thread>
4344namespace rclcpp
4445{
4546
47+ struct TimerInfo
48+ {
49+ Time expected_call_time;
50+ Time actual_call_time;
51+ };
52+
4653class TimerBase
4754{
4855public:
@@ -96,16 +103,20 @@ class TimerBase
96103 * The multithreaded executor takes advantage of this to avoid scheduling
97104 * the callback multiple times.
98105 *
99- * \return `true` if the callback should be executed, `false` if the timer was canceled.
106+ * \return a valid shared_ptr if the callback should be executed,
107+ * an invalid shared_ptr (nullptr) if the timer was canceled.
100108 */
101109 RCLCPP_PUBLIC
102- virtual bool
110+ virtual std::shared_ptr< void >
103111 call () = 0 ;
104112
105113 // / Call the callback function when the timer signal is emitted.
114+ /* *
115+ * \param[in] data the pointer returned by the call function
116+ */
106117 RCLCPP_PUBLIC
107118 virtual void
108- execute_callback () = 0 ;
119+ execute_callback (const std::shared_ptr< void > & data ) = 0 ;
109120
110121 RCLCPP_PUBLIC
111122 std::shared_ptr<const rcl_timer_t >
@@ -193,16 +204,17 @@ class TimerBase
193204 set_on_reset_callback (rcl_event_callback_t callback, const void * user_data);
194205};
195206
196-
197207using VoidCallbackType = std::function<void ()>;
198208using TimerCallbackType = std::function<void (TimerBase &)>;
209+ using TimerInfoCallbackType = std::function<void (const TimerInfo &)>;
199210
200211// / Generic timer. Periodically executes a user-specified callback.
201212template <
202213 typename FunctorT,
203214 typename std::enable_if<
204215 rclcpp::function_traits::same_arguments<FunctorT, VoidCallbackType>::value ||
205- rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value
216+ rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value ||
217+ rclcpp::function_traits::same_arguments<FunctorT, TimerInfoCallbackType>::value
206218 >::type * = nullptr
207219>
208220class GenericTimer : public TimerBase
@@ -250,27 +262,28 @@ class GenericTimer : public TimerBase
250262 * \sa rclcpp::TimerBase::call
251263 * \throws std::runtime_error if it failed to notify timer that callback will occurr
252264 */
253- bool
265+ std::shared_ptr< void >
254266 call () override
255267 {
256- rcl_ret_t ret = rcl_timer_call (timer_handle_.get ());
268+ auto timer_call_info_ = std::make_shared<rcl_timer_call_info_t >();
269+ rcl_ret_t ret = rcl_timer_call_with_info (timer_handle_.get (), timer_call_info_.get ());
257270 if (ret == RCL_RET_TIMER_CANCELED) {
258- return false ;
271+ return nullptr ;
259272 }
260273 if (ret != RCL_RET_OK) {
261274 throw std::runtime_error (" Failed to notify timer that callback occurred" );
262275 }
263- return true ;
276+ return timer_call_info_ ;
264277 }
265278
266279 /* *
267280 * \sa rclcpp::TimerBase::execute_callback
268281 */
269282 void
270- execute_callback () override
283+ execute_callback (const std::shared_ptr< void > & data ) override
271284 {
272285 TRACEPOINT (callback_start, reinterpret_cast <const void *>(&callback_), false );
273- execute_callback_delegate<>();
286+ execute_callback_delegate<>(* static_cast < rcl_timer_call_info_t *>(data. get ()) );
274287 TRACEPOINT (callback_end, reinterpret_cast <const void *>(&callback_));
275288 }
276289
@@ -282,7 +295,7 @@ class GenericTimer : public TimerBase
282295 >::type * = nullptr
283296 >
284297 void
285- execute_callback_delegate ()
298+ execute_callback_delegate (const rcl_timer_call_info_t & )
286299 {
287300 callback_ ();
288301 }
@@ -294,11 +307,26 @@ class GenericTimer : public TimerBase
294307 >::type * = nullptr
295308 >
296309 void
297- execute_callback_delegate ()
310+ execute_callback_delegate (const rcl_timer_call_info_t & )
298311 {
299312 callback_ (*this );
300313 }
301314
315+
316+ template <
317+ typename CallbackT = FunctorT,
318+ typename std::enable_if<
319+ rclcpp::function_traits::same_arguments<CallbackT, TimerInfoCallbackType>::value
320+ >::type * = nullptr
321+ >
322+ void
323+ execute_callback_delegate (const rcl_timer_call_info_t & timer_call_info_)
324+ {
325+ const TimerInfo info{Time{timer_call_info_.expected_call_time , clock_->get_clock_type ()},
326+ Time{timer_call_info_.actual_call_time , clock_->get_clock_type ()}};
327+ callback_ (info);
328+ }
329+
302330 // / Is the clock steady (i.e. is the time between ticks constant?)
303331 /* * \return True if the clock used by this timer is steady. */
304332 bool
@@ -317,7 +345,8 @@ template<
317345 typename FunctorT,
318346 typename std::enable_if<
319347 rclcpp::function_traits::same_arguments<FunctorT, VoidCallbackType>::value ||
320- rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value
348+ rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value ||
349+ rclcpp::function_traits::same_arguments<FunctorT, TimerInfoCallbackType>::value
321350 >::type * = nullptr
322351>
323352class WallTimer : public GenericTimer <FunctorT>
0 commit comments