lrw306 发表于 2014-12-29 13:44:22

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()。

泡泡 发表于 2014-12-31 00:06:09

看天书的路过。

司马英 发表于 2014-12-29 21:31:13

zhous 发表于 2014-12-30 00:50:39

非常不错!!有价值!

相逢 发表于 2014-12-31 08:04:30

小辰辰 发表于 2014-12-31 14:29:17

此乃真天书!!
来自安卓客户端

飞跃长城 发表于 2015-1-3 00:22:27

294689006 发表于 2015-1-10 23:42:21

看不懂,有没有完整固件,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
来自安卓客户端

237166589 发表于 2015-1-11 11:00:16

{:1_1:}{:1_1:}{:1_1:}{:1_1:}{:1_1:}{:1_1:}

摩羯衍 发表于 2015-1-20 15:29:09

准备开发 基于 arduino 的飞控板的路过

feng2260 发表于 2015-3-6 09:27:06

问个问题:在APM库文件夹AP_HAL中有头文件AnalogIn.h该头文件中定义了两个类AnalogSource和AnalogIn,这两个函数里定义了一些虚函数,而这些虚函数的是在各自的子类中实现的;
在文件夹AP_HAL_PX4中有文件AnalogIn.h和AnalogIn.cpp,在头文件AnalogIn.h中定义了两个类PX4AnalogSource,它继承类AnalogSource,类PX4AnalogIn继承AnalogIn,在AnalogIn.cpp文件中,实现的函数的定义

我想请问大家,如何引用头文件AnalogIn.h,因为在两个文件夹中都有这个头文件

feng2260 发表于 2015-3-6 09:28:15

这是有关APM中硬件抽象层的使用,目前老是搞不明白

baidxi 发表于 2015-3-6 17:45:34

我也在弄,他自己的工具链不好用,而且不知道为什么,v2的固件会提示少文件,里边也确实没有那个文件。

kpvrzip 发表于 2015-4-22 13:32:46

学习收藏

clark0311 发表于 2015-4-29 17:30:44

学习了,收藏下先。

老晋 发表于 2015-5-4 08:36:03

天书!
老晋看不懂{:1_12:}

wushu_0229 发表于 2015-5-12 16:11:56

ZWDXBB 发表于 2014-12-31 00:06 static/image/common/back.gif
看天书的路过。

泡泡老师也是看天书啊?

erdao 发表于 2015-6-23 15:25:32

这个真不错哦

淡兰色 发表于 2015-6-26 23:50:07




楼主,能不能让树莓派用上这些代码?

everjoy 发表于 2015-7-2 16:43:35

强{:1_1:}{:1_1:}{:1_1:}{:1_1:}{:1_1:}{:1_1:}路过路过路过~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
页: [1] 2
查看完整版本: Pixhawk源码笔记一:APM代码基本结构