在之前的教程中,我们已经学习了如何在 Gazebo 仿真环境中创建机器人模型并让其动起来。然而,一个完整的仿真环境不仅需要机器人模型,还需要一个自定义的世界来让机器人进行交互。本篇教程将详细介绍如何使用 SDF(Simulation Description Format)创建自定义世界,并添加各种元素,如物理引擎、插件、GUI、光源和模型。

一、定义世界

1. SDF 文件的基本结构

每个 SDF 世界都以以下标签开始:

<?xml version="1.0" ?>
<sdf version="1.8">
    <world name="world_demo">
    ...
    ...
    </world>
</sdf>
  • <?xml version="1.0" ?>:定义 XML 的版本。
  • <sdf version="1.8">:定义 SDF 的版本,当前版本为 1.8。
  • <world name="world_demo">:定义世界的名称为 world_demo

二、物理引擎

1. 定义物理引擎

物理引擎是仿真环境中非常重要的一部分,它负责模拟物体的物理行为。在 SDF 中,可以通过 <physics> 标签来定义物理引擎:

<physics name="1ms" type="ignored">
    <max_step_size>0.001</max_step_size>
    <real_time_factor>1.0</real_time_factor>
</physics>
  • <physics>:定义物理引擎的标签。
  • name="1ms":物理引擎的名称,表示步长为 1 毫秒。
  • type="ignored":物理引擎的类型,当前设置为 ignored,表示不通过此标签选择物理引擎类型。
  • <max_step_size>:最大步长,值越小,计算越精确,但需要更多的计算资源。
  • <real_time_factor>:仿真时间与真实时间的比率,1.0 表示仿真时间与真实时间同步。

三、插件

1. 插件的作用

插件是 Gazebo 中用于扩展功能的动态加载代码块。以下是一些常用的插件:

1.1 物理插件
<plugin
    filename="gz-sim-physics-system"
    name="gz::sim::systems::Physics">
</plugin>
  • <plugin>:定义插件的标签。
  • filename="gz-sim-physics-system":插件的文件名。
  • name="gz::sim::systems::Physics":插件的名称。

物理插件用于模拟世界的动力学行为。

1.2 用户命令插件
<plugin
    filename="gz-sim-user-commands-system"
    name="gz::sim::systems::UserCommands">
</plugin>

用户命令插件负责创建模型、移动模型、删除模型等用户命令。

1.3 场景广播插件
<plugin
    filename="gz-sim-scene-broadcaster-system"
    name="gz::sim::systems::SceneBroadcaster">
</plugin>

场景广播插件用于显示世界场景。

四、GUI 配置

1. 定义 GUI

<gui> 标签下,我们可以定义与 Gazebo GUI 相关的内容:

<gui fullscreen="0">
    ...
    ...
</gui>
  • <gui>:定义 GUI 的标签。
  • fullscreen="0":是否全屏显示,0 表示不全屏。

2. 添加插件

2.1 3D 场景插件
<plugin filename="MinimalScene" name="3D View">
  <gz-gui>
    <title>3D View</title>
    <property type="bool" key="showTitleBar">false</property>
    <property type="string" key="state">docked</property>
  </gz-gui>

  <engine>ogre2</engine>
  <scene>scene</scene>
  <ambient_light>0.4 0.4 0.4</ambient_light>
  <background_color>0.8 0.8 0.8</background_color>
  <camera_pose>-6 0 6 0 0.5 0</camera_pose>
  <camera_clip>
    <near>0.25</near>
    <far>25000</far>
  </camera_clip>
</plugin>
  • <plugin>:定义插件的标签。
  • filename="MinimalScene":插件的文件名。
  • name="3D View":插件的名称。
  • <gz-gui>:定义插件的 GUI 属性。
  • <title>:插件的标题。
  • <property>:定义插件的属性。
  • <engine>:渲染引擎,可以选择 ogreogre2
  • <scene>:场景名称。
  • <ambient_light>:环境光颜色。
  • <background_color>:背景颜色。
  • <camera_pose>:相机位置和姿态。
  • <camera_clip>:相机裁剪平面。
2.2 场景管理插件
<plugin filename="GzSceneManager" name="Scene Manager">
  <gz-gui>
    <property key="resizable" type="bool">false</property>
    <property key="width" type="double">5</property>
    <property key="height" type="double">5</property>
    <property key="state" type="string">floating</property>
    <property key="showTitleBar" type="bool">false</property>
  </gz-gui>
</plugin>

场景管理插件用于管理场景的显示。

3. 世界控制插件

<plugin filename="WorldControl" name="World control">
    <gz-gui>
        <title>World control</title>
        <property type="bool" key="showTitleBar">false</property>
        <property type="bool" key="resizable">false</property>
        <property type="double" key="height">72</property>
        <property type="double" key="width">121</property>
        <property type="double" key="z">1</property>

        <property type="string" key="state">floating</property>
        <anchors target="3D View">
        <line own="left" target="left"/>
        <line own="bottom" target="bottom"/>
        </anchors>
    </gz-gui>

    <play_pause>true</play_pause>
    <step>true</step>
    <start_paused>true</start_paused>
    <service>/world/world_demo/control</service>
    <stats_topic>/world/world_demo/stats</stats_topic>
</plugin>
  • <play_pause>:是否显示播放/暂停按钮。
  • <step>:是否显示步进按钮。
  • <start_paused>:仿真开始时是否暂停。
  • <service>:服务名称。
  • <stats_topic>:统计信息话题。

4. 世界统计插件

<plugin filename="WorldStats" name="World stats">
    <gz-gui>
        <title>World stats</title>
        <property type="bool" key="showTitleBar">false</property>
        <property type="bool" key="resizable">false</property>
        <property type="double" key="height">110</property>
        <property type="double" key="width">290</property>
        <property type="double" key="z">1</property>

        <property type="string" key="state">floating</property>
        <anchors target="3D View">
        <line own="right" target="right"/>
        <line own="bottom" target="bottom"/>
        </anchors>
    </gz-gui>

    <sim_time>true</sim_time>
    <real_time>true</real_time>
    <real_time_factor>true</real_time_factor>
    <iterations>true</iterations>
    <topic>/world/world_demo/stats</topic>
</plugin>
  • <sim_time>:是否显示仿真时间。
  • <real_time>:是否显示真实时间。
  • <real_time_factor>:是否显示时间比率。
  • <iterations>:是否显示迭代次数。
  • <topic>:统计信息话题。

五、添加光源

1. 定义光源

在仿真环境中,光源是必不可少的。可以通过 <light> 标签来定义光源:

<light type="directional" name="sun">
    <cast_shadows>true</cast_shadows>
    <pose>0 0 10 0 0 0</pose>
    <diffuse>0.8 0.8 0.8 1</diffuse>
    <specular>0.2 0.2 0.2 1</specular>
    <attenuation>
        <range>1000</range>
        <constant>0.9</constant>
        <linear>0.01</linear>
        <quadratic>0.001</quadratic>
    </attenuation>
    <direction>-0.5 0.1 -0.9</direction>
</light>
  • <light>:定义光源的标签。
  • type="directional":光源类型,可以是 pointdirectionalspot
  • name="sun":光源名称。
  • <cast_shadows>:是否投射阴影。
  • <pose>:光源的位置和姿态。
  • <diffuse>:漫反射光颜色。
  • <specular>:镜面反射光颜色。
  • <attenuation>:光衰减属性。
  • <direction>:光源方向。

六、添加模型

1. 使用 Gazebo Fuel 模型

Gazebo Fuel 是一个模型库,可以从中下载各种模型。可以通过以下方式添加模型:

1.1 使用模型 URI
<include>
    <uri>https://fuel.gazebosim.org/1.0/OpenRobotics/models/Coke</uri>
</include>
  • <include>:包含模型的标签。
  • <uri>:模型的 URI。
1.2 下载模型

下载模型后,可以通过以下方式引用:

<include>
    <uri>model://Coke</uri>
</include>
  • <uri>model://Coke</uri>:引用本地模型。

2. 设置模型位置

可以通过 <pose> 标签设置模型的位置和姿态:

<include>
    <name>Coke0</name>
    <pose>0 0 0 0 0 0</pose>
    <uri>https://fuel.gazebosim.org/1.0/OpenRobotics/models/Coke</uri>
</include>
<include>
    <name>Coke1</name>
    <pose>0 0.1 0 0 0 0</pose>
    <uri>https://fuel.gazebosim.org/1.0/OpenRobotics/models/Coke</uri>
</include>
  • <name>:模型名称。
  • <pose>:模型的位置和姿态。

七、总结

通过本篇教程,我们学习了如何使用 SDF 创建自定义世界,包括定义物理引擎、插件、GUI、光源和模型。这些技能将为我们后续的机器人仿真开发提供坚实的基础。

参考文献

Gazebo Tutorials

Logo

立足具身智能前沿赛道,致力于搭建全球化、开源化、全栈式技术交流与实践共创平台。

更多推荐