Gazebo (Robot Simulator)

Table of Contents

1. Gazebo 简介

Gazebo 是一个机器人模拟软件,手头没有真实机器人时,可用它在计算机中模拟机器人。

参考:
Gazebo 官网:http://gazebosim.org/
Gazebo 基本架构介绍:http://gazebosim.org/tutorials?tut=architecture&cat=get_started

1.1. SDF (Simulation Description Format)

Gazebo 中用 Simulation Description Format(SDF)文件来描述机器人及其环境。后文介绍的 World Files 和 Model Files 都是 SDF 格式的文件。

SDF 文件的规范可参考:http://sdformat.org/spec

2. Gazebo 基本使用

下面的测试环境为 Ubuntu 16.04 和 Gazebo 7。

安装完 Gazebo 后,用命令 gazebo 可启动 Gazebo。

$ gazebo

正常启动后,会出现如图 1 所示界面。

robot_gazebo.png

Figure 1: Gazebo

说明 1: gazebo 命令实际上会启动程序 gzserver (模拟机器人的核心程序)和 gzclient (提供图形界面,方便对模拟过程进行查看和交互)。
说明 2:如果看不到如图 1 所示界面,很可能是没有找到两个基本的 Model 文件(ground_plan 和 sun),可以手动下载它们:

$ wget http://models.gazebosim.org/ground_plane/model.tar.gz
$ tar xvf model.tar.gz -C ~/.gazebo/models
$ rm -rf model.tar.gz
$ wget http://models.gazebosim.org/sun/model.tar.gz
$ tar xvf model.tar.gz -C ~/.gazebo/models

2.1. World Files

The world description file contains all the elements in a simulation, including robots, lights, sensors, and static objects. This file is formatted using SDF (Simulation Description Format), and typically has a .world extension.

The Gazebo server (gzserver) reads this file to generate and populate a world.

安装完 Gazebo7 后,在/usr/share/gazebo-7/worlds/目录下有一些内置 World Files。

2.1.1. Gazebo 中启动 World File

gazebo /path/to/world_file ,可以启动相应的机器人。如启动内置的 pioneer2dx.world:

cig01@Ubuntu16:~$ cat /usr/share/gazebo-7/worlds/pioneer2dx.world
<?xml version="1.0" ?>
<sdf version="1.5">
  <world name="default">
    <!-- Ground -->
    <include>
      <uri>model://ground_plane</uri>
    </include>
    <!-- Pioneer2dx model -->
    <include>
      <uri>model://pioneer2dx</uri>
      <pose>0 0 0 0 0 0</pose>
    </include>
    <!-- A global light source -->
    <include>
      <uri>model://sun</uri>
    </include>
  </world>
</sdf>
cig01@Ubuntu16:~$ gazebo /usr/share/gazebo-7/worlds/pioneer2dx.world

上面命令可以省略默认路径/usr/share/gazebo-7/,即简写为: gazebo worlds/pioneer2dx.world 。启动成功后,如图 2 所示。

robot_gazebo_pioneer2dx.png

Figure 2: Gazebo 中内置的 pioneer2dx

说明:如果启动不成功,则可以指定 Verbose 模式启动 Gazebo,命令为 gazebo --verbose worlds/pioneer2dx.world ,观察终端中有没有错误或警告信息。

2.2. Model and Model Files

Models in Gazebo define a physical entity with dynamic, kinematic, and visual properties. In addition, a model may have one or more plugins, which affect the model's behavior. A model can represent anything from a simple shape to a complex robot; even the ground is a model.

A model file uses the same SDF format as world files, but should only contain a single <model> ... </model>. The purpose of these files is to facilitate model reuse, and simplify world files.

在 World Files 中通过 <include> ... </include> 可引入 Model,如:

<include>
  <uri>model://model_file_name</uri>
</include>

参考:http://gazebosim.org/tutorials?tut=components&cat=get_started

2.2.1. Model 的组成及其目录结构

一个 Model 一般由多个 SDF 格式的文件组成,且有固定的目录结构。Model 目录结构说明可参考:http://gazebosim.org/tutorials?tut=model_structure&cat=build_robot

http://models.gazebosim.org/ 中可以找到很多已经定义好的 Model。

下面是 ground_plane Model 的组成文件:

cig01@Ubuntu16:~$ cd ~/.gazebo/models/ground_plane/
cig01@Ubuntu16:~/.gazebo/models/ground_plane$ find .
.
./model-1_4.sdf
./model.sdf
./model-1_2.sdf
./model-1_3.sdf
./model.config

其中,文件 model.config 和 model.sdf 内容如下:

cig01@Ubuntu16:~/.gazebo/models/ground_plane$ cat model.config
<?xml version="1.0"?>

<model>
  <name>Ground Plane</name>
  <version>1.0</version>
  <sdf version="1.2">model-1_2.sdf</sdf>
  <sdf version="1.3">model-1_3.sdf</sdf>
  <sdf version="1.4">model-1_4.sdf</sdf>
  <sdf version="1.5">model.sdf</sdf>

  <author>
    <name>Nate Koenig</name>
    <email>nate@osrfoundation.org</email>
  </author>

  <description>
    A simple ground plane.
  </description>
</model>
cig01@Ubuntu16:~/.gazebo/models/ground_plane$ cat model.sdf
<?xml version="1.0" ?>
<sdf version="1.5">
  <model name="ground_plane">
    <static>true</static>
    <link name="link">
      <collision name="collision">
        <geometry>
          <plane>
            <normal>0 0 1</normal>
            <size>100 100</size>
          </plane>
        </geometry>
        <surface>
          <friction>
            <ode>
              <mu>100</mu>
              <mu2>50</mu2>
            </ode>
          </friction>
        </surface>
      </collision>
      <visual name="visual">
        <cast_shadows>false</cast_shadows>
        <geometry>
          <plane>
            <normal>0 0 1</normal>
            <size>100 100</size>
          </plane>
        </geometry>
        <material>
          <script>
            <uri>file://media/materials/scripts/gazebo.material</uri>
            <name>Gazebo/Grey</name>
          </script>
        </material>
      </visual>
    </link>
  </model>
</sdf>

说明:如果目录~/.gazebo/models 中没有 ground_plane Model,则可以下载它:

$ wget http://models.gazebosim.org/ground_plane/model.tar.gz
$ tar xvf model.tar.gz -C ~/.gazebo/models

2.2.2. 用“Model Editor”创建 Model

点击“Edit”菜单,可以看到“Model Editor”,点击它后可以打开 Model Editor。通过它可以创建 Model,完成例子可参考:参考:http://gazebosim.org/tutorials?tut=model_editor#OpentheModelEditor

3. Tips

3.1. 解决“启动时出现 No namespace found”的问题(手动下载 Model 文件)

启动 gazebo 时,主窗口是“黑屏”,用 gazebo --verbose 启动 Gazebo 时,有错误“[Err] [Node.cc:105] No namespace found”,其原因是 Model 文件找不到。

$ gazebo --verbose
Gazebo multi-robot simulator, version 7.0.0
Copyright (C) 2012-2016 Open Source Robotics Foundation.
Released under the Apache 2 License.
http://gazebosim.org

Gazebo multi-robot simulator, version 7.0.0
Copyright (C) 2012-2016 Open Source Robotics Foundation.
Released under the Apache 2 License.
http://gazebosim.org

[Msg] Waiting for master.
[Msg] Waiting for master.
[Msg] Connected to gazebo master @ http://127.0.0.1:11345
[Msg] Publicized address: 10.0.2.15
[Msg] Connected to gazebo master @ http://127.0.0.1:11345
[Msg] Publicized address: 10.0.2.15
[Wrn] [ModelDatabase.cc:339] Getting models from[http://gazebosim.org/models/]. This may take a few seconds.
[Err] [Node.cc:105] No namespace found
[Err] [Node.cc:105] No namespace found
[Err] [Node.cc:105] No namespace found
[Err] [Node.cc:105] No namespace found
[Err] [Node.cc:105] No namespace found
[Err] [Node.cc:105] No namespace found

解决办法是下载 Model 文件到目录/usr/share/gazebo-7/models 或者目录~/.gazebo/models 中。

$ hg clone https://bitbucket.org/osrf/gazebo_models
$ mkdir -p ~/.gazebo/models
$ cp -r gazebo_models/* ~/.gazebo/models

Author: cig01

Created: <2016-06-03 Fri>

Last updated: <2016-09-25 Sun>

Creator: Emacs 27.1 (Org mode 9.4)