URDF 集成 Gazebo

launch文件

<launch>
    <!-- 1. 需要再参数服务器中载入 urdf -->
    <param name = "robot_description" textfile = "$(find urdf02_gazebo)/urdf/demo01_helloworld.urdf" />
    <!-- 2. 启动Gazebo仿真环境 roslaunch gazebo_ros empty_world.launch-->
    <include file = "$(find gazebo_ros)/launch/empty_world.launch" />
    <!-- 3. 在Gazebo中添加机器人模型 -->
    <!-- rosrun gazebo_ros spawn_model -urdf -model mycar -param robot_description , 这里mycar是robot name-->
    <node pkg = "gazebo_ros" type = "spawn_model" name = "spawn_model" args = "-urdf -model mycar -param robot_description"/>
</launch>

urdf文件 demo01_helloworld.urdf

<robot name = "mycar">
    <link name = "base_link">
        <!-- 可视化部分 -->
        <visual>
            <geometry>
                <box size = "0.5 0.3 0.1" />
            </geometry>
            <origin xyz = "0 0 0" rpy = "0 0 0"/>
        </visual>

        <!-- 1. 设置连杆的碰撞参数 -->
        <!-- 如果是标准几何体,直接复制 visual 中的 geometry 和origin 即可 -->
        <collision>
            <geometry>
                <box size = "0.5 0.3 0.1" />
            </geometry>
            <origin xyz = "0 0 0" rpy = "0 0 0"/>
        </collision>

        <!-- 2. 设置连杆的惯性矩阵 mass是质量-->
        <inertial>
           <origin xyz = "0 0 0" /> 
           <mass value = "2" />
           <inertia ixx = "1" ixy = "0" ixz = "0" iyy = "0" iyz = "1" izz = "1" />
        </inertial>
    </link>
    <!-- gazebo有自己的颜色设置标签 -->
    <gazebo reference = "base_link">
        <material>Gazebo/Red</material>
    </gazebo>
</robot>

roslaunch urdf02_gazebo demo01_helloworld.launch

实操

封装惯性矩阵算法的xacro文件 head.xacro

<robot name="base" xmlns:xacro="http://wiki.ros.org/xacro">
    <!-- Macro for inertia matrix -->
    <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>

launch文件 demo02_car.launch

<launch>
    <!-- 1. 需要再参数服务器中载入 urdf -->
    <param name = "robot_description" command = "$(find xacro)/xacro $(find urdf02_gazebo)/urdf/car.urdf.xacro" />
    <!-- 2. 启动Gazebo仿真环境 roslaunch gazebo_ros empty_world.launch-->
    <include file = "$(find gazebo_ros)/launch/empty_world.launch" />
    <!-- 3. 在Gazebo中添加机器人模型 -->
    <!-- rosrun gazebo_ros spawn_model -urdf -model mycar -param robot_description , 这里mycar是robot name-->
    <node pkg = "gazebo_ros" type = "spawn_model" name = "spawn_model" args = "-urdf -model mycar -param robot_description"/>
</launch>

集成xacro文件 car.urdf.xacro

<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
    <!-- 包惯性矩阵文件 -->
    <xacro:include filename = "head.xacro" />
    <!-- 包含底盘、摄像头、雷达的xacro文件 -->
    <xacro:include filename = "demo02_car_base.urdf.xacro" />
    <xacro:include filename = "demo03_car_camera.urdf.xacro" />
    <xacro:include filename = "demo04_car_laser.urdf.xacro" />
</robot>

底盘xacro文件 demo02_car_base.xarco

<robot name = "mycar" xmlns:xacro = "http://wiki.ros.org/xacro">
    
    <xacro:property name = "footprint_radius" value = "0.001" />
    <!-- 1. 添加 base_footprint -->
    <link name = "base_footprint">
        <visual>
            <geometry>
                <sphere radius = "${footprint_radius}" />
            </geometry>
        </visual>
    </link>

    <!-- 2. 添加底盘 -->
    <!-- 
        形状: 底盘为圆柱状,半径 0.1m,高 0.08m
        离地间距: 0.015m
    -->
    <xacro:property name = "base_radius" value = "0.1" />
    <xacro:property name = "base_length" value = "0.08" />
    <xacro:property name = "base_mass" value = "2" />
    <xacro:property name = "lidi_space" value = "0.015" />
    <xacro:property name = "base_joint_z" value = "${(base_length) / 2 + lidi_space}" />

    <!-- 2-1 link -->
    <link name = "base_link">
        <visual>
            <geometry>
                <cylinder radius = "${base_radius}" length = "${base_length}" />
            </geometry>
            <origin xyz = "0 0 0" rpy = "0 0 0"/>
        </visual>
        <collision>
            <geometry>
                <cylinder radius = "${base_radius}" length = "${base_length}" />
            </geometry>
            <origin xyz = "0 0 0" rpy = "0 0 0"/>
        </collision>
        <!-- 调用惯性矩阵函数 -->
        <xacro:cylinder_inertial_matrix m = "${base_mass}" r = "${base_radius}" h = "${base_length}"/>
    </link>
    <!-- 设置颜色 -->
    <gazebo reference = "base_link">
        <material>Gazebo/Yellow</material>
    </gazebo>


    <!-- 2-2 joint 半个车体的高度0.08m / 2 + 0.015m-->
    <joint name = "link2footprint" type = "fixed">
        <parent link = "base_footprint" />
        <child link = "base_link" />
        <!-- 关节z上的设置 = 车体高度/2 + 离地高度 -->
        <origin xyz = "0 0 ${base_joint_z}" rpy = "0 0 0" />
    </joint>

    <!-- 3. 添加驱动轮 -->
    <!-- 
        形状: 圆柱
        半径: 0.0325m
        长度: 0.015m
    -->

    <!-- 属性 -->
    <xacro:property name = "wheel_radius" value = "0.0325" />
    <xacro:property name = "wheel_length" value = "0.015" />
    <xacro:property name = "wheel_mass" value = "0.05" />
    <xacro:property name = "PI" value = "3.1415927" />
    <!-- 注意: 结果是负数  -->
    <xacro:property name = "wheel_joint_z" value = "${(base_length / 2 + lidi_space - wheel_radius) * (-1)}" />

    <!-- 
        wheel_name: left或right
        flag: 1 或 -1
     -->
    
    <!-- 宏定义 -->
    <xacro:macro name = "wheel_func" params = "wheel_name flag">
        <!-- 3-1 link -->
        <link name = "${wheel_name}_wheel">
            <visual>
                <geometry>
                    <cylinder radius = "${wheel_radius}" length = "${wheel_length}" />
                </geometry>
                <!-- 正常情况下圆柱面朝上,这里驱动轮在左右,需要修改欧拉角,沿x轴翻滚90度,约为1.57弧度 -->
                <origin xyz = "0 0 0" rpy = "${PI / 2} 0 0"/>
            </visual>
            <collision>
                <geometry>
                    <cylinder radius = "${wheel_radius}" length = "${wheel_length}" />
                </geometry>
                <origin xyz = "0 0 0" rpy = "${PI / 2} 0 0"/>
            </collision>
            <xacro:cylinder_inertial_matrix m = "${wheel_mass}" r = "${wheel_radius}" h = "${wheel_length}" />
        </link>
        <gazebo reference = "${wheel_name}_wheel">
            <material>Gazebo/Red</material>
        </gazebo>


        <!-- 3-2 joint 关节 -->
        <joint name = "${wheel_name}2link" type = "continuous">
            <parent link = "base_link" />
            <child link = "${wheel_name}_wheel" />
            <!-- 
                x: 0 无偏移
                y: 0.1 车体半径
                z: 0.04 + 0.015 - 0.0325 = 0.055 - 0.0325 = 0.0225,在下面为负数
                关节高度 = 车体高度 / 2 + 离地间距 - 车轮半径
                -->
            <origin xyz = "0 ${0.1 * flag} ${wheel_joint_z}" rpy = "0 0 0" />
            <!-- 转动时绕Y轴 -->
            <axis xyz = "0 1 0" />
        </joint>
    </xacro:macro>
    <!-- 调用才生效 -->
    <xacro:wheel_func wheel_name = "left" flag = "1" />
    <xacro:wheel_func wheel_name = "right" flag = "-1" />


    <!-- 4. 添加万向轮 -->
    <!-- 
        形状: 球
        半径: 0.0075m
     -->

    <!-- 属性 -->
    <xacro:property name = "small_wheel_radius"  value = "0.0075" />
    <xacro:property name = "small_wheel_mass"  value = "0.01" />
    <!-- 关节高度: 车体高度 / 2 + 离地间距 - 万向轮半径 = 0.04 + 0.015 - 0.0075 / 2 = 0.0475 -->
    <xacro:property name = "small_joint_z" value = "${(base_length / 2 + lidi_space - small_wheel_radius) * (-1)}" />

    <!-- 宏 -->
    <xacro:macro name = "small_wheel_func" params = "small_wheel_name flag" >
        <!-- 4-1 link -->
        <link name = "${small_wheel_name}_wheel">
            <visual>
                <geometry>
                    <sphere radius = "${small_wheel_radius}" />
                </geometry>
                <origin xyz = "0 0 0" rpy = "0 0 0"/>
            </visual>
            <collision>
                <geometry>
                    <sphere radius = "${small_wheel_radius}" />
                </geometry>
                <origin xyz = "0 0 0" rpy = "0 0 0"/>
            </collision>
            <xacro:sphere_inertial_matrix m = "${small_wheel_mass}" r = "${small_wheel_radius}" />
        </link>
        <gazebo reference = "${small_wheel_name}_wheel">
            <material>Gazebo/Red</material>
        </gazebo>
        
        <!-- 4-2 joint -->
        <joint name = "${small_wheel_name}2link" type = "continuous">
            <parent link = "base_link" />
            <child link = "${small_wheel_name}_wheel" />
            <!-- 
                x: 0.08 小于半径
                y: 0
                z: 0.04 + 0.015 - 0.0075 = 0.0475
            -->
            <origin xyz = "${0.08 * flag} 0 ${small_joint_z}" rpy = "0 0 0" />
            <!-- 转动时绕Y轴 -->
            <axis xyz = "0 1 0" />
        </joint>
    </xacro:macro >
    <!-- 调用 -->
    <xacro:small_wheel_func small_wheel_name = "front" flag = "1" />    
    <xacro:small_wheel_func small_wheel_name = "back" flag = "-1" />    
</robot>

摄像xacro文件dem03_car_camera.xacro

<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
    <!-- 摄像头部件 -->
    <!-- 1. 参数 -->
    <!-- 
        参数:
            连杆属性: 厚度(x方向)、宽度(y方向)、高度(z方向)
            关节属性: x y z 偏移量,z偏移量 = 底盘高度的一半+摄像头高度的一半
     -->
     <xacro:property name = "camera_length" value = "0.02" /> <!-- 厚度(x)-->
     <xacro:property name = "camera_width" value = "0.05" /> <!-- 宽度(y)-->
     <xacro:property name = "camera_height" value = "0.05" /> <!-- 高度(z)-->
     <xacro:property name = "camera_mass" value = "0.01" /> <!-- 质量(m)-->
     <xacro:property name = "joint_camera_x" value = "0.08" /> <!-- 关节(x)-->
     <xacro:property name = "joint_camera_y" value = "0" /> <!-- 关节(y)-->
     <xacro:property name = "joint_camera_z" value = "${base_length / 2 + camera_height / 2}" /> <!-- 关节(z)-->

    <!-- 2. 设计连杆关节 -->
    <!-- 2-1 link -->
    <link name = "camera">
        <visual>
            <geometry>
                <box size = "${camera_length} ${camera_width} ${camera_height}" />
            </geometry>
        </visual>
        <collision>
            <geometry>
                <box size = "${camera_length} ${camera_width} ${camera_height}" />
            </geometry>
        </collision>
        <xacro:Box_inertial_matrix m = "${camera_mass}" l = "${camera_length}" w = "${camera_width}" h = "${camera_height}" />
    </link>
    <gazebo reference = "camera">
        <material>Gazebo/Blue</material>
    </gazebo>

    <!-- 2-2 关节 -->
    <joint name = "camera2base" type = "fixed">
        <parent link = "base_link" />
        <child link = "camera" />
        <origin xyz = "${joint_camera_x} ${joint_camera_y} ${joint_camera_z}" rpy = "0 0 0" />
    </joint>
</robot>

雷达xacro文件demo04_car_laser.xacro

<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
    <!-- 雷达部件 -->
    <!-- 参数 -->
    <!-- 
        1. 支架
            支架尺寸: 半径 高度
            关节偏移量: x y z
        2. 雷达
            支架尺寸: 半径 高度
            关节偏移量: x y z
    -->
    <xacro:property name = "support_radius" value = "0.01" />
    <xacro:property name = "support_length" value = "0.15" />
    <xacro:property name = "support_mass" value = "0.1" />

    <xacro:property name = "laser_radius" value = "0.03" />
    <xacro:property name = "laser_length" value = "0.05" />
    <xacro:property name = "laser_mass" value = "0.15" />

    <xacro:property name = "joint_support_x" value = "0" />
    <xacro:property name = "joint_support_y" value = "0" />
    <!-- joint1 z = 车体高度 / 2 + 支架高度 / 2 -->
    <xacro:property name = "joint_support_z" value = "${base_length / 2 + support_length / 2}" />

    <xacro:property name = "joint_laser_x" value = "0" />
    <xacro:property name = "joint_laser_y" value = "0" />
    <!-- joint2 z = 支架高度 / 2 + 雷达高度 / 2 -->
    <xacro:property name = "joint_laser_z" value = "${support_length / 2 + laser_length / 2}" />

    <!-- 1. 支架 -->
    <link name = "support">
        <visual>
            <geometry>
                <cylinder radius = "${support_radius}" length = "${support_length}" />
            </geometry>
        </visual>
        <collision>
            <geometry>
                <cylinder radius = "${support_radius}" length = "${support_length}" />
            </geometry>
        </collision>
        <xacro:cylinder_inertial_matrix m = "${support_mass}" r = "${support_radius}" h = "${support_length}" />
    </link>
    <gazebo reference = "support">
        <material>Gazebo/Grey</material>
    </gazebo>
    <joint name = "support2base" type = "fixed"> 
        <parent link = "base_link" />
        <child link = "support" />
        <origin xyz = "${joint_support_x} ${joint_support_y} ${joint_support_z}" rpy = "0 0 0"/>
    </joint>

    <!-- 2. 雷达 固定-->
    <link name = "laser">
        <visual>
            <geometry>
                <cylinder radius = "${laser_radius}" length = "${laser_length}" />
            </geometry>
        </visual>
        <collision>
            <geometry>
                <cylinder radius = "${laser_radius}" length = "${laser_length}" />
            </geometry>
        </collision>
        <xacro:cylinder_inertial_matrix m = "${laser_mass}" r = "${laser_radius}" h = "${laser_length}" />
    </link>
    <gazebo reference = "laser">
        <material>Gazebo/Black</material>
    </gazebo>
    <joint name = "laser2support" type = "fixed"> 
        <parent link = "support" />
        <child link = "laser" />
        <origin xyz = "${joint_laser_x} ${joint_laser_y} ${joint_laser_z}" rpy = "0 0 0"/>
    </joint>
</robot>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

2021 Nqq

你的鼓励是我学习的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值