PX4仿真方案

PX4 仿真调试

建议在 WSL 上进行仿真

不要在板载上进行仿真!!!!

该仿真方案所采用的环境为 Ubuntu20.04 ROS2-Foxy

如需进行 Ubuntu22.04 环境的仿真,自行将 Gazebo Classic 改为 gz_x500

前置环境

安装 ros2

请参考 于 orangepi5 plus 上部署 ros2

conda 安装

  • 看查内核版本

    1
    uname -m

    诺为 x86_64

  • 安装相应的 x86_64 版本的 conda:

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

    # 下载完后再安装
    sh Anaconda3-2024.10-1-Linux-x86_64.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
  • 创建 ros2 环境

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

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

    1
    conda activate ros2
  • 激活环境

    1
    source ~/.bashrc

安装依赖

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
sudo apt install --reinstall -y \
build-essential \
libc6-dev \
libc6-dev-i386 \
gcc \
g++ \

sudo apt-get install \
libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
gstreamer1.0-tools -y

sudo apt install -y gstreamer1.0-gl libfuse2 libxcb-xinerama0 libxkbcommon-x11-0 libxcb-cursor-dev
sudo apt install mesa-utils libgl1-mesa-glx -y

# python 相关
pip3 install -U pip setuptools -i https://mirrors.huaweicloud.com/repository/pypi/simple
pip3 install numpy==1.24.1 -i https://mirrors.huaweicloud.com/repository/pypi/simple
pip3 install kconfiglib -i https://mirrors.huaweicloud.com/repository/pypi/simple
pip3 install pyros-genmsg -i https://mirrors.huaweicloud.com/repository/pypi/simple
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 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
pip3 install lxml -i https://mirrors.huaweicloud.com/repository/pypi/simple
pip3 install empy==3.3.4 -i https://mirrors.huaweicloud.com/repository/pypi/simple
pip3 install opencv-python -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
sudo apt-get install ros-foxy-octomap*
sudo apt-get install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential
sudo apt-get install -y libxml2-dev libxslt-dev

科学上网

  • 学校校园网 git clone 很容易出现网络问题,需要跑梯子。

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

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 官网添加密钥

Gazebo Classic 安装

  • 添加软件源

    1
    sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'
  • 添加密钥

    1
    sudo wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
  • 安装 Gazebo

    1
    2
    sudo apt update
    sudo apt install gazebo11 libgazebo11-dev -y

安装 Micro XRCE - DDS

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
# 进入 conda 环境
conda activate ros2
cd ~/px4
git clone -b v2.4.2 https://github.com/eProsima/Micro-XRCE-DDS-Agent.git
cd Micro-XRCE-DDS-Agent
# 修改 CMakeLists 文件
vim ./CMakeLists.txt
# 找到 99 行
#------------------ 我是分割线 ------------------
if(UAGENT_FAST_PROFILE)
if(UAGENT_USE_SYSTEM_FASTDDS)
set(_fastdds_version 2)
else()
set(_fastdds_version 2.12)
set(_fastdds_tag v2.12.2) #修改这里,原先是 set(_fastdds_tag 2.12.x)
set(_foonathan_memory_tag v0.7-3) # This tag should be updated every time it gets updated in foonathan_memory_vendor eProsima's package
endif()
list(APPEND _deps "fastrtps\;${_fastdds_version}")
endif()
#------------------ 我是分割线 ------------------
mkdir build
cd build
cmake ..
# 核多的话可以 make -j8多核编译(看具体多少核 决定-j多少)
make -j8
sudo make install
sudo ldconfig /usr/local/lib/

# 测试
MicroXRCEAgent udp4 -p 8888

固件下载

一定要科学上网

确保 Micro XRCE - DDS 编译完成

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
31
32
33
34
35
# 进入 conda 环境
conda activate ros2
sudo apt install proxychains4
sudo apt-get install git
# 因为实体飞机用的是 1.14.3 版本的固件,这边仿真也用 1.14.3
mkdir px4
cd ./px4

git clone -b v1.14.3 git@github.com:PX4/PX4-Autopilot.git

cd ./PX4-Autopilot/
# 补全子模块
git submodule update --init –recursive

# 安装依赖
sudo apt install ros-dev-tools
cd ./Tools/setup

# 修改 requirements.txt 文件内容
vim ./requirements.txt
# 将 matplotlit>=3.0.*改为 3.0.1或3.0.0
# 保存退出

chmod +x ubuntu.sh
./ubuntu.sh

# 编译
cd ~/px4/PX4-Autopilot/
chmod +x Tools/check_submodules.sh
chmod +x /home/aya/px4/PX4-Autopilot/Tools/simulation/gazebo-classic/*.sh
make px4_sitl gazebo-classic

# 编译完成后会出现 Gazebo 页面
# 输入起飞指令进行验证
commander takeoff

安装 QGC 地面站

  • 方案一:

    在 Windows 上打开 QGC 地面站直接连接

  • 方案二(不推荐):

    专门下载 linux 的 QGC 地面站

工作空间配置

  • 创建工作空间

    1
    mkdir -p ~/px4/px4_ws/src
  • 安装 px4 功能包

    1
    2
    3
    cd ~/px4/px4_ws/src
    git clone https://github.com/PX4/px4_msgs.git -b release/1.14
    git clone https://github.com/PX4/px4_ros_com.git -b release/v1.14
  • 编译

    1
    colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=Release

总测试

  • 终端一(启动 Micro XRCE - DDS):

    1
    MicroXRCEAgent udp4 -p 8888
  • 终端二(启动仿真):

    1
    2
    cd ~/px4/PX4-Autopilot/
    make px4_sitl gazebo-classic
  • 终端三(启动 ros2 节点):

    1
    2
    3
    cd ~/px4/px4_ws/
    source ./install/setup.sh
    ros2 launch px4_ros_com offboard_control_launch.yaml

    嫌麻烦的话可以将 ros 环境直接存进.bashrc中

    1
    echo "source ~/px4/px4_ws//install/setup.bash" >> ~/.bashrc

如果全部正常,则可以看到无人机起飞并悬停