Publishers in ROS2

Hello guys,

I am new to ROS and before purchasing the course, I wanted to see whether I will be able to understand ROS concepts or not. Coming to my question, I understood that to publish a message periodically 1st we need to create the publisher handle using create_publisher followed by invoking a callback periodically the create_wall_timer method. Inside the callback you will make use of the publisher handle. Now my question is, Is a new seperate thread gets created for every callback of the create_wall_timer (that is using publisher handlers) so that there won’t be any underlying scheduling issues when publishing messages? If not how does it know to invoke the callback at that exact time. I know there is this concept of executors but if I understand correctly it’s related to subscribers. Please help me clarify this doubt.

Thank you.

Hi @n0nam3, welcome to the community!

To publish a message, all you need to do is create a publisher and then publish a message through it. You don’t need to use a timer callback for just a publisher.

The use of a timer callback can be useful in ROS 2 nodes when there is also a subscriber in the node in addition to the publisher, for example, a robot subscribed to a lidar /scan topic and publishing to its /cmd_vel topic to move based on the lidar sensor readings.

You probably saw this timer callback in one of our examples, and it is used to prevent blocking of the lidar subscriber callback: every subscriber triggers a callback every time a new message arrives.

It is good practice to run these two callbacks in separate threads, although not strictly necessary.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.