Unity-ROS2与URDF导入(三)

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”

在这里插入图片描述
3. Import URDF
在这里插入图片描述
4. 在Hierarchy窗口用Plane制作地板
在这里插入图片描述

6. ROS2控制Unity

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

  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信息。

在这里插入图片描述

·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”箭头就会变成蓝色。
在这里插入图片描述
在ROS侧输入以下命令,发布/cmd_vel主题。从而完成对Unity中的URDF模型运行

…详情请参照古月居

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敢敢のwings

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值