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

Resolved How to set up two different ROS connections from Unity?

Discussion in 'Robotics' started by lazyrobotboy, Mar 25, 2022.

  1. lazyrobotboy

    lazyrobotboy

    Joined:
    Jul 1, 2020
    Posts:
    16
    Hi everyone,

    I'm currently struggling to connect to two different ROS machines and robots at the same time. You can see my Unity setup in the attached screenshot. I have created two different objects using the provided ROSConnection script but I have several issues:
    1. I can connect to each system separately, so the basic setup is working. But as soon as I have two ROS_Connectors in my scene, none is working.
    2. When I have two ROS_Connector game objects in my scene, the previously assigned ROS_Connectors change when pressing the play button. Example: The connection for the first system called "ROS1" changes to "ROS_Connector2" despite setting it to "ROS_Connector1" before.

    Here is the code snippet from one of my publishers:
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Robotics.ROSTCPConnector;
    3. using PositionUnity = RosMessageTypes.Std.Float64MultiArrayMsg;
    4. using System.Collections;
    5.  
    6. public class PositionPublisher : MonoBehaviour
    7. {
    8.     [SerializeField] private string _rosTopic = "/PositionController/command";
    9.     [SerializeField] private float _publishMessageFrequency = 0.005f;
    10.     public ROSConnection ROS1;
    11.  
    12.     void Start()
    13.     {
    14.         ROS1 = ROSConnection.GetOrCreateInstance();
    15.         ROS1.RegisterPublisher<PositionUnity>(_rosTopic);
    16.         InitializeMessage();
    17.     }
    18.  
    19.     private void InitializeMessage()
    20.         {
    21.             ...
    22.         }
    Does anyone see an error or can tell me a more stable way to set up two different connections with two different IPs? Thanks in advance for any advice.

    EDIT: After a bit if fiddling I found out that getting rid of "ROSConnection.GetOrCreateInstance();" solved the problem for me. Thread can be closed.
     
    Last edited: May 3, 2022