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

Question Is there any way to get information about whether the ROS-TCP Connector is connected to ros?

Discussion in 'Robotics' started by agru, Jan 13, 2022.

  1. agru

    agru

    Joined:
    Jul 6, 2020
    Posts:
    13
    Is there any way to get information about whether the ROS-TCP Connector is connected to ros? I would like to disable some buttons in Unity if Unity is not connected to ROS. Is there an easy way to do this, like an attribute or a method?
     
  2. medinafb01

    medinafb01

    Joined:
    Feb 1, 2021
    Posts:
    14
    Hi, I wrote this code quickly.
    The ROSConnection class has a boolean called "HasConnectionError" that changes every time the ros_tcp_endpoint is connected on the ROS side.
    You can use it to check if ROS is connected in Unity or if it has disconnected in runtime.
    So you can use it to disable and enable your buttons.

    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Robotics.ROSTCPConnector;
    3.  
    4. public class ROSState : MonoBehaviour
    5. {
    6.     private ROSConnection ROS;
    7.  
    8.     void Start()
    9.     {
    10.         ROS = ROSConnection.GetOrCreateInstance(); // Get ROS connection static instance
    11.     }
    12.     void Update()
    13.     {
    14.         if (ROS.HasConnectionError)
    15.         {
    16.             Debug.Log("ROS is not connected");
    17.             //Do some stuff
    18.         }
    19.         else
    20.         {
    21.             Debug.Log("ROS is connected");
    22.             //Do some stuff
    23.         }
    24.     }
    25. }
    26.  
     
    agru and ivan_hmr like this.
  3. agru

    agru

    Joined:
    Jul 6, 2020
    Posts:
    13
    Thank you very much! :)
     
    medinafb01 likes this.