Robotics StackExchange | Archived questions

How can i execute a ROS2 node every 10 milli second

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.

Asked by aks on 2018-08-24 09:31:52 UTC

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¬

Asked by PeteBlackerThe3rd on 2018-08-24 10:06:03 UTC

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

Asked by aks on 2018-08-24 10:07:58 UTC

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?

Asked by PeteBlackerThe3rd on 2018-08-24 12:09:34 UTC

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.

Asked by aks on 2018-08-24 13:05:17 UTC

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?

Asked by PeteBlackerThe3rd on 2018-08-25 07:36:06 UTC

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

Asked by aks on 2018-08-25 12:22:25 UTC

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

Asked by PeteBlackerThe3rd on 2018-08-25 13:46:26 UTC

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?

Asked by gvdhoorn on 2018-08-25 14:11:48 UTC

makes sense. i will try that and get back ! In the mean time, do you have an idea how can i check the frequency of a published message ? q301593

Asked by aks on 2018-08-26 05:35:40 UTC

Tried publishing in the callback. Still the same synchronisation problem. I guess i need to know the frequency of the published message to debug further. Any idea how ?

Asked by aks on 2018-08-27 08:57:49 UTC

Tried publishing in the callback. Still the same synchronisation problem.

just a thought, but have you implemented a lock-step sync here? If the simulation does not wait for the incoming msg, then you'll always run into synchronisation issues. Regardless of how you time your node.

Asked by gvdhoorn on 2018-08-27 08:59:12 UTC

@gvdhoorn : I just read about it. No this is not the case, the simulation waits for the messages from ROS2, then processes it and publishes it.

Asked by aks on 2018-08-27 09:03:10 UTC

I'm confused by your last comment: do you or don't you have a lock-step implemented?

Asked by gvdhoorn on 2018-08-27 09:34:03 UTC

I figured out that the problem does not lies in synchronisation. The problem is that the simulation runs extremely slow. I posted this on the RTI community here. Shall i post it here as well?

Asked by aks on 2018-08-28 04:37:16 UTC

You could post an answer summarising the response by Gianpiero on the RTI forum.

Technically though, it would be an answer, as publishing a msg every 10ms is straightforward enough: just run a timer.

Asked by gvdhoorn on 2018-08-28 04:56:18 UTC

Answers