保姆级教程:在Ubuntu 20.04上搞定PX4 1.14.0 + Gazebo的9机编队仿真(附一键脚本) Ubuntu 20.04下PX4与Gazebo多机编队仿真实战指南当第一次在实验室看到九架无人机同步升空的场景时那种震撼感至今难忘。作为无人机算法开发者我们常常需要验证编队控制、避障算法或多机协同方案但真实飞行测试成本高、风险大。这时PX4Gazebo的仿真环境就成了最佳试验场。本文将带你从零搭建一个完整的九机编队仿真系统分享那些官方文档没写的实战细节。1. 环境准备与依赖安装在Ubuntu 20.04上搭建PX4仿真环境就像组装乐高——需要把正确的零件放在正确的位置。我们先来解决基础依赖问题sudo apt-get update sudo apt-get install git zip qtcreator cmake build-essential genromfs -y必须特别注意的依赖项是Gazebo版本。PX4 1.14.0对Gazebo 9有最佳兼容性但Ubuntu 20.04默认安装的是Gazebo 11。解决方法如下sudo sh -c echo deb http://packages.osrfoundation.org/gazebo/ubuntu-stable lsb_release -cs main /etc/apt/sources.list.d/gazebo-stable.list wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add - sudo apt-get update sudo apt-get install gazebo9 libgazebo9-dev -y常见问题排查表错误现象可能原因解决方案Gazebo黑屏显卡驱动问题安装NVIDIA专有驱动或设置环境变量LIBGL_ALWAYS_SOFTWARE1PX4编译失败缺少依赖运行make px4_sitl gazebo查看缺失库模型加载失败模型路径错误检查GAZEBO_MODEL_PATH环境变量提示建议使用SSD硬盘进行开发Gazebo加载大量模型时IO性能至关重要2. PX4源码编译与配置获取PX4 1.14.0源码需要特别注意子模块更新git clone https://github.com/PX4/PX4-Autopilot.git --recursive cd PX4-Autopilot git checkout v1.14.0 git submodule update --init --recursive编译时推荐使用以下参数优化构建过程make px4_sitl gazebo -j$(nproc) BUILD_BACKENDninja关键配置修改修改ROMFS/px4fmu_common/init.d-posix/rcS文件确保以下参数param set MAV_PROTO_VER 2.0 param set MAV_BROADCAST 1在launch/目录下创建多机配置文件时注意每个实例的UDP端口分配arg namevehicle_$(arg vehicle_id) defaultiris/ arg nameID_$(arg vehicle_id) default$(arg vehicle_id)/ arg namemavlink_udp_port_$(arg vehicle_id) default$(eval 14560 arg(vehicle_id) * 10)/3. 多机Launch文件生成技巧原始方法需要手动编写launch文件我们可以利用XTDrone的模板进行自动化生成。改进后的generator.py脚本应包含以下关键功能def generate_launch(vehicle_type, num_vehicles, formation_rows): base_port 14560 with open(px4_multi_vehicle.launch, w) as f: f.write(launch\n) for i in range(num_vehicles): f.write(f group nsuav_{i}\n) f.write(f arg namevehicle_id default{i}/\n) f.write(f arg namemavlink_udp_port default{base_port i*10}/\n) # 添加模型初始位置计算逻辑 x_pos (i % formation_rows) * 2.0 y_pos (i // formation_rows) * 2.0 f.write(f arg namex default{x_pos}/\n) f.write(f arg namey default{y_pos}/\n) f.write( include file$(find px4)/launch/single_vehicle.launch\n) f.write( !-- 参数传递 --\n) f.write( /include\n) f.write( /group\n) f.write(/launch\n)执行脚本时推荐使用以下参数组合python3 generator.py --vehicle typhoon_h480 --num 9 --rows 3 --spacing 2.5注意不同机型的气动参数差异很大typhoon_h480比默认iris更稳定适合多机测试4. 编队控制实战与问题排查启动仿真环境后真正的挑战才开始。以下是保证编队稳定的关键步骤通信配置检查# 查看MAVLink通信状态 mavlink-routerd -e 127.0.0.1:14550 -e 127.0.0.1:14551 -e 127.0.0.1:14552 ...Offboard模式切换脚本# keyboard_control_multirotor.py关键修改 def set_offboard_mode(vehicle_id): msg vehicle.message_factory.command_long_encode( 0, 0, # target system, target component mavutil.mavlink.MAV_CMD_DO_SET_MODE, # command 0, # confirmation 1, # custom mode (1 for OFFBOARD) 6, # base mode (6 for GUIDED) 0, 0, 0, 0, 0) # unused parameters vehicle.send_mavlink(msg)编队松散问题解决方案在PX4参数中调整位置控制器增益param set MPC_XY_P 1.5 param set MPC_Z_P 1.0 param set MPC_XY_VEL_P_ACC 0.2在Gazebo模型配置中增加质量惯性参数inertial mass3.0/mass inertia ixx0.1/ixx ixy0/ixy ixz0/ixz iyy0.1/iyy iyz0/iyz izz0.2/izz /inertia /inertial5. 一键自动化脚本实现将上述所有步骤整合为一个可复用的bash脚本#!/bin/bash # auto_setup.sh # 安装依赖 echo [1/5] Installing dependencies... sudo apt-get update sudo apt-get install -y git zip qtcreator cmake ninja-build # 克隆PX4源码 echo [2/5] Cloning PX4 repository... git clone https://github.com/PX4/PX4-Autopilot.git --recursive cd PX4-Autopilot git checkout v1.14.0 git submodule update --init --recursive # 编译PX4 echo [3/5] Building PX4... make px4_sitl gazebo -j$(nproc) BUILD_BACKENDninja # 生成多机launch文件 echo [4/5] Generating multi-vehicle launch... python3 ../generator.py --vehicle typhoon_h480 --num 9 --rows 3 # 启动仿真 echo [5/5] Starting simulation... roslaunch px4 px4_multi_vehicle.launch在实际项目中我发现最耗时的往往是环境变量配置。建议在~/.bashrc中添加以下设置export GAZEBO_MODEL_PATH$GAZEBO_MODEL_PATH:~/PX4-Autopilot/Tools/sitl_gazebo/models export ROS_PACKAGE_PATH$ROS_PACKAGE_PATH:~/PX4-Autopilot export ROS_PACKAGE_PATH$ROS_PACKAGE_PATH:~/PX4-Autopilot/Tools/sitl_gazebo