转载:Pixhawk源码笔记一:APM代码基本结构
?
?
?
?轉自 新浪微博@WalkAnt?
基礎知識
????????詳細參考:http://dev.ardupilot.com/wiki/learning-the-ardupilot-codebase/
第一部分:介紹
????????詳細參考:http://dev.ardupilot.com/wiki/learning-ardupilot-introduction/
????????ArduPilot 代碼分為5個主要部分,基本結構分類如下:
- ?vehicle directories
- ?AP_HAL
- ?libraries
- ?tools directories
- ?external support code
1、vehicle directories模型類型
????????當前共有4種模型:ArduPlane, ArduCopter, APMrover2 and AntennaTracker。都是.pde文件,就是為了兼容arduino平臺,以后可能會放棄。
2、AP_HAL硬件抽象層
????????硬件抽象層,使得在不同硬件平臺上的移植變得簡單。
????????其中AP_HAL目錄定義了一個通用的接口。其他的目錄AP_HAL_XXX針對不同硬件平臺進行詳細的定義。例如AP_HAL_AVR目錄對于AVR平臺,AP_HAL_PX4對應PX4平臺,AP_HAL_Linux對應Linux平臺。
3、tools directories工具目錄
????????主要提供支持。For examples, tools/autotest provides the autotest infrastructure behind theautotest.diydrones.com?site and tools/Replay provides our log replay utility.
4、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
5、系統編譯
????????針對不同的硬件板,編譯可以采用“make TARGET”的形式。
- ?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),主要幾個特點:
????????1、 pde文件包含很多 includes;
????????2、 定義了 hal 引用聲明;
????????3、 代碼非常粗糙;
????????4、 setup() 和 loop()函數
1、include文件
????????pde文件轉變為C++文件后,提供必要的庫引用支持。
2、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
3、setup()和loop()
????????每個sketch都有一個setup()和loop()函數。板子啟動時,setup()被調用。這些調用都來自HAL代碼中的main()函數調用(HAL_PX4_Class.cpp文件main_loop())。setup()函數只調用一次,用于初始化所有libraries。
????????Loop()循環被調用,執行主任務。
4、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()。
轉載于:https://www.cnblogs.com/pinlyu/p/4632454.html
總結
以上是生活随笔為你收集整理的转载:Pixhawk源码笔记一:APM代码基本结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ( function(){…} )()
- 下一篇: 读《一个程序猿的生命周期》有感