URDF(统一机器人描述格式)是一种用于在 ROS 中指定机器人几何和组织的文件格式。
从头开始构建可见机器人模型
构建一个可移动的机器人模型
添加物理和碰撞属性
使用 Xacro 清理您的代码
使用 URDF 与
robot_state_publisher
生成 URDF 文件
从头开始构建visual机器人模型
目标:学习如何构建一个可以在 Rviz 中查看的机器人visual模型
教程级别:中级
时间:20 分钟
目录
一种形状
多种形状
起源
材质女孩
完成模型
便条
本教程假设您知道如何编写格式良好的 XML 代码
在本教程中,我们将构建一个大致看起来像 R2D2 的机器人视觉模型。在后续教程中,您将学习如何表达模型 https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Building-a-Movable-Robot-Model-with-URDF.html ,添加一些物理属性 https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Adding-Physical-and-Collision-Properties-to-a-URDF-Model.html ,并使用 xacro 生成更整洁的代码 https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Using-Xacro-to-Clean-Up-a-URDF-File.html ,但现在,我们将专注于使视觉几何正确。
在继续之前,请确保已安装 joint_state_publisher 软件包https://index.ros.org/p/joint_state_publisher 。如果您安装了 urdf_tutorial 二进制文件 https://index.ros.org/p/urdf_tutorial/ ,这应该已经是这种情况。如果没有,请更新您的安装以包含该软件包(使用 rosdep
进行检查)。
本教程中提到的所有机器人模型(以及源文件)都可以在 urdf_tutorial 包中找到。https://github.com/ros/urdf_tutorial/tree/ros2/ 源码下载
一种形状
首先,我们将探索一个简单的形状。这是你能制作的最简单的 urdf 之一。[来源:01-myfirst.urdf]
<?xml version="1.0"?> <!-- 声明 XML 版本为 1.0 -->
<robot name="myfirst"> <!-- 定义一个名为 "myfirst" 的机器人 -->
<link name="base_link"> <!-- 定义一个名为 "base_link" 的链接 -->
<visual> <!-- 定义视觉元素 -->
<geometry> <!-- 定义几何形状 -->
<cylinder length="0.6" radius="0.2"/> <!-- 定义一个长度为 0.6,半径为 0.2 的圆柱体 -->
</geometry>
</visual>
</link>
</robot>
要将 XML 翻译成英文,这是一个名为 myfirst
的机器人,它只包含一个Link(即部分),其视觉组件只是一个长 0.6 米、半径 0.2 米的圆柱体。这对于一个简单的“hello world”类型的示例来说,可能看起来有很多封闭标签,但它会变得更复杂,相信我。
要检查模型,请启动 display.launch.py
文件:
from launch import LaunchDescription # 从launch模块导入LaunchDescription类
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription # 从launch.actions模块导入DeclareLaunchArgument和IncludeLaunchDescription类
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution # 从launch.substitutions模块导入LaunchConfiguration和PathJoinSubstitution类
from launch_ros.substitutions import FindPackageShare # 从launch_ros.substitutions模块导入FindPackageShare类
def generate_launch_description(): # 定义一个名为generate_launch_description的函数
ld = LaunchDescription() # 创建一个LaunchDescription对象
urdf_tutorial_path = FindPackageShare('urdf_tutorial') # 查找urdf_tutorial包的共享路径
default_model_path = PathJoinSubstitution(['urdf', '01-myfirst.urdf']) # 拼接默认的URDF模型路径
default_rviz_config_path = PathJoinSubstitution([urdf_tutorial_path, 'rviz', 'urdf.rviz']) # 拼接默认的RViz配置文件路径
# 这些参数是为了向后兼容而保留的
gui_arg = DeclareLaunchArgument(name='gui', default_value='true', choices=['true', 'false'],
description='Flag to enable joint_state_publisher_gui') # 声明一个名为gui的启动参数,默认值为true,可选值为true或false,描述为启用joint_state_publisher_gui的标志
ld.add_action(gui_arg) # 将gui_arg添加到LaunchDescription对象中
rviz_arg = DeclareLaunchArgument(name='rvizconfig', default_value=default_rviz_config_path,
description='Absolute path to rviz config file') # 声明一个名为rvizconfig的启动参数,默认值为default_rviz_config_path,描述为RViz配置文件的绝对路径
ld.add_action(rviz_arg) # 将rviz_arg添加到LaunchDescription对象中
# 这个参数的含义与以前的版本略有不同
ld.add_action(DeclareLaunchArgument(name='model', default_value=default_model_path,
description='Path to robot urdf file relative to urdf_tutorial package')) # 声明一个名为model的启动参数,默认值为default_model_path,描述为相对于urdf_tutorial包的机器人URDF文件路径
ld.add_action(IncludeLaunchDescription(
PathJoinSubstitution([FindPackageShare('urdf_launch'), 'launch', 'display.launch.py']), # 包含另一个启动文件display.launch.py
launch_arguments={
'urdf_package': 'urdf_tutorial', # 设置urdf_package参数为urdf_tutorial
'urdf_package_path': LaunchConfiguration('model'), # 设置urdf_package_path参数为model启动参数的值
'rviz_config': LaunchConfiguration('rvizconfig'), # 设置rviz_config参数为rvizconfig启动参数的值
'jsp_gui': LaunchConfiguration('gui')}.items() # 设置jsp_gui参数为gui启动参数的值
))
return ld # 返回LaunchDescription对
# display.launch.py
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.actions import IncludeLaunchDescription
from launch.conditions import IfCondition, UnlessCondition
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution
from launch_ros.actions import Node
from launch_ros.substitutions import FindPackageShare
# 生成启动描述函数
def generate_launch_description():
ld = LaunchDescription() # 创建一个LaunchDescription对象
urdf_launch_package = FindPackageShare('urdf_launch') # 查找urdf_launch包的共享路径
# 添加启动参数,用于启用joint_state_publisher_gui
ld.add_action(DeclareLaunchArgument(name='jsp_gui', default_value='true', choices=['true', 'false'],
description='Flag to enable joint_state_publisher_gui'))
# 设置rviz配置文件的默认路径
default_rviz_config_path = PathJoinSubstitution([urdf_launch_package, 'config', 'urdf.rviz'])
ld.add_action(DeclareLaunchArgument(name='rviz_config', default_value=default_rviz_config_path,
description='Absolute path to rviz config file'))
# 由于https://github.com/ros2/launch/issues/313,需要手动传递配置
ld.add_action(IncludeLaunchDescription(
PathJo