ROS_Gazebo仿真

ROS  indigo 默认安装了Gazebo2可以用( locate 文件名 查看 )

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

1、在终端中启动Gazebo:

gazebo

如图为启动后的窗口界面:

 

2、Gazebo简单认识

使用ros命令打开的一个empty world 的gazebo 和我直接用gazebo命令打开的软件是没有区别的,因为都是空的内容里面。

但是实际使用是有区别的,使用roslaunch命令 打开的gazebo实际上是包含了一个ros通讯节点,这样就可以使用其他的ros指令将gazebo和你自己编写的文件联系起来。

着急学习的朋友建议直接上官网,里面有详细教程。

a)界面认识

 

这个空的世界中,只有一个大地模型和光源效果。

b)添加一些东西上去


如图,添加了两个球体和一个圆柱以及一个正方体。

 

c)基本工具介绍

移动工具

 

旋转工具


缩放工具

 

 


或者使用ctrl+B来进行地图编辑

 

3、接下来按照ROS的教程走

a)写一个urdf的模型文件

使用终端,建立并编辑urdf文件,我推荐gedit方便

gedit object.urdf


写入代码:

    <robot name="simple_box">
      <link name="my_box">
        <inertial>
          <origin xyz="2 0 0" />
          <mass value="1.0" />
          <inertia  ixx="1.0" ixy="0.0"  ixz="0.0"  iyy="100.0"  iyz="0.0"  izz="1.0" />
        </inertial>
        <visual>
          <origin xyz="2 0 1"/>
          <geometry>
            <box size="1 1 2" />
          </geometry>
        </visual>
        <collision>
          <origin xyz="2 0 1"/>
          <geometry>
            <box size="1 1 2" />
          </geometry>
        </collision>
      </link>
      <gazebo reference="my_box">
        <material>Gazebo/Blue</material>
      </gazebo>
    </robot>

保存关闭文本编辑器。

其中:

原文解释在这:

Here the origins of the inertial center, visual and collision geometry centers are offset in +x by 2m relative to the model origin. The box geometry primitive is used here for visual and collision geometry, and has sizes 1m wide, 1m deep and 2m tall. Note that visual and collision geometries do not always have to be the same, sometimes you want to use a simpler collision geometry to save collision detection time when running a dynamic simulation. The box inertia is defined as 1kg mass and principal moments of inertia ixx=izz=1 kg*m2 and iyy=100 kg*m2.

大意就是表达上面的urdf文件,构建了一个长1m宽1m高2m的盒子对应box size = 1 1 2,并且最后给予了惯性要求,对应ixx = 1 iyy = 100,其他的标签意思有空再去查一下,看翻译虽然有个大概理解,但是具体的区别大家自己尝试一下吧,但注意一下,gezebo reference = my_box 这个标签,对应的是导入gazebo中的模型名称。

接下来我们打开终端,使用命令行将这个文件导入到gazebo。


首先使用终端打开gazebo,这次使用roslaunch命令打开,因为要开一个节点:

<span style="font-size:14px;">roslaunch gazebo_ros empty_world.launch</span>


接下来使用rosrun命令将前面写的urdf文件放进去(记得先用终端cd到你放置urdf文件的路径下):

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


运行结束之后,会发现你的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

将以下代码填入这个文件中:

    <?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

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

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值