Gazebo仿真-2

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

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

[plain]  view plain  copy
  1. roscore  


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

ros wiki上是用的是以下命令

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

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

[plain]  view plain  copy
  1. roslaunch gazebo_ros empty_world.launch  
打开了一个新的Gazebo界面。

3)将上次写的蓝色盒子的urdf文件导入进去,如果你不明白这里的话,请看我上一篇博客。
[plain]  view plain  copy
  1. rosrun gazebo_ros spawn_model -file `pwd`/object.urdf -urdf -z 1 -model my_object  

4)使用roslaunch命令为模拟器中添加一张桌子
打开终端,输入以下命令
[plain]  view plain  copy
  1. roslaunch gazebo_ros table.launch  
有的朋友说,我没有找到gazebo_ros 的launch文件夹下有这个table.launch文件,当然没有,这个是要自己写的,官方知识让你看看,具体的要自己动手。

首先,使用终端,进入到gazebo_ros文件夹下。
[plain]  view plain  copy
  1. cd /opt/ros/indigo/share/gazebo_ros  

建立一个objects文件夹,这里主要用来储存后续的模型文件
[plain]  view plain  copy
  1. sudo mkdir objects  

然后,使用终端,进入到gazebo_ros下launch文件夹下。
[plain]  view plain  copy
  1. cd /opt/ros/indigo/share/gazebo_ros/launch  

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

接着,我们使用命令,创建一个table.launch文件
[plain]  view plain  copy
  1. sudo gedit table.launch  

将以下代码复制进去(这段代码是官方提供的,Indigo版本不适用,后面我会讲到如何改过来)
[plain]  view plain  copy
  1. <launch>  
  2.   <!-- send table urdf to param server -->  
  3.   <param name="table_description" command="$(find xacro)/xacro.py $(find gazebo_worlds)/objects/table.urdf.xacro" />  
  4.   
  5.   <!-- push table_description to factory and spawn robot in gazebo -->  
  6.   <node name="spawn_table" pkg="gazebo" type="spawn_model" args="-urdf -param table_description -z 0.01 -model table_model" respawn="false" output="screen" />  
  7. </launch>  

保存并关闭文件。
使用roslaunch命令运行我们的launch文件
[plain]  view plain  copy
  1. rosluanch gazebo_ros table.launch  

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

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

不怕麻烦的,将以下代码填入这个文件中:
[html]  view plain  copy
  1. <?xml version="1.0"?>  
  2. <robot name="table"  
  3.        xmlns:xi="http://www.w3.org/2001/XInclude"  
  4.        xmlns:gazebo="http://playerstage.sourceforge.net/gazebo/xmlschema/#gz"  
  5.        xmlns:model="http://playerstage.sourceforge.net/gazebo/xmlschema/#model"  
  6.        xmlns:sensor="http://playerstage.sourceforge.net/gazebo/xmlschema/#sensor"  
  7.        xmlns:body="http://playerstage.sourceforge.net/gazebo/xmlschema/#body"  
  8.        xmlns:geom="http://playerstage.sourceforge.net/gazebo/xmlschema/#geom"  
  9.        xmlns:joint="http://playerstage.sourceforge.net/gazebo/xmlschema/#joint"  
  10.        xmlns:interface="http://playerstage.sourceforge.net/gazebo/xmlschema/#interface"  
  11.        xmlns:rendering="http://playerstage.sourceforge.net/gazebo/xmlschema/#rendering"  
  12.        xmlns:renderable="http://playerstage.sourceforge.net/gazebo/xmlschema/#renderable"  
  13.        xmlns:controller="http://playerstage.sourceforge.net/gazebo/xmlschema/#controller"  
  14.        xmlns:physics="http://playerstage.sourceforge.net/gazebo/xmlschema/#physics">  
  15.   
  16.   <property name="table_height" value="0.55" />  
  17.   <property name="table_width" value="1.0" />  
  18.   <property name="table_depth" value="2.0" />  
  19.   <property name="leg_radius" value="0.02" />  
  20.   <property name="table_x" value="0.98" />  
  21.   <property name="table_y" value="0.0" />  
  22.   <property name="table_z" value="0.0" />  
  23.   
  24.   <property name="table_top_thickness" value="0.05"/>  
  25.   
  26.   <property name="M_PI" value="3.1415926535897931" />  
  27.   
  28.   
  29.   <!-- tabletop height is .55+.01+.025=.585 -->  
  30.   <link name="table_top_link">  
  31.     <inertial>  
  32.       <mass value="1.0" />  
  33.       <origin xyz="${table_x} ${table_y} ${table_z+table_height-table_top_thickness/2}" />  
  34.       <inertia ixx="1" ixy="0"  ixz="0"  
  35.                iyy="1" iyz="0"  
  36.                izz="1" />  
  37.     </inertial>   
  38.     <visual>  
  39.       <origin xyz="${table_x} ${table_y} ${table_z+table_height-table_top_thickness/2}" />  
  40.       <geometry>  
  41.         <box size="${table_width} ${table_depth} ${table_top_thickness}" />  
  42.       </geometry>  
  43.     </visual>   
  44.     <collision>  
  45.       <origin xyz="${table_x} ${table_y} ${table_z+table_height-table_top_thickness/2}" />  
  46.       <geometry>  
  47.         <box size="${table_width} ${table_depth} ${table_top_thickness}" />  
  48.       </geometry>  
  49.     </collision>  
  50.   </link>  
  51.   <gazebo reference="table_top_link">  
  52.     <material>Gazebo/Wood</material>  
  53.     <mu1>50.0</mu1>  
  54.     <mu2>50.0</mu2>  
  55.     <kp>1000000.0</kp>  
  56.     <kd>1.0</kd>  
  57.   </gazebo>  
  58.   
  59.   <joint name="leg1_joint" type="fixed" >  
  60.     <parent link="table_top_link" />  
  61.     <origin xyz="${table_x+table_width/2} ${table_y+table_depth/2} ${table_z+table_height}" rpy="0 0 0" />  
  62.     <child link="leg1_link" />  
  63.   </joint>   
  64.   <link name="leg1_link">  
  65.     <inertial>  
  66.       <mass value="1.0" />  
  67.       <origin xyz="0 0 ${-table_height/2}" />  
  68.       <inertia ixx="0.1" ixy="0"  ixz="0"  
  69.                iyy="0.1" iyz="0"  
  70.                izz="0.01" />  
  71.     </inertial>   
  72.     <visual>  
  73.       <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0 0 0" />  
  74.       <geometry>  
  75.         <cylinder radius="${leg_radius}" length="${table_height}" />  
  76.       </geometry>  
  77.     </visual>   
  78.     <collision>  
  79.       <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0.0 0.0 0.0" />  
  80.       <geometry>  
  81.         <cylinder radius="${leg_radius}" length="${table_height}" />  
  82.       </geometry>  
  83.     </collision>  
  84.   </link>  
  85.   <gazebo reference="leg1_link">  
  86.     <material>Gazebo/Red</material>  
  87.     <mu1>1000.0</mu1>  
  88.     <mu2>1000.0</mu2>  
  89.     <kp>10000000.0</kp>  
  90.     <kd>1.0</kd>  
  91.     <selfCollide>true</selfCollide>  
  92.   </gazebo>  
  93.   
  94.   <joint name="leg2_joint" type="fixed" >  
  95.     <parent link="table_top_link" />  
  96.     <origin xyz="${table_x-table_width/2} ${table_y+table_depth/2} ${table_z+table_height}" rpy="0 0 0" />  
  97.     <child link="leg2_link" />  
  98.   </joint>   
  99.   <link name="leg2_link">  
  100.     <inertial>  
  101.       <mass value="1.0" />  
  102.       <origin xyz="0 0 ${-table_height/2}" />  
  103.       <inertia ixx="0.1" ixy="0"  ixz="0"  
  104.                iyy="0.1" iyz="0"  
  105.                izz="0.01" />  
  106.     </inertial>   
  107.     <visual>  
  108.       <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0 0 0" />  
  109.       <geometry>  
  110.         <cylinder radius="${leg_radius}" length="${table_height}" />  
  111.       </geometry>  
  112.     </visual>   
  113.     <collision>  
  114.       <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0.0 0.0 0.0" />  
  115.       <geometry>  
  116.         <cylinder radius="${leg_radius}" length="${table_height}" />  
  117.       </geometry>  
  118.     </collision>  
  119.   </link>  
  120.   <gazebo reference="leg2_link">  
  121.     <material>Gazebo/Red</material>  
  122.     <mu1>1000.0</mu1>  
  123.     <mu2>1000.0</mu2>  
  124.     <kp>10000000.0</kp>  
  125.     <kd>1.0</kd>  
  126.     <selfCollide>true</selfCollide>  
  127.   </gazebo>  
  128.   
  129.   <joint name="leg3_joint" type="fixed" >  
  130.     <parent link="table_top_link" />  
  131.     <origin xyz="${table_x+table_width/2} ${table_y-table_depth/2} ${table_z+table_height}" rpy="0 0 0" />  
  132.     <child link="leg3_link" />  
  133.   </joint>   
  134.   <link name="leg3_link">  
  135.     <inertial>  
  136.       <mass value="1.0" />  
  137.       <origin xyz="0 0 ${-table_height/2}" />  
  138.       <inertia ixx="0.1" ixy="0"  ixz="0"  
  139.                iyy="0.1" iyz="0"  
  140.                izz="0.01" />  
  141.     </inertial>   
  142.     <visual>  
  143.       <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0 0 0" />  
  144.       <geometry>  
  145.         <cylinder radius="${leg_radius}" length="${table_height}" />  
  146.       </geometry>  
  147.     </visual>   
  148.     <collision>  
  149.       <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0.0 0.0 0.0" />  
  150.       <geometry>  
  151.         <cylinder radius="${leg_radius}" length="${table_height}" />  
  152.       </geometry>  
  153.     </collision>  
  154.   </link>  
  155.   <gazebo reference="leg3_link">  
  156.     <material>Gazebo/Red</material>  
  157.     <mu1>1000.0</mu1>  
  158.     <mu2>1000.0</mu2>  
  159.     <kp>10000000.0</kp>  
  160.     <kd>1.0</kd>  
  161.     <selfCollide>true</selfCollide>  
  162.   </gazebo>  
  163.   
  164.   <joint name="leg4_joint" type="fixed" >  
  165.     <parent link="table_top_link" />  
  166.     <origin xyz="${table_x-table_width/2} ${table_y-table_depth/2} ${table_z+table_height}" rpy="0 0 0" />  
  167.     <child link="leg4_link" />  
  168.   </joint>   
  169.   <link name="leg4_link">  
  170.     <inertial>  
  171.       <mass value="1.0" />  
  172.       <origin xyz="0 0 ${-table_height/2}" />  
  173.       <inertia ixx="0.1" ixy="0"  ixz="0"  
  174.                iyy="0.1" iyz="0"  
  175.                izz="0.01" />  
  176.     </inertial>   
  177.     <visual>  
  178.       <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0 0 0" />  
  179.       <geometry>  
  180.         <cylinder radius="${leg_radius}" length="${table_height}" />  
  181.       </geometry>  
  182.     </visual>   
  183.     <collision>  
  184.       <origin xyz="0.0 0.0 ${-table_height/2}" rpy="0.0 0.0 0.0" />  
  185.       <geometry>  
  186.         <cylinder radius="${leg_radius}" length="${table_height}" />  
  187.       </geometry>  
  188.     </collision>  
  189.   </link>  
  190.   <gazebo reference="leg4_link">  
  191.     <material>Gazebo/Red</material>  
  192.     <mu1>1000.0</mu1>  
  193.     <mu2>1000.0</mu2>  
  194.     <kp>10000000.0</kp>  
  195.     <kd>1.0</kd>  
  196.     <selfCollide>true</selfCollide>  
  197.   </gazebo>  
  198.   <gazebo>  
  199.     <static>true</static>  
  200.     <canonicalBody>table_top_link</canonicalBody>  
  201.   </gazebo>  
  202.   
  203.   
  204. </robot>  
保存并关闭文件。

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

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

b)修改table.launch文件
将table.launch的代码中的这句
[plain]  view plain  copy
  1. <param name="table_description" command="$(find xacro)/xacro.py $(find gazebo_worlds)/objects/table.urdf.xacro" />  
替换为
[plain]  view plain  copy
  1. <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这个文件
发现其中尾部有一句关键句为:
[plain]  view plain  copy
  1. <node name="gazebo" pkg="gazebo_ros" type="$(arg script_type)" respawn="false" output="screen"  
  2. rgs="$(arg command_arg1) $(arg command_arg2) $(arg command_arg3) -e $(arg physics) $(arg extra_gazebo_args) $(arg world_name)" />  
我们可以看到,pkg=“gazebo_ros”
而table.launch文件对应的为:
[plain]  view plain  copy
  1. <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文件,将这段代码:
[plain]  view plain  copy
  1. <node name="spawn_table" pkg="gazebo" type="spawn_model" args="-urdf -param table_description -z 0.01 -model table_model" respawn="false" output="screen" />  
替换为:
[plain]  view plain  copy
  1. <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文件代码全文如下:
[plain]  view plain  copy
  1. <launch>  
  2.   <!-- send table urdf to param server -->  
  3.   <param name="table_description" command="$(find xacro)/xacro.py $(find gazebo_ros)/objects/table.urdf.xacro" />  
  4.   
  5.   <!-- push table_description to factory and spawn robot in gazebo -->  
  6.   <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" />  
  7. </launch>  

终端中运行正确的launch文件
[plain]  view plain  copy
  1. roslaunch gazebo_ros table.launch  

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

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

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

将以下代码复制:
[plain]  view plain  copy
  1. <launch>  
  2.   <!-- send table urdf to param server -->  
  3.   <param name="table_description" command="$(find xacro)/xacro.py $(find gazebo_ros)/objects/table.urdf.xacro" />  
  4.   <param name="box_description" textfile="$(find gazebo_ros)/objects/object.urdf" />  
  5.   
  6.   <!-- push table_description to factory and spawn robot in gazebo -->  
  7.   <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" />  
  8.   <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" />  
  9. </launch>  
保存并关闭

运行all.launch文件
[plain]  view plain  copy
  1. roslaunch gazebo_ros all.launch  
你就会看到你的两个物体先后生成到模拟器中,但要注意,如果你的模拟器中已经存在了这两个物体,那么不会重新添加,因为节点信息已经存在了。


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

猜你喜欢

转载自blog.csdn.net/weixin_41015581/article/details/80038159