Gazebo 加载xacro文件和URDF文件的方式

博客介绍了打开Gazebo的两种方式:rosrun和roslaunch,包括启动ROS节点的命令及参数,还提及了demo环境。此外,说明了创建Gazebo ROS Package的步骤,以及将URDF模型添加到gazebo环境的两种方法:ROS Service Call Spawn Method和Model Database Method。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 版权声明:

<--本博客所有内容均为自己在学习工作中的总结、摘录等-- --转载请注明出处-- --如有侵权请联系删除-->  https://blog.youkuaiyun.com/xuehuafeiwu123/article/details/71108959


打开Gazebo的方式主要有两种:rosrun 和 roslaunch。

1、启动ROS节点
      启动ROS节点、bring up 机器人的标准工具是roslaunch。打开一个空的Gazebo世界命令如下:

     roslaunch gazebo_ros empty_world.launch
参数:

  • paused 
  • 在暂停状态打开Gazebo (default false)
  • use_sim_time 
  • 节点启动模拟时间,启动主题topic /clock (default true)
  • gui 
  • 启动Gazebo用户接口 (default true)
  • headless 
  • 禁止仿真器调用任何渲染组件。不能在gui:=true (default false)是使用
  • debug 
  • 用gdb (default false)调试模式启动 gzserver (Gazebo Server)

例如:

roslaunch gazebo_ros empty_world.launch paused:=true use_sim_time:=false gui:=true throttled:=false headless:=false debug:=true

2、demo 环境
还有一些demo可以直接使用,它们在包含在gazebo_ros功能包中,包括:

roslaunch gazebo_ros mud_world.launch
roslaunch gazebo_ros shapes_world.launch
roslaunch gazebo_ros rubble_world.launch

注意在mud_world.launch中,加入了一个机械关节,文件如下:

  <!-- We resume the logic in empty_world.launch, changing only the name of the world to be launched -->
<launch>
  <include file="$(find gazebo_ros)/launch/empty_world.launch">
       <arg name="world_name" value="worlds/mud.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"/>
       <arg name="debug" value="false"/>
  </include>
</launch>

所有的文件都继承了empty.world,只改变了一个参数:world_name。

3、创建Gazebo ROS Package
    1、建立工作空间

              /home/user/catkin_ws/src
    2、编写sdf文件

文件夹目录如下:

../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

      3、创建通用的 World 文件

创建ROS功能包

创建launch文件夹

创建 YOUROBOT.launch 文件

<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="$(find MYROBOT_gazebo)/worlds/MYROBOT.world"/>
    <!-- more default parameters can be changed here -->
  </include>
</launch>

创建MYROBOT.world 文件

<?xml version="1.0" ?>
<sdf version="1.4">
  <world name="default">
    <include>
      <uri>model://ground_plane</uri>
    </include>
    <include>
      <uri>model://sun</uri>
    </include>
    <include>
      <uri>model://gas_station</uri>
      <name>gas_station</name>
      <pose>-2.0 7.0 0 0 0 0</pose>
    </include>
  </world>
</sdf>

运行文件

. ~/catkin_ws/devel/setup.bash
roslaunch MYROBOT_gazebo MYROBOT.launch

        4、加载 URDF 模型
如何把自己建立的URDF模型添加到gazebo环境中?

利用 roslaunch,有两种方法:

ROS Service Call Spawn Method
这种方法保持ROS软件包在不同电脑间轻松移植。只要软件包在一个ROS包路径中即可。不过要求编写一个ROS service call脚本。

  • Model Database Method

只要放置在默认的数据库路径中,或者自己建立一个数据库,并把它包含在环境变量中

1、ROS Service Call

利用 pawn_model脚本向 gazebo_ros 节点(在主题中,空间名为”gazebo” )发出服务请求,进而添加 URDF 到 Gazebo 中。

urdf

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

如果是.xacro,可以转化为 urdf 文件:

xacro

rosrun xacro xacro.py `rospack find pr2_description`/robots/pr2.urdf.xacro -o /tmp/pr2.urdf

查看其关节图:

check_urdf pr2.urdf
urdf_to_graphiz pr2.urdf
cd ~
rosrun tf view_frames
evince frames.pdf

查看 pawn_model的所有参数,可以运行:

rosrun gazebo_ros spawn_model -h
  • 在 launch文件中,可以编写为形如:

urdf

<!-- 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" />

xacro

<!-- Convert an xacro and put on parameter server -->
<param name="robot_description" command="$(find xacro)/xacro.py $(find pr2_description)/robots/pr2.urdf.xacro" />

<!-- Spawn a robot into Gazebo -->
<node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" args="-param robot_description -urdf -model pr2" />
  • Model Database

    此种方法需要编辑 sdf 文件,官方的模型都是采用的这种方法。

参考
https://bitbucket.org/osrf/gazebo_models/downloads/ 
http://gazebosim.org 
http://wiki.ros.org/urdf
--------------------- 
作者:飘零过客 
来源:优快云 
原文:https://blog.youkuaiyun.com/xuehuafeiwu123/article/details/71108959 
版权声明:本文为博主原创文章,转载请附上博文链接!

要使用`roslaunch`启动Gazebo,您需要先编写一个`.launch`文件,其中包含启动Gazebo所需的所有参数节点。 以下是一个简单的例子: ```xml <launch> <arg name="world_file" default="$(find my_robot_description)/worlds/my_world.world"/> <arg name="robot_file" default="$(find my_robot_description)/urdf/my_robot.urdf"/> <param name="robot_description" command="$(find xacro)/xacro --inorder $(arg robot_file)"/> <param name="use_sim_time" value="true"/> <node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" args="-urdf -model my_robot -param robot_description -x 0 -y 0 -z 0"/> <node name="gazebo" pkg="gazebo_ros" type="gazebo" args="$(arg world_file)"/> </launch> ``` 在这个例子中,我们通过`<arg>`标签定义了两个参数:`world_file``robot_file`。这些参数的默认值是我们机器人描述包中的世界文件URDF文件。 我们还定义了一个名为`robot_description`的ROS参数,其中包含了我们机器人的URDF描述。我们使用`$(find xacro)/xacro --inorder`命令将URDF文件中的xacro宏展开,并将结果作为参数值。 我们还设置了`use_sim_time`参数为`true`,以便Gazebo可以使用ROS仿真时间。 最后,我们启动了两个节点。第一个节点使用`gazebo_ros`包中的`spawn_model`命令将我们的机器人URDF模型加载Gazebo中。第二个节点启动Gazebo仿真环境,并将`world_file`参数传递给它。 将上面的代码保存为`my_gazebo.launch`文件,然后在终端窗口中输入以下命令启动它: ``` roslaunch my_robot_description my_gazebo.launch ``` 这将启动Gazebo仿真环境,并将您的机器人加载到其中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值