Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

ROS msgs not received in a topic if publisher starts after subscriber

Discussion in 'Robotics' started by felipebelocreatecrobotics, Jan 4, 2022.

  1. felipebelocreatecrobotics


    Apr 29, 2021
    If I subscribe to a topic before a publisher was registered at that topic, no ROS msgs are received by the subscriber callback once the publisher is registered.

    For the sake of simplicity let's call A the machine that is publishing and B the machine that is subscribing to the topic.

    If I run 'rostopic echo NAME_OF_TOPIC', I can see that the messages are indeed being published by A. However, the 'subscriber' in B does not seem to be able to receive them if B is run after A.

    I came up with a couple of "hacks" to handle the issue. If I set a timeout in B when listening for msgs on a subscriber and either: disconnect/reconnect to RosConnection; or, unsubscribe/resubscribe to the topic; then, the topic is properly subscribed (in B) and receives messages (from A).

    However, I've verified that these solutions have the side-effect that publishers in B lose "connection" to their related subscribers in other machines (e.g., in A). I also verified that to unsubscribe/resubscribe to the topic takes a long time (in the order of seconds), which means that messages get lost during that time, and B may not receive a message from A within time, failing the timeout check again.

    For completeness, I'm using the following to register a publisher:
    Code (CSharp):
    1. ros = ROSConnection.GetOrCreateInstance(); ros.RegisterPublisher<SOME_ROS_MSG>(topicName);
    And the following to subscribe to the publisher:

    Code (CSharp):
    1. ros = ROSConnection.GetOrCreateInstance();
    2. ros.Subscribe<SOME_ROS_MSG>topicName, CallbackFunction);
    I'm following the tutorial presented in the Unity Robotics Hub:
  2. amanda-unity


    Unity Technologies

    May 29, 2020
    Thanks for the report! I'm responding on your Github issue in order to consolidate discussion on this topic.