ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.