Skip to content

Commit b83c45f

Browse files
jmachowinskiatsogiasJanosch Machowinski
authored andcommitted
Added optional TimerInfo to timer callback (ros2#2343)
Signed-off-by: Alexis Tsogias <[email protected]> Signed-off-by: Janosch Machowinski <[email protected]> Co-authored-by: Alexis Tsogias <[email protected]> Co-authored-by: Janosch Machowinski <[email protected]>
1 parent f42ed52 commit b83c45f

File tree

11 files changed

+104
-45
lines changed

11 files changed

+104
-45
lines changed

rclcpp/include/rclcpp/executor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ class Executor
428428

429429
RCLCPP_PUBLIC
430430
static void
431-
execute_timer(rclcpp::TimerBase::SharedPtr timer);
431+
execute_timer(rclcpp::TimerBase::SharedPtr timer, const std::shared_ptr<void> & dataPtr);
432432

433433
RCLCPP_PUBLIC
434434
static void

rclcpp/include/rclcpp/experimental/executors/events_executor/events_executor_event_types.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef RCLCPP__EXPERIMENTAL__EXECUTORS__EVENTS_EXECUTOR__EVENTS_EXECUTOR_EVENT_TYPES_HPP_
1616
#define RCLCPP__EXPERIMENTAL__EXECUTORS__EVENTS_EXECUTOR__EVENTS_EXECUTOR_EVENT_TYPES_HPP_
1717

18+
#include <memory>
19+
1820
namespace rclcpp
1921
{
2022
namespace experimental
@@ -34,6 +36,7 @@ enum ExecutorEventType
3436
struct ExecutorEvent
3537
{
3638
const void * entity_key;
39+
std::shared_ptr<void> data;
3740
int waitable_data;
3841
ExecutorEventType type;
3942
size_t num_events;

rclcpp/include/rclcpp/experimental/timers_manager.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class TimersManager
8686
RCLCPP_PUBLIC
8787
TimersManager(
8888
std::shared_ptr<rclcpp::Context> context,
89-
std::function<void(const rclcpp::TimerBase *)> on_ready_callback = nullptr);
89+
std::function<void(const rclcpp::TimerBase *,
90+
const std::shared_ptr<void> &)> on_ready_callback = nullptr);
9091

9192
/**
9293
* @brief Destruct the TimersManager object making sure to stop thread and release memory.
@@ -164,9 +165,10 @@ class TimersManager
164165
* the TimersManager on_ready_callback was passed during construction.
165166
*
166167
* @param timer_id the timer ID of the timer to execute
168+
* @param data internal data of the timer
167169
*/
168170
RCLCPP_PUBLIC
169-
void execute_ready_timer(const rclcpp::TimerBase * timer_id);
171+
void execute_ready_timer(const rclcpp::TimerBase * timer_id, const std::shared_ptr<void> & data);
170172

171173
/**
172174
* @brief Get the amount of time before the next timer triggers.
@@ -529,7 +531,8 @@ class TimersManager
529531
void execute_ready_timers_unsafe();
530532

531533
// Callback to be called when timer is ready
532-
std::function<void(const rclcpp::TimerBase *)> on_ready_callback_;
534+
std::function<void(const rclcpp::TimerBase *,
535+
const std::shared_ptr<void> &)> on_ready_callback_ = nullptr;
533536

534537
// Thread used to run the timers execution task
535538
std::thread timers_thread_;

rclcpp/include/rclcpp/strategies/allocator_memory_strategy.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ class AllocatorMemoryStrategy : public memory_strategy::MemoryStrategy
368368
++it;
369369
continue;
370370
}
371-
if (!timer->call()) {
371+
auto data = timer->call();
372+
if (!data) {
372373
// timer was cancelled, skip it.
373374
++it;
374375
continue;
@@ -377,6 +378,7 @@ class AllocatorMemoryStrategy : public memory_strategy::MemoryStrategy
377378
any_exec.timer = timer;
378379
any_exec.callback_group = group;
379380
any_exec.node_base = get_node_by_group(group, weak_groups_to_nodes);
381+
any_exec.data = data;
380382
timer_handles_.erase(it);
381383
return;
382384
}

rclcpp/include/rclcpp/timer.hpp

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <atomic>
1919
#include <chrono>
2020
#include <functional>
21+
#include <optional>
2122
#include <memory>
2223
#include <sstream>
2324
#include <thread>
@@ -43,6 +44,12 @@
4344
namespace rclcpp
4445
{
4546

47+
struct TimerInfo
48+
{
49+
Time expected_call_time;
50+
Time actual_call_time;
51+
};
52+
4653
class TimerBase
4754
{
4855
public:
@@ -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-
197207
using VoidCallbackType = std::function<void ()>;
198208
using TimerCallbackType = std::function<void (TimerBase &)>;
209+
using TimerInfoCallbackType = std::function<void (const TimerInfo &)>;
199210

200211
/// Generic timer. Periodically executes a user-specified callback.
201212
template<
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
>
208220
class GenericTimer : public TimerBase
@@ -244,27 +256,28 @@ class GenericTimer : public TimerBase
244256
* \sa rclcpp::TimerBase::call
245257
* \throws std::runtime_error if it failed to notify timer that callback will occurr
246258
*/
247-
bool
259+
std::shared_ptr<void>
248260
call() override
249261
{
250-
rcl_ret_t ret = rcl_timer_call(timer_handle_.get());
262+
auto timer_call_info_ = std::make_shared<rcl_timer_call_info_t>();
263+
rcl_ret_t ret = rcl_timer_call_with_info(timer_handle_.get(), timer_call_info_.get());
251264
if (ret == RCL_RET_TIMER_CANCELED) {
252-
return false;
265+
return nullptr;
253266
}
254267
if (ret != RCL_RET_OK) {
255268
throw std::runtime_error("Failed to notify timer that callback occurred");
256269
}
257-
return true;
270+
return timer_call_info_;
258271
}
259272

260273
/**
261274
* \sa rclcpp::TimerBase::execute_callback
262275
*/
263276
void
264-
execute_callback() override
277+
execute_callback(const std::shared_ptr<void> & data) override
265278
{
266279
TRACEPOINT(callback_start, reinterpret_cast<const void *>(&callback_), false);
267-
execute_callback_delegate<>();
280+
execute_callback_delegate<>(*static_cast<rcl_timer_call_info_t *>(data.get()));
268281
TRACEPOINT(callback_end, reinterpret_cast<const void *>(&callback_));
269282
}
270283

@@ -276,7 +289,7 @@ class GenericTimer : public TimerBase
276289
>::type * = nullptr
277290
>
278291
void
279-
execute_callback_delegate()
292+
execute_callback_delegate(const rcl_timer_call_info_t &)
280293
{
281294
callback_();
282295
}
@@ -288,11 +301,26 @@ class GenericTimer : public TimerBase
288301
>::type * = nullptr
289302
>
290303
void
291-
execute_callback_delegate()
304+
execute_callback_delegate(const rcl_timer_call_info_t &)
292305
{
293306
callback_(*this);
294307
}
295308

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(const rcl_timer_call_info_t & timer_call_info_)
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+
296324
/// Is the clock steady (i.e. is the time between ticks constant?)
297325
/** \return True if the clock used by this timer is steady. */
298326
bool
@@ -311,7 +339,8 @@ template<
311339
typename FunctorT,
312340
typename std::enable_if<
313341
rclcpp::function_traits::same_arguments<FunctorT, VoidCallbackType>::value ||
314-
rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value
342+
rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value ||
343+
rclcpp::function_traits::same_arguments<FunctorT, TimerInfoCallbackType>::value
315344
>::type * = nullptr
316345
>
317346
class WallTimer : public GenericTimer<FunctorT>

rclcpp/src/rclcpp/executor.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ Executor::execute_any_executable(AnyExecutable & any_exec)
367367
TRACEPOINT(
368368
rclcpp_executor_execute,
369369
static_cast<const void *>(any_exec.timer->get_timer_handle().get()));
370-
execute_timer(any_exec.timer);
370+
execute_timer(any_exec.timer, any_exec.data);
371371
}
372372
if (any_exec.subscription) {
373373
TRACEPOINT(
@@ -498,9 +498,9 @@ Executor::execute_subscription(rclcpp::SubscriptionBase::SharedPtr subscription)
498498
}
499499

500500
void
501-
Executor::execute_timer(rclcpp::TimerBase::SharedPtr timer)
501+
Executor::execute_timer(rclcpp::TimerBase::SharedPtr timer, const std::shared_ptr<void> & dataPtr)
502502
{
503-
timer->execute_callback();
503+
timer->execute_callback(dataPtr);
504504
}
505505

506506
void
@@ -642,6 +642,7 @@ Executor::get_next_ready_executable(AnyExecutable & any_executable)
642642
if (entity_iter != current_collection_.timers.end()) {
643643
auto callback_group = entity_iter->second.callback_group.lock();
644644
if (callback_group && !callback_group->can_be_taken_from()) {
645+
current_timer_index++;
645646
continue;
646647
}
647648
// At this point the timer is either ready for execution or was perhaps
@@ -650,14 +651,17 @@ Executor::get_next_ready_executable(AnyExecutable & any_executable)
650651
// it from the wait result.
651652
wait_result_->clear_timer_with_index(current_timer_index);
652653
// Check that the timer should be called still, i.e. it wasn't canceled.
653-
if (!timer->call()) {
654+
any_executable.data = timer->call();
655+
if (!any_executable.data) {
656+
current_timer_index++;
654657
continue;
655658
}
656659
any_executable.timer = timer;
657660
any_executable.callback_group = callback_group;
658661
valid_executable = true;
659662
break;
660663
}
664+
current_timer_index++;
661665
}
662666
}
663667

rclcpp/src/rclcpp/executors/static_single_threaded_executor.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,15 @@ bool StaticSingleThreadedExecutor::execute_ready_executables(
154154
auto entity_iter = collection.timers.find(timer->get_timer_handle().get());
155155
if (entity_iter != collection.timers.end()) {
156156
wait_result.clear_timer_with_index(current_timer_index);
157-
if (timer->call()) {
158-
execute_timer(timer);
159-
any_ready_executable = true;
160-
if (spin_once) {return any_ready_executable;}
157+
auto data = timer->call();
158+
if (!data) {
159+
// someone canceled the timer between is_ready and call
160+
continue;
161161
}
162+
163+
execute_timer(std::move(timer), data);
164+
any_ready_executable = true;
165+
if (spin_once) {return any_ready_executable;}
162166
}
163167
}
164168

rclcpp/src/rclcpp/experimental/executors/events_executor/events_executor.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ EventsExecutor::EventsExecutor(
4040
// The timers manager can be used either to only track timers (in this case an expired
4141
// timer will generate an executor event and then it will be executed by the executor thread)
4242
// or it can also take care of executing expired timers in its dedicated thread.
43-
std::function<void(const rclcpp::TimerBase *)> timer_on_ready_cb = nullptr;
43+
std::function<void(const rclcpp::TimerBase *,
44+
const std::shared_ptr<void> &)> timer_on_ready_cb = nullptr;
4445
if (!execute_timers_separate_thread) {
45-
timer_on_ready_cb = [this](const rclcpp::TimerBase * timer_id) {
46-
ExecutorEvent event = {timer_id, -1, ExecutorEventType::TIMER_EVENT, 1};
46+
timer_on_ready_cb =
47+
[this](const rclcpp::TimerBase * timer_id, const std::shared_ptr<void> & data) {
48+
ExecutorEvent event = {timer_id, data, -1, ExecutorEventType::TIMER_EVENT, 1};
4749
this->events_queue_->enqueue(event);
4850
};
4951
}
@@ -88,7 +90,7 @@ EventsExecutor::EventsExecutor(
8890
}
8991

9092
ExecutorEvent event =
91-
{notify_waitable_entity_id, waitable_data, ExecutorEventType::WAITABLE_EVENT, 1};
93+
{notify_waitable_entity_id, nullptr, waitable_data, ExecutorEventType::WAITABLE_EVENT, 1};
9294
this->events_queue_->enqueue(event);
9395
});
9496

@@ -325,7 +327,7 @@ EventsExecutor::execute_event(const ExecutorEvent & event)
325327
case ExecutorEventType::TIMER_EVENT:
326328
{
327329
timers_manager_->execute_ready_timer(
328-
static_cast<const rclcpp::TimerBase *>(event.entity_key));
330+
static_cast<const rclcpp::TimerBase *>(event.entity_key), event.data);
329331
break;
330332
}
331333
case ExecutorEventType::WAITABLE_EVENT:
@@ -485,7 +487,7 @@ EventsExecutor::create_entity_callback(
485487
{
486488
std::function<void(size_t)>
487489
callback = [this, entity_key, event_type](size_t num_events) {
488-
ExecutorEvent event = {entity_key, -1, event_type, num_events};
490+
ExecutorEvent event = {entity_key, nullptr, -1, event_type, num_events};
489491
this->events_queue_->enqueue(event);
490492
};
491493
return callback;
@@ -497,7 +499,7 @@ EventsExecutor::create_waitable_callback(const rclcpp::Waitable * entity_key)
497499
std::function<void(size_t, int)>
498500
callback = [this, entity_key](size_t num_events, int waitable_data) {
499501
ExecutorEvent event =
500-
{entity_key, waitable_data, ExecutorEventType::WAITABLE_EVENT, num_events};
502+
{entity_key, nullptr, waitable_data, ExecutorEventType::WAITABLE_EVENT, num_events};
501503
this->events_queue_->enqueue(event);
502504
};
503505
return callback;

0 commit comments

Comments
 (0)