Rosanswers logo

In the documentation we can find the method create_wall_timer which "creates a timer".

I read that this timer is usually used to replace ROS1 way of using a while loop. Instead the timer created with wall timer will periodically execute a node publishing.

I have two questions:

  1. Can I use the wall timer for any other task. I have a rclcpp::Node and it subscribes and publishes but the publishing does not need a periodic timer. However I would like to have a timer for doing other unrelated tasks of this node and instead of implementing one myself (with threads etc) I was thinking why not use wall_timer and just write a callback function for what I want to do. Is this possible or advisable?

  2. Can I use more than one of these wall timers?

Originally posted by Kansai on ROS Answers with karma: 170 on 2020-05-13

Post score: 1


1 Answer 1


Rosanswers logo

You can add multiple timers to your node like this (I did it using lambdas, but you can also put the callback somewhere else).

auto timer1 = node->create_wall_timer(interval, [=]() {
          do stuff; 

auto timer2 = node->create_wall_timer(interval, [=]() {
          do other stuff; 

What actually happens in the background:

Every node has a list of handles (related to timers, subscribers, clients, services, waitables) that are put in a wait-set. Whenever the node spins (execution cycle), this wait-set is checked to see if anything is ready to be executed.

For timers this simply means checking if the timer has "expired" -> if yes execute the callback.

For subscriptions, you can imagine this more as the rcl-layer asking rmw if anything has happened at DDS level like receiving a message.

But basically, the node just checks if the correct event has triggered for the correct type of callback. What is in the callback does not matter. You can put anything in there.

So yes, you can have multiple timers and yes you can do other stuff in the callbacks.

Whether implementing it like this is a good idea depends on your use-case and requirements on these callbacks. Order of execution will rely on the underlying executor that is currently in ROS2. This means that if multiple events trigger in the same cycle (so lets say you have to execute 4 callbacks during 1 spin), the callbacks will be executed in some order one after another. This order depends of course on the inner workings and design of the executor.

Unless your system has very hardcore RealTime or deterministic ordering requirements you (normally) do not have to worry about this.

Originally posted by MCornelis with karma: 331 on 2020-05-18

This answer was ACCEPTED on the original site

Post score: 2


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.