ROS 2 标准仿真接口
ROS 2 仿真接口定义了一组标准的 ROS 2 service、message 和 action,用于控制和交互仿真环境。这些接口与仿真器无关,旨在提供一种统一的方式来使用 ROS 2 控制和观察仿真。
Gazebo 已经实现了这些接口,支持通过标准的 ROS 2 调用来完成生成实体、推进仿真、查询 world 状态等任务。本教程介绍如何与正在运行的 Gazebo 仿真交互,完成以下任务:
以下 service 和 action 可用于控制仿真时间的流转。
| 接口名称 | Topic 名称 | 类型 | 描述 |
|---|---|---|---|
ResetSimulation | /gzserver/reset_simulation | Service | 将仿真重置到初始状态 |
StepSimulation | /gzserver/step_simulation | Service | 将仿真向前推进指定步数 |
GetSimulationState | /gzserver/get_simulation_state | Service | 获取当前仿真状态(运行/暂停/停止) |
SetSimulationState | /gzserver/set_simulation_state | Service | 设置仿真状态(运行/暂停/停止) |
SimulateSteps | /gzserver/simulate_steps | Action | 将仿真向前推进指定步数,支持反馈和取消 |
ResetSimulation 服务
Section titled “ResetSimulation 服务”将仿真重置到初始状态。
ros2 service call /gzserver/reset_simulation simulation_interfaces/srv/ResetSimulation "{}"StepSimulation 服务
Section titled “StepSimulation 服务”将仿真向前推进指定步数。
ros2 service call /gzserver/step_simulation simulation_interfaces/srv/StepSimulation "{steps: 10}"GetSimulationState 服务
Section titled “GetSimulationState 服务”获取当前仿真状态(运行/暂停/停止)。
ros2 service call /gzserver/get_simulation_state simulation_interfaces/srv/GetSimulationState "{}"SetSimulationState 服务
Section titled “SetSimulationState 服务”设置仿真状态(运行/暂停/停止)。
-
将仿真状态设置为停止。
Terminal window ros2 service call /gzserver/set_simulation_state simulation_interfaces/srv/SetSimulationState "{state: {state: 0}}" -
将仿真状态设置为运行。
Terminal window ros2 service call /gzserver/set_simulation_state simulation_interfaces/srv/SetSimulationState "{state: {state: 1}}" -
将仿真状态设置为暂停。
Terminal window ros2 service call /gzserver/set_simulation_state simulation_interfaces/srv/SetSimulationState "{state: {state: 2}}" -
将仿真状态设置为退出。
Terminal window ros2 service call /gzserver/set_simulation_state simulation_interfaces/srv/SetSimulationState "{state: {state: 3}}"
SimulateSteps Action
Section titled “SimulateSteps Action”将仿真向前推进指定步数,支持反馈和取消。
ros2 action send_goal /gzserver/simulate_steps simulation_interfaces/action/SimulateSteps "{steps: 10}" --feedback以下接口用于在运行时创建或移除仿真中的实体。
| 接口名称 | Topic 名称 | 类型 | 描述 |
|---|---|---|---|
SpawnEntity | /gzserver/spawn_entity | Service | 在仿真中的指定位置生成一个新实体 |
DeleteEntity | /gzserver/delete_entity | Service | 按名称删除现有实体 |
SpawnEntity 服务
Section titled “SpawnEntity 服务”在仿真中的指定位置生成一个新实体。
ros2 service call /gzserver/spawn_entity simulation_interfaces/srv/SpawnEntity "{ name: 'my_model', entity_resource: { uri: '/path/to/model.sdf' }, allow_renaming: false, initial_pose: { pose: { position: {x: 0.0, y: 0.0, z: 0.0}, orientation: {w: 1.0, x: 0.0, y: 0.0, z: 0.0} } }}"DeleteEntity 服务
Section titled “DeleteEntity 服务”按名称删除现有实体。
ros2 service call /gzserver/delete_entity simulation_interfaces/srv/DeleteEntity "{entity: 'my_model'}"以下接口用于查看(introspect)仿真 world 和实体的状态。
| 接口名称 | Topic 名称 | 类型 | 描述 |
|---|---|---|---|
GetEntityState | /gzserver/get_entity_state | Service | 获取指定实体的位姿(pose)和速度(twist) |
GetEntitiesStates | /gzserver/get_entities_states | Service | 获取多个实体的状态(可选过滤) |
GetEntities | /gzserver/get_entities | Service | 获取实体列表(可选过滤) |
GetEntityState 服务
Section titled “GetEntityState 服务”获取指定实体的位姿和速度。
ros2 service call /gzserver/get_entity_state simulation_interfaces/srv/GetEntityState "{entity: 'my_model'}"GetEntitiesStates 服务
Section titled “GetEntitiesStates 服务”获取多个实体的状态(可选过滤)。
ros2 service call /gzserver/get_entities_states simulation_interfaces/srv/GetEntitiesStates "{filters: {filter: ''}}"GetEntities 服务
Section titled “GetEntities 服务”获取实体列表(可选过滤)。
ros2 service call /gzserver/get_entities simulation_interfaces/src/GetEntities "{filters: {filter: ''}}"以下接口用于设置实体状态。
| 接口名称 | Topic 名称 | 类型 | 描述 |
|---|---|---|---|
SetEntityState | /gzserver/set_entity_state | Service | 设置指定实体的位姿和速度 |
SetEntityState 服务
Section titled “SetEntityState 服务”设置指定实体的位姿和速度。
ros2 service call /gzserver/set_entity_state simulation_interfaces/srv/SetEntityState "{ entity: 'my_model', state: {pose: { position: { x: -2.0, z: 0.5 }}, twist: {linear: {x: 0.5}}}}"注意:使用
SetEntityState时,目前无法只设置实体的pose或只设置twist。实体的两个分量都会根据所提供消息的值被赋值。例如,如果pose留空,实体的位姿将被设置为原点。该问题将来可能会得到修复,参见 issue 18。
有些仿真器可能只支持部分接口。可使用以下 service 来检查支持的功能。
| 接口名称 | Topic 名称 | 类型 | 描述 |
|---|---|---|---|
GetSimulatorFeatures | /gzserver/get_simulator_features | Service | 查询支持哪些接口功能 |
GetSimulatorFeatures 服务
Section titled “GetSimulatorFeatures 服务”查询支持哪些接口功能。
ros2 service call /gzserver/get_simulator_features simulation_interfaces/srv/GetSimulationFeatures "{}"- 在
PoseStamped消息的frame_id字段中,目前只能使用空字符串或“world”。将来计划支持使用Tf已知的 frame。 SpawnEntity服务不支持实体命名空间(namespace)。- 生成实体时,如果
SpawnEntity.allow_renaming设置为true且 Gazebo 中发生了重命名,新名称不会在SpawnEntity服务返回的Result对象中返回。