Timer

mrs_lib::Timer

class Timer

Timer wrapper supporting ROS and thread-based implementations.

You can chose the timer implementation by calling the constructor with the respective config type (RosTimerOptions, ThreadTimerOptions).

Note

Although the thread timer implementation tries to be as close as possible to the ROS timer, there are some important differences:

  • Clocks

    • Ros timer uses the clock provided by the node interfaces.

    • Thread timer uses standard library clock (does not handle sim time!).

  • Callback Groups - Thread timer does not handle rclcpp callback groups. This means that code relying on thread timers MUST manually ensure thread safe access to everything accessed by the timer callback. Using the ROS backend uses the callback groups as expected.

  • Callback Reentrancy - Thread timer runs all callbacks on a single thread and thus is unable to start multiple callbacks in parallel like the ROS timer.

Public Types

using EventCallback = std::function<void()>

Type of the accepted callback.

using EventCoroCallback = CoroCallback<void()>

Type of the accepted coroutine callback.

Public Functions

explicit Timer(const RosTimerOptions &options, std::chrono::nanoseconds period, EventCallback callback)

Construct a ROS-based timer with a regular callback.

Parameters:
  • options – Timer configuration options.

  • period – Timer period.

  • callback – Callback invoked when the timer expires.

explicit Timer(const RosTimerOptions &options, std::chrono::nanoseconds period, EventCoroCallback callback)

Construct a ROS-based timer with a coroutine callback.

Parameters:
  • options – Timer configuration options.

  • period – Timer period.

  • callback – Coroutine callback invoked when the timer expires.

explicit Timer(const ThreadTimerOptions &options, std::chrono::nanoseconds period, EventCallback callback)

Construct a thread-based timer with a regular callback.

Parameters:
  • options – Timer configuration options.

  • period – Timer period.

  • callback – Callback invoked when the timer expires.

explicit Timer(const ThreadTimerOptions &options, std::chrono::nanoseconds period, EventCoroCallback callback)

Construct a thread-based timer with a coroutine callback.

Parameters:
  • options – Timer configuration options.

  • period – Timer period.

  • callback – Coroutine callback invoked when the timer expires.

void stop()

Stop the timer.

After calling this method, the timer will not invoke its callback until start_or_reset is called.

void start_or_reset()

Start or reset the timer.

If the timer is stopped, this starts it. If it is already running, its countdown is reset using the currently configured period.

void set_period(std::chrono::nanoseconds period)

Set the timer period.

Changing the period also resets the timer countdown and starts the timer if it was previously stopped.

Parameters:

period – New timer period.

bool is_running() const

Check whether the timer is currently running.

Returns:

true if the timer is running, otherwise false.

Options

mrs_lib::RosTimerOptions

struct RosTimerOptions

Options for creating a ROS-based timer.

When configuring timer with these options, it will use rclcpp::Timer internally. (See mrs_lib::Timer documentation for differences in implementations.)

Public Members

TimerNodeInterfaces node_interfaces

ROS node interfaces used by the timer.

bool autostart = true

Whether the timer should start automatically after construction.

bool oneshot = false

Whether the timer should execute its callback only once.

If true, the timer stops after exactly one callback is executed.

std::shared_ptr<rclcpp::CallbackGroup> callback_group = nullptr

Callback group in which the timer callback should be executed.

If nullptr, the default callback group is used.

mrs_lib::ThreadTimerOptions

struct ThreadTimerOptions

Options for creating a thread-based timer.

These options configure a timer that runs on a separate thread. (See mrs_lib::Timer documentation for differences in implementations.)

Public Members

TimerNodeInterfaces node_interfaces

ROS node interfaces used by the timer.

bool autostart = true

Whether the timer should start automatically after construction.

bool oneshot = false

Whether the timer should execute its callback only once.

If true, the timer stops after exactly one callback is executed.

Example

Example node using the mrs_lib::Timer.
 1using namespace std::chrono_literals;
 2
 3class ExampleNode : public rclcpp::Node
 4{
 5public:
 6  ExampleNode(const rclcpp::NodeOptions& options = rclcpp::NodeOptions{})
 7      : Node("example_node", options),
 8        logger_(*this),
 9        timer_(
10            mrs_lib::RosTimerOptions{
11                .node_interfaces = *this,
12                // You can specify other options here, but the defaults
13                // are good for us.
14            },
15            100ms, [this]() { timer_callback(); })
16  {
17  }
18
19  [[nodiscard]] size_t get_callbacks_count() const
20  {
21    return callbacks_count_;
22  }
23
24private:
25  void timer_callback()
26  {
27    logger_.info("Timer callback!");
28    callbacks_count_ += 1;
29  }
30
31  mrs_lib::Logger logger_;
32
33  size_t callbacks_count_ = 0;
34
35  mrs_lib::Timer timer_;
36};