ROS 从新开始3D仿真四轮小车和导航运动 二

gazbo中仿真小车与控制小车运动

本节根据书中第7章内容做简单修改而来,涉及文件较多,可以去下面的链接下下载

小车模型搭建

同前一篇创建一个新的ros包,在新的gzb_carbot包中练习
cd testCarBot/src;catkin_create_pkg gzb_carbot
cd myCar;mkdir launch;mkdir urdf

在urdf目录中分别创建下面几个文件,gzb_carbot.xacro robot.gazebo robot.world hokuyo.dae

小车模型描述文件 gzb_carbot.xacro
xacro 相比urdf文件做了一定的简化处理,类似于C语言中的宏功能。 该文件引用了另外两个文件robot.gazebo,hokuyo.dae;
gazbo中的仿真与上一章的区别在于,模型中需要增加 惯性质量与冲突检测相关的描述,多了相机与激光探测器模块

<?xml version="1.0"?>

<robot xmlns:xacro="http://www.ros.org/wiki/xacro" 
    xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor" 
    xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller" 
    xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface" name="robot1_xacro">

    <xacro:property name="length_wheel" value="0.05" />
    <xacro:property name="radius_wheel" value="0.05" />
    <xacro:property name="camera_link" value="0.05" />
    <xacro:macro name="default_inertial" params="mass">
        <inertial>
            <mass value="${mass}" />
            <inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0" />
        </inertial>
    </xacro:macro>

    <link name="base_footprint">
        <visual>
            <geometry>
                <box size="0.001 0.001 0.001" />
            </geometry>
            <origin rpy="0 0 0" xyz="0 0 0" />
        </visual>
        <xacro:default_inertial mass="0.0001" />
    </link>
    <xacro:include filename="$(find gzb_carbot)/urdf/robot.gazebo" />

    <gazebo reference="base_footprint">
        <material>Gazebo/Green</material>
        <turnGravityOff>false</turnGravityOff>
    </gazebo>

    <joint name="base_footprint_joint" type="fixed">
        <origin xyz="0 0 0" />
        <parent link="base_footprint" />
        <child link="base_link" />
    </joint>

    <link name="base_link">
        <visual>
            <geometry>
                <box size="0.2 .3 .1" />
            </geometry>
            <origin rpy="0 0 1.54" xyz="0 0 0.05" />
            <material name="white">
                <color rgba="1 1 1 1" />
            </material>
        </visual>
        <collision>
            <geometry>
                <box size="0.2 .3 0.1" />
            </geometry>
        </collision>
        <xacro:default_inertial mass="10" />
    </link>

    <link name="wheel_1">
        <visual>
            <geometry>
                <cylinder length="${length_wheel}" radius="${radius_wheel}" />
            </geometry>
            <!-- <origin rpy="0 1.5 0" xyz="0.1 0.1 0"/> -->
            <origin rpy="0 0 0" xyz="0 0 0" />
            <material name="black">
                <color rgba="0 0 0 1" />
            </material>
        </visual>
        <collision>
            <geometry>
                <cylinder length="${length_wheel}" radius="${radius_wheel}" />
            </geometry>
        </collision>
        <xacro:default_inertial mass="1" />
    </link>

    <link name="wheel_2">
        <visual>
            <geometry>
                <cylinder length="${length_wheel}" radius="${radius_wheel}" />
            </geometry>
            <!-- <origin rpy="0 1.5 0" xyz="-0.1 0.1 0"/> -->
            <origin rpy="0 0 0" xyz="0 0 0" />
            <material name="black" />
        </visual>
        <collision>
            <geometry>
                <cylinder length="${length_wheel}" radius="${radius_wheel}" />
            </geometry>
        </collision>
        <xacro:default_inertial mass="1" />

    </link>

    <link name="wheel_3">
        <visual>
            <geometry>
                <cylinder length="${length_wheel}" radius="${radius_wheel}" />
            </geometry>
            <!-- <origin rpy="0 1.5 0" xyz="0.1 -0.1 0"/> -->

            <origin rpy="0 0 0" xyz="0 0 0" />
            <material name="black" />
        </visual>
        <collision>
            <geometry>
                <cylinder length="${length_wheel}" radius="${radius_wheel}" />
            </geometry>
        </collision>
        <xacro:default_inertial mass="1" />
    </link>

    <link name="wheel_4">
        <visual>
            <geometry>
                <cylinder length="${length_wheel}" radius="${radius_wheel}" />
            </geometry>
            <!--    <origin rpy="0 1.5 0" xyz="-0.1 -0.1 0"/> -->
            <origin rpy="0 0 0" xyz="0 0 0" />
            <material name="black" />
        </visual>
        <collision>
            <geometry>
                <cylinder length="${length_wheel}" radius="${radius_wheel}" />
            </geometry>
        </collision>
        <xacro:default_inertial mass="1" />

    </link>

    <joint name="base_to_wheel1" type="continuous">
        <parent link="base_link" />
        <child link="wheel_1" />
        <origin rpy="1.5707 0 0" xyz="0.1 0.15 0" />
        <axis xyz="0 0 1" />
    </joint>

    <joint name="base_to_wheel2" type="continuous">
        <axis xyz="0 0 1" />
        <anchor xyz="0 0 0" />
        <limit effort="100" velocity="100" />
        <parent link="base_link" />
        <child link="wheel_2" />
        <origin rpy="1.5707 0 0" xyz="-0.1 0.15 0" />
    </joint>

    <joint name="base_to_wheel3" type="continuous">
        <parent link="base_link" />
        <axis xyz="0 0 1" />
        <child link="wheel_3" />
        <origin rpy="1.5707 0 0" xyz="0.1 -0.15 0" />
    </joint>

    <joint name="base_to_wheel4" type="continuous">
        <parent link="base_link" />
        <axis xyz="0 0 1" />
        <child link="wheel_4" />
        <origin rpy="1.5707 0 0" xyz="-0.1 -0.15 0" />
    </joint>

    <joint name="camera_joint" type="fixed">
        <axis xyz="0 1 0" />
        <origin xyz="0.125 0 0.125" rpy="0 0 0" />
        <parent link="base_link" />
        <child link="camera_link" />
    </joint>

    <!-- Camera -->
    <link name="camera_link">
        <collision>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <geometry>
                <box size="0.05 0.05 0.05" />
            </geometry>
        </collision>

        <visual>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <geometry>
                <box size="0.05 0.05 0.05" />
            </geometry>
            <material name="red">
                <color rgba="1 0 0 1" />
            </material>
        </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>

    <joint name="hokuyo_joint" type="fixed">
        <axis xyz="0 1 0" />
        <origin xyz="0.125 0.05 0.125" rpy="0 0 0" />
        <parent link="base_link" />
        <child link="hokuyo_link" />
    </joint>

    <!-- Hokuyo Laser -->
    <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://gzb_carbot/urdf/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>

</robot>

hokuyo.dae 激光探测器模型文件,文件过大,省去,可以去链接中下载

robot.gazebo 机器人在gazbo中仿真的必要描述

<?xml version="1.0"?>
<robot>
  <!-- materials -->
  <gazebo reference="base_link">
    <material>Gazebo/Orange</material>
  </gazebo>

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

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

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

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


  <!-- ros_control plugin -->
  <gazebo>
    <plugin name="gazebo_ros_control" filename="libgazebo_ros_control.so">
      <robotNamespace>/robot</robotNamespace>

    </plugin>
  </gazebo>

  <!-- Link1 -->
  <gazebo reference="link1">
    <material>Gazebo/Orange</material>
  </gazebo>

  <!-- Link2 -->
  <gazebo reference="link2">
    <mu1>0.2</mu1>
    <mu2>0.2</mu2>
    <material>Gazebo/Black</material>
  </gazebo>

  <!-- Link3 -->
  <gazebo reference="link3">
    <mu1>0.2</mu1>
    <mu2>0.2</mu2>
    <material>Gazebo/Orange</material>
  </gazebo>

  <!-- camera_link -->
  <gazebo reference="camera_link">
    <mu1>0.2</mu1>
    <mu2>0.2</mu2>
    <material>Gazebo/Red</material>
  </gazebo>

  <!-- hokuyo -->
  <gazebo reference="hokuyo_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>
          <!-- Noise parameters based on published spec for Hokuyo laser
               achieving "+-30mm" accuracy at range < 10m.  A mean of 0.0m and
               stddev of 0.01m will put 99.7% of samples within 0.03m of the true
               reading. -->
          <mean>0.0</mean>
          <stddev>0.01</stddev>
        </noise>
      </ray>
      <plugin name="gazebo_ros_head_hokuyo_controller" filename="libgazebo_ros_laser.so">
        <topicName>/robot/laser/scan</topicName>
        <frameName>hokuyo_link</frameName>
      </plugin>
    </sensor>
  </gazebo>

  <!-- camera -->
  <gazebo reference="camera_link">
    <sensor type="camera" name="camera1">
      <update_rate>30.0</update_rate>
      <camera name="head">
        <horizontal_fov>1.3962634</horizontal_fov>
        <image>
          <width>800</width>
          <height>800</height>
          <format>R8G8B8</format>
        </image>
        <clip>
          <near>0.02</near>
          <far>300</far>
        </clip>
        <noise>
          <type>gaussian</type>
          <!-- Noise is sampled independently per pixel on each frame.  
               That pixel's noise value is added to each of its color
               channels, which at that point lie in the range [0,1]. -->
          <mean>0.0</mean>
          <stddev>0.007</stddev>
        </noise>
      </camera>
      <plugin name="camera_controller" filename="libgazebo_ros_camera.so">
        <alwaysOn>true</alwaysOn>
        <updateRate>0.0</updateRate>
        <cameraName>robot/camera1</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>

</robot>

仿真启动文件

launch目录中创建 carbot.launch文件,内容如下
在gazbo中仿真显示 必须先有world模型才可以正常显示

<?xml version="1.0"?>
<launch>

  <!-- these are the arguments you can pass this launch file, for example paused:=true -->
  <arg name="paused" default="true"/>
  <arg name="use_sim_time" default="false"/>
  <arg name="gui" default="true"/>
  <arg name="headless" default="false"/>
  <arg name="debug" default="true"/>
  
  <!-- We resume the logic in empty_world.launch, changing only the name of the world to be launched -->
  <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>
  
  <!-- Load the URDF into the ROS Parameter Server -->
  <arg name="model" />
  <param name="robot_description" 
     command="$(find xacro)/xacro.py $(arg model)" />

  <!-- Run a python script to the send a service call to gazebo_ros to spawn a URDF robot -->
   <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
    args="-urdf -model robot1 -param robot_description -z 0.05"/> 

</launch>

去工作空间根目录,执行catkin_make,生成包后,执行以下命令即可看到gazbo中仿真结果

roslaunch gzb_carbot carbot.launch model:="`rospack find gzb_carbot`/urdf/gzb_carbot.xacro"
image.png

运动控制

待更新

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容