Unity to ROS2 round trip time

Hi guys,

I have a very simple setup:
A string is published to a “topic A” from Unity → ROS2 node (subscribed to “topic A” and then immediately publishes the same message to a “topic B”) → back in Unity subscribed to “topic B”.

When I measure the round trip time on average it takes about 20 - 50 ms, which is too slow for my intended application.

I also tested the same setup except all in ROS2 (2 ROS2 nodes) and the round trip time was under 2 ms.

Does anyone know what this may be due to?


Thanks for reaching out. This could be an issue with your target frame rate. With negligible network latency, the expected delay is the frame rate (25-50fps). Assuming you put your latency measuring code inside of the Update function, that will only get invoked at the target frame rate, not necessarily when the message is received in the buffer reader thread. If you’re willing, you can change the reader thread to see what kind of latency you get from there. We would love to know what you find. Below are instructions on the solutions.

To adjust the target frame rate, change the target frame rate for the object that is calculating the latency. Here is the documentation for reference.

To manipulate the ReaderThread, go into your TCP-Connector package and change this code.

Good luck :slight_smile:

Hi, I have tried changing the target frame rate but there is almost no change.

I have the round trip time measuring code not within the update() function but in the subscriber’s callback function so I would think that is not be the problem.

I did notice there were lots of sleep/delay calls maybe that could have an effect?

Hi, the subscriber callback will have the same delay.
Here’s how RosConnection works: There’s a reader thread that reads in the raw data from the TCP connection, and puts it in a queue to be processed by the main thread. The main thread then runs once per frame (in a standard Unity update function), and goes through the queue deserializing all the messages and sending them out to subscribers.
If you have a use case that really needs minimal latency, and you don’t mind that your code is not running on the main thread, I suggest you add your code directly in the Reader thread.

Great, thank you! I will give that a go!