Pixhawk源码笔记一:APM代码基本结构
想要学习Pixhawk源码的朋友有福了,后边我会陆续的将Pixhawk的源码学习笔记整理出来分享给大家。敬请关注.欢迎交流。基础知识 详细参考: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()。
看天书的路过。 非常不错!!有价值! 此乃真天书!!
来自安卓客户端 看不懂,有没有完整固件,http://www.moz8.com/mobcent/app/web/../../app/data/phiz/default/03.pnghttp://www.moz8.com/mobcent/app/web/../../app/data/phiz/default/03.pnghttp://www.moz8.com/mobcent/app/web/../../app/data/phiz/default/03.pnghttp://www.moz8.com/mobcent/app/web/../../app/data/phiz/default/03.png
来自安卓客户端 {:1_1:}{:1_1:}{:1_1:}{:1_1:}{:1_1:}{:1_1:} 准备开发 基于 arduino 的飞控板的路过 问个问题:在APM库文件夹AP_HAL中有头文件AnalogIn.h该头文件中定义了两个类AnalogSource和AnalogIn,这两个函数里定义了一些虚函数,而这些虚函数的是在各自的子类中实现的;
在文件夹AP_HAL_PX4中有文件AnalogIn.h和AnalogIn.cpp,在头文件AnalogIn.h中定义了两个类PX4AnalogSource,它继承类AnalogSource,类PX4AnalogIn继承AnalogIn,在AnalogIn.cpp文件中,实现的函数的定义
我想请问大家,如何引用头文件AnalogIn.h,因为在两个文件夹中都有这个头文件 这是有关APM中硬件抽象层的使用,目前老是搞不明白 我也在弄,他自己的工具链不好用,而且不知道为什么,v2的固件会提示少文件,里边也确实没有那个文件。
学习收藏 学习了,收藏下先。 天书!
老晋看不懂{:1_12:}
ZWDXBB 发表于 2014-12-31 00:06 static/image/common/back.gif
看天书的路过。
泡泡老师也是看天书啊?
这个真不错哦
楼主,能不能让树莓派用上这些代码?
强{:1_1:}{:1_1:}{:1_1:}{:1_1:}{:1_1:}{:1_1:}路过路过路过~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
页:
[1]
2