Pixhawk代码分析-源码框架
源碼框架
pixhawk代碼框架:
pixhawk代碼框架基礎分析:
The basic structure of ArduPilot is broken up into 5 main parts:
- (1) vehicle directories
- (2) AP_HAL
- (3) libraries
- (4) tools directories
- (5) external support code
(1)Vehicle Directories
The vehicle directories are the top level directories that define the firmware for each vehicle type. Currently there are 4 vehicle types – Plane、 Copter、APMrover2 and AntennaTracker。
(2) AP_HAL
The AP_HAL layer (Hardware Abstraction Layer) is how we make ArduPilot portable to lots of different platforms。 There is a top level AP_HAL in libraries/AP_HAL that defines the interface that the rest of the code has to specific board features, then there is a AP_HAL_XXX subdirectory for each board type, for example AP_HAL_AVR for AVR based boards, AP_HAL_PX4 for PX4 boards and AP_HAL_Linux for Linux based boards。
(3) libraries
(4) Tools directories
The tools directories are miscellaneous support directories. For examples, tools/autotest provides the autotest infrastructure behind the autotest.diydrones.com site and tools/Replay provides our log replay utility.
(5) External support code
On some platforms we need external support code to provide additional features or board support. Currently the external trees are:
- PX4NuttX – the core NuttX RTOS used on PX4 boards
- PX4Firmware – the base PX4 middleware and drivers used on PX4 boards
- uavcan – the uavcan CANBUS implementation used in ArduPilot
- mavlink – the mavlink protocol and code generator
2)Libraries介紹
(1)核心庫
AP_AHRS:采用DCM(方向余弦矩陣方法)或EKF(擴展卡爾曼濾波方法)預估飛行器姿態(tài)。
AP_Common:所有執(zhí)行文件(sketch格式,arduino IDE的文件)和其他庫都需要的基礎核心庫。
AP_Math:包含了許多數(shù)學函數(shù),特別對于矢量運算。
AC_PID:PID控制器庫。
AP_InertialNav:擴展帶有gps和氣壓計數(shù)據(jù)的慣性導航庫。
AC_AttitudeControl:姿態(tài)控制相關庫。
AP_WPNav:航點相關的導航庫。
AP_Motors:多旋翼和傳統(tǒng)直升機混合的電機庫。
RC_Channel:更多的關于從APM_RC的PWM輸入/輸出數(shù)據(jù)轉換到內部通用單位的庫,比如角度。
AP_HAL,AP_HAL_AVR,AP_HAL_PX4:硬件抽象層庫,提供給其他高級控制代碼一致的接口,而不必擔心底層不同的硬件。AP_HAL_PX4:GPIO、I2C、UART、RCinput/output、scheduler、semaphores、storage。
(2)傳感器相關庫
AP_InertialSensor:讀取陀螺儀和加速度計數(shù)據(jù),并向主程序執(zhí)行標準程序和提供標準單位數(shù)據(jù)(deg/s,m/s)。
AP_RangerFinder:聲吶和紅外測距傳感器的交互庫
AP_Baro:氣壓計相關庫
AP_GPS:GPS相關庫
AP_Compass:三軸羅盤相關庫
AP_OpticalFlow:光流傳感器相關庫
(3)其他庫
AP_Mount,AP_Camera, AP_Relay:相機安裝控制庫,相機快門控制庫
AP_Mission: 從eeprom(電可擦只讀存儲器)存儲/讀取飛行指令相關庫
AP_Buffer:慣性導航時所用到的一個簡單的堆棧(FIFO,先進先出)緩沖區(qū)
AP_AccelCal、AP_Declination、AP_RCMapper、AP_RPM、AP_RSSI
AP_ADC:Analog to Digital
APM_Control: pitch/roll/yaw controller
DataFlash:flash memory
GCS_Console/GCS_MAVLink:地面站通信、飛行日志
3)關于主控MCU STM32F4的選擇和協(xié)處理器STM32F1
在源代碼中,大部分代碼都是運行在主控MCU STM32F4芯片上,并且是通過直接配置寄存器來實現(xiàn)相應的功能,代碼位于“/ardupilot/modules/PX4Firmware/Build/px4fmu-v2_APM.build/nuttx-export/arch/chip”中。
4)關于pixhawk使用的OS:NuttX
在modules /PX4NuttX/nuttx/sched文件中有os_start.c定義文件,內部進行了一系列的關于操作系統(tǒng)的初始化。在os_start()函數(shù)里面進行了如下初始化。
以上并非必須初始化,可以有選擇性的初始化。Ifdef/ifndef….
重點了解一下關于nuttx中的modules /PX4NuttX/nuttx/mm。
5)姿態(tài)控制的軟件流程
簡單的軟件流程官方給出了大致的介紹,該部分詳見官方介紹:Code Overview
6)再深入一點
如下順序不分先后,都是平時自己整理的,等以后對ardupilot的整體框架了解的比較透徹以后再回頭修改吧。- 1)關于飛行模式
- 2)關于參數(shù)的使用
- 3)關于一些報警和燈顯、日志
- 4)關于校準和失控保護
- 5)關于RC輸入和輸出(接收機)
- 6)關于機型選擇和電機控制
代碼賞析:
如下事例摘自motors.cpp中的一段關于電機解鎖和上鎖的源碼
// arm_motors_check - checks for pilot input to arm or disarm the copter // called at 10hz void Copter::arm_motors_check() { static int16_t arming_counter; // ensure throttle is down首先判斷油門是否為最小 if (channel_throttle->control_in > 0) { arming_counter = 0; return; } //油門最小則檢測yaw的行程量 int16_t tmp = channel_yaw->control_in; // full right 解鎖 if (tmp > 4000) { // increase the arming counter to a maximum of 1 beyond the auto trim counter if( arming_counter <= AUTO_TRIM_DELAY ) { arming_counter++; } // arm the motors and configure for flight if (arming_counter == ARM_DELAY && !motors.armed()) { // reset arming counter if arming fail if (!init_arm_motors(false)) { arming_counter = 0; } } // arm the motors and configure for flight if (arming_counter == AUTO_TRIM_DELAY && motors.armed() && control_mode == STABILIZE) { auto_trim_counter = 250; // ensure auto-disarm doesn't trigger immediately auto_disarm_begin = millis(); } // full left 上鎖 } else if (tmp < -4000) { if (!mode_has_manual_throttle(control_mode) && !ap.land_complete) { arming_counter = 0; return; } // increase the counter to a maximum of 1 beyond the disarm delay if( arming_counter <= DISARM_DELAY ) { arming_counter++; } // disarm the motors if (arming_counter == DISARM_DELAY && motors.armed()) { init_disarm_motors(); } // Yaw is centered so reset arming counter } Else { arming_counter = 0; } }?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Pixhawk代码分析-源码框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pixhawk代码分析-基础知识
- 下一篇: Pixhawk代码分析-启动代码及入口函