3.5 KiB
ROS–Unity Integration: Subscriber
Create a simple Unity scene which subscribes to a ROS topic to change the colour of a GameObject.
NOTE: If following from Publisher tutorial proceed to Setting Up Unity Scene step.
Setting Up ROS
(You can skip this if you already did the ROS–Unity Integration Publisher tutorial.)
-
Copy the
tutorials/ros_packages/robotics_demo
folder of this repo into thesrc
folder in your Catkin workspace. -
Follow the ROS–Unity Initial Setup guide.
-
Open a new terminal window, navigate to your Catkin workspace, and run the following commands:
source devel/setup.bash rosrun robotics_demo server_endpoint.py
Once the server_endpoint has started, it will print something similar to [INFO] [1603488341.950794]: Starting server on 192.168.50.149:10000
.
Setting Up Unity Scene
- Generate the C# code for
UnityColor
message by going toRosMessageGeneration
->AutoGenerateMessages
->Single Message...
- Set the input file path to
PATH/TO/Unity-Robotics-Hub/tutorials/ros_packages/robotics_demo/msg/UnityColor.msg
and clickGENERATE!
- The generated file will be saved in the default directory
Assets/RosMessages/msg
- The generated file will be saved in the default directory
- Create a script and name it
RosSubscriberExample.cs
- Paste the following code into
RosSubscriberExample.cs
- Note Script can be found at
tutorials/ros_unity_integration/unity_scripts
- Note Script can be found at
using UnityEngine;
using RosColor = RosMessageTypes.RoboticsDemo.UnityColor;
public class RosSubscriberExample : MonoBehaviour
{
public ROSConnection ros;
public GameObject cube;
void Start()
{
ros.Subscribe<RosColor>("color", ColorChange);
}
void ColorChange(RosColor colorMessage)
{
cube.GetComponent<Renderer>().material.color = new Color32((byte)colorMessage.r, (byte)colorMessage.g, (byte)colorMessage.b, (byte)colorMessage.a);
}
}
-
Create an empty GameObject, name it
RosConnection
and attach thePackages/ROS TCP Connection/Runtime/TcpConnector/ROSConnection
script. (Or, if you're reusing the same scene from the Publisher tutorial, you can just keep the existing RosConnection object.)- Change the host name and port to match the ROS IP and port variables defined when you set up ROS.
- The IP for Unity to listen on should be determined automatically, but if you're having trouble, you can set it manually in the
Override Unity IP
field. Finding the IP address of your local machine (the one running Unity) depends on your operating system.- On a Mac, open
System Preferences > Network
. Your IP address should be listed on the active connection. - On Windows, click the Wi-Fi icon on the taskbar, and open
Properties
. Your IP address should be listed near the bottom, next to "IPv4 address."
- On a Mac, open
-
Create an empty GameObject and name it
RosSubscriber
-
Attach the
RosSubscriberExample
script to theRosSubscriber
GameObject, drag the cube GameObject onto thecube
parameter in the Inspector window and theRosConnection
object onto theros
parameter. -
Press play in the editor
In ROS Terminal Window
- After the scene has entered Play mode, run the following command:
rosrun robotics_demo color_publisher.py
to change the color of the cube GameObject in Unity to a random color
Continue to the ROS–Unity Integration Service.