BetaFlight开源工程结构简明介绍

BetaFlight开源工程结构简明介绍

  • Step1 获取开源代码
    • 开源代码版本
    • 克隆开源代码
  • Step2 了解工程情况
    • 支持模型类型:多旋翼&固定翼
    • 支持特性&功能
    • 安装&文档链接
    • 配置工具下载
    • 其他介绍(略)
  • Step3 Makefile结构
    • make help
    • KAKUTEF7
    • Makefile文件关联
    • 示例变量
  • Step4 工程目录结构
    • 目录结构查询
    • 重要目录分析
  • Step5 工程分析回顾

BetaFlight是穿越机比较成熟的飞控OpenSouce。这里有一点需要注意的是BetaFlight官网只是提供开源软件工程,任何相关穿越机硬件的生产制造都不是BetaFlight提供。

当然开源飞控代码并非BetaFlight一家,这里提供一张开源飞控代码历史发展图,可以给大家一个更直观的了解。有兴趣的朋友可以点开链接,以便有更多信息(当然这个是youtube的,国内不知道哪里有这个视频链接,VPN还是需要的,否则无法打开)。
飞控开源代码历史进程因为,之前DIY四轴飞控调试起飞用的就是这套代码,所以这里深入一下开源工程结构,为后续进一步深入了解内部模块和控制逻辑奠定基础。

为了更好的理解开源代码的工程结构,这里基于4.3.0代码进行分析和整理归纳,大致分以下几个步骤:

  1. 获取开源代码
  2. 了解工程情况
  3. Makefile结构
  4. 工程目录结构
  5. 工程分析回顾

注:这里还是采用工程步骤的方式进行推进,其实很多开源代码也是这种方式去摸索和理解的。

Step1 获取开源代码

开源代码版本

首先,获取代码的步骤很便捷的,直接通过github下载来获取,要注意的是版本(本次实验采用4.3.0,具体version不用太在意,结构化的东西只要大版本差异不大,基本是类似的)

commit 9e894586b71b29393ce0c38aa3c332c9e436f6d9 (HEAD -> master, origin/master, origin/HEAD)
Merge: 1a6fa4ddb 3af0ee7a7
Author: haslinghuis <mark@numloq.nl>
Date:   Mon May 9 13:28:00 2022 +0200

    Merge pull request #11584 from SteveCEvans/fix_ICM_20689

    Fix ICM-20689 initialisation

克隆开源代码

然后,要确保系统上已经安装git工具。通过clone命令将代码下载到本地,版本问题请大家自行匹配,这里为了确保一致性,所以将代码版本记录下来了(其实这个对学习来说并不太重要,主要是方法论 _)。

# git clone https://github.com/betaflight/betaflight.git

Step2 了解工程情况

了解工程最简单的方法,就是阅读README。通常github上code页面显示的就是README.md文件,在浏览器上格式更美观(当然vi也是可以的)。
这里摘要部分内容,做个情况了解,为后续研读提供方向。

支持模型类型:多旋翼&固定翼

Betaflight is flight controller software (firmware) used to fly multi-rotor craft and fixed wing craft.

支持特性&功能

* Multi-color RGB LED strip support (each LED can be a different color using variable length WS2811 Addressable RGB strips - use for Orientation Indicators, Low Battery Warning, Flight Mode Status, Initialization Troubleshooting, etc)
* DShot (150, 300 and 600), Multishot, Oneshot (125 and 42) and Proshot1000 motor protocol support
* Blackbox flight recorder logging (to onboard flash or external microSD card where equipped)
* Support for targets that use the STM32 F4, G4, F7 and H7 processors
* PWM, PPM, SPI, and Serial (SBus, SumH, SumD, Spektrum 1024/2048, XBus, etc) RX connection with failsafe detection
* Multiple telemetry protocols (CRSF, FrSky, HoTT smart-port, MSP, etc)
* RSSI via ADC - Uses ADC to read PWM RSSI signals, tested with FrSky D4R-II, X8R, X4R-SB, & XSR
* OSD support & configuration without needing third-party OSD software/firmware/comm devices
* OLED Displays - Display information on: Battery voltage/current/mAh, profile, rate profile, mode, version, sensors, etc
* In-flight manual PID tuning and rate adjustment
* PID and filter tuning using sliders
* Rate profiles and in-flight selection of them
* Configurable serial ports for Serial RX, Telemetry, ESC telemetry, MSP, GPS, OSD, Sonar, etc - Use most devices on any port, softserial included
* VTX support for Unify Pro and IRC Tramp
* and MUCH, MUCH more.

安装&文档链接

## Installation & Documentation

See: https://github.com/betaflight/betaflight/wiki

配置工具下载

## Configuration Tool

To configure Betaflight you should use the Betaflight-configurator GUI tool (Windows/OSX/Linux) which can be found here:

https://github.com/betaflight/betaflight-configurator/releases/latest

其他介绍(略)

这里我们着重列举对我们了解开源工程比较重要的部分,其他当然也重要,不过没有那么重要,了解我的意思_

Step3 Makefile结构

C是编译性语言,从现在的角度来说,这种工程也是相对比较古老的。毕竟现在解释性语言大量的应用,比如:Java,Python等等。作为古老C语言工程来说,最重要的工程入口是Makefile(关于Makefile的学习可以通过GNU Makefile Mannual学习,也可以参阅如何编写Makefile)

make help

首先,看下make help都有哪些动作可以执行。


$ make help

Makefile for the betaflight firmware

Usage:
        make [V=] [TARGET=] [OPTIONS=""]
Or:
        make  [V=] [OPTIONS=""]

Valid TARGET values are: AFROMINI AG3XF4 AG3XF7 AIKONF4 AIORACERF3 AIR32 AIRBOTF4 AIRBOTF4SD AIRBOTF7 AIRF7 AIRHEROF3 ALIENFLIGHTF1 ALIENFLIGHTF3 ALIENFLIGHTF4 ALIENFLIGHTNGF7 ALIENWHOOPF4 ALIENWHOOPF7 ANYFCF7 ANYFCM7 BEEBRAIN BEEBRAIN_V2D BEEBRAIN_V2F BEEROTORF4 BEESTORM BETAFLIGHTF3 BETAFLIGHTF4 BETAFPVF4SX1280 BLUEJAYF4 CC3D CC3D_OPBL CHEBUZZF3 CJMCU CLRACINGF4 CLRACINGF7 COLIBRI COLIBRI_OPBL COLIBRI_RACE CRAZYBEEF3DX CRAZYBEEF3FR CRAZYBEEF3FS CRAZYBEEF4DX CRAZYBEEF4FR CRAZYBEEF4FS CRAZYBEEF4SX1280 CRAZYFLIE2 CRAZYFLIE2BQ DALRCF405 DALRCF722DUAL DOGE DYSF4PRO EACHIF3 ELINF405 ELINF722 ELLE0 EMAXF4SX1280 EXF722DUAL EXUAVF4PRO F4BY FF_ACROWHOOPSP FF_FORTINIF4 FF_FORTINIF4_REV03 FF_KOMBINI FF_PIKOBLX FF_PIKOF4 FF_PIKOF4OSD FF_RACEPIT FF_RADIANCE FISHDRONEF4 FLIP32F3OSD FLYWOOF405 FLYWOOF411 FLYWOOF7DUAL FOXEERF405 FOXEERF722DUAL FPVM_BETAFLIGHTF7 FRSKYF3 FRSKYF4 FURYF3 FURYF3OSD FURYF4 FURYF4OSD HAKRCF405 HAKRCF411 HAKRCF722 IFLIGHT_H743_AIO IFLIGHT_H743_AIO_V2 IMPULSERCF3 IRCFUSIONF3 IRCSYNERGYF3 ISHAPEDF3 JHEF7DUAL KAKUTEF4 KAKUTEF4V2 KAKUTEF7 KAKUTEF7MINI KAKUTEF7V2 KISSCC KISSFC KISSFCV2F7 KIWIF4 KIWIF4V2 KROOZX LUMBAF3 LUXF4OSD LUXMINIF7 LUXV2_RACE LUX_RACE MAMBAF411 MAMBAF722 MATEKF405 MATEKF411 MATEKF411RX MATEKF722 MATEKF722SE MATEKH743 MERAKRCF405 MERAKRCF722 MICROSCISKY MIDELICF3 MLTEMPF4 MLTYPHF4 MOTOLAB MULTIFLITEPICO NAZE NERO NEUTRONRCF411SX1280 NOX NUCLEOF103RG NUCLEOF303RE NUCLEOF446RE NUCLEOF7 NUCLEOF722 NUCLEOH723ZG NUCLEOH725ZG NUCLEOH743 NUCLEOH743_RAMBASED NUCLEOH7A3ZI OMNIBUS OMNIBUSF4 OMNIBUSF4FW OMNIBUSF4NANOV7 OMNIBUSF4SD OMNIBUSF4V6 OMNIBUSF7 OMNIBUSF7NANOV7 OMNIBUSF7V2 OMNINXT4 OMNINXT7 PIRXF4 PLUMF4 PODIUMF4 PYRODRONEF4 RACEBASE RCEXPLORERF3 REVO REVOLT REVOLTOSD REVONANO REVO_OPBL RG_SSD_F3 RMDO RUSHCORE7 SINGULARITY SIRINFPV SITL SKYZONEF405 SOULF4 SPARKY SPARKY2 SPEEDYBEEF4 SPEEDYBEEF7 SPEKTRUMF400 SPRACINGF3 SPRACINGF3EVO SPRACINGF3MINI SPRACINGF3MQ SPRACINGF3NEO SPRACINGF4EVO SPRACINGF4NEO SPRACINGF7DUAL SPRACINGH7EXTREME SPRACINGH7NANO SPRACINGH7ZERO STACKX STM32F3DISCOVERY STM32F405 STM32F411 STM32F411DISCOVERY STM32F4DISCOVERY STM32F4DISCOVERY_DEBUG STM32F745 STM32F7X2 STM32G47X STM32H743 SYNERGYF4 TINYBEEF3 TINYFISH TMOTORF4 TMOTORF7 TRANSTECF411 TRANSTECF7 UAVPNG030MINI VGOODRCF4 VRRACE WORMFC XILOF4 XRACERF4 X_RACERSPI YUPIF4 YUPIF7 ZCOREF3

V                 : Set verbosity level based on the V= parameter
                    V=0 Low
                    V=1 High
compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
all               : Build all currently built targets
all_all : Build all targets (including legacy / unsupported)
unified : build all Unified Targets
unified_zip : build all Unified Targets as zip files (for posting on GitHub)
legacy : Build legacy targets
unsupported : Build unsupported targets
pre-push : The minimum verification that should be run before pushing, to check if CI has a chance of succeeding
targets-group-1   : build some targets
targets-group-2   : build some targets
targets-group-rest: build the rest of the targets (not listed in the other groups)
clean             : clean up temporary / machine-generated files
test_clean        : clean up temporary / machine-generated files (tests)
_clean    : clean up one specific target (alias for above)
clean_all         : clean all valid targets
_flash    : build and flash a target
tty_flash         : flash firmware (.hex) onto flight controller via a serial port
dfu_flash         : flash firmware (.bin) onto flight controller via a DFU mode
st-flash          : flash firmware (.bin) onto flight controller
_zip    : build target and zip it (useful for posting to GitHub)
unbrick           : unbrick flight controller
cppcheck          : run static analysis on C source code
version           : print firmware version
help              : print this help message and exit
targets           : print a list of all valid target platforms (for consumption by scripts)
target-mcu        : print the MCU type of the target
targets-by-mcu    : make all targets that have a MCU_TYPE mcu
targets-f3        : make all F3 targets
targets-f4        : make all F4 targets
targets-f7        : make all F7 targets
test              : run the Betaflight test suite
junittest         : run the Betaflight test suite, producing Junit XML result files.
test-representative: run a representative subset of the Betaflight test suite (i.e. run all tests, but run each expanded test only for one target)
test-all: run the Betaflight test suite including all per-target expanded tests
test_help         : print the help message for the test suite (including a list of the available tests)
test_versions         : print the compiler versions used for the test suite
test_%            : run test 'test_%' from the test suite
arm_sdk_install   : Install Arm SDK
arm_sdk_clean     : Uninstall Arm SDK

KAKUTEF7

四轴DIY硬件使用的是Kakute F7 AIO V1.5,通过上面help内容,可以看到KAKUTEF7 是有效的FlightControl配置,后续主要以这个为示例进行介绍。

Holybro Kakute F7 AIO review

Makefile文件关联

从根目录下的Makefile文件作为入口,先了解工程里面各个include makefile关联。

从Makefile的设计和历史遗迹上可以看出,已经针对功能和特定FlightControl进行了模块化切分。

  • 通用代码部分抽象出来通用规划
  • 专用FlightControl通过$(TARGET_MCU).mk和target.mk独立出来
Makefile
 ├──> include $(ROOT)/make/build_verbosity.mk
 ├──> include $(ROOT)/make/system-id.mk
 ├──> -include $(ROOT)/make/local.mk
 ├──> include $(ROOT)/make/checks.mk
 ├──> include $(ROOT)/make/$(OSFAMILY).mk          // 编译环境,取决于system-id.mk
 ├──> include $(ROOT)/make/tools.mk                // 交叉编译gcc-arm-none-eabi等工具
 ├──> include $(ROOT)/make/targets.mk              // 不同airframe配置
 │   ├──> include $(ROOT)/make/targets_list.mk
 │   ├──> -include $(ROOT)/src/main/target/$(BASE_TARGET)/$(TARGET).mk
 │   └──> -include $(ROOT)/src/main/target/$(BASE_TARGET)/target.mk
 ├──> include $(ROOT)/make/mcu/$(TARGET_MCU).mk    // 不同FlightControl对应的MCU类型,F4/F7/H7等
 ├──> include $(ROOT)/make/openocd.mk
 ├──> include $(ROOT)/make/source.mk               // 主要通用源代码
 └──> -include $(TARGET_DEPS)

示例变量

Ubuntu 20.04环境搭建编译环境,安装arm-gcc 9.3.1

# make arm_sdk_install

当时使用KAKUTEF7作为FlightControl时,Makefile内部关键变量如下所示:

$(OSFAMILY)    = linux
$(BASE_TARGET) = KAKUTEF7
$(TARGET_MCU)  = STM32F7
$(TARGET)      = STM32F7

Step4 工程目录结构

目录结构查询

工程目录结构通过tree命令可以非常便捷的查看。

# tree --help
# tree -L 2

重要目录分析

实际上我们更加关注的是重要目录主要用途Kakute F7为例)。

betaflight
 ├──> src/main          //源代码&引用头文件目录
 │   ├──> main.c        //C代码入口main()
 │   ├──> ...           //省略很多模块,后续会深入分析,比如:blackbox,osd等
 │   ├──> drivers       //各类硬件芯片驱动
 │   ├──> target        //各类目标板(airframe):代码+配置
 │   │   └──> KAKUTEF7  //举例:kakute F7 AIO V1.5
 │   │       ├──> config.c
 │   │       ├──> target.c
 │   │       ├──> target.h
 │   │       └──> target.mk
 │   └──> startup
 │       └──> startup_stm32f745xx.s
 ├──> obj               //编译目标文件目录
 │   ├──> main
 │   └──> betaflight_4.3.0_KAKUTEF7_9e894586b.hex
 ├──> lib/main          //库代码目录
 │   ├──> MAVLink
 │   ├──> CMSIS
 │   ├──> STM32_USB-FS-Device_Driver
 │   ├──> FatFS
 │   └──> STM32F7
 ├──> src/link          //链接脚本目录
 │   ├──> stm32_flash_f7_split.ld
 │   └──> stm32_flash_f74x.ld
 ├──> make              //通用makefile目录
 ├──> tools             //本地工具安装,比如:gcc-arm-none-eabi-9-2020-q2-update
 └──> downloads         //下载目录,比如:下载的工具链压缩包gcc-arm-none-eabi-9-2020-q2-update-x86_64-linux.tar.bz2

Step5 工程分析回顾

整个研读过程,也重新回顾了Makefile的一些基础知识,可见不用就荒废了。哈哈~~~

  1. Makefile常用函数
  2. 变量“MAKEFILE_LIST”:make程序在使用指示符“include”多个makefile文件时,读取的文件名将会被自动依次追加到变量“MAKEFILE_LIST”的定义域中。通过获取此变量的最后一个字串来获取当前make程序正在处理的makefile文件名。
$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
$(lastword $(MAKEFILE_LIST))
  1. SITL(Software In The Loop):JSBSim Simulation

好了,希望上面的对大家了解整个工程结构有个简单了解,详细的我们后面有时间再一个一个研读。

你可能感兴趣的:(xFlight,git,github,stm32)