Publishing and Subscribing not possible at the same time

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”)

For answering the general questions @LaurieUnity or @amanda-unity could you please help?

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?

Edit:
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:

          this.rosWrap.GetOrCreateInstance();
            this.rosWrap.RegisterPublisher<LaserScanMsg>(topic);


            while (isRunning)
            {
                try
                {
                    scanMsg = sensor.GetLatestData();
                    rosMsg = new LaserScanMsg(
                    //fill the rosMsg with my own scanMsg
                    );             
                    this.rosWrap.Publish(topic, rosMsg);
                }
                catch
                {
                    Debug.Log(string.Format(Logs.ros2_closedError, Logs.ros2_lidarThread, topic));
                    isRunning = false;
                }


                //reset event to wait for next laser rotation
                resetEvent.WaitOne();
                resetEvent.Reset();
            }
        }