文章目录


前言

想在ROS系统中对我们自定义的机器人进行仿真,需要使用Gazebo,其是一种适用于复杂室内环境和室外环境的仿真环境。

一、gazebo启动

gazebo独立于ROS,并可以在Ubuntu中以独立功能包安装。我的ROS系统(melodic)是完整安装包,所以包含了gazebo,版本为9.0.0,在此省略了安装步骤。
查询版本命令如下:

dpkg -l | grep gazebo

可以使用如下命令启动gazebo,确认是否安装:

gazebo

结果遇到如下错误:

解决方法,使用如下命令打开文件config.yaml:

sudo gedit ~/.ignition/fuel/config.yaml

添加如下一行:

url: https://api.ignitionrobotics.org

可以正常启动gazebo了。

二、创建编译功能包

切换到catkin_ws/src目录下,如下:

catkin_create_pkg mbot_sim_gazebo urdf xacro

切换到catkin_ws目录下,编译该功能包,如下:

catkin_make mbot_sim_gazebo

在该功能包下创建include、src、launch、urdf文件夹,在urdf文件下创建urdf文件夹和xacro文件夹。

三、更新xacro文件

1、更新robot_base.xacro

robot_base.xacro文件内容如下:

<?xml version="1.0"?>
<robot name="mbot" xmlns:xacro="http://www.ros.org/wiki/xacro">

    <!-- PROPERTY LIST -->
    <xacro:property name="M_PI" value="3.1415926"/>
    <xacro:property name="base_mass"   value="20" /> 
    <xacro:property name="base_radius" value="0.20"/>
    <xacro:property name="base_length" value="0.16"/>

    <xacro:property name="wheel_mass"   value="2" />
    <xacro:property name="wheel_radius" value="0.06"/>
    <xacro:property name="wheel_length" value="0.025"/>
    <xacro:property name="wheel_joint_y" value="0.19"/>
    <xacro:property name="wheel_joint_z" value="0.05"/>

    <xacro:property name="caster_mass"    value="0.5" /> 
    <xacro:property name="caster_radius"  value="0.015"/> <!-- wheel_radius - ( base_length/2 - wheel_joint_z) -->
    <xacro:property name="caster_joint_x" value="0.18"/>

    <!-- Defining the colors used in this robot -->
    <material name="yellow">
        <color rgba="1 0.4 0 1"/>
    </material>
    <material name="black">
        <color rgba="0 0 0 0.95"/>
    </material>
    <material name="gray">
        <color rgba="0.75 0.75 0.75 1"/>
    </material>
    
    <!-- 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>

    <!-- Macro for robot wheel -->
    <xacro:macro name="wheel" params="prefix reflect">
        <joint name="${prefix}_wheel_joint" type="continuous">
            <origin xyz="0 ${reflect*wheel_joint_y} ${-wheel_joint_z}" rpy="0 0 0"/>
            <parent link="base_link"/>
            <child link="${prefix}_wheel_link"/>
            <axis xyz="0 1 0"/>
        </joint>

        <link name="${prefix}_wheel_link">
            <visual>
                <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
                <geometry>
                    <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
                </geometry>
                <material name="gray" />
            </visual>
            <collision>
                <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
                <geometry>
                    <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
                </geometry>
            </collision>
            <cylinder_inertial_matrix  m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
        </link>

        <gazebo reference="${prefix}_wheel_link">
            <material>Gazebo/Gray</material>
        </gazebo>

        <!-- Transmission is important to link the joints and the controller-->
        <transmission name="${prefix}_wheel_joint_trans">
            <type>transmission_interface/SimpleTransmission</type>
            <joint name="${prefix}_wheel_joint" >
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
            </joint>
            <actuator name="${prefix}_wheel_joint_motor">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
                <mechanicalReduction>1</mechanicalReduction>
            </actuator>
        </transmission>
    </xacro:macro>

    <!-- Macro for robot caster -->
    <xacro:macro name="caster" params="prefix reflect">
        <joint name="${prefix}_caster_joint" type="continuous">
            <origin xyz="${reflect*caster_joint_x} 0 ${-(base_length/2 + caster_radius)}" rpy="0 0 0"/>
            <parent link="base_link"/>
            <child link="${prefix}_caster_link"/>
            <axis xyz="0 1 0"/>
        </joint>

        <link name="${prefix}_caster_link">
            <visual>
                <origin xyz="0 0 0" rpy="0 0 0"/>
                <geometry>
                    <sphere radius="${caster_radius}" />
                </geometry>
                <material name="black" />
            </visual>
            <collision>
                <origin xyz="0 0 0" rpy="0 0 0"/>
                <geometry>
                    <sphere radius="${caster_radius}" />
                </geometry>
            </collision>      
            <sphere_inertial_matrix  m="${caster_mass}" r="${caster_radius}" />
        </link>

        <gazebo reference="${prefix}_caster_link">
            <material>Gazebo/Black</material>
        </gazebo>
    </xacro:macro>

    <xacro:macro name="mbot_base_gazebo">
        <link name="base_footprint">
            <visual>
                <origin xyz="0 0 0" rpy="0 0 0" />
                <geometry>
                    <box size="0.001 0.001 0.001" />
                </geometry>
            </visual>
        </link>
        <gazebo reference="base_footprint">
            <turnGravityOff>false</turnGravityOff>
        </gazebo>

        <joint name="base_footprint_joint" type="fixed">
            <origin xyz="0 0 ${base_length/2 + caster_radius*2}" rpy="0 0 0" />        
            <parent link="base_footprint"/>
            <child link="base_link" />
        </joint>

        <link name="base_link">
            <visual>
                <origin xyz=" 0 0 0" rpy="0 0 0" />
                <geometry>
                    <cylinder length="${base_length}" radius="${base_radius}"/>
                </geometry>
                <material name="yellow" />
            </visual>
            <collision>
                <origin xyz=" 0 0 0" rpy="0 0 0" />
                <geometry>
                    <cylinder length="${base_length}" radius="${base_radius}"/>
                </geometry>
            </collision>   
            <cylinder_inertial_matrix  m="${base_mass}" r="${base_radius}" h="${base_length}" />
        </link>

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

        <wheel prefix="left"  reflect="-1"/>
        <wheel prefix="right" reflect="1"/>

        <caster prefix="front" reflect="-1"/>
        <caster prefix="back"  reflect="1"/>

        <!-- controller -->
        <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_wheel_joint</leftJoint>
                <rightJoint>right_wheel_joint</rightJoint>
                <wheelSeparation>${wheel_joint_y*2}</wheelSeparation>
                <wheelDiameter>${2*wheel_radius}</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> 
    </xacro:macro>

</robot>

2、更新robot_camera.xacro

上一篇我们只添加了摄像头外观,在这里我们添加摄像头属性,更新robot_camera.xacro文件内容如下:

<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="camera">

    <xacro:macro name="usb_camera" params="prefix:=camera">
        <!-- Create laser reference frame -->
        <link name="${prefix}_link">
            <inertial>
                <mass value="0.1" />
                <origin xyz="0 0 0" />
                <inertia ixx="0.01" ixy="0.0" ixz="0.0"
                         iyy="0.01" iyz="0.0"
                         izz="0.01" />
            </inertial>

            <visual>
                <origin xyz=" 0 0 0 " rpy="0 0 0" />
                <geometry>
                    <box size="0.01 0.04 0.04" />
                </geometry>
                <material name="black"/>
            </visual>

            <collision>
                <origin xyz="0.0 0.0 0.0" rpy="0 0 0" />
                <geometry>
                    <box size="0.01 0.04 0.04" />
                </geometry>
            </collision>
        </link>
        <gazebo reference="${prefix}_link">
            <material>Gazebo/Black</material>
        </gazebo>

        <gazebo reference="${prefix}_link">
            <sensor type="camera" name="camera_node">
                <update_rate>30.0</update_rate>
                <camera name="head">
                    <horizontal_fov>1.3962634</horizontal_fov>
                    <image>
                        <width>1280</width>
                        <height>720</height>
                        <format>R8G8B8</format>
                    </image>
                    <clip>
                        <near>0.02</near>
                        <far>300</far>
                    </clip>
                    <noise>
                        <type>gaussian</type>
                        <mean>0.0</mean>
                        <stddev>0.007</stddev>
                    </noise>
                </camera>
                <plugin name="gazebo_camera" filename="libgazebo_ros_camera.so">
                    <alwaysOn>true</alwaysOn>
                    <updateRate>0.0</updateRate>
                    <cameraName>/camera</cameraName>
                    <imageTopicName>image_raw</imageTopicName>
                    <cameraInfoTopicName>camera_info</cameraInfoTopicName>
                    <frameName>camera_link</frameName>
                    <hackBaseline>0.07</hackBaseline>
                    <distortionK1>0.0</distortionK1>
                    <distortionK2>0.0</distortionK2>
                    <distortionK3>0.0</distortionK3>
                    <distortionT1>0.0</distortionT1>
                    <distortionT2>0.0</distortionT2>
                </plugin>
            </sensor>
        </gazebo>

    </xacro:macro>
</robot>

3、更新robot_lidar.xacro

通过plugin标签,在gazebo中插入激光雷达的相关插件,更新robot_lidar.xacro文件内容如下:

<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="lidar">
	<xacro:macro name="hokuyo_lidar" params="prefix:=lidar">
			<link name="hokuyo_link">
	    		<collision>
	      			<origin xyz="0 0 0" rpy="0 0 0"/>
	      			<geometry>
						<box size="0.1 0.1 0.1"/>
	     			</geometry>
	    		</collision>
	    		<visual>
	      			<origin xyz="0 0 0" rpy="0 0 0"/>
	      			<geometry>
	        			<mesh filename="package://mbot_sim_gazebo/meshes/hokuyo.dae"/>
	      			</geometry>
	    		</visual>
	    		<inertial>
	      			<mass value="1e-5" />
	      			<origin xyz="0 0 0" rpy="0 0 0"/>
	      			<inertia ixx="1e-6" ixy="0" ixz="0" iyy="1e-6" iyz="0" izz="1e-6" />
	    		</inertial>
	  		</link>
			<gazebo reference="lidar_link">
			    <sensor type="ray" name="head_hokuyo_sensor">
			      <pose>0 0 0 0 0 0</pose>
			      <visualize>false</visualize>
			      <update_rate>40</update_rate>
			      <ray>
			        <scan>
			          <horizontal>
			            <samples>720</samples>
			            <resolution>1</resolution>
			            <min_angle>-1.570796</min_angle>
			            <max_angle>1.570796</max_angle>
			          </horizontal>
			        </scan>
			        <range>
			          <min>0.10</min>
			          <max>30.0</max>
			          <resolution>0.01</resolution>
			        </range>
			        <noise>
			          <type>gaussian</type>
			          <mean>0.0</mean>
			          <stddev>0.01</stddev>
			        </noise>
			      </ray>
			      <plugin name="gazebo_ros_head_hokuyo_controller" filename="libgazebo_ros_laser.so">
			        <topicName>/scan</topicName>
			        <frameName>hokuyo_link</frameName>
			      </plugin>
			    </sensor>
			  </gazebo>
	</xacro:macro>
</robot>

4、更新robot.xacro

由于机器人主体宏定义的名字改了,因此robot.xacro更新如下:

<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">

    <xacro:include filename="$(find mbot_sim_gazebo)/urdf/xacro/robot_base.xacro" />
    <xacro:include filename="$(find mbot_sim_gazebo)/urdf/xacro/robot_camera.xacro" />
    <xacro:include filename="$(find mbot_sim_gazebo)/urdf/xacro/robot_lidar.xacro" />

    <xacro:property name="camera_offset_x" value="0.17" />
    <xacro:property name="camera_offset_y" value="0" />
    <xacro:property name="camera_offset_z" value="0.10" />
    <xacro:property name="lidar_offset_x" value="-0.17" />
    <xacro:property name="lidar_offset_y" value="0" />
    <xacro:property name="lidar_offset_z" value="0.10" />

    <xacro:mbot_base_gazebo/>

    <!-- Camera -->
    <joint name="camera_joint" type="fixed">
        <origin xyz="${camera_offset_x} ${camera_offset_y} ${camera_offset_z}" rpy="0 0 0" />
        <parent link="base_link"/>
        <child link="camera_link"/>
    </joint>

    <xacro:usb_camera prefix="camera"/>

    <joint name="lidar_joint" type="fixed">
        <origin xyz="${lidar_offset_x} ${lidar_offset_y} ${lidar_offset_z}" rpy="0 0 0" />
        <parent link="base_link"/>
        <child link="lidar_link"/>
    </joint>
    <xacro:hokuyo_lidar prefix="lidar"/>

</robot>

四、更新launch文件

launch文件需要加入gazebo的设置,并将rviz相关设置去掉:

<launch>
        <!-- 设置launch文件的参数 -->
    <arg name="paused" default="false"/>
    <arg name="use_sim_time" default="true"/>
    <arg name="gui" default="true"/>
    <arg name="headless" default="false"/>
    <arg name="debug" default="false"/>

    <!-- 运行gazebo仿真环境 -->
    <include file="$(find gazebo_ros)/launch/empty_world.launch">
        <arg name="debug" value="$(arg debug)" />
        <arg name="gui" value="$(arg gui)" />
        <arg name="paused" value="$(arg paused)"/>
        <arg name="use_sim_time" value="$(arg use_sim_time)"/>
        <arg name="headless" value="$(arg headless)"/>
    </include>

    
    <!--机器人参数设置-->
    <arg name="model" default="$(find xacro)/xacro --inorder '$(find mbot_sim_gazebo)/urdf/xacro/robot.xacro'" />
     
    <!--robot_description 参数名称是参数服务器预先设定好的,不能改变-->
    <param name="robot_description" command="$(arg model)" />


    <!-- 运行joint_state_publisher节点,发布机器人的关节状态  -->
    <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />

    <!-- 运行robot_state_publisher节点,发布tf  -->
    <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

      <!-- 在gazebo中加载机器人模型-->
    <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
          args="-urdf -model mrobot -param robot_description"/> 

</launch>

四、仿真效果

运行如下命令,仿真效果如下:

roslaunch mbot_sim_gazebo robot.launch

可以通过rqt_image_view查看图像:

rqt_image_view

此时,摄像头显示内容是灰色的,这是因为环境为空的缘故。

五、添加地图

首先尝试加载柳树库公司(Willow Garage)办公室的地图,这张地图在ROS中是默认安装的,这个3D模型保存在/usr/share/gazebo-9/worlds中。我们使用如下命令加载:

roslaunch gazebo_ros willowgarage_world.launch

发现界面一直卡在这里,效果如下:

这是因为模型加载失败,解决方法如下:
切换到/usr/share/gazebo-9/目录下,执行如下命令:

sudo chmod 777 models

再切换到/usr/share/gazebo-9/models/目录下,执行如下命令:

sudo wget http://file.ncnynl.com/ros/gazebo_models.txt
sudo wget -i gazebo_models.txt
ls model.tar.g* | xargs -n1 tar xzvf

再次运行柳树库公司办公室的launch文件,效果如下:

现在要做的是创建一个新的launch文件来同时加载地图和机器人,在launch文件夹下创建robot_gazebo_wg.launch,文件内容如下:

<launch>
    <!-- 运行gazebo仿真环境 -->
    <include file="$(find gazebo_ros)/launch/willowgarage_world.launch">
    </include>

    
    <!--机器人参数设置-->
    <arg name="model" default="$(find xacro)/xacro --inorder '$(find mbot_sim_gazebo)/urdf/xacro/robot.xacro'" />
     
    <!--robot_description 参数名称是参数服务器预先设定好的,不能改变-->
    <param name="robot_description" command="$(arg model)" />


    <!-- 运行joint_state_publisher节点,发布机器人的关节状态  -->
    <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" />

    <!-- 运行robot_state_publisher节点,发布tf  -->
    <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

      <!-- 在gazebo中加载机器人模型-->
    <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
          args="-urdf -model mrobot -param robot_description"/> 

</launch>

通过如下命令运行:

roslaunch mbot_sim_gazebo robot_gazebo_wg.launch

效果如下,蓝色点即为机器人模型:

再运行如下命令查看图像:

rqt_image_view

此时,摄像头是有内容的,效果如下:

此时,通过如下命令,可以查看激光雷达的数据:

rostopic echo /scan

五、总结

截止到目前,我们为自定义的机器人模型,添加了摄像头和激光雷达 ,又为其添加了地图,下一篇开始介绍如何控制机器人在仿真环境下的移动。