本文记录在 Jetson Orin NX 上从零搭建 ROS 2 Humble、PX4 仿真和 MAVROS 的完整流程,最后编译 onboard 机载控制程序,用 3 架仿真无人机验证整条链路。Jetson 上特有的 OpenCV 版本冲突单独列了出来。
环境信息
硬件:Jetson Orin NX 开发板
系统:Ubuntu 22.04(对应版本的 JetPack)
ARM 设备上部分官方软件存在兼容性或性能问题,下面几个工具在 Jetson 上用起来更顺手:
一、安装 ROS 2 Humble
参考官方文档:https://docs.ros.org/en/humble/Installation/Ubuntu-Install-Debs.html
1. 配置本地化并添加软件源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 locale sudo apt update && sudo apt install -y localessudo locale-gen en_US en_US.UTF-8sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8export LANG=en_US.UTF-8locale sudo apt install -y software-properties-commonsudo add-apt-repository universe -ysudo apt update && sudo apt install -y curlexport ROS_APT_SOURCE_VERSION=$(curl -s https://api.github.com/repos/ros-infrastructure/ros-apt-source/releases/latest | grep -F "tag_name" | awk -F\" '{print $4}' )curl -L -o /tmp/ros2-apt-source.deb "https://github.com/ros-infrastructure/ros-apt-source/releases/download/${ROS_APT_SOURCE_VERSION} /ros2-apt-source_${ROS_APT_SOURCE_VERSION} .$(. /etc/os-release && echo ${UBUNTU_CODENAME:-${VERSION_CODENAME}}) _all.deb" sudo dpkg -i /tmp/ros2-apt-source.debsudo apt updatesudo apt upgrade -y
2. 安装 ROS 2 与开发工具,写入环境变量
1 2 3 4 5 sudo apt install -y ros-humble-desktopsudo apt install -y ros-dev-toolsecho "source /opt/ros/humble/setup.bash" >> ~/.bashrcsource ~/.bashrc
二、安装 PX4 仿真环境(可选)
1 2 3 4 5 6 7 8 9 10 git clone https://github.com/PX4/PX4-Autopilot.git --recursive cd PX4-Autopilotgit submodule update --init --recursive bash ./Tools/setup/ubuntu.sh make px4_sitl gz_x500
Jetson 上的 OpenCV 版本冲突
编译或运行时如果出现:
1 2 /usr/lib/cmake/opencv4/OpenCVModules.cmake:172 (message): The imported target "opencv_core" references the file "/usr/lib/libopencv_core.so.4.8.0" but this file does not exist.
原因是 JetPack 自带的 OpenCV 与 ROS 2 Humble 期望的版本不一致,导致链接缺失。安装 NVIDIA 提供的 OpenCV 包即可:
1 2 sudo apt updatesudo apt install -y 'nvidia-opencv*'
验证:
1 ldconfig -p | grep opencv_core
参考:https://forums.developer.nvidia.com/t/opencv-libraries-missing-or-links-are-broken-on-a-fresh-install/335400
仿真启动后提示没有地面站
如果出现 Preflight Fail: No connection to the GCS,可以在 MAVLink 控制台执行下面的命令,临时取消对地面站连接的要求。这只适合仿真调试,不建议长期使用:
三、安装 MAVROS
1 2 3 4 sudo apt install -y ros-$ROS_DISTRO -mavros ros-$ROS_DISTRO -mavros-extraswget https://gitee.com/tyx6/mytools/raw/main/mavros/install_geographiclib_datasets.sh chmod a+x ./install_geographiclib_datasets.shsudo ./install_geographiclib_datasets.sh
四、创建工作空间并拉取代码
1 2 mkdir -p ~/ros2_ws/srccd ~/ros2_ws/src
1. BehaviorTree.CPP
1 2 3 4 git clone https://github.com/BehaviorTree/BehaviorTree.CPP.git cd BehaviorTree.CPPgit checkout 4.7.2 cd ..
2. onboard 项目(私有仓库)
onboard 是私有仓库,需要向作者获取 token 后再 clone:
1 git clone https://<token>@github.com/dreamer198/onboard.git
安装 JSON 头文件依赖:
1 sudo apt-get install -y nlohmann-json3-dev
五、编译
在工作空间根目录编译。不需要 GROOT 可视化时,关闭 ZeroMQ 接口:
1 2 3 4 5 cd ~/ros2_wscolcon build --cmake-args -DBTCPP_GROOT_INTERFACE=OFF echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrcsource ~/.bashrc
六、运行与测试
1. 启动 3 架仿真无人机
1 2 3 4 cd ~/PX4-AutopilotPX4_SYS_AUTOSTART=4001 PX4_SIM_MODEL=gz_x500 PX4_GZ_MODEL_POSE="0,0" ./build/px4_sitl_default/bin/px4 -i 0 PX4_GZ_STANDALONE=1 PX4_SYS_AUTOSTART=4001 PX4_GZ_MODEL_POSE="1,0" PX4_SIM_MODEL=gz_x500 ./build/px4_sitl_default/bin/px4 -i 1 PX4_GZ_STANDALONE=1 PX4_SYS_AUTOSTART=4001 PX4_GZ_MODEL_POSE="0,1" PX4_SIM_MODEL=gz_x500 ./build/px4_sitl_default/bin/px4 -i 2
2. 启动机载控制与通信节点
1 2 3 4 5 6 7 8 9 10 ros2 launch onboard_control mainTree.launch.py uav_id:=1 fcu_url:="udp://:14540@127.0.0.1:14557" ros2 launch onboard_control mainTree.launch.py uav_id:=2 fcu_url:="udp://:14541@127.0.0.1:14558" ros2 launch onboard_control mainTree.launch.py uav_id:=3 fcu_url:="udp://:14542@127.0.0.1:14559" ros2 run onboard_comunication send_cmd ros2 run onboard_comunication receive_status
实例编号、系统 ID 和端口的对应关系见《PX4 多机仿真与实测启动指南》 。
3. 怎样算成功
receive_status 终端能收到全部无人机的状态;在 send_cmd 中按约定格式发布指令后,Gazebo 中的无人机执行对应动作。具体检查四点:
所有 PX4 实例进入正常飞控循环,没有持续的错误输出;
MAVROS 与机载节点能正常收发心跳和状态消息;
指令发布后的延迟在可接受范围内(通常小于 200 ms);
多机之间没有端口冲突(14540/14557 这类端口组合互不重复)。
参考链接