Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity to ROS2 round trip time

Discussion in 'Robotics' started by gft-ai, Aug 27, 2021.

  1. gft-ai

    gft-ai

    Joined:
    Jan 12, 2021
    Posts:
    44
    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?
     
  2. unity_pOmzg4o0fQikBQ

    unity_pOmzg4o0fQikBQ

    Unity Technologies

    Joined:
    Aug 25, 2021
    Posts:
    2
    Hi,

    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 :)
     
  3. gft-ai

    gft-ai

    Joined:
    Jan 12, 2021
    Posts:
    44
    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?
     
  4. LaurieUnity

    LaurieUnity

    Unity Technologies

    Joined:
    Oct 23, 2020
    Posts:
    77
    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.
     
  5. gft-ai

    gft-ai

    Joined:
    Jan 12, 2021
    Posts:
    44
    Great, thank you! I will give that a go!