-
Notifications
You must be signed in to change notification settings - Fork 398
User_App_ROS_UnitySimulationExample
Note: This tutorial assumes that you have completed the previous tutorials:
This application of ROS# illustrates the communication between Unity and ROS in which the real time simulation takes place in Unity. The following figure illustrates the general schema of the application.

The control signals are sent from ROS to Unity and the outcomes of Unity are captured by ROS for illustrative purposes.
-
Messages to be subscribed by Unity using ROS#:
-
/sensor_msgs/joy -
Topics to be published by Unity using ROS#:
-
/odom -
/joint_states -
/unity_image/compressed
The movement of the mouse cursor in Ubuntu is used to control the TurtleBot2 in Unity. Therefore, the ROS node mouse_to_joy.py maps the movement of the mouse cursor to messages of the type sensor_msgs/joy. These are sent to the rosbridge_websocket to be captured by Unity.
In Unity the robot will move according to the captured movement of the mouse cursor in Ubuntu and the topics /odom, /joint_states and /unity_image/compressed are published for further processing using ROS#.
Setting up the Unity scene: Linking URDF to ROS#
-
Make sure that you followed this tutorial. You should already have the URDF model imported into Unity.
-
Import the Unity scene
UnitySimulationScene.unityfrom here and the required Unity scene will be complete and ready to use. -
OR: Create the Unity scene by hand by reproducing the following steps to learn more about particular components and how they collaborate. (a video will be published soon)
-
Create an empty GameObject in the scene. Rename it
RosConnectorfor convenience. Drag and drop the scriptRosConnector.csto this GameObject and change theRos Bridge Server Urlto the IP address ofenp0s8mentioned earlier in this tutorial.ws://xxx.xxx.xxx.xxx:9090 -
In the same GameObject
RosConnectordrag and drop the scriptUrdfPatcher.csand link the URDF Model to the UrdfPatcher by dragging the objectturtlebot/base_footprint/base_linkand dropping it into the slotUrdf Modelof theUrdf Patcher. -
Create an empty GameObject
JointStateProviderand drag and drop the scriptJointStateProvider.csinto it. InsertUnityFrameIdasFrame Id. -
In the GameObject
RosConnectorchange the settings as follows:
- Drag the GameObject
JointStateProviderinto the corresponding slot in theUrdf Patcher - Click
Apply - Create a new empty GameObject in the scene and rename it
JoyReceiverfor convenience. Drag and drop the scriptJoyReceiver.csto this GameObject (TODO?) - In the object
turtlebot/base_footprint/base_linkdrop the scriptPoseProvider.csand change theFrameIdtoUnity - In the object
turtlebot/base_footprint/base_link/camera_rgb_framedrop the scriptImageProvider.csand change theFrameIdtoCameraand set the resolution width and height - As the physical simulation takes place in Unity and some necessary physical properties are not included in the URDF, the following properties have to be changed in Unity:
- in the objects
turtlebot/base_footprint/base_link/wheel_left_linkandturtlebot/base_footprint/base_link/wheel_right_linkdrop the scriptsJointStateReader.csandJoyAxisJointMotorWriter.cs. Set theMaxVelocityto900and tag theUse Motortag. - in the objects
turtlebot/base_footprint/base_link/wheel_left_link,turtlebot/base_footprint/base_link/wheel_right_linkset theMotor/Forceto0.01 - in the objects
turtlebot/base_footprint/base_link/wheel_left_link,turtlebot/base_footprint/base_link/wheel_right_link,turtlebot/base_footprint/base_link/caster_front_linkandturtlebot/base_footprint/base_link/caster_back_linkset theMass Scaleto1and theConnected Mass Scaleto240 - In the GameObject
JoyReceiverchange theJoy Axis Writers Sizeto2and drag and drop the objectsturtlebot/base_footprint/base_link/wheel_left_linkandturtlebot/base_footprint/base_link/wheel_right_linkinto the corresponding slotsElement 0andElement 1 - In the GameObject
RosConnectordrop the scriptSubscriber.cs, change thetopicto/joyand drag and drop the GameObjectJoyReceiverto the slotMessageReceiver - In the GameObject
RosConnectordrop the scriptUnityTimePublisher.cs, change thetopicto/odomand drag and drop the GameObject/turtlebot/base_footprint/base_linkto the slotMessageProvider - Repeat this again, but now change the
topicto/unity_image/compressedand drag and drop the GameObject/turtlebot/base_footprint/base_link/camera_rgb_frameto the slotMessageProvider
- Make sure that you followed this and this tutorial.
- Place the folder
unity_simulation_sceneinside thesrcfolder of your workspace and re-build your workspace. - Run the following command in your terminal:
$ roslaunch unity_simulation_scene unity_simulation_scene.launch
This will launch rosbridge_websocket, file_server, mouse_to_joy and rqt_graph.
As soon as all ROS nodes are launched, the robot in Unity is ready to move.
- When the
Playbutton in pressed and the mouse cursor in Ubuntu is moved the TurtleBot2 will move in Unity. - After clicking the refresh button in the
rqt_graph, a network similar to the following figure appears:
It can be seen here that the topic/joyis published by the ROS nodemouse_to_joyand it is sent to Unity usingrosbridge_websocket.
© Siemens AG, 2017-2018 Author: Verena Röhrl ([email protected])
-
- 1.3.1 R2D2 Setup
- 1.3.2 Gazebo Setup on VM
- 1.3.3 TurtleBot Setup (Optional for ROS2)
- 2.1 Quick Start
- 2.2 Transfer a URDF from ROS to Unity
- 2.3 Transfer a URDF from Unity to ROS
- 2.4 Unity Simulation Scene Example
- 2.5 Gazebo Simulation Scene Example
- 2.6 Fibonacci Action Client
- 2.7 Fibonacci Action Server
- 3.1 Import a URDF on Windows
- 3.2 Create, Modify and Export a URDF Model
- 3.3 Animate a Robot Model in Unity
- 4.1 Introduction to RosBridgeClient
- 4.2 Image Publication
- 4.3 URDF Transfer
- 4.4 Fibonacci Action Client/Server
- Message Handling: Readers & Writers
- Thread Safety for Message Reception
- File Server Package
- ROS-Unity Coordinate System Conversions
- Post Build Events
- Preprocessor Directives in ROS#
- Adding New Message Types
- RosBridgeClient Protocols
- RosBridgeClient Serializers
- Actions in ROS#
- Action Server State Machine Model
© Siemens AG, 2017-2025