一、使用ROS命令将新的对象加入到Gazebo模拟器中。
1)首先我们打开终端,roscore核心记得运行起来。
- roscore
ros wiki上是用的是以下命令
- roslaunch gazebo_worlds empty_world.launch
如果你是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
打开终端,输入以下命令
- roslaunch gazebo_ros 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
接着,我们使用命令,创建一个table.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>
使用roslaunch命令运行我们的launch文件
- rosluanch gazebo_ros table.launch
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文件你会发现出现这样的错误
![](https://i-blog.csdnimg.cn/blog_migrate/0337aec4653daee1a380a37eb79673d4.png)
那就对了,我们继续向下看。
b)修改table.launch文件
将table.launch的代码中的这句
替换为
原因是因为我们使用的indigo版本对应的文件夹名称为gazebo_ros而非gazebo_worlds
- <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" />
保存退出,然后你发现运行roslaunch gazebo_ros table.launch
你会发现出现这样的错误:
关键错误出来了,ERROR:cannot launch node of type [gazebo/spawn_model]: gazebo
![](https://i-blog.csdnimg.cn/blog_migrate/ecd9035f6f3f8f2befb47ccb97d78fe4.png)
关键错误出来了,ERROR:cannot launch node of type [gazebo/spawn_model]: gazebo
原来是在已经运行的节点名称不对,找不到这个gazebo名字啊
熟悉ros的朋友这会估计已经可以自己解决了,跳过c)也能自己正常做到了
好的,我们根据这个,找到我们前面打开的empty_worlds.launch这个文件
好的,我们根据这个,找到我们前面打开的empty_worlds.launch这个文件
发现其中尾部有一句关键句为:
- <node name="gazebo" pkg="gazebo_ros" type="$(arg script_type)" respawn="false" output="screen"
- rgs="$(arg command_arg1) $(arg command_arg2) $(arg command_arg3) -e $(arg physics) $(arg extra_gazebo_args) $(arg world_name)" />
而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" />
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" />
然后保存关闭。
- <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
然后你会看到你的gazebo模拟器中,出现了如图所示的一张桌子:
![](https://i-blog.csdnimg.cn/blog_migrate/eeeed7fb23e172526f3c62898603dbb1.png)
恭喜你,你已经学会了如何通过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这种类型不适用。
不明所以,希望有才者可以解我疑惑。