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命令打开,因为要开一个节点:
roslaunch gazebo_ros empty_world.launch
接下来使用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
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
rosrun gazebo_ros spawn_model -file `pwd`/object.urdf -urdf -z 1 -model my_object
roslaunch gazebo_ros table.launch
有的朋友说,我没有找到gazebo_ros 的launch文件夹下有这个table.launch文件,当然没有,这个是要自己写的,官方知识让你看看,具体的要自己动手。cd /opt/ros/indigo/share/gazebo_ros
sudo mkdir objects
cd /opt/ros/indigo/share/gazebo_ros/launch
sudo gedit table.launch
<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>
rosluanch gazebo_ros table.launch
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版本的继续往下看
那就对了,我们继续向下看。
b)修改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关键错误出来了,ERROR:cannot launch node of type [gazebo/spawn_model]: gazebo
好的,我们根据这个,找到我们前面打开的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” <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改正过来
<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" />
<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>
roslaunch gazebo_ros table.launch
恭喜你,你已经学会了如何通过roslaunch命令导入相关的模型了。
5)使用launch文件导入多个模型
为了方便,我们把这两个物体写到一个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>
保存并关闭roslaunch gazebo_ros all.launch
你就会看到你的两个物体先后生成到模拟器中,但要注意,如果你的模拟器中已经存在了这两个物体,那么不会重新添加,因为节点信息已经存在了。来源:CSDN
作者:博瓦
链接:https://blog.csdn.net/u010925447/article/details/69821609