首页 > 代码库 > ROS之Gazebo

ROS之Gazebo

<?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="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="http://www.mamicode.com/$(find carModel)/worlds/robot.world"/>
    <arg name="debug" value="http://www.mamicode.com/$(arg debug)" />
    <arg name="gui" value="http://www.mamicode.com/$(arg gui)" />
    <arg name="paused" value="http://www.mamicode.com/$(arg paused)"/>
    <arg name="use_sim_time" value="http://www.mamicode.com/$(arg use_sim_time)"/>
    <arg name="headless" value="http://www.mamicode.com/$(arg headless)"/>
  </include>

  <!-- Load the URDF into the ROS Parameter Server -->
  <arg name="model" />
  <param name="robot_description" command="$(find xacro)/xacro.py $(find carModel)/urdf/robot1_base_02.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 robot1 -param robot_description -z 0.05"/> 

</launch>

 首先是把ros自带的gazebo_ros包的启动文件加载进来, 并启动里面的节点

   <include file="$(find gazebo_ros)/launch/empty_world.launch"> 

 然后解码xacro

<param name="robot_description" command="$(find xacro)/xacro.py $(find carModel)/urdf/robot1_base_02.xacro" />

 然后启动一个urdf转换节点:

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

 

再看看这个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="http://www.mamicode.com/0.05" />
	<xacro:property name="radius_wheel" value="http://www.mamicode.com/0.05" />
	<xacro:macro name="default_inertial" params="mass">
               <inertial>
                       <mass value="http://www.mamicode.com/${mass}" />
                       <inertia ixx="1.0" ixy="0.0" ixz="0.0"
                                iyy="1.0" iyz="0.0"
                                izz="1.0" />
               </inertial>
	</xacro:macro>
	<xacro:include filename="$(find carModel)/urdf/robot.gazebo" />
	<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>

	<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="darkblue">
        			<color rgba=".1 .1 .5 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>

 分别定义了若干link, 关节.

运行下面的命令:

roslaunch carModel gazebo.launch 

 技术分享

为啥有个轮子是绿色的呢?

	<xacro:include filename="$(find carModel)/urdf/robot.gazebo" />

 这个gazebo文件里面是轮子的贴图:

 

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

 <gazebo reference="wheel_1">
        <material>Gazebo/Green</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>

 

ROS之Gazebo