IT/Unity

[ROS] Unity Robotics Simulation

루벤초이 2023. 1. 15. 22:43

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:

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

To save the map file:  $ros2 run nav2_map_server map_saver_cli -f ./test
 

References

  • 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
반응형

'IT > Unity' 카테고리의 다른 글

[Unity] Mixamo Animation  (0) 2023.01.15
[Unity] VS Code and WebGL Deploy  (0) 2023.01.15