PX4开发之旅

PX4 上位机开发

PX4 开发文档

安装 conda

  • 看查内核版本

    1
    cat /proc/version

    诺为 arrch64

    1
    Linux version 6.1.43-rockchip-rk3588 (root@yml-virtual-machine) (aarch64-none-linux-gnu-gcc (GNU Toolchain for the Arm Architecture 11.2-2022.02 (arm-11.14)) 11.2.1 20220111, GNU ld (GNU Toolchain for the Arm Architecture 11.2-2022.02 (arm-11.14)) 2.37.20220122) #1.0.10 SMP Mon Jun 24 10:36:36 CST 2024
  • 安装相应的 arrch64 版本的 conda:

    1
    2
    3
    4
    wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2024.10-1-Linux-aarch64.sh

    # 下载完后再安装
    sh Anaconda3-2024.10-1-Linux-aarch64.sh
  • 执行 Enter(回车)同意继续往下安装:

    1
    2
    3
    4
    5
    6
    Welcome to Anaconda3 2024.10-1

    In order to continue the installation process, please review the license
    agreement.
    Please, press ENTER to continue
    >>>
  • 直到出现询问 [yes|no] 输入 yes

    1
    2
    3
    4
    Version 4.0 | Last Modified: March 31, 2024 | ANACONDA TOS

    Do you accept the license terms? [yes|no]
    >>> yes

    询问是否初始化:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    Do you wish to update your shell profile to automatically initialize conda?
    This will activate conda on startup and change the command prompt when activated.
    If you'd prefer that conda's base environment not be activated on startup,
    run the following command when conda is activated:

    conda config --set auto_activate_base false

    You can undo this by running `conda init --reverse $SHELL`? [yes|no]
    [no] >>> yes

创建环境并启动环境

  • 激活 conda 环境

    1
    source ~/.bashrc
  • 修改镜像源

    1
    2
    cd ~/anaconda3
    vim .condarc
  • 将里面的内容替换为清华源

    1
    2
    3
    4
    5
    6
    channels:
    - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
    - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
    - defaults
    show_channel_urls: true
  • 重新更新镜像源

    1
    conda update conda
  • 创建 px4 环境

    1
    conda create -n px4 python=3.8
  • 修改 conda 配置

    1
    2
    conda config --set auto_activate_base false	# 默认不进入base环境
    vim ~/.bashrc
  • 在文件末尾添加

    1
    conda activate px4
  • 激活环境

    1
    source ~/.bashrc

安装 OpenCv

1
2
pip3 install numpy==1.24.1 -i https://mirrors.huaweicloud.com/repository/pypi/simple
pip3 install opencv-python -i https://mirrors.huaweicloud.com/repository/pypi/simple

git 配置

  • 关联 github 账号邮箱

    1
    2
    3
    4
    git config --global user.name "Your github username"
    # "Your github Name" 中填写你的github账号用户名
    git config --global user.email "yourgitemail@yourdomain.com"
    # "yourgitemail@yourdomain.com" 中填写你github注册的邮箱
  • 生成密钥

    1
    ssh-keygen -t ed25519 -C "username@email.com"
  • 用 cat 查看密钥

    1
    cat ~/.ssh/id_ed25519.pub

    前往 github 官网添加密钥

科学上网

  • 学校校园网 git clone 很容易出现网络问题,需要在板载上跑自己电脑的梯子。

    1
    2
    3
    4
    5
    6
    # 自己电脑的ip:自己梯子开的端口号,默认是7890
    export http_proxy=http://192.168.1.179:7890
    export https_proxy=https://192.168.1.179:7890
    # 记得将自己的梯子的允许局域网打开
    # 没有梯子的话可以去搞个精灵学院的 60R 还是 68R 的 400G 流量梯子, 比按月计费的好
    # 400G 足够用好几年了

安装 ros1

请参考 于 orangepi5 plus 上部署 ros1

注意!

px4 目前已弃用 ros1, 请使用 ros2, ros2 兼容性更好。

安装 ros2

请参考 于 orangepi5 plus 上部署 ros2

安装 mavros2 功能包

记得提前将 python 环境切换至 px4 环境

  • 创建工作空间

    1
    mkdir -p ~/px4_ws/src
  • 安装相关依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # python 相关
    pip3 install empy==3.3.4 -i https://mirrors.huaweicloud.com/repository/pypi/simple
    pip3 install pytest -i https://mirrors.huaweicloud.com/repository/pypi/simple
    pip3 install pytext-nlp -i https://mirrors.huaweicloud.com/repository/pypi/simple
    pip3 install pyyaml -i https://mirrors.huaweicloud.com/repository/pypi/simple
    pip3 install lark -i https://mirrors.huaweicloud.com/repository/pypi/simple
    pip3 install catkin_pkg -i https://mirrors.huaweicloud.com/repository/pypi/simple

    # 安装 pytext-nlp 时会出现 numpy 版本报错 无视即可

    # ros2 相关
    sudo apt-get install libmavlink-dev libmavconn-dev
    sudo apt-get install ros-foxy-ament-cmake
    sudo apt-get install -y python3-vcstool python3-rosinstall-generator python3-osrf-pycommon
    sudo apt-get install geographiclib-tools
    sudo apt-get install ros-foxy-geographic-msgs
    sudo apt-get install ros-foxy-libmavconn
    sudo apt-get install ros-foxy-geographic-*
    sudo apt-get install geographiclib-*
    sudo apt-get install libgeographic-*
    sudo apt-get install ros-foxy-eigen-stl-containers
    sudo apt-get install ros-foxy-diagnostic-updater
    sudo apt-get install ros-foxy-xacro -y
  • 安装 MAVROS2 功能包

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    cd ~/px4_ws

    rosinstall_generator --format repos mavlink | tee /tmp/mavlink.repos
    rosinstall_generator --format repos --upstream mavros | tee -a /tmp/mavros.repos
    vcs import src < /tmp/mavlink.repos

    rosdep update

    # 失败的话则无视
    rosdep install --from-paths src --ignore-src -y

    cd ~/px4_ws/src

    git clone -b 2.7.0 git@github.com:mavlink/mavros.git

    cd ~/px4_ws/src/mavros/mavros/scripts/

    # install_geographiclib_datasets.sh 下载很慢, 没有进度条, 耐心等待
    sudo ./install_geographiclib_datasets.sh

    # 重新打开终端后编译
    cd ~/px4_ws/

    # 将 #include"tf2_eigen.hpp" 替换为 tf2_eigen.h
    # ROS2 foxy 的 tf2_eigen 头文件为 .h 而不是 .hpp

    find ./src/mavros/ -type f -exec sed -i 's/tf2_eigen\.hpp/tf2_eigen\.h/g' {} +

    # 编译
    colcon build

    如果编译过程中出些 Waring , 需要解决后删除对应的编译文件, 再重新编译

    如:mavros 编译过程中出现 Waring

    1
    2
    3
    4
    5
    6
    # 分别删除工作空间下的 build , install 里对应的编译文件
    cd ~/px4_ws/
    rm -rf ./build/mavros*
    rm -rf ./install/mavros*

    colcon build

安装 t265 功能包

  • 添加密钥

    1
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key F6E65AC044F831AC80A06380C8B3A55A6F3EFCDE || sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-key F6E65AC044F831AC80A06380C8B3A55A6F3EFCDE
  • 添加第三方源

    1
    sudo add-apt-repository "deb https://librealsense.intel.com/Debian/apt-repo $(lsb_release -cs) main" -u
  • 安装依赖

    1
    2
    3
    4
    5
    6
    sudo apt-get install librealsense2-utils -y
    sudo apt-get install librealsense2-dev -y
    sudo apt install librealsense2-udev-rules -y
    sudo apt-get install git libssl-dev libusb-1.0-0-dev pkg-config
    sudo apt-get install libgtk-3-dev
    sudo apt-get install libglfw3-dev
  • 下载 t265 功能包

    Intel® RealSense™ SDK 2.0 (v2.54.1) 中已经明确说明版本不再支持 T265 最后一个支持的版本为 v2.50.0 ,而期间的几个版本虽然支持 T265 但官方说明不在对其进行测试。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    cd ~/px4_ws/src

    # 克隆 t265 依赖包
    git clone -b 4.0.4 https://github.com/IntelRealSense/realsense-ros.git

    # 克隆 t265 转换飞控的坐标的包
    # 自己三次开发的功能包 可能有误
    git clone https://github.com/AyasOwen/ros2_foxy_vision_to_mavros.git

    # 克隆 t265 的工具包
    git clone -b v2.50.0 https://github.com/IntelRealSense/librealsense.git
    # 无法下载的话可以先在 PC 上下载 .zip 再解压进板载中的工作空间内
  • 编译

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

    # 修改文件权限
    sudo chmod -R 777 /home/orangepi/px4_ws

    # 编译
    cd ~/px4_ws/
    colcon build

    # 激活环境
    source ~/px4_ws/install/setup.sh

    # 自启动环境(可选)
    sudo vim ~/.bashrc

    # 在最下方添加
    source /home/orangepi/px4_ws/install/setup.sh
    # /home/orangepi/ 为板载目录

安装 Offboard 功能包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 进入工作空间
cd ~/px4_ws/src

# 克隆 px4_msgs仓库
git clone https://github.com/PX4/px4_msgs.git
# 下不下来的话使用 git clone git@github.com:PX4/px4_msgs.git

# 克隆 px4_ros_com 仓库
git clone https://github.com/PX4/px4_ros_com.git
# 下不下来的话使用 git clone git@github.com:PX4/px4_ros_com.git

# 编译
cd ~/px4_ws/
colcon build

# 激活环境
source ./install/setup.sh
# 如果安装了多个工作环境的话请使用 source ./install/local_setup.sh
# 以免发生工作空间冲突