继续补充Gazebo的使用,在这之前说明下我的环境。

操作系统:Ubuntu14.04lts

ROS系统版本:Indigo

 

看这篇文章前,建议先看我写的上一篇文章,【ROS学习笔记】二、Gazebo的使用上

好的,我们继续。

我们继续按照教程走。

一、使用ROS命令将新的对象加入到Gazebo模拟器中。

1)首先我们打开终端,roscore核心记得运行起来。

roscore

2)再开一个新的终端,使用roslaunch命令打开一个空的gazebo(注意这是重新开始了,如果你是直接从上篇看过来的,请直接进入到4)小节)

ros wiki上是用的是以下命令

roslaunch gazebo_worlds empty_world.launch

原因是因为官方在此处用的并不是ros indigo版本,可能是jade也可能是hydro,具体的我也不清楚,这里给大家推荐一个方法,如何确定gazebo_后面的参数,一般来说,ros会安装在/opt路径下,那么我们进入到/opt/ros/xxx(你的版本,我的是indigo)/share/ , 在这个文件夹下,可以找到你的gazebo_xxx,你可以看到你的后续应该跟的文件名称,是worlds 还是ros就在这里看。

如果你是indigo版本,使用以下命令

roslaunch gazebo_ros empty_world.launch

打开了一个新的Gazebo界面。

3)将上次写的蓝色盒子的urdf文件导入进去,如果你不明白这里的话,请看我上一篇博客。

rosrun gazebo_ros spawn_model -file `pwd`/object.urdf -urdf -z 1 -model my_object

4)使用roslaunch命令为模拟器中添加一张桌子
打开终端,输入以下命令

roslaunch gazebo_ros table.launch

有的朋友说,我没有找到gazebo_ros 的launch文件夹下有这个table.launch文件,当然没有,这个是要自己写的,官方知识让你看看,具体的要自己动手。

首先,使用终端,进入到gazebo_ros文件夹下。

cd /opt/ros/indigo/share/gazebo_ros

建立一个objects文件夹,这里主要用来储存后续的模型文件

sudo mkdir objects

然后,使用终端,进入到gazebo_ros下launch文件夹下。

cd /opt/ros/indigo/share/gazebo_ros/launch

你会发现这里有一些launch文件,比如你最开始是用的empty_worlds.launch文件就在这里。

接着,我们使用命令,创建一个table.launch文件

sudo gedit table.launch

将以下代码复制进去(这段代码是官方提供的,Indigo版本不适用,后面我会讲到如何改过来)

<launch>
  <!-- send table urdf to param server -->
  <param name="table_description" command="$(find xacro)/xacro.py $(find gazebo_worlds)/objects/table.urdf.xacro" />

  <!-- push table_description to factory and spawn robot in gazebo -->
  <node name="spawn_table" pkg="gazebo" type="spawn_model" args="-urdf -param table_description -z 0.01 -model table_model" respawn="false" output="screen" />
</launch>

保存并关闭文件。
使用roslaunch命令运行我们的launch文件

rosluanch gazebo_ros table.launch

终端会提示错误,一大堆,使用indigo版本的人到这里其实是有三处错误,使用其他版本的也许只有一处,这里以indigo版本为例。
a)首先,你缺少了一个模型文件,即table的模型文件,我们使用的launch文件只是添加模型,但模型的具体文件不存在,需要我们手动建立。
我们进入到前面建立的objects文件夹下,建立一个名为table.urdf.xacro格式的文件

cd /opt/ros/indigo/share/gazebo_ros/objects
sudo gedit table.urdf.xacro

如果你嫌麻烦,我已经将这个文件上传了,你们下载下来,放到这个路径下就行了,,table.urdf.xcaro下载

不怕麻烦的,将以下代码填入这个文件中:

<?xml version="1.0"?>
<robot name="table"
       xmlns:xi="http://www.w3.org/2001/XInclude"
       xmlns:gazebo="http://playerstage.sourceforge.net/gazebo/xmlschema/#gz"
       xmlns:model="http://playerstage.sourceforge.net/gazebo/xmlschema/#model"
       xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor"
       xmlns:body="http://playerstage.sourceforge.net/gazebo/xmlschema/#body"
       xmlns:geom="http://playerstage.sourceforge.net/gazebo/xmlschema/#geom"
       xmlns:joint="http://playerstage.sourceforge.net/gazebo/xmlschema/#joint"
       xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface"
       xmlns:rendering="http://playerstage.sourceforge.net/gazebo/xmlschema/#rendering"
       xmlns:renderable="http://playerstage.sourceforge.net/gazebo/xmlschema/#renderable"
       xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller"
       xmlns:physics="http://playerstage.sourceforge.net/gazebo/xmlschema/#physics">
 
  <property name="table_height" value="0.55" />
  <property name="table_width" value="1.0" />
  <property name="table_depth" value="2.0" />
  <property name="leg_radius" value="0.02" />
  <property name="table_x" value="0.98" />
  <property name="table_y" value="0.0" />
  <property name="table_z" value="0.0" />
 
  <property name="table_top_thickness" value="0.05"/>
 
  <property name="M_PI" value="3.1415926535897931" />
 
 
  <!-- tabletop height is .55+.01+.025=.585 -->
  <link name="table_top_link">
    <inertial>
      <mass value="1.0" />
      <origin xyz="${table_x} ${table_y} ${table_z+table_height-table_top_thickness/2}" />
      <inertia ixx="1" ixy="0"  ixz="0"
               iyy="1" iyz="0"
               izz="1" />
    </inertial> 
    <visual>
      <origin xyz="${table_x} ${table_y} ${table_z+table_height-table_top_thickness/2}" />
      <geometry>
        <box size="${table_width} ${table_depth} ${table_top_thickness}" />
      </geometry>
    </visual> 
    <collision>
      <origin xyz="${table_x} ${table_y} ${table_z+table_height-table_top_thickness/2}" />
      <geometry>
        <box size="${table_width} ${table_depth} ${table_top_thickness}" />
      </geometry>
    </collision>
  </link>
  <gazebo reference="table_top_link">
    <material>Gazebo/Wood</material>
    <mu1>50.0</mu1>
    <mu2>50.0</mu2>
    <kp>1000000.0</kp>
    <kd>1.0</kd>
  </gazebo>
 
  <joint name="leg1_joint" type="fixed" >
    <parent link="table_top_link" />
    <origin xyz="${table_x+table_width/2} ${table_y+table_depth/2} ${table_z+table_height}" rpy="0 0 0" />
    <child link="leg1_link" />
  </joint> 
  <link name="leg1_link">
    <inertial>
      <mass value="1.0" />
      <origin xyz="0 0 ${-table_height/2}" />
      <inertia ixx="0.1" ixy="0"  ixz="0"
               iyy="0.1" iyz="0"
               izz="0.01" />
    </inertial> 
    <visual>
      <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0 0 0" />
      <geometry>
        <cylinder radius="${leg_radius}" length="${table_height}" />
      </geometry>
    </visual> 
    <collision>
      <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0.0 0.0 0.0" />
      <geometry>
        <cylinder radius="${leg_radius}" length="${table_height}" />
      </geometry>
    </collision>
  </link>
  <gazebo reference="leg1_link">
    <material>Gazebo/Red</material>
    <mu1>1000.0</mu1>
    <mu2>1000.0</mu2>
    <kp>10000000.0</kp>
    <kd>1.0</kd>
    <selfCollide>true</selfCollide>
  </gazebo>
 
  <joint name="leg2_joint" type="fixed" >
    <parent link="table_top_link" />
    <origin xyz="${table_x-table_width/2} ${table_y+table_depth/2} ${table_z+table_height}" rpy="0 0 0" />
    <child link="leg2_link" />
  </joint> 
  <link name="leg2_link">
    <inertial>
      <mass value="1.0" />
      <origin xyz="0 0 ${-table_height/2}" />
      <inertia ixx="0.1" ixy="0"  ixz="0"
               iyy="0.1" iyz="0"
               izz="0.01" />
    </inertial> 
    <visual>
      <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0 0 0" />
      <geometry>
        <cylinder radius="${leg_radius}" length="${table_height}" />
      </geometry>
    </visual> 
    <collision>
      <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0.0 0.0 0.0" />
      <geometry>
        <cylinder radius="${leg_radius}" length="${table_height}" />
      </geometry>
    </collision>
  </link>
  <gazebo reference="leg2_link">
    <material>Gazebo/Red</material>
    <mu1>1000.0</mu1>
    <mu2>1000.0</mu2>
    <kp>10000000.0</kp>
    <kd>1.0</kd>
    <selfCollide>true</selfCollide>
  </gazebo>
 
  <joint name="leg3_joint" type="fixed" >
    <parent link="table_top_link" />
    <origin xyz="${table_x+table_width/2} ${table_y-table_depth/2} ${table_z+table_height}" rpy="0 0 0" />
    <child link="leg3_link" />
  </joint> 
  <link name="leg3_link">
    <inertial>
      <mass value="1.0" />
      <origin xyz="0 0 ${-table_height/2}" />
      <inertia ixx="0.1" ixy="0"  ixz="0"
               iyy="0.1" iyz="0"
               izz="0.01" />
    </inertial> 
    <visual>
      <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0 0 0" />
      <geometry>
        <cylinder radius="${leg_radius}" length="${table_height}" />
      </geometry>
    </visual> 
    <collision>
      <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0.0 0.0 0.0" />
      <geometry>
        <cylinder radius="${leg_radius}" length="${table_height}" />
      </geometry>
    </collision>
  </link>
  <gazebo reference="leg3_link">
    <material>Gazebo/Red</material>
    <mu1>1000.0</mu1>
    <mu2>1000.0</mu2>
    <kp>10000000.0</kp>
    <kd>1.0</kd>
    <selfCollide>true</selfCollide>
  </gazebo>
 
  <joint name="leg4_joint" type="fixed" >
    <parent link="table_top_link" />
    <origin xyz="${table_x-table_width/2} ${table_y-table_depth/2} ${table_z+table_height}" rpy="0 0 0" />
    <child link="leg4_link" />
  </joint> 
  <link name="leg4_link">
    <inertial>
      <mass value="1.0" />
      <origin xyz="0 0 ${-table_height/2}" />
      <inertia ixx="0.1" ixy="0"  ixz="0"
               iyy="0.1" iyz="0"
               izz="0.01" />
    </inertial> 
    <visual>
      <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0 0 0" />
      <geometry>
        <cylinder radius="${leg_radius}" length="${table_height}" />
      </geometry>
    </visual> 
    <collision>
      <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0.0 0.0 0.0" />
      <geometry>
        <cylinder radius="${leg_radius}" length="${table_height}" />
      </geometry>
    </collision>
  </link>
  <gazebo reference="leg4_link">
    <material>Gazebo/Red</material>
    <mu1>1000.0</mu1>
    <mu2>1000.0</mu2>
    <kp>10000000.0</kp>
    <kd>1.0</kd>
    <selfCollide>true</selfCollide>
  </gazebo>
  <gazebo>
    <static>true</static>
    <canonicalBody>table_top_link</canonicalBody>
  </gazebo>
 
 
</robot>

保存并关闭文件。

官方wiki一样ros版本的朋友你可以直接允许roslaunch table.launch命令了,然后跳过b)、c)两个小节,indigo版本的继续往下看
indigo版本运行roslaunch gazebo_ros table.launch文件你会发现出现这样的错误


那就对了,我们继续向下看。

b)修改table.launch文件
将table.launch的代码中的这句
<param name="table_description" command="$(find xacro)/xacro.py $(find gazebo_worlds)/objects/table.urdf.xacro" />

替换为

<param name="table_description" command="$(find xacro)/xacro.py $(find gazebo_ros)/objects/table.urdf.xacro" />
原因是因为我们使用的indigo版本对应的文件夹名称为gazebo_ros而非gazebo_worlds
 
保存退出,然后你发现运行roslaunch gazebo_ros table.launch
你会发现出现这样的错误:
关键错误出来了,ERROR:cannot launch node of type [gazebo/spawn_model]: gazebo
原来是在已经运行的节点名称不对,找不到这个gazebo名字啊
熟悉ros的朋友这会估计已经可以自己解决了,跳过c)也能自己正常做到了
好的,我们根据这个,找到我们前面打开的empty_worlds.launch这个文件
发现其中尾部有一句关键句为:
 <node name="gazebo" pkg="gazebo_ros" type="$(arg script_type)" respawn="false" output="screen"
	args="$(arg command_arg1) $(arg command_arg2) $(arg command_arg3) -e $(arg physics) $(arg extra_gazebo_args) $(arg world_name)" />
我们可以看到,pkg=“gazebo_ros”
而table.launch文件对应的为:
  <node name="spawn_table" pkg="gazebo" type="spawn_model" args="-urdf -param table_description -z 0.01 -model table_model" respawn="false" output="screen" />
发现不同,原来这里pkg="gazebo"

c)将pkg改正过来
我们再次编辑table.launch文件,将这段代码:
  <node name="spawn_table" pkg="gazebo" type="spawn_model" args="-urdf -param table_description -z 0.01 -model table_model" respawn="false" output="screen" />
替换为:
  <node name="spawn_table" pkg="gazebo_ros" type="spawn_model" args="-urdf -param table_description -z 0.01 -model table_model" respawn="false" output="screen" />
然后保存关闭。
 

那么ros indigo 版本下,正确的table.launch文件代码全文如下:

<launch>
  <!-- send table urdf to param server -->
  <param name="table_description" command="$(find xacro)/xacro.py $(find gazebo_ros)/objects/table.urdf.xacro" />

  <!-- push table_description to factory and spawn robot in gazebo -->
  <node name="spawn_table" pkg="gazebo_ros" type="spawn_model" args="-urdf -param table_description -z 0.01 -model table_model" respawn="false" output="screen" />
</launch>

终端中运行正确的launch文件

roslaunch gazebo_ros table.launch

你会发现没有报错,如果这个时候还报错,你就从头看一下,是否是哪个步骤没做好?
然后你会看到你的gazebo模拟器中,出现了如图所示的一张桌子:


恭喜你,你已经学会了如何通过roslaunch命令导入相关的模型了。

5)使用launch文件导入多个模型
为了方便,我们把这两个物体写到一个launch文件中,一次性运行可以导入两个;
首先,把最开始写的object.urdf复制到/opt/ros/indigo/share/gazebo_ros/objects路径下,然后到launch文件夹下建立all.launch文件

sudo gedit all.launch

将以下代码复制:

<launch>
  <!-- send table urdf to param server -->
  <param name="table_description" command="$(find xacro)/xacro.py $(find gazebo_ros)/objects/table.urdf.xacro" />
  <param name="box_description" textfile="$(find gazebo_ros)/objects/object.urdf" />

  <!-- push table_description to factory and spawn robot in gazebo -->
  <node name="spawn_table" pkg="gazebo_ros" type="spawn_model" args="-urdf -param table_description -z 0.01 -model table_model" respawn="false" output="screen" />
  <node name="spawn_box" pkg="gazebo_ros" type="spawn_model" args="-urdf -param box_description -z 0.01 -model box_model" respawn="false" output="screen" />
</launch>
保存并关闭
 
运行all.launch文件
roslaunch gazebo_ros all.launch

你就会看到你的两个物体先后生成到模拟器中,但要注意,如果你的模拟器中已经存在了这两个物体,那么不会重新添加,因为节点信息已经存在了。


(×)有一个问题,我自己也没法解决?
这个问题我自己尝试解决,试了很多也没有找到解决方法,希望有人可以指点一下我。
问题描述:
官方文档中最后有一步是添加一杯咖啡,但我失败了!
我在官方模型库中发现模型文件的类型除了urdf外,还有一个model类型,但这种类型我没法通过roslaunch进行添加,我在官方源里找到对应的的roslaunch文件,发现并没有办法运行,错误提示为:gazebo这种类型不适用。
不明所以,希望有才者可以解我疑惑。