Pixhawk源码笔记一:APM代码基本结构

来源:互联网 发布:淘宝感谢卡内容简短 编辑:程序博客网 时间:2024/04/30 00:10

Pixhawk源码笔记一:APM代码基本结构

 (2014-12-09 17:00:56)
转载
标签: 

代码分析

  

pixhawk 

 

apm 

 

源码分析

  

基本结构

 分类: 航模

Pixhawk源码笔记一:APM代码基本结构

        想要学习Pixhawk源码的朋友有福了,后边我会陆续的将Pixhawk的源码学习笔记整理出来分享给大家。敬请关注:新浪微博@WalkAnt,3017224@qq.com。欢迎交流。

基础知识

        详细参考: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()。

0 0
原创粉丝点击