使用URDF(Unified Robot Description Format)
URDF是ROS中用于描述机器人模型的XML格式文件。你可以使用XML文件定义机器人的几何形状、惯性参数、关节和链接等。
示例URDF文件(my_robot.urdf
):
<?xml version="1.0"?>
<robot name="my_robot"><link name="base_link"><visual><geometry><box size="0.5 0.5 0.2"/></geometry><material name="blue"><color rgba="0 0 1 1"/></material></visual><collision><geometry><box size="0.5 0.5 0.2"/></geometry></collision><inertial><mass value="10"/><inertia ixx="1" ixy="0" ixz="0" iyy="1" iyz="0" izz="1"/></inertial></link><joint name="base_to_arm" type="fixed"><parent link="base_link"/><child link="arm_link"/><origin xyz="0 0 0.2"/></joint><link name="arm_link"><visual><geometry><cylinder radius="0.05" length="0.5"/></geometry><material name="red"><color rgba="1 0 0 1"/></material></visual><collision><geometry><cylinder radius="0.05" length="0.5"/></geometry></collision><inertial><mass value="2"/><inertia ixx="0.1" ixy="0" ixz="0" iyy="0.1" iyz="0" izz="0.1"/></inertial></link>
</robot>
使用SDF(Simulation Description Format)
SDF是Gazebo中用于描述仿真环境的XML格式文件。你可以使用SDF定义仿真世界、机器人模型、传感器等。
示例SDF文件(my_world.world
):
<?xml version="1.0"?>
<sdf version="1.6"><world name="default"><include><uri>model://ground_plane</uri></include><include><uri>model://sun</uri></include><model name="my_robot"><pose>0 0 0.1 0 0 0</pose><link name="base_link"><collision name="base_collision"><geometry><box><size>0.5 0.5 0.2</size></geometry></collision><visual name="base_visual"><geometry><box><size>0.5 0.5 0.2</size></geometry><material><ambient>0 0 1 1</ambient><diffuse>0 0 1 1</diffuse></material></visual><inertial><mass>10</mass><inertia><ixx>1</ixx><ixy>0</ixy><ixz>0</ixz><iyy>1</iyy><iyz>0</iyz><izz>1</izz></inertia></inertial></link><link name="arm_link"><collision name="arm_collision"><geometry><cylinder><radius>0.05</radius><length>0.5</length></cylinder></geometry></collision><visual name="arm_visual"><geometry><cylinder><radius>0.05</radius><length>0.5</length></cylinder></geometry><material><ambient>1 0 0 1</ambient><diffuse>1 0 0 1</diffuse></material></visual><inertial><mass>2</mass><inertia><ixx>0.1</ixx><ixy>0</ixy><ixz>0</ixz><iyy>0.1</iyy><iyz>0</iyz><izz>0.1</izz></inertia></inertial></link><joint name="base_to_arm" type="fixed"><parent>base_link</parent><child>arm_link</child><pose>0 0 0.2 0 0 0</pose></joint></model></world>
</sdf>
下面是一个包含GNSS(全球导航卫星系统)、IMU(惯性测量单元)、LiDAR(激光雷达)和Camera(摄像头)等多种传感器的机器人的.xacro
模型文件示例。我们将使用Xacro(XML Macros)来简化模型描述。
1. 创建my_robot.xacro
文件
<?xml version="1.0"?>
<robot name="my_robot" xmlns:xacro="http://www.ros.org/wiki/xacro"><!-- Robot base link --><link name="base_link"><visual><geometry><box size="0.5 0.5 0.2"/></geometry><material name="blue"><color rgba="0 0 1 1"/></material></visual><collision><geometry><box size="0.5 0.5 0.2"/></geometry></collision><inertial><mass value="10"/><inertia ixx="1" ixy="0" ixz="0" iyy="1" iyz="0" izz="1"/></inertial></link><!-- GNSS Sensor --><xacro:macro name="gnss_sensor" params="parent xyz rpy"><link name="gnss_link"><visual><geometry><box size="0.05 0.05 0.02"/></geometry><material name="green"><color rgba="0 1 0 1"/></material></visual><collision><geometry><box size="0.05 0.05 0.02"/></geometry></collision><inertial><mass value="0.1"/><inertia ixx="0.001" ixy="0" ixz="0" iyy="0.001" iyz="0" izz="0.001"/></inertial></link><joint name="gnss_joint" type="fixed"><parent link="${parent}"/><child link="gnss_link"/><origin xyz="${xyz}" rpy="${rpy}"/></joint></xacro:macro><!-- IMU Sensor --><xacro:macro name="imu_sensor" params="parent xyz rpy"><link name="imu_link"><visual><geometry><box size="0.05 0.05 0.02"/></geometry><material name="yellow"><color rgba="1 1 0 1"/></material></visual><collision><geometry><box size="0.05 0.05 0.02"/></geometry></collision><inertial><mass value="0.1"/><inertia ixx="0.001" ixy="0" ixz="0" iyy="0.001" iyz="0" izz="0.001"/></inertial></link><joint name="imu_joint" type="fixed"><parent link="${parent}"/><child link="imu_link"/><origin xyz="${xyz}" rpy="${rpy}"/></joint></xacro:macro><!-- LiDAR Sensor --><xacro:macro name="lidar_sensor" params="parent xyz rpy"><link name="lidar_link"><visual><geometry><cylinder radius="0.05" length="0.1"/></geometry><material name="gray"><color rgba="0.5 0.5 0.5 1"/></material></visual><collision><geometry><cylinder radius="0.05" length="0.1"/></geometry></collision><inertial><mass value="0.5"/><inertia ixx="0.01" ixy="0" ixz="0" iyy="0.01" iyz="0" izz="0.01"/></inertial></link><joint name="lidar_joint" type="fixed"><parent link="${parent}"/><child link="lidar_link"/><origin xyz="${xyz}" rpy="${rpy}"/></joint></xacro:macro><!-- Camera Sensor --><xacro:macro name="camera_sensor" params="parent xyz rpy"><link name="camera_link"><visual><geometry><box size="0.03 0.03 0.03"/></geometry><material name="black"><color rgba="0 0 0 1"/></material></visual><collision><geometry><box size="0.03 0.03 0.03"/></geometry></collision><inertial><mass value="0.2"/><inertia ixx="0.001" ixy="0" ixz="0" iyy="0.001" iyz="0" izz="0.001"/></inertial></link><joint name="camera_joint" type="fixed"><parent link="${parent}"/><child link="camera_link"/><origin xyz="${xyz}" rpy="${rpy}"/></joint></xacro:macro><!-- Instantiate sensors --><xacro:gnss_sensor parent="base_link" xyz="0.25 0 0.1" rpy="0 0 0"/><xacro:imu_sensor parent="base_link" xyz="0.25 0 0.15" rpy="0 0 0"/><xacro:lidar_sensor parent="base_link" xyz="0.25 0 0.2" rpy="0 0 0"/><xacro:camera_sensor parent="base_link" xyz="0.25 0 0.25" rpy="0 0 0"/></robot>
2. 将.xacro
文件转换为.urdf
文件
您可以使用以下命令将.xacro
文件转换为.urdf
文件:
rosrun xacro xacro --inorder my_robot.xacro > my_robot.urdf
3. 在Gazebo中加载模型
创建一个.world
文件(例如my_world.world
)并包含您的机器人模型:
<?xml version="1.0"?>
<sdf version="1.6"><world name="default"><include><uri>model://ground_plane</uri></include><include><uri>model://sun</uri></include><model name="my_robot"><include><uri>model://my_robot</uri></include></model></world>
</sdf>
然后在终端中运行:
roslaunch gazebo_ros empty_world.launch world_name:=my_world.world