IT/Unity
[ROS] Unity Robotics Simulation
루벤초이
2023. 1. 15. 22:43
Unity-Robotics-Hub
Unity provides the simulation for robotics based on ROS. It also provides 4 tutorial/examples.
ROS-Unity Integration (ROS-TCP)
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
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
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);
}
}
}
}
728x90
반응형