Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question [ROS TCP Connector] How to Disconnect and Reconnect ROS Properly

Discussion in 'Robotics' started by GerenMeric, Jul 26, 2023.

  1. GerenMeric

    GerenMeric

    Joined:
    May 15, 2023
    Posts:
    47
    Hi everyone,

    I' m trying to disconnect from a ros master and reconnect from a different ros master. I'm using Disconnect() function and then, set the ROSConnection instance to null. But, when i trying to reconnect the HUD shows 2 connections and logs to following message:

    HUDPanel already contains a header registered at index 0! Registering at index 1 instead.


    Also, i still see the feed from the first master i have connected and publisher still publishes to the first master. This is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using Unity.Robotics.ROSTCPConnector;
    7. using UnityEngine.InputSystem;
    8. using RosImage = RosMessageTypes.Sensor.CompressedImageMsg;
    9. using ROString = RosMessageTypes.Std.StringMsg;
    10. using RosMessageTypes.Geometry;
    11. using UnityEngine.UI;
    12. using TMPro;
    13.  
    14. public class RosPubnSub : MonoBehaviour
    15. {
    16.  
    17.     private Process process = new Process();
    18.  
    19.     ROSConnection ros = null;
    20.     ROString theBtnMsg;
    21.  
    22.     RosTopicState btnPublisher;
    23.  
    24.     private Texture2D liveFeedTexture;
    25.     public string topicRight = "/camera/unity/;
    26.  
    27.    public RawImage theScreen;
    28.    private bool isStopped = false;
    29.    public TextMeshProUGUI startstopText;
    30.  
    31.    public Button theBtn;
    32.  
    33.    void Start()
    34.    {
    35.  
    36.        ros = ROSConnection.GetOrCreateInstance();
    37.        SetRosEnv();
    38.  
    39.        liveFeedTexture = new Texture2D(1832, 1920, TextureFormat.RGB24, false);
    40.        theScreen.texture = liveFeedTexture;
    41.  
    42.        ros.Subscribe<RosImage>(topicRight, ChangeFeedTexture);
    43.        btnPublisher = ros.RegisterPublisher<ROString>("/unity/button");
    44.  
    45.    }
    46.  
    47.    void ChangeFeedTexture(RosImage videoFeed)
    48.    {
    49.        if (isStopped == false)
    50.        {
    51.            liveFeedTexture.LoadImage(videoFeed.data);
    52.  
    53.            liveFeedTexture.Apply();
    54.        }
    55.    }
    56.  
    57.  
    58.    private void SetRosEnv()
    59.    {
    60.        //Creates process start info object for setting starting options
    61.        ProcessStartInfo startInfo = new ProcessStartInfo();
    62.        //Sets file to be opened the cmd.exe for opening the command prompt
    63.        startInfo.FileName = "cmd.exe";
    64.        // Set UseShellExecute to false to redirect input/output
    65.        startInfo.UseShellExecute = false;
    66.        //Set verb to "runas" for running as administrator
    67.        startInfo.Verb = "runas";
    68.        //Hide and prevent creation of cmd window
    69.        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    70.        startInfo.CreateNoWindow = true;
    71.        //Redirect standard input/output/error to receive output from the ROS command
    72.        startInfo.RedirectStandardInput = true;
    73.        startInfo.RedirectStandardOutput = true;
    74.        startInfo.RedirectStandardError = true;
    75.        // Create and start the process
    76.        process.StartInfo = startInfo;
    77.        process.OutputDataReceived += (sender, args1) => UnityEngine.Debug.Log("Output: \n" + "\n" + args1.Data);
    78.        process.ErrorDataReceived += (sender, args1) => UnityEngine.Debug.Log("Error: \n" + "\n" + args1.Data);
    79.        process.Start();
    80.        DataKeeper.currentProcessID = process.Id;
    81.        process.BeginOutputReadLine();
    82.        process.BeginErrorReadLine();
    83.        process.StandardInput.WriteLine("cd /d C:");
    84.        process.StandardInput.WriteLine("cd \\");
    85.        process.StandardInput.WriteLine(".\\my_catkin_wp\\devel\\setup.bat");
    86.        process.StandardInput.WriteLine("set \"ROS_IP=\"");
    87.         process.StandardInput.WriteLine("set ROS_HOSTNAME=meric");
    88.         process.StandardInput.WriteLine("set ROS_MASTER_URI=http://" + DataKeeper.currentDeviceIP + ":11311");
    89.         process.StandardInput.WriteLine("roslaunch ros_tcp_endpoint endpoint.launch");
    90.     }
    91.  
    92.  
    93.     public void OnStartStopClicked()
    94.     {
    95.         isStopped = !isStopped;
    96.         UnityEngine.Debug.Log("Changed to " + isStopped.ToString());
    97.         if (isStopped == false)
    98.         {
    99.             UnityEngine.Debug.Log("Starting the live stream!");
    100.             startstopText.text = "Stop";
    101.         }
    102.         else
    103.         {
    104.             UnityEngine.Debug.Log("Stopping the live stream!");
    105.             startstopText.text = "Start";
    106.         }
    107.     }
    108.  
    109.  
    110.     public void OnTheButton()
    111.     {
    112.         theBtnMsg = new ROString("The button clicked...");
    113.         btnPublisher.Publish(theBtnMsg);
    114.     }
    115.  
    116.  
    117.     public void OnExitButton()
    118.     {
    119.         directionPublisher.Connection.Disconnect();
    120.         directionPublisher = null;
    121.         useModePublisher.Connection.Disconnect();
    122.         directionPublisher = null;
    123.         ros.Unsubscribe("camera/image_left/compressed");
    124.         ros.Unsubscribe("camera/image_right/compressed");
    125.         ros.Disconnect();
    126.         ros = null;
    127.         process.OutputDataReceived += (sender, args1) => UnityEngine.Debug.Log("Output: \n" + "\n" + args1.Data);
    128.         process.ErrorDataReceived += (sender, args1) => UnityEngine.Debug.Log("Error: \n" + "\n" + args1.Data);
    129.         process.StandardInput.WriteLine("\x3");
    130.         System.Threading.Thread.Sleep(10000);
    131.         process.Kill();
    132.         DataKeeper.currentProcessID = null;
    133.         DataKeeper.currentDeviceIP = "";
    134.         SceneManager.LoadScene("DevicesMenu");
    135.     }
    136.  
    137. }
    What should i do to prevent the connection to the first master i have connected?
     

    Attached Files:

    Last edited: Jul 26, 2023