gazebo教程---使用roslaunch来启动gazebo,加载models

1.使用roslaunch加载一个世界模型

roslaunch gazebo_ros willowgarage_world.launch

运行效果如图:
在这里插入图片描述
下面看一下willowgarage_world.launch里的内容:

<?xml version="1.0"?>
<launch>

  <!-- We resume the logic in empty_world.launch, changing only the name of the world to be launched -->
  <include file="$(find gazebo_ros)/launch/empty_world.launch">
    <arg name="world_name" value="worlds/willowgarage.world"/> <!-- Note: the world_name is with respect to GAZEBO_RESOURCE_PATH environmental variable -->
    <arg name="paused" value="false"/>
    <arg name="use_sim_time" value="true"/>
    <arg name="gui" value="true"/>
    <arg name="headless" value="false"/> <!-- Inert - see gazebo_ros_pkgs issue #491 -->
    <arg name="recording" value="false"/>
    <arg name="debug" value="false"/>
  </include>

</launch>

里面又加载一个empty_world.launch,并传入一些参数,主要是world_name,使得empty_world.launch加载willowgarage.world。empty_world.launch里的内容是:

<?xml version="1.0"?>
<launch>

  <!-- these are the arguments you can pass this launch file, for example paused:=true -->
  <arg name="paused" default="false"/>
  <arg name="use_sim_time" default="true"/>
  <arg name="extra_gazebo_args" default=""/>
  <arg name="gui" default="true"/>
  <arg name="recording" default="false"/>
  <!-- Note that 'headless' is currently non-functional.  See gazebo_ros_pkgs issue #491 (-r arg does not disable
       rendering, but instead enables recording). The arg definition has been left here to prevent breaking downstream
       launch files, but it does nothing. -->
  <arg name="headless" default="false"/>
  <arg name="debug" default="false"/>
  <arg name="physics" default="ode"/>
  <arg name="verbose" default="false"/>
  <arg name="output" default="screen"/>
  <arg name="world_name" default="worlds/empty.world"/> <!-- Note: the world_name is with respect to GAZEBO_RESOURCE_PATH environmental variable -->
  <arg name="respawn_gazebo" default="false"/>
  <arg name="use_clock_frequency" default="false"/>
  <arg name="pub_clock_frequency" default="100"/>
  <arg name="enable_ros_network" default="true" />
  <arg name="server_required" default="false"/>
  <arg name="gui_required" default="false"/>

  <!-- set use_sim_time flag -->
  <param name="/use_sim_time" value="$(arg use_sim_time)"/>

  <!-- set command arguments -->
  <arg unless="$(arg paused)" name="command_arg1" value=""/>
  <arg     if="$(arg paused)" name="command_arg1" value="-u"/>
  <arg unless="$(arg recording)" name="command_arg2" value=""/>
  <arg     if="$(arg recording)" name="command_arg2" value="-r"/>
  <arg unless="$(arg verbose)" name="command_arg3" value=""/>
  <arg     if="$(arg verbose)" name="command_arg3" value="--verbose"/>
  <arg unless="$(arg debug)" name="script_type" value="gzserver"/>
  <arg     if="$(arg debug)" name="script_type" value="debug"/>

  <!-- start gazebo server-->
  <group if="$(arg use_clock_frequency)">
    <param name="gazebo/pub_clock_frequency" value="$(arg pub_clock_frequency)" />
  </group>
  <group>
    <param name="gazebo/enable_ros_network" value="$(arg enable_ros_network)" />
  </group>
  <node name="gazebo" pkg="gazebo_ros" type="$(arg script_type)" respawn="$(arg respawn_gazebo)" output="$(arg output)"
	args="$(arg command_arg1) $(arg command_arg2) $(arg command_arg3) -e $(arg physics) $(arg extra_gazebo_args) $(arg world_name)"
  required="$(arg server_required)" />

  <!-- start gazebo client -->
  <group if="$(arg gui)">
    <node name="gazebo_gui" pkg="gazebo_ros" type="gzclient" respawn="false" output="$(arg output)" args="$(arg command_arg3)"
    required="$(arg gui_required)"/>
  </group>

</launch>

主要是启动两个节点,一个是gzserver,另一个是gzclient。这里的两个节点启动是利用条件判断:(1)gzserver启动的条件是debug为false(这个参数值由willowgarage_world.launch传入,debug为false);
(2)gzclient的启动条件是gui为true(这个参数值由willowgarage_world.launch传入,gui为true)。
2. 方法一:往gazebo中加载model
(1)使用一个python脚本spawn_model向gazebo_ros(在rostopic的命名空间简称gazebo)请求往gazebo中添加一个自定义的URDF,而这个脚本是放在gazebo_ros功能包。可以运行如下命令来使用这个脚本:

rosrun gazebo_ros spawn_model -file `rospack find MYROBOT_description`/urdf/MYROBOT.urdf -urdf -x 0 -y 0 -z 1 -model MYROBOT

其中上述命令对应的文件结构为:

../catkin_ws/src
    /MYROBOT_description
        package.xml
        CMakeLists.txt
        /urdf
            MYROBOT.urdf
        /meshes
            mesh1.dae
            mesh2.dae
            ...
        /materials
        /cad
    /MYROBOT_gazebo
        /launch
            MYROBOT.launch
        /worlds
            MYROBOT.world
        /models
            world_object1.dae
            world_object2.stl
            world_object3.urdf
        /materials
        /plugins

在我的工作空间中,运行如下命令加载一个汽车模型,前提是已经在gazebo中加载一个世界(如上面的使用roslaunch加载的世界):

rosrun gazebo_ros spawn_model -file `rospack find simulation`/urdf/car/prius.urdf -urdf -x 0 -y 0 -z 1 -model prius

运行结果如下图:
在这里插入图片描述
也可以将上述命令加入到launch文件中,以上述文件结构为例,在MYROBOT_gazebo/launch/YOUROBOT.launch中,添加如下内容:

<!-- Spawn a robot into Gazebo -->
<node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" args="-file $(find baxter_description)/urdf/baxter.urdf -urdf -z 1 -model baxter" />

而在我的工作文件结构中,可以往willowgarage_world.launch添加一个小车,具体内容如下:

<?xml version="1.0"?>
<launch>

  <!-- We resume the logic in empty_world.launch, changing only the name of the world to be launched -->
  <include file="$(find gazebo_ros)/launch/empty_world.launch">
    <arg name="world_name" value="worlds/willowgarage.world"/> <!-- Note: the world_name is with respect to GAZEBO_RESOURCE_PATH environmental variable -->
    <arg name="paused" value="false"/>
    <arg name="use_sim_time" value="true"/>
    <arg name="gui" value="true"/>
    <arg name="headless" value="false"/> <!-- Inert - see gazebo_ros_pkgs issue #491 -->
    <arg name="recording" value="false"/>
    <arg name="debug" value="false"/>
  </include>
  <!-- Spawn a robot into Gazebo -->
  <node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" args="-file $(find simulation)/urdf/car/prius.urdf -urdf -z 0 -model prius" />
  <!--name:用户自定义;-urdf:加载的是urdf文件,-model prius:加载模型的名字;-->
</launch>

(2)方法二:将用户模型导入到gazebo的模型数据库中
该方法需要设置环境变量,不利于跨平台,也就是说当把你的功能包移植到其他电脑时,还需要在其他电脑配置环境变量。具体可以参考gazebo官方教程

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值