How can i execute a ROS2 node every 10 milli second

asked 2018-08-24 09:31:52 -0500

aks gravatar image

I have a ROS2 node in Python which is integrated with a simulation tool.

The simulation tool runs with a timestep of 10ms.

The ROS2 node first subscribes the data from the simulation tool and then publishes some control signals based on the subscribed messages (both on different topics)

For both systems to work efficiently, there timesteps should be same.

How can I make a ROS2 node execute every 10ms.

One way is using a timer_callback. But is there some alternative where i must not explicitly call the timer function for publishing and subscribing seperately and ask the node to execure at a certain time interval.

edit retag flag offensive close merge delete

Comments

Just to confirm you're asking how to execute a block of code within a node exactly 100 times per second? You'd never want start a new node instances 100 times per secondĀ¬

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-08-24 10:06:03 -0500 )edit

Yes. You are correct. As explained, I would like to subscribe it after 10ms and publish it immediately

aks gravatar imageaks ( 2018-08-24 10:07:58 -0500 )edit

The rate at which you receive messages is determined by the publishing node not the receiving node. At what rate are messages being published by the simulation tool?

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-08-24 12:09:34 -0500 )edit

Yes. True. The simulation tool publishes messages at 10ms(100Hz) which is subscribed by ROS2.Calcultion based on these messges in ROS2 and then publishes the messages which is then subscribed by the simulation tool. So it works in the loop.

aks gravatar imageaks ( 2018-08-24 13:05:17 -0500 )edit

So if you subscribe to the messages from the simulator and process and publish the response within this callback it sounds as if it will have achieved what you want. No timers required. Am I missing something?

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-08-25 07:36:06 -0500 )edit

Unfortunately i am not publishing it within the callback. I am publishing it seperately.

aks gravatar imageaks ( 2018-08-25 12:22:25 -0500 )edit

Is there a reason why your not publishing it within the callback because that would solve this problem?

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-08-25 13:46:26 -0500 )edit

I'm with @PeteBlackerThe3rd: @aks: if you're looking for some sort of synchronisation with your simulation, then wouldn't publishing the response as part of processing the incoming msg immediately result in the desired behaviour?

gvdhoorn gravatar imagegvdhoorn ( 2018-08-25 14:11:48 -0500 )edit