前言
本文提供一套完整的 ROS2 + Gazebo Classic + PX4 仿真环境搭建方案,从源码编译到无人机在仿真器中起飞。实际安装过程可能因系统版本略有差异,但核心逻辑一致。
前置环境
确保系统已安装以下基础组件:
- ROS2 Humble
- Gazebo Classic (版本 11)
1. 获取 PX4 源码
在 Home 目录下克隆仓库并更新子模块。建议使用 Git 而非直接下载 ZIP,否则可能缺失关键依赖。
git clone https://github.com/PX4/PX4-Autopilot.git --recursive
**注意:**国内网络克隆速度较慢,建议开启网络加速工具。
2. 运行自动安装脚本
PX4 提供了自动化脚本处理依赖。若仅进行仿真测试,无需编译固件或 NuttX 环境,可跳过耗时较长的编译步骤。
cd PX4-Autopilot
bash ./Tools/setup/ubuntu.sh --no-nuttx
3. 编译并运行仿真示例
进入项目目录执行构建命令。首次编译耗时较长,请耐心等待。
cd ~/PX4-Autopilot
make px4_sitl gazebo-classic_iris
成功后将弹出 Gazebo 界面,显示黑色 Iris 无人机模型,即表示启动成功。
4. 配置地面站 QGroundControl
QGroundControl (QGC) 用于监控和操控。Ubuntu 需做少量配置以支持串口通信和视频流。
# 添加用户权限
sudo usermod -aG dialout "$(id -un)"
# 移除冲突服务
sudo apt-get remove modemmanager -y
# 安装视频流库
sudo apt install gstreamer1.0-plugins-bad gstreamer1.0-libav gstreamer1.0-gl -y
sudo apt install libfuse2 -y
sudo apt install libxcb-xinerama0 libxkbcommon-x11-0 libxcb-cursor-dev -y
前往官网下载 Linux 版 AppImage 包,赋予执行权限后运行:
chmod +x ./QGroundControl-x86_64.AppImage
./QGroundControl-x86_64.AppImage
5. 部署 Micro-XRCE-DDS-Agent
这是 ROS2 与 PX4 SITL 之间的通信桥梁。编译过程对网络稳定性要求较高。
sudo apt install -y ros-humble-ros-base ros-humble-geometry-msgs ros-humble-sensor-msgs
cd ~
git clone https://github.com/eProsima/Micro-XRCE-DDS-Agent.git
Micro-XRCE-DDS-Agent
build && build
cmake ..
make
make install
ldconfig /usr/local/lib/

