Skip to content

User_App_ROS_UnitySimulationExample

Martin Bischoff edited this page Mar 9, 2018 · 13 revisions

2.3 Unity Simulation Example

Note: This tutorial assumes that you have completed the previous tutorials:

Overview

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.

General Picture

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#.

Preparation

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.unity from 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 RosConnector for convenience. Drag and drop the script RosConnector.cs to this GameObject and change the Ros Bridge Server Url to the IP address of enp0s8 mentioned earlier in this tutorial. ws://xxx.xxx.xxx.xxx:9090

  • In the same GameObject RosConnector drag and drop the script UrdfPatcher.cs and link the URDF Model to the UrdfPatcher by dragging the object turtlebot/base_footprint/base_link and dropping it into the slot Urdf Model of the Urdf Patcher.

  • Create an empty GameObject JointStateProvider and drag and drop the script JointStateProvider.cs into it. Insert UnityFrameId as Frame Id.

  • In the GameObject RosConnector change the settings as follows:

ROSConnector

  • Drag the GameObject JointStateProvider into the corresponding slot in the Urdf Patcher
  • Click Apply
  • Create a new empty GameObject in the scene and rename it JoyReceiver for convenience. Drag and drop the script JoyReceiver.cs to this GameObject (TODO?)
  • In the object turtlebot/base_footprint/base_link drop the script PoseProvider.cs and change the FrameId to Unity
  • In the object turtlebot/base_footprint/base_link/camera_rgb_frame drop the script ImageProvider.cs and change the FrameId to Camera and 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_link and turtlebot/base_footprint/base_link/wheel_right_link drop the scripts JointStateReader.cs and JoyAxisJointMotorWriter.cs. Set the MaxVelocity to 900 and tag the Use Motor tag.
  • in the objects turtlebot/base_footprint/base_link/wheel_left_link, turtlebot/base_footprint/base_link/wheel_right_link set the Motor/Force to 0.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_link and turtlebot/base_footprint/base_link/caster_back_link set the Mass Scale to 1 and the Connected Mass Scale to 240
  • In the GameObject JoyReceiver change the Joy Axis Writers Size to 2 and drag and drop the objects turtlebot/base_footprint/base_link/wheel_left_link and turtlebot/base_footprint/base_link/wheel_right_link into the corresponding slots Element 0 and Element 1
  • In the GameObject RosConnector drop the script Subscriber.cs, change the topic to /joy and drag and drop the GameObject JoyReceiver to the slot MessageReceiver
  • In the GameObject RosConnector drop the script UnityTimePublisher.cs, change the topic to /odom and drag and drop the GameObject /turtlebot/base_footprint/base_link to the slot MessageProvider
  • Repeat this again, but now change the topic to /unity_image/compressed and drag and drop the GameObject /turtlebot/base_footprint/base_link/camera_rgb_frame to the slot MessageProvider

Setting up ROS

  • Make sure that you followed this and this tutorial.
  • Place the folder unity_simulation_scene inside the src folder 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.

Execution

As soon as all ROS nodes are launched, the robot in Unity is ready to move.

  • When the Play button 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: rqt_graph
    It can be seen here that the topic/joy is published by the ROS node mouse_to_joy and it is sent to Unity using rosbridge_websocket. playbutton

© Siemens AG, 2017-2018 Author: Verena Röhrl ([email protected])

Clone this wiki locally