轉載:Pixhawk源碼筆記一:APM代碼基本結構

 

 

 

Pixhawk源碼筆記一:APM代碼基本結構

 轉自 新浪微博@WalkAnt html

基礎知識

        詳細參考:http://dev.ardupilot.com/wiki/learning-the-ardupilot-codebase/git

第一部分:介紹

        詳細參考:http://dev.ardupilot.com/wiki/learning-ardupilot-introduction/github

        ArduPilot 代碼分爲5個主要部分,基本結構分類以下:less

  •  vehicle directories
  •  AP_HAL
  •  libraries
  •  tools directories
  •  external support code

一、vehicle directories模型類型

        當前共有4種模型:ArduPlane, ArduCopter, APMrover2 and AntennaTracker。都是.pde文件,就是爲了兼容arduino平臺,之後可能會放棄。ide

二、AP_HAL硬件抽象層

        硬件抽象層,使得在不一樣硬件平臺上的移植變得簡單。函數

        其中AP_HAL目錄定義了一個通用的接口。其餘的目錄AP_HAL_XXX針對不一樣硬件平臺進行詳細的定義。例如AP_HAL_AVR目錄對於AVR平臺,AP_HAL_PX4對應PX4平臺,AP_HAL_Linux對應Linux平臺。工具

三、tools directories工具目錄

        主要提供支持。For examples, tools/autotest provides the autotest infrastructure behind theautotest.diydrones.com site and tools/Replay provides our log replay utility.oop

四、external support code外部支持代碼

        對於其餘平臺,須要外部支持代碼。例如Pixhawk、PX4的支持代碼以下:學習

  •  PX4NuttX – 板載實時系統。the core NuttX RTOS used on PX4 boards
  •  PX4Firmware – PX4固件。the base PX4 middleware and drivers used on PX4 boards
  •  uavcan – 飛行器CAN通訊協議。the uavcan CANBUS implementation used in ArduPilot
  •  mavlink – Mavlink通訊協議。the mavlink protocol and code generator

五、系統編譯

        針對不一樣的硬件板,編譯能夠採用「make TARGET」的形式。ui

  •  make apm1 – the APM1 board
  •  make apm2 – the APM2 board
  •  make px4-v1 – the PX4v1
  •  make px4-v2 – the Pixhawk

        若是要移植到新的硬件,能夠在mk/targets.mk文件中添加。

        好比: make apm2-octa -j8

        或者: make px4-v2 -j8

        採用8通道並行編譯方式,針對APM、Pixhawk硬件板(AVR、STM32),編譯八旋翼代碼。

第二部分: 學習sketch例程代碼

        http://dev.ardupilot.com/wiki/learning-ardupilot-the-example-sketches/

        sketch,是指使用 .pde 文件編寫的主程序。

        開始以前,你能夠試着閱讀、編譯並運行下面的sketches

  •  libraries/AP_GPS/examples/GPS_AUTO_test
  •  libraries/AP_InertialSensor/examples/INS_generic
  •  libraries/AP_Compass/examples/AP_Compass_test
  •  libraries/AP_Baro/examples/BARO_generic
  •  libraries/AP_AHRS/examples/AHRS_Test

        例如,下面的編譯方法,將在Pixhawk上安裝AP_GPS例程sketch。

               cd libraries/AP_GPS/examples/GPS_AUTO_test

                make px4-clean

               make px4-v2

               make px4-v2-upload

        正確理解sketch例程代碼,咱們以GPS_AUTO_test.pde代碼爲例(目錄ardupilot\libraries\AP_GPS\examples\GPS_AUTO_test),主要幾個特色:

        一、 pde文件包含不少 includes;

        二、 定義了 hal 引用聲明;

        三、 代碼很是粗糙;

        四、 setup() 和 loop()函數

一、include文件

        pde文件轉變爲C++文件後,提供必要的庫引用支持。

二、hal引用聲明

        定義以下:

        const AP_HAL::HAL& hal = AP_HAL_BOARD_DRIVER;// pixhawk等價於AP_HAL_PX4

        該定義,方便訪問硬件接口,好比console終端、定時器、I2C、SPI接口等。

        實際的定義是在HAL_PX4_Class.cpp中定義,以下:

                const HAL_PX4 AP_HAL_PX4;

        hal是針對 AP_HAL_PX4 的引用。

        常常使用的方法以下:

  •  終端字符輸出。hal.console->printf() and hal.console->printf_P() to print strings (use the _P to use less memory on AVR)
  •  獲取當前運行時間。hal.scheduler->millis() and hal.scheduler->micros() to get the time since boot
  •  延時。hal.scheduler->delay() and hal.scheduler->delay_microseconds() to sleep for a short time
  •  IO輸入輸出。hal.gpio->pinMode(), hal.gpio->read() and hal.gpio->write() for accessing GPIO pins
  •  I2C操做,hal.i2c
  •  SPI操做,hal.spi

三、setup()和loop()

        每一個sketch都有一個setup()和loop()函數。板子啓動時,setup()被調用。這些調用都來自HAL代碼中的main()函數調用(HAL_PX4_Class.cpp文件main_loop())。setup()函數只調用一次,用於初始化全部libraries。

        Loop()循環被調用,執行主任務。

四、AP_HAL_MAIN()宏指令

        每個sketch(.pde文件)最底部,都有一個「AP_HAL_MAIN();」指令,它是一個HAL宏,用於定義一個C++ main函數,整個程序的入口。它真正的定義在AP_HAL_PX4_Main.h中。

                #define AP_HAL_MAIN() \

                extern "C" __EXPORT int SKETCH_MAIN(int argc, char * const argv[]); \

                int SKETCH_MAIN(int argc, char * const argv[]) { \

                hal.init(argc, argv); \

                return OK; \

                }

        做爲程序的起點,在AP_HAL_MAIN()裏,就正式調用了hal.init()初始化代碼。

        程序的執行過程就是:程序起點AP_HAL_MAIN() à hal.init()  à hal.main_loop() à sketch中的setup()和loop()。

相關文章
相關標籤/搜索