课程介绍


预计完成时间:10分钟

1.11   Gazebo Sim 服务器与GUI(客户端)

你几乎已经完成了本单元的学习。在本节中,学习如何在后台不使用 GUI 的情况下运行 Gazebo Sim。在没有 GUI 的情况下在后台运行 Gazebo 可以节省系统资源并以无头模式执行模拟,这对于批处理或基于服务器的应用等任务非常有用。

如果 Gazebo Sim 窗口仍在打开和运行,请将其关闭。 在你启动 Gazebo Sim 的 Web Shell 中按 Ctrl + C

启动 Gazebo Sim 服务器(无 GUI)

在 Gazebo Sim 中以服务器模式启动世界文件的一般命令:

gz sim -s <path/to/world_file.sdf>

-s 参数表示仅服务器模式。它将启动不带 GUI 的 Gazebo Sim。

在任意一个 Web Shell 中运行以下命令:

  Execute in Web Shell 1

In [ ]:
gz sim -s shapes.sdf

启动 Gazebo Sim GUI(客户端)

启动 Gazebo Sim GUI 的一般命令:

gz sim -g

-g 参数表示仅 GUI 模式。它将启动 Gazebo Sim GUI。

在任意一个 Web Shell 中运行以下命令:

  Execute in Web Shell 2

In [ ]:
gz sim -g

检查 Gazebo 话题列表

检查 Gazebo Sim 所有可用话题的一般命令:

gz topic --listgz topic -l

--list-l 相同——它们都列出所有可用的话题。

在任意一个 Web Shell 中运行以下命令:

  Execute in Web Shell 3

In [ ]:
gz topic --list

你应该会得到类似下图的输出:

/clock
/gazebo/resource_paths
/gui/camera/pose
/stats
/world/shapes/clock
/world/shapes/dynamic_pose/info
/world/shapes/pose/info
/world/shapes/scene/deletion
/world/shapes/scene/info
/world/shapes/state
/world/shapes/stats

检查 Gazebo 进程

在任意一个 Web Shell 中运行以下命令:

  Execute in Web Shell 4

In [ ]:
ps faux | grep gz

你应该会得到类似下图的输出:

user      9640 15.2  0.8 1323688 133444 pts/0  Rl+  12:13   0:02  |       \_ gz sim -s shapes.sdf
user      9762  0.0  0.0   3464  1592 pts/1    S+   12:14   0:00  |   \_ grep --color=auto gz
user      9681 89.4  5.2 4581108 803972 pts/2  Sl+  12:13   0:04  |       \_ gz sim -g
- 注意 -

使用 grep 时,grep --color=auto 进程总是会出现,你可以安全地忽略它。

—— 注意结束 ——

运行无头 Gazebo Sim(仅服务器,无 GUI)

如果你不想因为运行 Gazebo Sim GUI 而导致进程/图形过载(这会消耗内存),这会很有帮助。

请记住,如果你想运行无头 Gazebo Sim 实例,你要确保模拟过程中不会有任何需要调试的问题。一个很好的例子是使用带有避障功能的移动机器人在完全封闭的房间中进行建图。你可以确信机器人不会撞到任何障碍物,并将继续探索和建图直到经过一定时间后建图过程终止。

**重要提示:** 在继续之前,请确保你已终止所有终端中的所有进程。如果 Gazebo Sim GUI 在任意窗口中打开,请关闭它。在所有有活动进程运行的终端上按 Ctrl + C

1. 使用世界文件启动 Gazebo Sim 服务器

  Execute in Web Shell 1

In [ ]:
gz sim -s shapes.sdf

  Execute in Web Shell 3

In [ ]:
gz topic --list

请注意,即使 GUI 未激活和运行,Gazebo Sim 的话题也会被列出。

  Execute in Web Shell 4

In [ ]:
ps faux | grep "gz"

你会注意到 Gazebo Sim 服务器作为一个进程运行。

user      9640 12.9  0.7 1323688 121132 pts/0  Sl+  12:13   0:15  |       \_ gz sim -s shapes.sdf

2. 启动 Gazebo Sim GUI 并开始模拟

  Execute in Web Shell 2

In [ ]:
gz sim -g

GUI 启动后,按 Play(播放)按钮开始模拟。 请注意场景右下角不断变化的 RTF 值。 单击 RTF 按钮展开并显示模拟状态。

  Execute in Web Shell 3

In [ ]:
gz topic --list

请注意出现了新话题,例如 /gui/camera/pose,表明有 Gazebo Sim GUI(客户端)发布的话题。

/clock
/gazebo/resource_paths
/gui/camera/pose
/stats
/world/shapes/clock
/world/shapes/dynamic_pose/info
/world/shapes/pose/info
/world/shapes/scene/deletion
/world/shapes/scene/info
/world/shapes/state
/world/shapes/stats

  Execute in Web Shell 4

In [ ]:
ps faux | grep "gz"

你会注意到 Gazebo Sim 服务器和 GUI 作为两个进程运行。

user      9640 14.2  0.7 1323688 112496 pts/0  Rl+  12:13   1:01  |       \_ gz sim -s shapes.sdf
user     10449 65.0  3.4 4431256 534356 pts/2  Rl+  12:20   0:02  |       \_ gz sim -g

3. 按 Ctrl + C 关闭 Gazebo Sim GUI

  Execute in Web Shell 2

快速记下 Gazebo Sim GUI 中的迭代次数。 按 Ctrl + C 终止 Gazebo Sim GUI 进程。

  Execute in Web Shell 3

In [ ]:
gz topic --list

请注意基于 GUI 的话题现在已经消失,例如上一步提到的 /gui/camera/pose。 你仍然可以看到模拟服务器发布的话题。

  Execute in Web Shell 4

In [ ]:
ps faux | grep "gz"

你会注意到 Gazebo Sim 服务器作为进程之一运行,而 Gazebo Sim GUI 进程已消失。

4. 重新启动 Gazebo Sim GUI 并检查模拟状态

  Execute in Web Shell 2

In [ ]:
gz sim -g

请注意模拟迭代步数增加了,实时因子(RTF)值持续变化。这表明即使 GUI 关闭,服务器仍在后台运行。

  Execute in Web Shell 3

In [ ]:
gz topic --list

现在,你会注意到 GUI 话题以及服务器话题都可以再次看到了。

  Execute in Web Shell 4

In [ ]:
ps faux | grep "gz"

你会再次注意到 Gazebo Sim 服务器和 Gazebo Sim GUI 作为不同进程运行。

5. 按 Ctrl + C 关闭 Gazebo Sim 服务器

  Execute in Web Shell 1

Ctrl + C 终止 Gazebo Sim 服务器进程。

  Execute in Web Shell 3

In [ ]:
gz topic --list

现在,相反,你会注意到所有服务器话题都消失了,只剩下 GUI 话题。

  Execute in Web Shell 4

In [ ]:
ps faux | grep "gz"

你会注意到 Gazebo Sim GUI 作为进程之一运行,但 Gazebo Sim 服务器进程已消失。

6. 检查 Gazebo Sim GUI

打开显示 Gazebo Sim GUI 的窗口。你会注意到模拟已停止。这表明 Gazebo Sim 服务器已关闭。

请注意播放/暂停按钮仍处于"播放模式",显示暂停符号,但模拟已停止,RTF 显示中的数字不再变化。

7. 按 Ctrl + C 关闭 Gazebo Sim GUI

  Execute in Web Shell 2

Ctrl + C 终止 Gazebo Sim GUI 进程。执行此操作后,你将不再看到 Gazebo Sim GUI 窗口。

  Execute in Web Shell 3

In [ ]:
gz topic --list

现在,你会看到列出的话题中不再属于服务器或 GUI。

  Execute in Web Shell 4

In [ ]:
ps faux | grep "gz"

你现在会注意到没有正在运行的 Gazebo Sim 进程显示,所有进程都已终止。