0

This question deals with ROS2.

In ROS1 when you had a publisher you usually put a while loop to execute the node publishing periodically. Instead in ROS2 you can use create_wall_timer to create a timer so as to call a callback function that publish your messages periodically.

My question is (to anyone with experience in ROS2) can I create this timer and use it for other tasks unrelated to publishing? In other words using like a normal timer? I need to do somethings periodically and I was going to implement a timer, but since this is already built in the ROS2 system, I would like to use it to do the processing I want. Is this possible or advisable?

Also, can I use or create more than one of these wall timers?

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

1 Answers1

0

The wall timer calls a function called a "callback function" every time it goes off. You define this function yourself and that means you can do anything you like inside it.

For example: in your constructor you instantiate the wall timer: auto timer_ = this->create_wall_timer( 500ms, std::bind(&ClassName::timer_callback, this)); then define a function for it to call

void timer_callback()
{
    //Whatever code you would like to run every time the timer goes
}

If you follow the linked tutorial, you can use that code. Source: https://index.ros.org/doc/ros2/Tutorials/Writing-A-Simple-Cpp-Publisher-And-Subscriber/

Regarding multiple, yes you can make as many as you would like. Just put it in a new variable each time.

Emile Akbarzadeh
  • 326
  • 2
  • 14