Skip to content

Commit de1ee4f

Browse files
atsogiasJanosch Machowinski
authored andcommitted
Use rcl_timer_call_with_info instead if rcl_timer_call in Timer and add interface for a callback with TimerInfo argument
Signed-off-by: Alexis Tsogias <[email protected]>
1 parent f0283d4 commit de1ee4f

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

rclcpp/include/rclcpp/timer.hpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,23 @@ class TimerBase
193193
set_on_reset_callback(rcl_event_callback_t callback, const void * user_data);
194194
};
195195

196+
struct TimerInfo
197+
{
198+
Time expected_call_time;
199+
Time actual_call_time;
200+
};
196201

197202
using VoidCallbackType = std::function<void ()>;
198203
using TimerCallbackType = std::function<void (TimerBase &)>;
204+
using TimerInfoCallbackType = std::function<void (const TimerInfo &)>;
199205

200206
/// Generic timer. Periodically executes a user-specified callback.
201207
template<
202208
typename FunctorT,
203209
typename std::enable_if<
204210
rclcpp::function_traits::same_arguments<FunctorT, VoidCallbackType>::value ||
205-
rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value
211+
rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value ||
212+
rclcpp::function_traits::same_arguments<FunctorT, TimerInfoCallbackType>::value
206213
>::type * = nullptr
207214
>
208215
class GenericTimer : public TimerBase
@@ -253,7 +260,7 @@ class GenericTimer : public TimerBase
253260
bool
254261
call() override
255262
{
256-
rcl_ret_t ret = rcl_timer_call(timer_handle_.get());
263+
rcl_ret_t ret = rcl_timer_call_with_info(timer_handle_.get(), &timer_call_info_);
257264
if (ret == RCL_RET_TIMER_CANCELED) {
258265
return false;
259266
}
@@ -299,6 +306,21 @@ class GenericTimer : public TimerBase
299306
callback_(*this);
300307
}
301308

309+
310+
template<
311+
typename CallbackT = FunctorT,
312+
typename std::enable_if<
313+
rclcpp::function_traits::same_arguments<CallbackT, TimerInfoCallbackType>::value
314+
>::type * = nullptr
315+
>
316+
void
317+
execute_callback_delegate()
318+
{
319+
const TimerInfo info{Time{timer_call_info_.expected_call_time, clock_->get_clock_type()},
320+
Time{timer_call_info_.actual_call_time, clock_->get_clock_type()} };
321+
callback_(info);
322+
}
323+
302324
/// Is the clock steady (i.e. is the time between ticks constant?)
303325
/** \return True if the clock used by this timer is steady. */
304326
bool
@@ -311,6 +333,7 @@ class GenericTimer : public TimerBase
311333
RCLCPP_DISABLE_COPY(GenericTimer)
312334

313335
FunctorT callback_;
336+
rcl_timer_call_info_t timer_call_info_;
314337
};
315338

316339
template<

0 commit comments

Comments
 (0)