0基础在ROS系统中实现RRT算法(四)URDF集成gazebo并搭建gazebo仿真环境

小白一枚,毕设突发奇想加入了ROS的内容,不知道自己还能不能毕业。以下均为通过看视频,翻博客等整理而成的笔记,并非我的原创。可能会出现一些报错的修改或者简单的代码是我自己做的。哈哈。

Gazebo是一款3D动态模拟器,用于显示机器人模型并创建仿真环境,能够在复杂的室内和室外环境中准确有效地模拟机器人。与游戏引擎提供高保真度的视觉模拟类似,Gazebo提供高保真度的物理模拟,其提供一整套传感器模型,以及对用户和程序非常友好的交互方式。

这篇主要学习:

  • URDF 与 Gazebo 的基本集成流程;
  • 如果要在 Gazebo 中显示机器人模型,URDF 需要做的一些额外配置;
  • 关于Gazebo仿真环境的搭建。比如建立房子、马路等。

如果 gazebo没有安装,请自行安装:

1.添加源:

sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" 
>
 /etc/apt/sources.list.d/gazebo-stable.list'
wget http://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -

2.安装:

sudo apt update
sudo apt install gazebo11 
sudo apt install libgazebo11-dev

一、URDF与Gazebo基本集成流程与相关设置

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

  1. 创建功能包,导入依赖项

  2. 编写 URDF 或 Xacro 文件

  3. 启动 Gazebo 并显示机器人模型

接下来我们以创建盒状机器人显示在gazebo中为例,学习一下URDF集成gazebo。

1.创建功能包

在工作空间的src文件夹下打开终端进行以下操作,新建功能包urdf02_gazebo,并且导入依赖包: urdf、xacro、gazebo_ros、gazebo_ros_control、gazebo_plugins

catkin_create_pkg urdf02_gazebo urdf xacro gazebo_ros gazebo_ros_control gazebo_plugins

2.编写URDF文件

在urdf02_gazebo文件夹下新建urdf文件夹,在此文件夹下新建demo01_helloworld.urdf:

 接下来编写urdf文件,完整的文件内容:

<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/Black</material>
    </gazebo>

</robot>

接下来对上面的文件内容进行解释:

机器人的可视化部分visual:

盒装机器人,所以几何形状geometry选择为box,它在xyz上的尺寸分别是:0.5 ,0.2,0.1

颜色color为黄色,rgba为0.5 0.3 0,透明度为1

        <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:碰撞参数用于检测机器人是否与外界环境发生碰撞。

如果是标准几何体,直接复制visual的geometry和origin即可。

        <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用于设置重心偏移量,如果连杆质量均匀,设置为0 0 0。

mass质量设置为 6 kg

inertial是在不同维度上的坐标值,实际应用时要根据情况进行计算来防止机器人打滑和翻车等,在这里我们先随意设置。

    
        <inertial>
            <origin xyz="0 0 0" />
            <mass value="6" />
            <inertia ixx="1" ixy="0" ixz="0" iyy="1" iyz="0" izz="1" />
        </inertial>

gazebo自己的颜色设置:

    <gazebo reference="base_link">
        <material>Gazebo/Black</material>
    </gazebo>

3.启动Gazebo并显示模型

在/DEMO05_WS/src/urdf02_gazebo目录下新建launch文件夹,在launch文件夹下新建demo01_helloworld.launch

我们要:

  • 将 Urdf 文件的内容加载到参数服务器:调用param标签,name="robot_description",textfile定位到 urdf02_gazebo/urdf/helloworld.urdf
  • 启动 gazebo:gazebo已经内置了launch文件empty_world.launch,是一个空的世界环境,我们在这里包含这个launch文件。
  • 在 gazebo 中显示机器人模型:    在 Gazebo 中加载一个机器人模型,该功能由 gazebo_ros 下的 spawn_model 提供:
        -urdf 加载的是 urdf 文件
        -model car 模型名称是 car
        -param robot_description 从参数 robot_description 中载入模型

launch文件内容:

<launch>

    <param name="robot_description" textfile="$(find urdf02_gazebo)/urdf/helloworld.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>

运行一下launch文件:

milk@milk:~/DEMO05_WS$ roslaunch urdf02_gazebo demo01_helloworld.launch

可以看到gazebo中显示的盒装机器人

 (不知道为什么我第一次不显示机器人模型,等我放弃了关机之后,再开机启动launch文件就显示出来了……)

二、URDF集成Gazebo实操

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

结果演示:

实现流程:

  1. 需要编写封装惯性矩阵算法的 xacro 文件:我们的车轮、雷达等都要涉及到惯性矩阵的计算。

  2. 为机器人模型中的每一个 link 添加 collision 和 inertial 标签,并且重置颜色属性

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

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

在/DEMO05_WS/src/urdf02_gazebo/urdf目录下新建用于封装惯性矩阵算法的文件: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>

 接下来把之前编写过的小车底盘、雷达、摄像头、汇总文件的xacro文件复制过来:

 并且在car.urdf.xacro中包含惯性矩阵文件:<xacro:include filename="head.xacro" />

<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
    
        <xacro:include filename="head.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文件:在/DEMO05_WS/src/urdf02_gazebo/launch目录下新建car.launch

<launch>

    <param name="robot_description" command="$(find xacro)/xacro $(find urdf02_gazebo)/urdf/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>

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

在demo05_car_base.urdf.xacro中

设置底盘相关参数:

连杆:找到连杆相关代码

    <link name="base_link">
      <visual>
        <geometry>
          <cylinder radius="${base_link_radius}" length="${base_link_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>
    </link>

添加collision:只要是规则的立方体,collision都和visual中的geometry和origin一样,直接复制。

添加interial:我们前面已经把interial封装成专门的宏。打开head.xacro,由于底盘是圆柱体,我们找到圆柱体的interial宏:cylinder_inertial_matrix

添加颜色

修改完成后的小车主体部分代码如下:


    <link name="base_link">
      <visual>
        <geometry>
          <cylinder radius="${base_link_radius}" length="${base_link_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_link_length}" />
        </geometry>
        <origin xyz="0 0 0" rpy="0 0 0" />
      </collision>
  
      <xacro:cylinder_inertial_matrix   m= "${base_link_mass}"  r= "${base_link_radius}"  h = "${base_link_length}"   />
    </link>
    
    <gazebo reference = "base_link">
      <material>Gazebo/Yellow</material>
    </gazebo>

保存之后,执行一下代码看一下gazebo中的机器人模型:

milk@milk:~/DEMO05_WS$ source ./devel/setup.bash
milk@milk:~/DEMO05_WS$ roslaunch urdf02_gazebo demo02_car.launch

 驱动轮:

   <!-- qudonglun -->
    <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: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>
        <collosion>
            <geometry>
            <cylinder radius="${wheel_radius}" length="${wheel_length}" />
          </geometry>
          <origin xyz="0.0 0.0 0.0" rpy="${PI / 2} 0.0 0.0" />      
        </collosion>
         <xacro:cylinder_inertial_matrix   m= "${wheel_mass}"  r= "${wheel_radius}"  h = "${wheel_length}"   />
      </link>
      <gazebo reference = "${name}_wheel">
        <material>Gazebo/Red</material>
      </gazebo>

支撑轮

      <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_mass}"    r = "${support_wheel_radius}"  />
      </link>

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

demo05_car_base.urdf.xacro整体代码:

<robot name="my_base" 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" /> <!-- base_footprint  -->
    <xacro:property name="base_link_radius" value="0.1" /> <!-- base_link  -->
    <xacro:property name="base_link_length" value="0.08" /> <!-- base_link  -->
        <xacro:property name="base_link_mass" value="2" /> <!-- base_link  -->
    <xacro:property name="earth_space" value="0.015" /> <!-- lidijianju -->

    <!-- dipan -->
    <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_link_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_link_length}" />
        </geometry>
        <origin xyz="0 0 0" rpy="0 0 0" />
      </collision>
  
      <xacro:cylinder_inertial_matrix   m= "${base_link_mass}"  r= "${base_link_radius}"  h = "${base_link_length}"   />
    </link>

    <gazebo reference = "base_link">
      <material>Gazebo/Yellow</material>
    </gazebo>

    <joint name="base_link2base_footprint" type="fixed">
      <parent link="base_footprint" />
      <child link="base_link" />
      <origin xyz="0 0 ${earth_space + base_link_length / 2 }" />
    </joint>

    <!-- qudonglun -->
    <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: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>
        <collosion>
            <geometry>
            <cylinder radius="${wheel_radius}" length="${wheel_length}" />
          </geometry>
          <origin xyz="0.0 0.0 0.0" rpy="${PI / 2} 0.0 0.0" />      
        </collosion>
         <xacro:cylinder_inertial_matrix   m= "${wheel_mass}"  r= "${wheel_radius}"  h = "${wheel_length}"   />
      </link>
      <gazebo reference = "${name}_wheel">
        <material>Gazebo/Red</material>
      </gazebo>

      <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_link_length / 2 - wheel_radius) }" />
        <axis xyz="0 1 0" />
      </joint>
    </xacro:macro>
    <xacro:add_wheels name="left" flag="1" />
    <xacro:add_wheels name="right" flag="-1" />
    <!-- zhichenglun -->
    <xacro:property name="support_wheel_radius" value="0.0075" />
    <xacro:property name="support_wheel_mass" value="0.0100" />
    <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_mass}"    r = "${support_wheel_radius}"  />
      </link>

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

      <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_link_length / 2 + earth_space / 2)}" />
          <axis xyz="1 1 1" />
      </joint>
    </xacro:macro>

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

</robot>

 gazebo:

 设置摄像头相关参数:demo06_car_camera.urdf.xacro

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

<xacro:property  name = "camera_length"   value =  "0.02" /> 
<xacro:property  name = "camera_width"   value =  "0.05" /> 
<xacro:property  name = "camera_height"   value =  "0.05" /> 
<xacro:property  name = "camera_mass"   value =  "0.01" /> 
<xacro:property  name = "joint_camera_x"   value =  "0.08" />
<xacro:property  name = "joint_camera_y"   value =  "0" />
<xacro:property  name = "joint_camera_z"   value =  "${base_link_length / 2 + camera_height  / 2}" />
    <link   name = "camera">
            <visual>
                    <geometry>
                             <box  size = "${camera_length}  ${ camera_width}  ${camera_height}"  />
                    </geometry>

                    <material    name = "black ">
                                <color  rgba = "0  0  0  0.8"  />
                    </material>
            </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>

    <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>

设置雷达相关参数:demo07_car_laser.urdf.xacro

<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
	<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.02"  />

	<xacro:property name = "joint_support_x"  value = "0"  />
	<xacro:property name = "joint_support_y"  value = "0"  />
	<xacro:property name = "joint_support_z"  value = "${base_link_length / 2 +support_length / 2}"  />

	<xacro:property name = "joint_laser_x"  value = "0"  />
	<xacro:property name = "joint_laser_y"  value = "0"  />
	<xacro:property name = "joint_laser_z"  value = "${support_length / 2 + laser_length / 2}"  />

	<link name ="support">
		<visual>
			<geometry>
				<cylinder  radius = "${support_radius}"	length = "${support_length}"/>
			</geometry>
			<material name = "yellow">
				<color   rgba = "0.8 0.5 0.0  0.5" />
			</material>
		</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/Gray</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>

	<link name ="laser">
		<visual>
			<geometry>
				<cylinder  radius = "${laser_radius}"	length = "${laser_length}"/>
			</geometry>
			<material name = "black">
				<color   rgba = "0.0 0.0 0.0 0.5" />
			</material>
		</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>

gazebo:

三、Gazebo仿真环境搭建

到目前为止,我们已经可以将机器人模型显示在 Gazebo 之中了,但是当前默认情况下,在 Gazebo 中机器人模型是在 empty world 中,并没有类似于房间、家具、道路、树木... 之类的仿真物,如何在 Gazebo 中创建仿真环境呢?

Gazebo 中创建仿真实现方式有两种:

  • 方式1: 直接添加内置组件创建仿真环境

  • 方式2: 手动绘制仿真环境(更为灵活)

也还可以直接下载使用官方或第三方提高的仿真环境插件。

1.创建仿真环境

首先打开新终端,启动roscore

$roscore

然后启动gazebo

$rosrun gazebo_ros gazebo

 点击上边栏的立方体添加

 2.自定义仿真环境

启动 gazebo ,打开构建面板,绘制仿真环境

 

保存构建的环境

点击: 左上角 file ---> Save (保存路径功能包下的: models)

然后 file ---> Exit Building Editor

 保存为 world 文件

可以像方式1一样再添加一些插件,然后保存为 world 文件(保存路径功能包下的: worlds)

3.使用官方提供的插件

 还可以下载官方提供的插件,下载地址:

https://github.com/osrf/gazebo_models

解压后的文件中有大量的模型:

 这些模型占用空间太大了,我下载后只留下了几个自己需要的模型。

四、启动gazebo并控制机器人运动

1.编写一个单独的 xacro 文件,为机器人模型添加传动装置以及控制器

1.1 为 joint 添加传动装置以及控制器

两轮差速配置

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

    <!-- 传动实现:用于连接控制器与关节 -->
    <xacro:macro name="joint_trans" params="joint_name">
        <!-- Transmission is important to link the joints and the controller -->
        <transmission name="${joint_name}_trans">
            <type>transmission_interface/SimpleTransmission</type>
            <joint name="${joint_name}">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
            </joint>
            <actuator name="${joint_name}_motor">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
                <mechanicalReduction>1</mechanicalReduction>
            </actuator>
        </transmission>
    </xacro:macro>

    <!-- 每一个驱动轮都需要配置传动装置 -->
    <xacro:joint_trans joint_name="left_wheel2base_link" />
    <xacro:joint_trans joint_name="right_wheel2base_link" />

    <!-- 控制器 -->
    <gazebo>
        <plugin name="differential_drive_controller" filename="libgazebo_ros_diff_drive.so">
            <rosDebugLevel>Debug</rosDebugLevel>
            <publishWheelTF>true</publishWheelTF>
            <robotNamespace>/</robotNamespace>
            <publishTf>1</publishTf>
            <publishWheelJointState>true</publishWheelJointState>
            <alwaysOn>true</alwaysOn>
            <updateRate>100.0</updateRate>
            <legacyMode>true</legacyMode>
            <leftJoint>left_wheel2base_link</leftJoint> <!-- 左轮 -->
            <rightJoint>right_wheel2base_link</rightJoint> <!-- 右轮 -->
            <wheelSeparation>${base_link_radius * 2}</wheelSeparation> <!-- 车轮间距 -->
            <wheelDiameter>${wheel_radius * 2}</wheelDiameter> <!-- 车轮直径 -->
            <broadcastTF>1</broadcastTF>
            <wheelTorque>30</wheelTorque>
            <wheelAcceleration>1.8</wheelAcceleration>
            <commandTopic>cmd_vel</commandTopic> <!-- 运动控制话题 -->
            <odometryFrame>odom</odometryFrame> 
            <odometryTopic>odom</odometryTopic> <!-- 里程计话题 -->
            <robotBaseFrame>base_footprint</robotBaseFrame> <!-- 根坐标系 -->
        </plugin>
    </gazebo>

</robot>

1.2 xacro文件集成

最后还需要将上述 xacro 文件集成进总的机器人模型文件,代码示例如下:

<!-- 组合小车底盘与摄像头 -->
<robot name="my_car_camera" xmlns:xacro="http://wiki.ros.org/xacro">
    <xacro:include filename="my_head.urdf.xacro" />
    <xacro:include filename="my_base.urdf.xacro" />
    <xacro:include filename="my_camera.urdf.xacro" />
    <xacro:include filename="my_laser.urdf.xacro" />
    <xacro:include filename="move.urdf.xacro" />
</robot>

2.编写launch文件:demo03_env.launch

<launch>
    <!--1.需要在参数服务器中载入 urdf -->
    <param name="robot_description" command="$(find xacro)/xacro $(find urdf02_gazebo)/urdf/car.urdf.xacro" />
    <!--2.启动 Gazebo 仿真环境 -->
    <include file="$(find gazebo_ros)/launch/empty_world.launch" >
        <arg name="world_name" value="$(find urdf02_gazebo)/worlds/my_world/model.world" />
    </include>
    <!--3.在 Gazebo 中添加机器人模型 -->
    <node pkg="gazebo_ros" type="spawn_model" name="spawn_model" args="-urdf -model car -param robot_description" />
</launch>

3.启动 launch 文件

$ source ./devel/setup.bash
$ roslaunch urdf02_gazebo demo03_env.launch

使用 topic list 查看话题列表:

$ rostopic list

会发现多了 /cmd_vel 然后发布 vmd_vel 消息控制即可

使用命令控制(或者可以编写单独的节点控制)

rostopic pub -r 10 /cmd_vel geometry_msgs/Twist '{linear: {x: 0.2, y: 0, z: 0}, angular: {x: 0, y: 0, z: 0.5}}'

 机器人开始做圆周运动。

五、rviz查看里程计信息

1.启动 Rviz

launch文件:demo04_sensor.launch

<launch>
    <!-- 启动 rviz -->
    <node pkg="rviz" type="rviz" name="rviz" />

    <!-- 关节以及机器人状态发布节点 -->
    <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />
    <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

</launch>

$ roslaunch urdf02_gazebo demo04_sensor.launch

 2.添加组件

添加robotmodel和odometry组件

 

  • 4
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值