本文记录在 Jetson Orin NX 上,从系统准备到 ROS 2 Humble、PX4 仿真与 MAVROS 的完整环境搭建的可复制流程,并列出常见问题与排查方法。力求做到:结构清晰、命令可直接粘贴、出现异常可快速定位。

环境信息

  • 硬件: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  # 检查 UTF-8

sudo apt update && sudo apt install -y locales
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8

locale # 验证设置

sudo apt install -y software-properties-common
sudo add-apt-repository universe -y

sudo apt update && sudo apt install -y curl
export 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.deb

sudo apt update
sudo apt upgrade -y

步骤 2:安装 ROS 2 与开发工具并写入环境变量

1
2
3
4
5
sudo apt install -y ros-humble-desktop
sudo apt install -y ros-dev-tools

echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
source ~/.bashrc

安装 PX4 仿真环境(可选)

1
2
3
4
5
6
7
8
9
10
# 获取源码
git clone https://github.com/PX4/PX4-Autopilot.git --recursive
cd PX4-Autopilot
git submodule update --init --recursive

# 一键安装依赖
bash ./Tools/setup/ubuntu.sh

# 首次编译运行 Gazebo 仿真
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 期望版本不一致,导致链接缺失。

解决步骤:

1
2
sudo apt update
sudo 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

1
2
3
4

> 若启动后出现提示:`Preflight Fail: No connection to the GCS`,可在 MAVLink 控制台执行:
> ```bash
> param set NAV_DLL_ACT 0

临时绕过对地面站的要求(仅用于仿真调试,不建议长期使用)。

安装 MAVROS

1
2
3
4
sudo apt install -y ros-$ROS_DISTRO-mavros ros-$ROS_DISTRO-mavros-extras
wget https://gitee.com/tyx6/mytools/raw/main/mavros/install_geographiclib_datasets.sh
chmod a+x ./install_geographiclib_datasets.sh
sudo ./install_geographiclib_datasets.sh

创建 ROS 2 工作空间

1
2
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src

拉取依赖与项目代码

1) BehaviorTree.CPP

1
2
3
4
git clone https://github.com/BehaviorTree/BehaviorTree.CPP.git
cd BehaviorTree.CPP
git 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_ws
colcon build --cmake-args -DBTCPP_GROOT_INTERFACE=OFF

echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc
source ~/.bashrc

运行与测试

1. 启动 PX4 Gazebo 仿真(示例:3 台无人机)

1
2
3
4
cd ~/PX4-Autopilot
PX4_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
# 启动主控制(uav_id 与端口需与 PX4 实例对应)
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

3. 成功判定

receive_status 终端可监听到全部无人机状态;在 send_cmd 按约定格式发布指令,可观察 Gazebo 中无人机执行对应动作。

成功标准:

  1. 所有 PX4 实例进入正常飞控循环,无持续错误输出。
  2. MAVROS 与机载节点能正确收发心跳与状态消息。
  3. 指令发布后延迟在可接受范围(通常 < 200 ms)。
  4. 多机之间不存在端口冲突(如 14540/14557 组合互不重复)。

官方与仓库