搭建ArduPilot的SITL仿真环境

摘要

搭建APM飞控(ArduCopter)的SITL仿真环境,在gazebo中实现无人机起飞.

关键信息

  • 飞控:ArduCopter 4.4.4
  • 系统:Ubuntu 20.04
  • gazebo 11
  • ROS1:noetic

原理简介

SITL仿真简介

[https://doc.cuav.net/tutorial/plane/simulation.html]
SITL(循环中的软件)模拟器允许您在没有任何硬件的情况下运行Plane,Copter或Rover。 它是使用普通C++编译器构建的自动驾驶仪代码,为您提供了一个本机可执行文件,允许您在没有硬件的情况下测试代码的行为。

SITL允许您直接在PC上运行ArduPilot,无需任何特殊硬件。 它利用了ArduPilot是一个可以在各种平台上运行的便携式自动驾驶仪。 您的PC只是ArduPilot可以构建和运行的另一个平台。

在SITL中运行时,传感器数据来自飞行模拟器中的飞行动力学模型。 ArduPilot拥有多种内置的飞行器模拟器,可以连接到几个外部模拟器。 这使得ArduPilot可以在各种类型的模型上进行测试。 例如,SITL可以模拟:

  • 多旋翼飞机
  • 固定翼飞机
  • 地面车辆
  • 相机的万向节
  • 天线跟踪器

各种可选的传感器,如激光雷达和光学流量传感器
添加新的模拟飞行器类型或传感器类型很简单。
ArduPilot在SITL上的一大优势是它可以让您访问桌面C ++开发所需的各种开发工具,如交互式调试器,静态分析器和动态分析工具。 这使得开发和测试ArduPilot的新功能变得更加简单。

waf编译工具简介

[https://waf.io/book/#_introduction]
Build systems are often tied to the specific frameworks they are designed for, such as Visual Studio projects with MSBuild and Angular.js projects with Npm. These solutions excel in providing specialized features for their respective frameworks but may fall short when dealing with other languages or project types. For example, Ant is better suited than Make for managing Java projects, while the reverse holds true for simple C projects. Given the constant evolution of programming languages and solutions, creating a one-size-fits-all build system is challenging, resulting in trade-offs between framework specialization and genericity.

Despite these differences, build systems aim to tackle common issues:

Executing compilers and scripts as separate processes

Running processes only when necessary by tracking "what has changed"

Parallelizing processes for efficiency

Assisting in executing software tests, including configuration checks

Offering support for typical configurations of popular compilers and tools

The Waf build system comes with several built-in features and offers the ability to extend its functionality with ease. Its design sets it apart from other frameworks in the following ways:

Waf requires only Python to function and does not depend on any additional software or libraries.

Waf does not introduce a new language, as it is built using reusable Python modules.

Waf does not rely on a code generator such as Makefiles, resulting in efficient and extensible builds.

Waf defines targets as objects, distinguishing between the definition of targets and the execution of commands.

Waf是一个用于自动化软件开发过程中特定阶段的工具,特别是针对源代码处理和自定义命令执行的构建系统。以下是关于Waf工具的简介,结合了您提供的网址内容和我自身的知识。
全称:Waf代表“Where's All the Files?”,意在帮助开发者找到项目中所有的文件,并自动化构建过程。

设计目标:Waf旨在提供一个灵活且可扩展的构建系统,它能够适应不同的编程语言和项目类型,而不仅仅局限于特定的框架。

主要特点

  1. 语言无关性:Waf是用Python编写的,这意味着它可以在任何支持Python的平台上运行,且不需要额外的软件或库。
  2. 无需新语言:与某些构建系统不同,Waf不引入新的语言或复杂的配置文件。它使用Python模块来构建项目,使得学习和使用变得更加简单。
  3. 无需Makefiles:Waf不依赖于Makefiles或其他代码生成器,这使得构建过程更加高效和可扩展。
  4. 目标作为对象:在Waf中,构建目标被定义为对象,这样可以清晰地区分目标的定义和命令的执行。

功能

  • 编译器执行:Waf可以执行编译器和脚本作为独立进程。
  • 依赖性跟踪:Waf仅在必要时运行进程,通过跟踪“什么发生了变化”来确定。
  • 并行处理:Waf能够并行化进程以提高效率。
  • 软件测试:Waf协助执行软件测试,包括配置检查。
  • 支持流行编译器和工具:Waf提供对流行编译器和工具的典型配置的支持。

使用场景

  • 构建自动化:Waf可以自动化项目的构建过程,包括编译源代码、创建归档文件等。
  • 跨平台支持:由于Waf基于Python,它能够跨平台工作,为不同操作系统提供一致的构建体验。
  • 扩展性:Waf允许开发者通过添加新的模块和功能来扩展其能力,以适应特定的构建需求。

获取和安装

  • Waf的发行文件可以从官方网站或镜像站点下载。
  • 源代码可以从Gitlab获取。
  • Waf提供了一个内嵌的签名,可以通过脚本进行验证,以确保文件的完整性。

运行Waf

  • Waf可以通过Python解释器直接运行,无需安装。
  • Waf在执行时会将自身的库解压到一个隐藏文件夹中,这个文件夹在删除后会被重新创建。

定制和分发

  • 用户可以构建自己的Waf可执行文件,并添加额外的扩展进行分发。
  • Waf的文件(包括waf-light和waflib下的文件)在BSD许可证下发布,这是一个非常宽松的许可证,但要求在衍生作品中包含版权声明。

Waf是一个强大而灵活的构建系统,适用于需要高度定制化构建过程的软件开发项目。通过提供对构建过程的精细控制,Waf帮助开发者提高构建效率并简化项目管理。

实现

下载ArduCopter源码

[https://doc.cuav.net/flight-controller/v5-autopilot/zh-hans/load-firmware/ardupilot-firmware.html]

我用夸克网盘分享了「ArduCopter4.4.4_full.zip」,点击链接即可保存。
链接:[https://pan.quark.cn/s/b2bfd3015745]
提取码:EkM5
大小:706 MB

我用夸克网盘分享了「waf-master.zip」,点击链接即可保存。
链接:[https://pan.quark.cn/s/a34a2d17ffc0]
提取码:FgfV

我用夸克网盘分享了「gcc-arm-none-eabi-10-2020-q4-major-aarch64-linux.tar.bz2」,点击链接即可保存。
链接:[https://pan.quark.cn/s/065acac0c443]
提取码:dbrD
大小:160MB

git clone https://gitcode.com/ArduPilot/ardupilot.git
cd ardupilot
git checkout Copter-4.4.4
rm -r modules
git submodule update --init --recursive
cd modules 
git clone https://gitcode.com/dronecan/libuavcan.git uavcan
git clone https://gitcode.com/QS2002/waf.git waf
git clone https://gitcode.com/google/benchmark.git gbenchmark
git clone https://gitcode.com/ArduPilot/mavlink.git mavlink
git clone https://gitcode.com/QS2002/googletest.git gtest
git clone https://gitcode.com/ArduPilot/ChibiOS.git ChibiOS
git clone https://gitcode.com/ArduPilot/gsoap.git gsoap
git clone https://gitcode.com/dronecan/DSDL.git DroneCAN/DSDL
git clone https://gitcode.com/QS2002/CrashDebug.git CrashDebug
git clone https://gitcode.com/dronecan/pydronecan.git DroneCAN/pydronecan
git clone https://gitcode.com/QS2002/dronecan_dsdlc.git DroneCAN/dronecan_dsdlc
git clone https://gitcode.com/dronecan/libcanard.git DroneCAN/libcanard

编译为sitl模式

[https://ardupilot.org/dev/docs/building-setup-linux.html]
[https://ardupilot.org/dev/docs/sitl-with-webots.html]
[https://ardupilot.org/dev/docs/setting-up-sitl-on-linux.html]
[https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh]
The SITL (software in the loop) simulator allows you to run Plane, Copter or Rover without any hardware. It is a build of the autopilot code using an ordinary C++ compiler, giving you a native executable that allows you to test the behaviour of the code without hardware.

# 安装conda
uname -m
bash Miniconda3-latest-Linux-aarch64.sh
source ~/.bashrc
conda --version
cd ~/Desktop/ArduCopter4.4.4
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
conda create -n waf python=3.8 -y
# 激活新创建的环境
conda init bash
conda activate waf
cd ~/Desktop/ArduCopter4.4.4/models/waf
# git checkout master # 这里使用夸克网盘的压缩包
cd ~/Desktop/ArduCopter4.4.4
./waf configure
# 使用 pip 安装剩余的包
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple imgviz==1.5.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple empy==3.3.4
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pexpect
# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pymavlink
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple mavproxy
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple dronecan
# 编译到sitl模式
./waf clean
cd ardupilot/ArduCopter
git reset --hard db53a28f321dc8233ee49d3cc6b395b5835c768b
./waf configure --board sitl --debug
# cd ~/Desktop/ArduCopter4.4.4/models/mavlink
# git clone https://gitcode.com/mavlink/c_library_v1.git
# git clone https://gitcode.com/mavlink/mavros.git
# git clone https://gitcode.com/mavlink/c_library_v2.git
# git reset --hard a2a3aebad8e913a0257d0011c5b7ea3c7d173a88
./waf -j8 copter -v
python /home/qsbye/Desktop/ArduCopter4.4.4/Tools/autotest/sim_vehicle.py --console --map -w -v ArduCopter
pip install --upgrade pymavlink MAVProxy --user
sudo apt-get install -y flightgear
# 测试
cd ~/Desktop/ArduCopter4.4.4 && python /home/qsbye/Desktop/ArduCopter4.4.4/Tools/autotest/sim_vehicle.py -L KSFO -v ArduCopter
# 配置protobuf版本
git clone https://gitcode.com/google/protobuf.git
cd protobuf
git checkout tags/v3.6.1
git checkout -b v3.6.1
git submodule update --init --recursive
./autogen.sh
# ./configure –prefix=/usr/local/
export CXXFLAGS="-fPIC"
./configure
sudo make -j20 -l20
# sudo make check
sudo make install
# 查看最终的版本
protoc --version 
# libprotoc 3.6.1
# 安装ardupilot_gazebo插件
git clone https://gitcode.com/ArduPilot/ardupilot_gazebo.git
cd ardupilot_gazebo
git checkout gazebo11
mkdir build
cd build
cmake ..
make
sudo make install

启动gazebo并进行仿真

pip install pyyaml rospkg
gazebo --verbose /home/qsbye/ardupilot_gazebo/worlds/iris_arducopter_runway.world
cd ~/Desktop/ArduCopter4.4.4 && python /home/qsbye/Desktop/ArduCopter4.4.4/Tools/autotest/sim_vehicle.py -v ArduCopter -f gazebo-iris --map --console --out=udp:0.0.0.0:14550
# nc -lu 0.0.0.0 14550
roslaunch mavros apm2.launch fcu_url:=udp://0.0.0.0:14550@ > /dev/null

进入mavproxy控制台后,依次输入:

mode guided
arm throttle
takeoff 1
position 1 0 0
mode land

效果

终端 gazebo中无人机悬停
posted @ 2024-06-03 15:32  qsBye  阅读(84)  评论(0编辑  收藏  举报