0. 简介

在我们上一章讲的对于Unity与ROS之间的通信,Toolbox等比较基础的指令。下面我们将结合代码来介绍一下如何导入URDF文件,并通过键盘完成Unity的控制。

1. URDF模型创建

  1. 首先先创建并编辑URDF文件,并命名为toio_style.urdf

<?xml version="1.0"?>
<robot name="toio_style">

<!--车身-->
<link name="base_link">
<visual>
<geometry>
<box size="0.3 0.3 0.23" />
</geometry>
<material name="white">
<color rgba="1.0 1.0 1.0 1.0" />
</material>
</visual>
<collision>
<geometry>
<box size="0.3 0.3 0.23" />
</geometry>
</collision>
<inertial>
<mass value="1.0" />
<inertia ixx="0.015" iyy="0.0375" izz="0.0375" ixy="0" ixz="0" iyz="0" />
</inertial>
</link>

<!--右车轮-->
<link name="right_wheel">
<visual>
<geometry>
<cylinder length="0.05" radius="0.035" />
</geometry>
<material name="gray">
<color rgba="0.2 0.2 0.2 1" />
</material>
</visual>
<collision>
<geometry>
<cylinder length="0.05" radius="0.035" />
</geometry>
</collision>
<inertial>
<mass value="0.1" />
<inertia ixx="5.1458e-5" iyy="5.1458e-5" izz="6.125e-5" ixy="0" ixz="0" iyz="0" />
</inertial>
</link>
<joint name="right_wheel_joint" type="continuous">
<axis xyz="0 0 1" />
<parent link="base_link" />
<child link="right_wheel" />
<origin rpy="-1.5708 0 0" xyz="0.0 -0.125 -.09" />
</joint>

<!--左车轮-->
<link name="left_wheel">
<visual>
<geometry>
<cylinder length="0.05" radius="0.035" />
</geometry>
<material name="gray" />
</visual>
<collision>
<geometry>
<cylinder length="0.05" radius="0.035" />
</geometry>
</collision>
<inertial>
<mass value="0.1" />
<inertia ixx="5.1458e-5" iyy="5.1458e-5" izz="6.125e-5" ixy="0" ixz="0" iyz="0" />
</inertial>
</link>
<joint name="left_wheel_joint" type="continuous">
<axis xyz="0 0 1" />
<parent link="base_link" />
<child link="left_wheel" />
<origin rpy="-1.5708 0 0" xyz="0.0 0.125 -.09" />
</joint>
</robot>

2. ROS处准备

  1. 启动Docker映像,并指定10000和5005端口打开。因为Unity和ROS之间的通信需要端口号10000和5005。

docker run -v ~/ros2_ws:/home/ubuntu/colcon_ws:cached -p 6080:80 -p 10000:10000 -p 5005:5005 --shm-size=1024m tiryoh/ros2-desktop-vnc:galactic

  1. 安装“ROS-TCP-Endpoint”软件包。并使用“main-ros2”分支中的ROS-TCP-Endpoint包

cd ~/colcon_ws/src
git clone -b main-ros2 https://github.com/Unity-Technologies/ROS-TCP-Endpoint

  1. 然后创建工作空间

cd ~/colcon_ws
colcon build
source ~/colcon_ws/install/setup.bash

3. Unity处准备

Unity方面的准备步骤基本和第一章提到的类似,即:

  1. 在Unity菜单“Window→Package Manager”中打开“Package Manager”。
  2. 在“Package Manager”中选择“+→Add Package from Git URL”,输入以下URL,按下“Add”按钮。并输入以下链接加载ROS-TCP-Connector插件

https://github.com/Unity-Technologies/ROS-TCP-Connector.git?path=/com.unity.robotics.ros-tcp-connector

  1. Package Manager”中选择“+→Add Package from Git URL”,输入以下URL,按下“Add”按钮。并输入以下链接加载URDF Importer插件

https://github.com/Unity-Technologies/URDF-Importer.git?path=/com.unity.robotics.urdf-importer

4. URDF模型导入

将URDF模型导入Unity的场景中。

  1. 在Unity的Assets中配置“toio_style.urdf”。
  2. 在Project窗口中,右键点击“toio_style.urdf”,选择“Import Robot Select form URDF file”

Unity-ROS2与URDF导入(三)_游戏引擎


3. Import URDF

Unity-ROS2与URDF导入(三)_unity_02


4. 在Hierarchy窗口用Plane制作地板

Unity-ROS2与URDF导入(三)_unity_03

6. ROS2控制Unity

为了操作URDF模型,在URDF模型中添加​​Robotics-Nav2-SLAM-Example​​中的AGVController。这样Unity就可以接受来自/cmd_vel主话题的信息,用于控制Unity当中的机器人了。

Unity-ROS2与URDF导入(三)_github_04

  1. 在Hierarchy窗口中选择URDF模型(toio_style),在Inspector窗口中取消Controller组件的检查。
  2. 下载Robotics-Nav2-SLAM-Example包并将AGVController脚本文件放到Unity的Assets中。
  3. 在Unity中将导入的URDF模型(toio_style)中添加AGVController脚本,即在加载脚本后,将脚本的Wheel1变量添加toio_style的left_wheel的TF信息, Wheel2中添加toio_style的right_wheel的TF信息。

Unity-ROS2与URDF导入(三)_unity_05

·Wheel 1:车轮1
·Wheel 2:车轮2
·Mode:模式(ROS / Keyboard)
·Max Linear Speed:最大直行速度
·Max Rotation Speed:最大转速
·Wheel Radius:车轮半径
·Track Width:轨道宽度
·Force Limit:力量的最大值
·Damping:衰减
·ROS Timeout:一段时间内没有ROS消息时停止

  1. 在AGVController”的Mode中指定Keyboard,按下Play按钮,用键盘确认机器人的动作
  2. 然后打开终端,指定ROS的IP和端口,执行ROS2的ROS-TCP-Endpoint指令

ros2 run ros_tcp_endpoint default_server_endpoint --ros-args -p ROS_IP:=0.0.0.0

在“AGVController”的Mode中指定ROS,按下Play按钮。如果成功连接到ROS2,左上角的“ROS IP”箭头就会变成蓝色。

Unity-ROS2与URDF导入(三)_Endpoint_06


在ROS侧输入以下命令,发布/cmd_vel主题。从而完成对Unity中的URDF模型运行