Ask Your Question
0

create wall timer

asked 2020-05-13 07:51:14 -0500

Kansai gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-05-18 03:34:33 -0500

MCornelis gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

Asked: 2020-05-13 07:51:14 -0500

Seen: 196 times

Last updated: May 18