打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
源码笔记:APM代码基本结构

基础知识
 详细参考:http://dev.ardupilot.com/wiki/learning-the-ardupilot-codebase/


第一部分:介绍

详细参考:http://dev.ardupilot.com/wiki/learning-ardupilot-introduction/
       ArduPilot 代码分为5个主要部分,基本结构分类如下:

vehicle directoriesAP_HALlibrariestools directoriesexternal 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 boardsPX4Firmware – PX4固件。the base PX4 middleware and drivers used on PX4 boardsuavcan – 飞行器CAN通信协议。the uavcan CANBUS implementation used in ArduPilotmavlink – Mavlink通信协议。the mavlink protocol and code generator

5、系统编译
       针对不同的硬件板,编译可以采用“make TARGET”的形式。

make apm1 – the APM1 boardmake apm2 – the APM2 boardmake px4-v1 – the PX4v1make 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_testlibraries/AP_InertialSensor/examples/INS_genericlibraries/AP_Compass/examples/AP_Compass_testlibraries/AP_Baro/examples/BARO_genericlibraries/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 timeIO输入输出。hal.gpio->pinMode(), hal.gpio->read() and hal.gpio->write() for accessing GPIO pinsI2C操作,hal.i2cSPI操作,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()。


源自:CSDN博客


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ArduPilot代码库学习
APM飞控若干整理
不能不知道的几大无人机飞控系统
知行合一ArduPilot | ArduPilot系统框架简述
[转载]Pixhawk源码笔记二:APM线程
APM,开源飞控的领军者
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服