ros gazebo仿真环境搭建

1.准备工作

创建工作空间

mkdir -p ~/car_ws/src

创建功能包

cd car_ws/src
roscreate-pkg car

编译工作空间

cd ..
catkin_make
source devel/setup.bash

创建urdf和launch文件,配置文件

cd src/car
mkdir urdf launch config
cd urdf
touch car.xacro car.gazebo
cd ../launch
touch gazebo.launch display.launch
cd ../config
touch car.yaml

car.xacro

查看代码
<?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 car)/urdf/car.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>
                <box size="0.1 0.1 0.1" />
            </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>

car.gazebo

查看代码
 <?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>true</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>

gazebo.launch

查看代码
 <launch>

  <!-- these are the arguments you can pass this launch file, for example paused:=true -->
  <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"/>

  <!-- 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="world_name" value="$(find car)/cont.world"/>
    <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 -->
  <param name="robot_description"
    command="$(find xacro)/xacro --inorder '$(find car)/urdf/car.xacro'" />

  <!-- 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 car -param robot_description"/>

</launch>

display.launch

查看代码
 <launch>

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

  <!-- send fake joint values -->
  <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher">
    <param name="use_gui" value="TRUE"/>
  </node>

  <!-- Combine joint values -->
  <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher"/>

  <!-- Show in Rviz   -->
  <node name="rviz" pkg="rviz" type="rviz" args="-d $(find car)/launch/car.rviz"/>
 

</launch>

启动gazebo

roslaunch car gazebo.launch

启动rviz

roslaunch car display.launch

 

posted @ 2024-06-11 15:52  yeesn  阅读(7)  评论(0编辑  收藏  举报