Files
PX4-Autopilot/docs/zh/ros2/multi_vehicle.md
T
2025-09-24 10:33:22 +10:00

55 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 使用 ROS 2 进行多车辆模拟
[XRCE-DDS](../middleware/uxrce_dds.md) 支持多个客户端通过 UDP 协议连接到同一个代理。
这在模拟中特别有用,因为只有一个代理需要启动。
## 设置和要求
唯一的要求是
- 能够在不依赖 ROS 2 的情况下,使用所需的仿真器([Gazebo](../sim_gazebo_gz/multi_vehicle_simulation.md), [Gazebo Classic](../sim_gazebo_classic/multi_vehicle_simulation.md#multiple-vehicle-with-gazebo-classic), [FlightGear](../sim_flightgear/multi_vehicle.md) 和 [JMAVSim](../sim_jmavsim/multi_vehicle.md))运行多车辆仿真[multi-vehicle simulation](../simulation/multi-vehicle-simulation.md)。
- 能够在单一车辆仿真中使用ROS 2(机器人操作系统 2)
## 工作原理
在仿真中,每个 PX4 实例都会获得一个唯一的px4_instance编号,编号从0开始。
该值用于为 [UXRCE_DDS_KEY](../advanced_config/parameter_reference.md#UXRCE_DDS_KEY)分配一个唯一值:
```sh
参数设置 UXRCE_DDS_KEY $(px4_instance+1))
```
:::info
通过这种方式,UXRCE_DDS_KEY 将始终与 [MAV_SYS_ID] 保持一致(../advanced_config/parameter_reference.md#MAV_SYS_ID)
:::
此外,当 px4_instance 大于 0 时,会添加一个格式为 px4_$px4_instance 的唯一 ROS 2 命名空间前缀:
```sh
uxrce_dds_ns="-n px4_$px4_instance"
```
:::info
环境变量 PX4_UXRCE_DDS_NS 若已设置,将覆盖上文所述的命名空间行为。
:::
第一个实例(px4_instance=0)没有额外的命名空间,此举是为了与真实载具上 xrce-dds 客户端的默认行为保持一致。
这种不匹配可以通过手动使用 `PX4_UXRCE_DDS_NS` 来修复,或者通过从索引 `1` 中添加车辆而不是 `0` (这是Gazebo Classic的 [sitl_multiple_run.sh](https://github.com/PX4/PX4-Autopilot/blob/main/Tools/simulation/gazebo-classic/sitl_multiple_run.sh) 的默认行为)。
模拟中的默认客户端配置概述如下:
| `PX4_UXRCE_DDS_NS` | `px4_instance` | `UXRCE_DDS_KEY` | 客户端命名空间 |
| ------------------ | -------------- | ---------------- | --------------------- |
| 未提供 | 0 | `px4_instance+1` | 无 |
| 已提供 | 0 | `px4_instance+1` | `PX4_UXRCE_DDS_NS` |
| 未提供 | > 0 | `px4_instance+1` | `px4_${px4_instance}` |
| 已提供 | > 0 | `px4_instance+1` | `PX4_UXRCE_DDS_NS` |
## 调整 `target_system` 值
PX4 只在他们的 `target_system` 字段为 0 (路由通信) 或与 `MAV_SYS_ID` 一致时,才接受 [VehicleCommand](../msg_docs/VehicleCommand.md)。
在所有其他情况下,信息都被忽视。
因此,当 ROS 2 节点需向 PX4 发送 VehicleCommand(载具指令)消息时,必须确保消息中填写了合适的 target_system(目标系统)字段值。
例如,若你想向 px4_instance=2 的第三台飞行器发送指令,则需要在所有 VehicleCommand消息中设置 target_system=3。