Skip to content

Connecting ROS and Unity

tancredelg edited this page Nov 30, 2024 · 10 revisions

1. Open the auv-sim-unity project in Unity

2. Check that the ROS TCP Connector package is installed

Go to/open the package manager window (Window > Package Manager), and check that the ROS TCP Connector (v0.7.0) package is installed in the project.

image image

If it is not installed, click the + to add a package and choose 'Add package from git URL', and use this URL: https://github.com/Unity-Technologies/ROS-TCP-Connector.git?path=/com.unity.robotics.ros-tcp-connector#v0.7.0

image image

3. Check that the Ros Connection GameObject exists and is set-up correctly

Make sure that the Ros Connection GameObject exists in the scene you want to run (e.g. SquarePool, Quali), and that on the GameObject, the ROSConnection component has the field Ros IP Address set to 127.0.0.1 and the field Ros Port set to 10000.

image

4. Run the Docker container

First, make sure you have pulled the mcgillrobotics/auv_2025:sim image from the mcgillrobotics Docker Hub. You can do this either through Docker Desktop, or in the CLI via docker pull mcgillrobotics/auv_2025:sim.

Then, from the AUV-2025/docker/sim/ directory, launch the docker container with docker compose up. This will take a few minutes the first time around because of dependency downloads.

image

Subsequent executions of docker compose up will be a lot faster, e.g.

image

NOTE: If the docker compose up command is failing with an error during connect message, it may be because your Docker Engine's daemon is not running (put simply, Docker is not actively running on your machine).

  • If you installed Docker via Docker Desktop then the fix is as simple as checking that the Docker Desktop app is running, and that you see the Engine running status in the app:

image

  • If you didn't install Docker via Docker Desktop, see the Docker Docs page for starting the daemon.

5. Connect/attach to the container

Now that the container is running, we need to attach a terminal to the container so that you can run commands in its shell.

You can do this using VS Code by opening the Docker panel on the primary sidebar, then right clicking on the running AUV sim container, and clicking 'Attach Visual Studio Code'.

image

This will open up an entirely new VS code window, from which you can open a terminal (Ctrl + J is the default shortcut to do that) which will be in the container. Once the terminal is opened, you need to cd .. to go back from the home directory (~) to the root directory (/) where all the files have been installed.

image

From there is where you can execute commands in the container.

More Docker info can be found in the AUV-2025 repo's wiki.

6. Launch the ROS endpoint in the container

From the container's terminal, run roslaunch sim endpoint.launch tcp_ip:=127.0.0.1 to launch the TCP endpoint for Unity to connect to.

image

NOTE: If the roslaunch command does not work, you may need you first cd AUV-2025/catkin-ws/ and run catkin build, followed by source devel/setup.bash.

image

7. Play the Unity sim

Go back to Unity, and press play. In the container's terminal, check that subscribers/publishers were registered (this means the Unity sim robot is connected to the ROS endpoint), and your simulation is set up and working!

Subscriber/publisher registrations look like this:

image

NOTE: If your are not getting any subscribers/publishers, and in Unity you are getting the following message(s) in the console:

image

then try to establish the ROS connection on a different port, e.g. 10001.

To do this, follow the previous steps, changing the ROSConnection component's Ros Port field to 10001, and for step 6, specify the port explicitly in the roslaunch command, i.e. run roslaunch sim endpoint.launch tcp_ip:=127.0.0.1 tcp_port:=10001 - this will override the default value for the tcp_port arg in AUV-2025/catkin_ws/src/sim/launch/endpoint.launch (i.e. this line: <arg name="tcp_port" default="10000"/>).

image

If this happens to you - and changing the port fixes the problem - bring it up, so that if we realise it is a recurring issue, we will change the default port to something else, making everyone's lives just a little easier.

If this guide wasn't enough, and/or you want a video guide, see this video.