连接到 ROS 2


预计完成时间:6 分钟

3.6   Gazebo Sim 基础服务

在开始本节之前,请先使用 Gazebo Sim 启动 shapes 世界,并让仿真运行起来。

  在 Web Shell 1 中执行

In [ ]:
gz sim -r shapes.sdf

命令中的 -r 表示启动 Gazebo Sim 时直接让仿真开始运行。

现在,请在一个新的 web shell 中执行下面的命令。

获取模型位姿

从仿真中获取某个已有模型的位姿(位置和朝向)。

  1. 获取世界中所有模型/实体的列表。
  2. 获取世界中特定模型的位姿,例如 box。

  在 Web Shell 2 中执行

In [ ]:
gz model --list
Requesting state for world [shapes]...

Available models:
    - ground_plane
    - box
    - cylinder
    - sphere
    - capsule
    - ellipsoid
In [ ]:
gz model --model box --pose

你的输出应与下图类似。

Requesting state for world [shapes]...

Model: [8]
  - Name: box
  - Pose [ XYZ (m) ] [ RPY (rad) ]:
    [-0.000000 -0.000000 0.500000]
    [0.000000 0.000000 -0.000000]

设置模型位姿

为仿真中的已有模型设置一个新的位姿。

In [ ]:
gz service --service /world/shapes/set_pose \
--reqtype gz.msgs.Pose  \
--reptype gz.msgs.Boolean  \
--timeout 500 \
--req 'name:"box",position:{x:1.0,y:0.0,z:0.5},orientation:{x:0.0,y:0.0,z:0.383,w:0.924}'

你的输出应为:

data: true

暂停仿真

暂停仿真。仿真画面中的实体会停在原地不动,仿真时间也会停止。

In [ ]:
gz service --service /world/shapes/control \
--reqtype gz.msgs.WorldControl \
--reptype gz.msgs.Boolean \
--timeout 1000 \
--req 'pause:true'

预期输出为:

data: true

启动/继续仿真

运行仿真,或在暂停后继续仿真。被冻结的实体会重新开始运动,仿真时间也会从暂停处继续。

In [ ]:
gz service --service /world/shapes/control \
--reqtype gz.msgs.WorldControl \
--reptype gz.msgs.Boolean \
--timeout 1000 \
--req 'pause:false'

你的输出应为:

data: true

重置整个仿真

将所有实体重置回初始位置,并且把仿真从 0.0 秒重新开始。

In [ ]:
gz service --service /world/shapes/control \
--reqtype gz.msgs.WorldControl \
--reptype gz.msgs.Boolean \
--timeout 1000 \
--req 'reset:{all:true}'

你的输出应如下所示:

data: true

重置仿真模型

将所有实体重置回初始位置,但不会重新启动仿真。

In [ ]:
gz service --service /world/shapes/control \
--reqtype gz.msgs.WorldControl \
--reptype gz.msgs.Boolean \
--timeout 1000 \
--req 'reset:{model_only:true}'

重置仿真时间

将仿真时间重置回 0.0 秒,但不会重新启动仿真。

In [ ]:
gz service --service /world/shapes/control \
--reqtype gz.msgs.WorldControl \
--reptype gz.msgs.Boolean \
--timeout 1000 \
--req 'reset:{time_only:true}'
- 实践 -

在空场景中添加一些简单物体,并调用你刚学到的这些服务,观察它们的效果。

- 实践结束 -