【ROS入门】机器人系统仿真——URDF集成Gazebo

文章结构

  • URDF与Gazebo基本集成流程
    • 创建功能包
    • 编写URDF或Xacro文件
    • 启动 Gazebo 并显示机器人模型
  • URDF集成Gazebo相关设置
    • collision
    • inertial
    • 颜色设置
  • URDF集成Gazebo实操
    • 编写封装惯性矩阵算法的 xacro 文件
    • 复制相关 xacro 文件,并设置 collision inertial 以及 color 等参数
    • 在 launch 文件中启动 gazebo 并添加机器人模型

URDF与Gazebo基本集成流程

URDF 与 Gazebo 集成流程与 Rviz 实现类似,主要步骤如下:

创建功能包

创建功能包,导入依赖项:urdfxacrogazebo_rosgazebo_ros_controlgazebo_plugins

编写URDF或Xacro文件

<robot name="mycar">
    <link name="base_link">
        <visual>
            <geometry>
                <box size="0.5 0.2 0.1" />
            geometry>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
            <material name="yellow">
                <color rgba="0.5 0.3 0.0 1" />
            material>
        visual>
        <collision>
            <geometry>
                <box size="0.5 0.2 0.1" />
            geometry>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
        collision>
        <inertial>
            <origin xyz="0 0 0" />
            <mass value="6" />
            <inertia ixx="1" ixy="0" ixz="0" iyy="1" iyz="0" izz="1" />
        inertial>
    link>
    <gazebo reference="base_link">
        <material>Gazebo/Redmaterial>
    gazebo>
robot>

注意, 当 URDF 需要与 Gazebo 集成时,和 Rviz 有明显区别:

  1. 必须使用 collision 标签,因为既然是仿真环境,那么必然涉及到碰撞检测,collision 提供碰撞检测的依据。

  2. 必须使用 inertial 标签,此标签标注了当前机器人某个刚体部分的惯性矩阵,用于一些力学相关的仿真计算。

  3. 颜色设置,也需要重新使用 gazebo 标签标注,因为之前的颜色设置为了方便调试包含透明度,仿真环境下没有此选项。而且如果没设置的话,Gazebo环境里头是看不到机器人的 (气急败坏)

启动 Gazebo 并显示机器人模型

launch文件实现:

<launch>
    
    <param name="robot_description" textfile="$(find URDF_Gazebo)/urdf/demo01.urdf" />
    
    <include file="$(find gazebo_ros)/launch/empty_world.launch" />
    
    <node pkg="gazebo_ros" type="spawn_model" name="model" args="-urdf -model mycar -param robot_description"  />
launch>

实现效果:

【ROS入门】机器人系统仿真——URDF集成Gazebo_第1张图片

URDF集成Gazebo相关设置

较之于 rviz,gazebo在集成 URDF 时,需要做些许修改,比如:必须添加 collision 碰撞属性相关参数、必须添加 inertial 惯性矩阵相关参数,另外,如果直接移植 Rviz 中机器人的颜色设置是没有显示的,颜色设置也必须做相应的变更。

collision

如果机器人link是标准的几何体形状,和link的 visual 属性设置一致即可。

inertial

惯性矩阵的设置需要结合link的质量与外形参数动态生成,标准的球体、圆柱与立方体的惯性矩阵公式如下(已经封装为 xacro 实现):

球体惯性矩阵

<xacro:macro name="sphere_inertial_matrix" params="m r">
    <inertial>
        <mass value="${m}" />
        <inertia ixx="${2*m*r*r/5}" ixy="0" ixz="0"
            iyy="${2*m*r*r/5}" iyz="0" 
            izz="${2*m*r*r/5}" />
    inertial>
xacro:macro>

圆柱惯性矩阵

<xacro:macro name="cylinder_inertial_matrix" params="m r h">
    <inertial>
        <mass value="${m}" />
        <inertia ixx="${m*(3*r*r+h*h)/12}" ixy = "0" ixz = "0"
            iyy="${m*(3*r*r+h*h)/12}" iyz = "0"
            izz="${m*r*r/2}" /> 
    inertial>
xacro:macro>

立方体惯性矩阵

<xacro:macro name="Box_inertial_matrix" params="m l w h">
    <inertial>
            <mass value="${m}" />
            <inertia ixx="${m*(h*h + l*l)/12}" ixy = "0" ixz = "0"
                iyy="${m*(w*w + l*l)/12}" iyz= "0"
                izz="${m*(w*w + h*h)/12}" />
    inertial>
xacro:macro>

需要注意的是,原则上,除了 base_footprint 外,机器人的每个刚体部分都需要设置惯性矩阵,且惯性矩阵必须经计算得出,如果随意定义刚体部分的惯性矩阵,那么可能会导致机器人在 Gazebo 中出现抖动,移动等现象。

颜色设置

在 gazebo 中显示 link 的颜色,必须要使用指定的标签:

<gazebo reference="link节点名称">
     <material>Gazebo/Bluematerial>
gazebo>

URDF集成Gazebo实操

需求描述: 将之前的机器人模型(xacro版)显示在 gazebo 中

编写封装惯性矩阵算法的 xacro 文件

head.xacro

<robot name="base" xmlns:xacro="http://wiki.ros.org/xacro">
    
    <xacro:macro name="sphere_inertial_matrix" params="m r">
        <inertial>
            <mass value="${m}" />
            <inertia ixx="${2*m*r*r/5}" ixy="0" ixz="0"
                iyy="${2*m*r*r/5}" iyz="0" 
                izz="${2*m*r*r/5}" />
        inertial>
    xacro:macro>

    <xacro:macro name="cylinder_inertial_matrix" params="m r h">
        <inertial>
            <mass value="${m}" />
            <inertia ixx="${m*(3*r*r+h*h)/12}" ixy = "0" ixz = "0"
                iyy="${m*(3*r*r+h*h)/12}" iyz = "0"
                izz="${m*r*r/2}" /> 
        inertial>
    xacro:macro>

    <xacro:macro name="Box_inertial_matrix" params="m l w h">
       <inertial>
               <mass value="${m}" />
               <inertia ixx="${m*(h*h + l*l)/12}" ixy = "0" ixz = "0"
                   iyy="${m*(w*w + l*l)/12}" iyz= "0"
                   izz="${m*(w*w + h*h)/12}" />
       inertial>
   xacro:macro>
robot>

复制相关 xacro 文件,并设置 collision inertial 以及 color 等参数

底盘Xacro文件:demo05_car_base.urdf.xacro



<robot name="mycar" xmlns:xacro="http://www.ros.org/wiki/xacro">
    
    
    <xacro:property name="PI" value="3.1415926"/>
    
    <material name="black">
        <color rgba="0.0 0.0 0.0 1.0" />
    material>
    
    <xacro:property name="base_footprint_radius" value="0.001" /> 
    <xacro:property name="base_link_radius" value="0.1" /> 
    <xacro:property name="base_length" value="0.08" /> 
    <xacro:property name="earth_space" value="0.015" /> 
    <xacro:property name="base_link_m" value="0.5" /> 

    
    <link name="base_footprint">
      <visual>
        <geometry>
          <sphere radius="${base_footprint_radius}" />
        geometry>
      visual>
    link>

    <link name="base_link">
      <visual>
        <geometry>
          <cylinder radius="${base_link_radius}" length="${base_length}" />
        geometry>
        <origin xyz="0 0 0" rpy="0 0 0" />
        <material name="yellow">
          <color rgba="0.5 0.3 0.0 0.5" />
        material>
      visual>
      <collision>
        <geometry>
          <cylinder radius="${base_link_radius}" length="${base_length}" />
        geometry>
        <origin xyz="0 0 0" rpy="0 0 0" />
      collision>
      <xacro:cylinder_inertial_matrix m="${base_link_m}" r="${base_link_radius}" h="${base_length}" />
    link>


    <joint name="base_link2base_footprint" type="fixed">
      <parent link="base_footprint" />
      <child link="base_link" />
      <origin xyz="0 0 ${earth_space + base_length / 2 }" />
    joint>
    <gazebo reference="base_link">
        <material>Gazebo/Yellowmaterial>
    gazebo>

    
    
    <xacro:property name="wheel_radius" value="0.0325" />
    <xacro:property name="wheel_length" value="0.015" />
    <xacro:property name="wheel_m" value="0.05" /> 

    
    <xacro:macro name="add_wheels" params="name flag">
      <link name="${name}_wheel">
        <visual>
          <geometry>
            <cylinder radius="${wheel_radius}" length="${wheel_length}" />
          geometry>
          <origin xyz="0.0 0.0 0.0" rpy="${PI / 2} 0.0 0.0" />
          <material name="black" />
        visual>
        <collision>
          <geometry>
            <cylinder radius="${wheel_radius}" length="${wheel_length}" />
          geometry>
          <origin xyz="0.0 0.0 0.0" rpy="${PI / 2} 0.0 0.0" />
        collision>
        <xacro:cylinder_inertial_matrix m="${wheel_m}" r="${wheel_radius}" h="${wheel_length}" />

      link>

      <joint name="${name}_wheel2base_link" type="continuous">
        <parent link="base_link" />
        <child link="${name}_wheel" />
        <origin xyz="0 ${flag * base_link_radius} ${-(earth_space + base_length / 2 - wheel_radius) }" />
        <axis xyz="0 1 0" />
      joint>

      <gazebo reference="${name}_wheel">
        <material>Gazebo/Redmaterial>
      gazebo>

    xacro:macro>
    <xacro:add_wheels name="left" flag="1" />
    <xacro:add_wheels name="right" flag="-1" />
    
    
    <xacro:property name="support_wheel_radius" value="0.0075" /> 
    <xacro:property name="support_wheel_m" value="0.03" /> 

    
    <xacro:macro name="add_support_wheel" params="name flag" >
      <link name="${name}_wheel">
        <visual>
            <geometry>
                <sphere radius="${support_wheel_radius}" />
            geometry>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <material name="black" />
        visual>
        <collision>
            <geometry>
                <sphere radius="${support_wheel_radius}" />
            geometry>
            <origin xyz="0 0 0" rpy="0 0 0" />
        collision>
        <xacro:sphere_inertial_matrix m="${support_wheel_m}" r="${support_wheel_radius}" />
      link>

      <joint name="${name}_wheel2base_link" type="continuous">
          <parent link="base_link" />
          <child link="${name}_wheel" />
          <origin xyz="${flag * (base_link_radius - support_wheel_radius)} 0 ${-(base_length / 2 + earth_space / 2)}" />
          <axis xyz="1 1 1" />
      joint>
      <gazebo reference="${name}_wheel">
        <material>Gazebo/Redmaterial>
      gazebo>
    xacro:macro>

    <xacro:add_support_wheel name="front" flag="1" />
    <xacro:add_support_wheel name="back" flag="-1" />

robot>

摄像头Xacro文件:demo06_car_camera.urdf.xacro


<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
    
    <xacro:property name="camera_length" value="0.01" /> 
    <xacro:property name="camera_width" value="0.025" /> 
    <xacro:property name="camera_height" value="0.025" /> 
    <xacro:property name="camera_x" value="0.08" /> 
    <xacro:property name="camera_y" value="0.0" /> 
    <xacro:property name="camera_z" value="${base_length / 2 + camera_height / 2}" /> 

    <xacro:property name="camera_m" value="0.01" /> 

    
    <link name="camera">
        <visual>
            <geometry>
                <box size="${camera_length} ${camera_width} ${camera_height}" />
            geometry>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
            <material name="black" />
        visual>
        <collision>
            <geometry>
                <box size="${camera_length} ${camera_width} ${camera_height}" />
            geometry>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
        collision>
        <xacro:Box_inertial_matrix m="${camera_m}" l="${camera_length}" w="${camera_width}" h="${camera_height}" />
    link>

    <joint name="camera2base_link" type="fixed">
        <parent link="base_link" />
        <child link="camera" />
        <origin xyz="${camera_x} ${camera_y} ${camera_z}" />
    joint>
    <gazebo reference="camera">
        <material>Gazebo/Bluematerial>
    gazebo>
robot>

雷达Xacro文件:demo07_car_laser.urdf.xacro


<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">

    
    <xacro:property name="support_length" value="0.15" /> 
    <xacro:property name="support_radius" value="0.01" /> 
    <xacro:property name="support_x" value="0.0" /> 
    <xacro:property name="support_y" value="0.0" /> 
    <xacro:property name="support_z" value="${base_length / 2 + support_length / 2}" /> 
    <xacro:property name="support_m" value="0.02" /> 

    <link name="support">
        <visual>
            <geometry>
                <cylinder radius="${support_radius}" length="${support_length}" />
            geometry>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
            <material name="red">
                <color rgba="0.8 0.2 0.0 0.8" />
            material>
        visual>

        <collision>
            <geometry>
                <cylinder radius="${support_radius}" length="${support_length}" />
            geometry>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
        collision>

        <xacro:cylinder_inertial_matrix m="${support_m}" r="${support_radius}" h="${support_length}" />
    link>

    <joint name="support2base_link" type="fixed">
        <parent link="base_link" />
        <child link="support" />
        <origin xyz="${support_x} ${support_y} ${support_z}" />
    joint>

    <gazebo reference="support">
        <material>Gazebo/Whitematerial>
    gazebo>

    
    <xacro:property name="laser_length" value="0.05" /> 
    <xacro:property name="laser_radius" value="0.03" /> 
    <xacro:property name="laser_x" value="0.0" /> 
    <xacro:property name="laser_y" value="0.0" /> 
    <xacro:property name="laser_z" value="${support_length / 2 + laser_length / 2}" /> 
    <xacro:property name="laser_m" value="0.1" /> 

    
    <link name="laser">
        <visual>
            <geometry>
                <cylinder radius="${laser_radius}" length="${laser_length}" />
            geometry>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
            <material name="black" />
        visual>
        <collision>
            <geometry>
                <cylinder radius="${laser_radius}" length="${laser_length}" />
            geometry>
            <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
        collision>
        <xacro:cylinder_inertial_matrix m="${laser_m}" r="${laser_radius}" h="${laser_length}" />
    link>

    <joint name="laser2support" type="fixed">
        <parent link="support" />
        <child link="laser" />
        <origin xyz="${laser_x} ${laser_y} ${laser_z}" />
    joint>
    <gazebo reference="laser">
        <material>Gazebo/Blackmaterial>
    gazebo>
robot>

组合底盘、摄像头与雷达的 Xacro 文件:car.urdf.xacro


<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
    <xacro:include filename="my_head.urdf.xacro" />
    <xacro:include filename="demo05_car_base.urdf.xacro"/>
    <xacro:include filename="demo06_car_camera.urdf.xacro"/>
    <xacro:include filename="demo07_car_laser.urdf.xacro"/>
robot>

在 launch 文件中启动 gazebo 并添加机器人模型

launch文件:

<launch>
    
    <param name="robot_description" command="$(find xacro)/xacro $(find URDF_Gazebo)/urdf/xacro/car.urdf.xacro" />
    
    <include file="$(find gazebo_ros)/launch/empty_world.launch" />

    
    <node pkg="gazebo_ros" type="spawn_model" name="model" args="-urdf -model mycar -param robot_description"  />
launch>

实现效果:
【ROS入门】机器人系统仿真——URDF集成Gazebo_第2张图片

你可能感兴趣的:(ROS,机器人)