[ROS] Unity Robotics Simulation
Unity-Robotics-Hub
GitHub - Unity-Technologies/Unity-Robotics-Hub: Central repository for tools, tutorials, resources, and documentation for roboti
Central repository for tools, tutorials, resources, and documentation for robotics simulation in Unity. - GitHub - Unity-Technologies/Unity-Robotics-Hub: Central repository for tools, tutorials, re...
github.com
Unity provides the simulation for robotics based on ROS. It also provides 4 tutorial/examples.
ROS-Unity Integration (ROS-TCP)
GitHub - Unity-Technologies/Unity-Robotics-Hub: Central repository for tools, tutorials, resources, and documentation for roboti
Central repository for tools, tutorials, resources, and documentation for robotics simulation in Unity. - GitHub - Unity-Technologies/Unity-Robotics-Hub: Central repository for tools, tutorials, re...
github.com
The communication between ROS and Unity is conducted by ROS-Unity Integration (called ROS-TCP Endpoint / Connector), which is similar to Rosbridge_suite. In fact, there are also FAQ &discussion about comparing them as follows:
- https://github.com/Unity-Technologies/Unity-Robotics-Hub/blob/main/faq.md
- https://github.com/RobotWebTools/rosbridge_suite/issues/546
There's more. ROS2 For Unity, an open source from Robotec.ai, uses DDS so it would be the fastest protocol between ROS2 and Unity.
Anyway, Let's install ROS2 foxy and install ROS-TCP-Endpoint.
$ source /opt/ros/foxy/setup.bash
$ git clone -b main-ros2 https://github.com/Unity-Technologies/ROS-TCP-Endpoint.git
$ colcon build
$ source install/setup.bash
$ ros2 run ros_tcp_endpoint default_server_endpoint --ros-args -p ROS_IP:=127.0.0.1
It provides ROS topic pub/sub and service call, not action.
NAV2 Slam Example
GitHub - Unity-Technologies/Robotics-Nav2-SLAM-Example: An example project which contains the Unity components necessary to comp
An example project which contains the Unity components necessary to complete Navigation2's SLAM tutorial with a Turtlebot3, using a custom Unity environment in place of Gazebo. - GitHub - Unity...
github.com
Build
$ source /opt/ros/foxy/setup.bash
$ cd Robotics-Nav2-SLAM-Example/ros2_docker/colcon_ws
$ colcon build
$ source install/local_setup.bash
Run
$ ros2 launch unity_slam_example unity_slam_example.py
Then play the unity scene. It then performs Nav2 SLAM Navigating While Mapping
![](https://blog.kakaocdn.net/dn/baLgYk/btrWis8xWro/jCSzjKLcfrVX8LhfRAnBJ1/img.png)
![](https://blog.kakaocdn.net/dn/G1KLH/btrWg0q1kqx/0IyJwsxAJR2MwKnBHPvuC0/img.png)
To save the map file: $ros2 run nav2_map_server map_saver_cli -f ./test
References
- Robotec.ai Blog <Open-source release of ROS2 For Unity>
- Blog about ROS2 For Unity
- Mouse 3D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Robotics.ROSTCPConnector;
using RosMessageTypes.Geometry;
using RosMessageTypes.Std;
public class MousePosition3D : MonoBehaviour
{
ROSConnection ros;
public string topicName = "/goal_pose";
[SerializeField] private Camera mainCamera;
void Start()
{
ros = ROSConnection.GetOrCreateInstance();
ros.RegisterPublisher<PoseStampedMsg>(topicName);
}
void Update()
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit raycastHit))
{
transform.position = raycastHit.point;
if (Input.GetMouseButtonDown(0))
{
GameObject turtle = GameObject.Find("turtlebot3_manual_config");
Debug.Log(turtle.transform.position);
Debug.Log(transform.position);
HeaderMsg header = new HeaderMsg();
PoseMsg pose = new PoseMsg(
new PointMsg(transform.position.z, transform.position.x * (-1), 0),
new QuaternionMsg(0, 0, 0, 1)
);
PoseStampedMsg poseStamped = new PoseStampedMsg(header, pose);
ros.Publish(topicName, poseStamped);
}
}
}
}