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. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Publishing and Subscribing not possible at the same time

Discussion in 'Robotics' started by ActiveSim, Mar 3, 2022.

  1. ActiveSim


    May 10, 2019
    Hi there,

    my ROS connection works fine when I just subscribe or publish data. When I try both with the same ROSConnection component I do not receive the published data anymore.
    Another weird thing is that if I restart my game multiple times (without changing anything) I receive different logs in the ros_tcp_endpoint running in a docker.


    Have you made the same experience and how to fix that?

    Side questions:
    • Do I always get an error when I close my game (I already use the disconnect command)?
    • How to unsubscribe from a topic (warning "publisher already registered")
    Last edited: Mar 3, 2022
  2. ActiveSim


    May 10, 2019
  3. LaurieUnity


    Unity Technologies

    Oct 23, 2020
    The exceptions/warnings when you close the game or re-register a publisher are normal. At some point it would be nice to make this a little more graceful, but it's not actually breaking anything so it's not a high priority fix.

    As for the publishing+subscribing issue you're getting, I'm not sure what would cause the symptoms seen here. It seems likely there's a problem with how things are set up on the Unity side, how are you actually registering that laserscan publisher?
  4. ActiveSim


    May 10, 2019
    I spend the last day again with debugging and find the following workaround:
    • I got it running by changing the interface script to a MonoBehaviour.
    • Then a "topic is not registered" error showed up, so I added a Thread.Sleep after the registration
    Thanks for answering the common questions @LaurieUnity

    The first reply was:

    Thanks for your response.
    • My sensors are running as MonoBehaviour on GameObjects
    • To make the sensor interface exchangeable (ROS2, TCP, HTTP request, etc.) it is running in a separate thread.
    • The sensors and the interfaces are controlled by an SensorBase class, to control all sensors and interfaces the same way.
    • To test the ROS connection I use a ROS wrapper which just call the according ROSConnection method
    In the ROS thread first the topic is registered and after that the sensor data is send in a while loop when there is new data available. Here is a part of the sending thread:

    Code (CSharp):
    1.           this.rosWrap.GetOrCreateInstance();
    2.             this.rosWrap.RegisterPublisher<LaserScanMsg>(topic);
    5.             while (isRunning)
    6.             {
    7.                 try
    8.                 {
    9.                     scanMsg = sensor.GetLatestData();
    10.                     rosMsg = new LaserScanMsg(
    11.                     //fill the rosMsg with my own scanMsg
    12.                     );            
    13.                     this.rosWrap.Publish(topic, rosMsg);
    14.                 }
    15.                 catch
    16.                 {
    17.                     Debug.Log(string.Format(Logs.ros2_closedError, Logs.ros2_lidarThread, topic));
    18.                     isRunning = false;
    19.                 }
    22.                 //reset event to wait for next laser rotation
    23.                 resetEvent.WaitOne();
    24.                 resetEvent.Reset();
    25.             }
    26.         }
    Last edited: Mar 8, 2022