|
| 1 | +# ROS–Unity Integration Networking |
| 2 | + |
| 3 | +## Table of Contents |
| 4 | +- [Definitions](#definitions) |
| 5 | +- [Communicaton Minimum Requirements](#communication-minimum-requirements) |
| 6 | + - [If Using Docker](#if-using-docker) |
| 7 | + - [Explicitly Setting UNITY_IP](#explicitly-setting-unity_ip) |
| 8 | +- [Troubleshoooting](#troubleshooting) |
| 9 | + - [Where Does Communication Break Down](#where-does-communication-break-down) |
| 10 | + - [1. Can Unity send messages to ROS?](#1-can-unity-send-messages-to-ros) |
| 11 | + - [2. Can ROS send messages to Unity?](#2-can-ros-send-messages-to-unity) |
| 12 | + - [Open Port on Windows Firewalll](#open-port-on-windows-firewall) |
| 13 | + |
| 14 | +# Definitions |
| 15 | + |
| 16 | +`ROS_IP` : The IP address of the machine, VM, or container running ROS. |
| 17 | + |
| 18 | +`UNITY_IP` : The IP address of the machine running Unity. |
| 19 | + |
| 20 | +> It is possible to set both of these variables on the machines running Unity and ROS. The specifics of where and why each of these settings will be described below. |
| 21 | +
|
| 22 | +On the ROS machine these settings are set as a rosparam and will typically be set in a launch file like [this](https://github.com/Unity-Technologies/Unity-Robotics-Hub/blob/main/tutorials/ros_packages/robotics_demo/launch/robo_demo.launch) or in a [param file](https://github.com/Unity-Technologies/Unity-Robotics-Hub/blob/main/tutorials/pick_and_place/ROS/src/niryo_moveit/config/params.yaml) loaded by a launch file like [this](https://github.com/Unity-Technologies/Unity-Robotics-Hub/blob/main/tutorials/pick_and_place/ROS/src/niryo_moveit/launch/part_3.launch#L2). The param file can also be loaded manually by running the `rosparam load params.yaml` command. |
| 23 | + |
| 24 | +On the Unity machine these settings are set from the menu by going to `Robotics` -> `ROS Settings` |
| 25 | + |
| 26 | + |
| 27 | +# Communication Minimum Requirements |
| 28 | + |
| 29 | +The minimum settings required for Unity to communicate with ROS is to set the `ROS_IP` on the ROS side in a launch file and on the Unity side in `Robotics` -> `ROS Settings`. |
| 30 | + |
| 31 | +## If Using Docker |
| 32 | + |
| 33 | +The container will need to be started with the following arguments to forward the ports used for communication between ROS and Unity. |
| 34 | + |
| 35 | +`-p 10000:10000 -p 5005:5005` |
| 36 | + |
| 37 | + |
| 38 | +- On the ROS side, set `ROS_IP` to `0.0.0.0`. |
| 39 | + |
| 40 | +- On the Unity side, set `ROS_IP` to `127.0.0.1` and the `Override Unity IP Address` to your local machine's IP address. |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +## Explicitly setting `UNITY_IP` |
| 45 | + |
| 46 | +The `UNITY_IP` can be set in two different places. |
| 47 | + |
| 48 | +1. If set on the ROS side as a rosparam, the `server_endpoint` will only use this IP to send messages to Unity. |
| 49 | +1. If set on the Unity side as the `Override Unity IP Address`, the `UNITY_IP` on the ROS side will be set to this value during the initial handshake between ROS and Unity once play is pressed in the Editor. |
| 50 | + |
| 51 | +> If the `UNITY_IP` is not set in either of these places, then the IP that makes the first connection to ROS during the initial handshake will be used. |
| 52 | +
|
| 53 | +# Troubleshooting |
| 54 | + |
| 55 | +## Where Does Communication Break Down |
| 56 | + |
| 57 | +### 1. Can Unity send messages to ROS? |
| 58 | + |
| 59 | +When play is pressed in the Editor, a handshake message is sent from Unity to ROS. |
| 60 | + |
| 61 | +If Unity can communicate with ROS, the following message should be printed to the console screen running the `server_endpoint.py` script. |
| 62 | + |
| 63 | +```[UnityTcpSender]: ROS-Unity Handshake received, will connect to UNITY_IP_ADDRESS:5005``` |
| 64 | + |
| 65 | +With the corresponding response message printed to the Unity console, |
| 66 | + |
| 67 | +```ROS-Unity server listening on UNITY_IP_ADDRESS:5005``` |
| 68 | + |
| 69 | + |
| 70 | +If the previous message is not shown and either of the following errors are thrown instead: |
| 71 | + |
| 72 | +``` |
| 73 | +SocketException: A connection attempt failed because the connected party did not respond after a period of time, or established connection failed because connected host has failed to respond. |
| 74 | +``` |
| 75 | + |
| 76 | +``` |
| 77 | +SocketException: Connection refused |
| 78 | +``` |
| 79 | + |
| 80 | +Confirm that: |
| 81 | + |
| 82 | +- `server_endpoint` is running |
| 83 | +- You can ping ROS machine from Unity machine |
| 84 | + - From a terminal on the Unity machine, run the following command to confirm whether the ROS machine is reachable over the network. ```ping ROS_IP``` |
| 85 | + |
| 86 | +If issue still persists: |
| 87 | + |
| 88 | +- Confirm your IP addresses |
| 89 | +- If on Windows you may need to [open ports for the firewall](#open-port-on-windows-firewall). |
| 90 | + |
| 91 | +### 2. Can ROS send messages to Unity? |
| 92 | + |
| 93 | +After it is confirmed that Unity can communicate with ROS, publish a message to a ROS topic to which Unity has instantiated a subscriber. |
| 94 | + |
| 95 | +If an error is thrown in the `server_endpoint` console then ROS cannot connect to Unity. |
| 96 | + |
| 97 | +If issue still persists: |
| 98 | + |
| 99 | +- Confirm your IP addresses |
| 100 | +- Explicitly set the `UNITY_IP` |
| 101 | +- If on Windows you may need to [open ports for the firewall](#open-port-on-windows-firewall). |
| 102 | + |
| 103 | +## Open port on Windows Firewall |
| 104 | +- Open port in Windows Firewall by opening `Windows Defender Firewall` then selecting `Action` -> `New Rule...` |
| 105 | + - Port `10000` will need to be added as an outbound rule |
| 106 | + - Port `5005` will need to obe added as an inbound rule |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +In some instances opening a port did not resolve the communication issues and the Unity Editor had to be allowed through the firewall |
| 111 | + |
| 112 | +- Allow the Editor through the firewall by opening `Windows Defender Firewall` -> `Allowed Apps` and then modifying the settings for the Unity Editor to allow communication on private and public networks. |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
0 commit comments