0


Gazebo——仿真平台搭建(基于Ubuntu20.04)

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'

2.设置秘钥

wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -

3.安装Gazebo

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

4.检查你的安装是否有效果=

gazebo

5.打开 /.gazebo文件夹 下载模型

cd ~/.gazebo
git clone https://github.com/osrf/gazebo_models

如果出现fatal连接GitHub失败请使用以下代码

git clone https://gitclone.com/github.com/osrf/gazebo_models

下载时间可能偏久,读者可以选择和我一样另起终端进行其他操作,等到需要使用场景模型的时候大概也已经下载完成。

6.其他关联安装包(不定时更新)
sudo apt-get install ros-noetic-rviz

sudo apt-get install ros-noetic-robot-state-publisher

sudo apt-get install ros-noetic-gazebo-ros-pkgs ros-noetic-gazebo-ros-control

sudo apt install ros-noetic-moveit

然后运行安装包索引

sudo apt-get update

接下来进行机器人模型的搭建

机器人模型配置

首先进入到mbot_description/urdf/xacro下

cd ~/catkin_ws/src/mbot_description/urdf/xacro
mkdir gazebo 
cd gazebo
sudo gedit mbot_base_gazebo.xacro

编辑模型文件

<?xml version="1.0"?>
<robot name="mbot" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!--存放下面相关定义内容-->
</robot>
    <!-- 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 -->
              <!-- the same with visual -->
              <!--增加惯性属性和碰撞属性-->
            <collision>
                <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />  
                <geometry>
                    <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
                </geometry>
            </collision>
              <!-- inertial -->
            <cylinder_inertial_matrix  m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />             
        </link>
        <!--添加gazebo标签为各link配颜色 ,gazebo与rivz颜色设置不兼容-->
                     <!-- Add gazebo tag to link -->
        <gazebo reference="${prefix}_wheel_link">
            <material>Gazebo/Gray</material>          
        </gazebo>
        <!--joint添加传动装置,用得 transmission 标签,小车轮子用速度控制接口-->
                <!-- 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标签,为各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>
           <!-- 给 base_footprint 添加标签 -->
        <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>
        <!--base_link添加碰撞属性和惯性属性-->
                <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>
        <!--base_link添加gazebo标签-->
        <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"/> <!-- 调用支撑轮子宏定义 -->
    </xacro:macro>
        <!-- controller -->
        <gazebo>
            <plugin name="differential_drive_controller" 
                    filename="libgazebo_ros_diff_drive.so"> <!-- gazebo提供得差速控制器插件 -->
                    <!-- 控制器所需参数 -->
                <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> <!-- 控制得joint在哪里,必须和上面得joint名称一致 -->
                <rightJoint>right_wheel_joint</rightJoint><!-- 控制得joint在哪里,必须和上面得joint名称一致 -->
                <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><!-- 设置controler所控制的机器人的坐标系是哪个坐标系 -->
            </plugin>
        </gazebo> 

再编辑mbot_gazebo.xacro

<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
    <xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" /> <!-- 包含文件 -->
    <mbot_base_gazebo/>    <!-- 调用宏定义 -->
</robot>

编辑launch文件

cd ~/catkin_ws/src/mbot_description/launch/xacro
mkdir gazebo
cd gazebo
sudo gedit mbot_base_gazebo.launch
<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>

    <!-- 加载机器人模型描述参数 -->
    <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find mbot_description)/urdf/xacro/gazebo/mbot_gazebo.xacro'" /> 

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

    <!-- 运行robot_state_publisher节点,发布tf  -->
    <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher"  output="screen" >
        <param name="publish_frequency" type="double" value="50.0" />
    </node>

    <!-- 在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_description mbot_base_gazebo.launch

注意

由于Ubuntu20.04对应的ros版本为noetic,所以xacro模型文件的宏定义和宏调用方式都需要更改

Ubuntu20.04的mbot_base_gazebo.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>
            <xacro: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>      
            <xacro: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>   
            <xacro:cylinder_inertial_matrix  m="${base_mass}" r="${base_radius}" h="${base_length}" />
        </link>

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

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

        <xacro:caster prefix="front" reflect="-1"/>
        <xacro: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>

需要更改的地方为

在轮子的宏定义语法以及支撑轮的宏定义语法前添加xacro引用

cylinder_inertial_matrix,

sphere_inertial_matrix

两个个标签引用的时候也需要加上xacro引用

以及mbot_gazebo.xacro文件也需要更改宏定义

<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
    <xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" /> <!-- 包含文件 -->
    <xacro:mbot_base_gazebo/>    <!-- 调用宏定义 -->
</robot>

launch文件同样需要更改,这里有三个更改方法,一般情况下将

 <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find mbot_description)/urdf/xacro/gazebo/mbot_gazebo.xacro'" /> 

改为xacro 即可

也有其他的解决方法例如将其改为

xacro --i

还有

xacro.py

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

    <!-- 加载机器人模型描述参数 -->
    <param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_gazebo.xacro'" /> 

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

    <!-- 运行robot_state_publisher节点,发布tf  -->
    <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher"  output="screen" >
        <param name="publish_frequency" type="double" value="50.0" />
    </node>

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

正常启动后如下所示

**创建仿真环境 **

这里有两种方法添加场景模型

第一:直接添加

将模型放置到~/.gazebo/models 文件夹下——在gazebo的左侧列表点击“insert”(可以看到里面有很多的模型,我们只需要从列表中拖出我们需要的模型放置到仿真环境中就可以)

https://bitbucket.org/osrf/gazebo_models/downloads/

这里本文开始时的下载如果已经下载完成可以跳过,如果还未下载完成请等待下载完成再进行后续操作。

选择需要的模型直接拖动到场景中搭建即可

注意这一步需要将原本的机器人模型进行删除然后保存。

这里如果显示conectting----说明还未连接完成,等待出现http-----连接完成后即可正常显示模型,再选择需要的模型添加即可。

自定义文件名称和位置即可

建议存放在catkin_ws/src/mbot_descritpion/worlds下

第二:使用Building editor

模型创建:

Edit——Building editor——绘制环境模型——File——Save保存我们的模型文件(自己设置模型文件名字)——Exit Building Editor(退出编辑界面),可以看到我们的仿真环境已经在gazebo中显示;

保存环境模型同第一种方法一致,然后关闭gazebo界面即可。

仿真使用

    <!-- 设置launch文件的参数 -->
    <arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/><!-- 要加入的部分 -->
    <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"/>

**传感器仿真 **

cd ~/catkin_ws/src/mbot_description/urdf/xacro
mkdir sensors
cd sensors
sudo gedit camera_gazebo.xacro
<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="camera">
    <xacro:macro name="usb_camera" params="prefix:=camera">
        <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代表的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>
cd ~/catkin_ws/src/mbot_description/urdf/xacro/gazebo
sudo gedit mbot_with_camera_gazebo.xacro

在文件中写入以下内容即可

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

    <xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
    <xacro:include filename="$(find mbot_description)/urdf/xacro/sensors/camera_gazebo.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" />

    <mbot_base/>

    <!-- 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"/>

    <xacro:mbot_base_gazebo/>

</robot>

带摄像头的机器人launch启动文件的编写

cd ~/catkin_ws/src/mbot_description/launch/xacro/gazebo
sudo gedit view_mbot_with_camera_gazebo.launch
<launch>
    <arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/>
    <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"/>
  
    <include file="$(find gazebo_ros)/launch/empty_world.launch">
        <arg name="world_name" value="$(arg world_name)" />
        <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>

    <param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_with_camera_gazebo.xacro'" /> 

    
    <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node> 

    <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher"  output="screen" >
        <param name="publish_frequency" type="double" value="50.0" />
    </node>

    <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
          args="-urdf -model mrobot -param robot_description"/> 

</launch>

然后运行显示模型

roslaunch mbot_description view_mbot_with_camera_gazebo.launch 

注意如果说出现

[ERROR] [1666006852.591762, 106.467000]: Spawn service failed. Exiting.
[urdf_spawner-6] process has died [pid 35261, exit code 1, cmd /opt/ros/noetic/lib/gazebo_ros/spawn_model -urdf -model mrobot -param robot_description __name:=urdf_spawner __log:=/home/q/.ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6.log].
log file: /home/q/.ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6*.log
则说明gazebo进程关闭不完全,需要将所有gazebo进程关闭

killall gzserver

然后发现可以正常启动

使用qt可视化工具查看摄像头画面显示

另起终端

rqt_image_view

注意摄像头参数选定

启用键盘控制

roslaunch mbot_teleop mbot_teleop.launch

注意键盘控制按键

这里如果报错无法定位python 软件包

需要进行下载配置python插件

sudo ln -s /usr/bin/python3 /usr/bin/python

然后再允许键盘控制节点即可

如果读者功能包的src目录下没有mbot_teleop功能包,也可下载另键盘控制功能包

cd ~/carkin_ws/src
git clone https://github.com/ros-teleop/teleop_twist_keyboard.git
//然后启用即可
rosrun teleop_twist_keyboard teleop_twist_keyboard.py

注意该teleop_twist_keyboard.py需要更改为可执行的文件

如果不想使用该功能包,想知道如何自主创建mbot_teleop功能包可参考Arbotix+rviz那篇文章mbot_teleop 功能包创建方法

Rviz查看摄像头采集的信息

rosrun rviz rviz

首先固定框架(fixed frame)选择base_footprint

然后Add——robotmodel——ok

然后Add——image——ok

然后image——image topic——/camera/image_raw

** 激光雷达仿真**

cd ~/catkin_ws/src/mbot_description/urdf/xacro/sensors
sudo gedit lidar_gazebo.xacro
<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="laser">

    <xacro:macro name="rplidar" params="prefix">
        <!-- 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>
                    <cylinder length="0.05" radius="0.05"/>
                </geometry>
                <material name="black"/>
            </visual>

            <collision>
                <origin xyz="0.0 0.0 0.0" rpy="0 0 0" />
                <geometry>
                    <cylinder length="0.06" radius="0.05"/>
                </geometry>
            </collision>
        </link>
        <gazebo reference="${prefix}_link">
            <material>Gazebo/Black</material>
        </gazebo>

        <gazebo reference="${prefix}_link">
            <sensor type="ray" name="rplidar">
                <pose>0 0 0 0 0 0</pose>
                <visualize>false</visualize>
                <update_rate>5.5</update_rate>
                <ray>
                    <scan>
                      <horizontal>
                        <samples>360</samples>
                        <resolution>1</resolution>
                        <min_angle>-3</min_angle>
                        <max_angle>3</max_angle>
                      </horizontal>
                    </scan>
                    <range>
                      <min>0.10</min>
                      <max>6.0</max>
                      <resolution>0.01</resolution>
                    </range>
                    <noise>
                      <type>gaussian</type>
                      <mean>0.0</mean>
                      <stddev>0.01</stddev>
                    </noise>
                </ray>
                <plugin name="gazebo_rplidar" filename="libgazebo_ros_laser.so">
                    <topicName>/scan</topicName>
                    <frameName>laser_link</frameName>
                </plugin>
            </sensor>
        </gazebo>

    </xacro:macro>
</robot>

然后编写主体xacro文件

cd ~/catkin_ws/src/mbot_description/urdf/xacro/gazebo
sudo gedit mbot_with_laser_gazebo.xacro

内容如下所示

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

    <xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
    <xacro:include filename="$(find mbot_description)/urdf/xacro/sensors/lidar_gazebo.xacro" />

    <xacro:property name="lidar_offset_x" value="0" />
    <xacro:property name="lidar_offset_y" value="0" />
    <xacro:property name="lidar_offset_z" value="0.105" />

    <!-- lidar -->
    <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="laser_link"/>
    </joint>
    <xacro:rplidar prefix="laser"/>
    <xacro:mbot_base_gazebo/>
</robot>

然后编写launch 启动文件

cd ~/catkin_ws/src/mbot_description/launch/xacro/gazebo
sudo gedit view_mbot_with_laser_gazebo.launch
<launch>

    <arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/>
    <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"/>

    <include file="$(find gazebo_ros)/launch/empty_world.launch">
        <arg name="world_name" value="$(arg world_name)" />
        <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>

 
    <param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_with_laser_gazebo.xacro'" />

   
    <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node> 

   
    <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher"  output="screen" >
        <param name="publish_frequency" type="double" value="50.0" />
    </node>

    
    <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
          args="-urdf -model mrobot -param robot_description"/> 
</launch>

激光雷达正常显示

然后启用键盘控制

roslaunch mbot_teleop mbot_teleop.launch

rviz查看雷达采集信息

rosrun rviz rviz

需要注意的地方与刚才类似

首先固定框架(fixed frame)选择base_footprint

然后Add——robotmodel——ok

然后Add——laserscan——ok

然后laserscan——topic——scan

在图中的红色线条即为雷达检测到的障碍物,由于我的小车卡在消防车和救护车之间懒得动了,就不附图说明了,红色线条很淡,建议将小车周围拿障碍物包满。

Kinect仿真

方法跟上面摄像头和激光雷达实现的方法类似,这里就不多加说明了,直接给出结果显示了。
gazebo显示

cd ~/catkin_ws/src/mbot_description/urdf/xacro/sensors
sudo gedit kinect_gazebo.xacro
<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="kinect_camera">

    <xacro:macro name="kinect_camera" params="prefix:=camera">
        <!-- Create kinect reference frame -->
        <!-- Add mesh for kinect -->
        <link name="${prefix}_link">
            <origin xyz="0 0 0" rpy="0 0 0"/>
            <visual>
                <origin xyz="0 0 0" rpy="0 0 ${M_PI/2}"/>
                <geometry>
                    <mesh filename="package://mbot_description/meshes/kinect.dae" />
                </geometry>
            </visual>
            <collision>
                <geometry>
                    <box size="0.07 0.3 0.09"/>
                </geometry>
            </collision>
        </link>

        <joint name="${prefix}_optical_joint" type="fixed">
            <origin xyz="0 0 0" rpy="-1.5708 0 -1.5708"/>
            <parent link="${prefix}_link"/>
            <child link="${prefix}_frame_optical"/>
        </joint>

        <link name="${prefix}_frame_optical"/>

        <gazebo reference="${prefix}_link">
            <sensor type="depth" name="${prefix}">
                <always_on>true</always_on>
                <update_rate>20.0</update_rate>
                <camera>
                    <horizontal_fov>${60.0*M_PI/180.0}</horizontal_fov>
                    <image>
                        <format>R8G8B8</format>
                        <width>640</width>
                        <height>480</height>
                    </image>
                    <clip>
                        <near>0.05</near>
                        <far>8.0</far>
                    </clip>
                </camera>
                <plugin name="kinect_${prefix}_controller" filename="libgazebo_ros_openni_kinect.so">
                    <cameraName>${prefix}</cameraName>
                    <alwaysOn>true</alwaysOn>
                    <updateRate>10</updateRate>
                    <imageTopicName>rgb/image_raw</imageTopicName>
                    <depthImageTopicName>depth/image_raw</depthImageTopicName>
                    <pointCloudTopicName>depth/points</pointCloudTopicName>
                    <cameraInfoTopicName>rgb/camera_info</cameraInfoTopicName>
                    <depthImageCameraInfoTopicName>depth/camera_info</depthImageCameraInfoTopicName>
                    <frameName>${prefix}_frame_optical</frameName>
                    <baseline>0.1</baseline>
                    <distortion_k1>0.0</distortion_k1>
                    <distortion_k2>0.0</distortion_k2>
                    <distortion_k3>0.0</distortion_k3>
                    <distortion_t1>0.0</distortion_t1>
                    <distortion_t2>0.0</distortion_t2>
                    <pointCloudCutoff>0.4</pointCloudCutoff>
                </plugin>
            </sensor>
        </gazebo>

    </xacro:macro>
</robot>

然后编写主体xacro文件

cd ~/catkin_ws/src/mbot_desctiption/urdf/xacro/gazebo
sudo gedit mbot_with_kinect_gazebo.xacro
<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">

    <xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
    <xacro:include filename="$(find mbot_description)/urdf/xacro/sensors/kinect_gazebo.xacro" />

    <xacro:property name="kinect_offset_x" value="0.15" />
    <xacro:property name="kinect_offset_y" value="0" />
    <xacro:property name="kinect_offset_z" value="0.11" />

    <mbot_base/>

    <!-- kinect -->
    <joint name="kinect_joint" type="fixed">
        <origin xyz="${kinect_offset_x} ${kinect_offset_y} ${kinect_offset_z}" rpy="0 0 0" />
        <parent link="base_link"/>
        <child link="kinect_link"/>
    </joint>

    <xacro:kinect_camera prefix="kinect"/>

    <xacro:mbot_base_gazebo/>

</robot>

然后编写launch 启动文件

cd ~/catkin_ws/src/mbot_description/launch/xacro/gazebo
sudo gedit view_mbot_with_kinect_gazebo.launch
<launch>

    <!-- 设置launch文件的参数 -->
    <arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/><!-- 设置仿真环境文件路径 -->
    <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="world_name" value="$(arg world_name)" />
        <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>

    <!-- 加载机器人模型描述参数 -->
    <param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_with_kinect_gazebo.xacro'" /> <!-- 设置机器人模型文件路径 -->

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

    <!-- 运行robot_state_publisher节点,发布tf  -->
    <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher"  output="screen" >
        <param name="publish_frequency" type="double" value="50.0" />
    </node>

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

运行后如下所示

查看kinect采集到的信息

rosrun rviz rviz

注意:

首先固定框架(fixed frame)选择base_footprint

然后Add——robotmodel——ok

然后Add——pointcloud2——ok

然后pointcloud2——topic——/kinect/depth/points

以上就是本节内容,关于Gazebo物理仿真平台搭建已经全部完成。

问题解决:

1.gazebo--SpawnModel: Failure - model name mrobot already exists.

roslaunch gazebo_ros empty_world.launch

在开启后的gazebo界面中选中mrobot模型delete删除即可

然后退出。

在上述模型创建且.world 保存前需要把机器人模型删除再保存,不然会出现模型重名报错。

  1. [ERROR] [1666006852.591762, 106.467000]: Spawn service failed. Exiting.
    [urdf_spawner-6] process has died [pid 35261, exit code 1, cmd
    /opt/ros/noetic/lib/gazebo_ros/spawn_model -urdf -model mrobot -param robot_description __name:=urdf_spawner __log:=/home/q/.ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6.log].
    log file: /home/q/.ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6*.log

将所有gazebo进程关闭再重启即可

killall gzserver
标签: ubuntu linux 运维

本文转载自: https://blog.csdn.net/LYY_WJL/article/details/127352386
版权归原作者 哩lililili 所有, 如有侵权,请联系我们删除。

“Gazebo——仿真平台搭建(基于Ubuntu20.04)”的评论:

还没有评论