![]() |
ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
|
Phase 1: multi-rate attitude estimation on STM32WB55, control hooks in place. More...
#include "app_main.h"#include "app_config.h"#include "cmsis_os2.h"#include "imu_i2c.h"#include "imu_lsm6dsv16x.h"#include "est_pipeline.h"#include "est_config.h"#include "control_hooks.h"#include "motors.h"#include "telemetry.h"#include "tlm_uart.h"#include "tlm_ble.h"#include "so3.h"#include "tlm_build.h"#include "fc_core.h"#include "fc_shared.h"#include "app_task.h"#include "alt_est.h"#include "ext_sens.h"#include "proto_v2.h"#include "batt_task.h"#include "batt_adc.h"#include "log_task.h"#include "log_feed.h"#include "log_queue.h"#include "log_flash_ram.h"#include "usb_cdc.h"#include "usb_link.h"#include <string.h>#include <math.h>Go to the source code of this file.
Data Structures | |
| struct | tlm_msg_t |
Macros | |
| #define | FLAG_IMU_IRQ 0x0001u |
| #define | APP_IWDG_RELOAD 500u |
| #define | FC_SERVICE_MS 4u /* flight core ~250 Hz */ |
| #define | IMU_STALE_MS 50u /* no gyro for 50 ms: estimate not usable */ |
Functions | |
| static void | dwt_init (void) |
| void | app_exti (uint16_t pin) |
| void | app_i2c_rx_cplt (I2C_HandleTypeDef *h) |
| void | app_i2c_error (I2C_HandleTypeDef *h) |
| void | app_uart_tx_cplt (UART_HandleTypeDef *h) |
| static void | push_frame (size_t len) |
| static void | on_state (const est_t *e, void *user) |
| static void | on_init (const est_t *e, void *user) |
| static void | iwdg_start (void) |
| static void | correct_events (imu_event_t *ev, size_t n) |
| static void | take_calibrations (void) |
| static void | take_ext (uint32_t now, float cos_tilt, const float omega[3]) |
| static void | publish_est (uint32_t now) |
| static void | log_records (uint32_t now, const float q[4], const fc_vert_t *v, const fc_setpoint_t *sp) |
| static void | flight_service (uint32_t *lastSvc) |
| static void | ImuTask (void *arg) |
| static void | tx_frame (const uint8_t *f, uint16_t len) |
| static void | send_status (uint8_t *frame, size_t cap) |
| static void | TelemetryTask (void *arg) |
| static size_t | get_init (uint8_t *dst, size_t cap) |
| static void | motor_test_hook (uint8_t m, float duty) |
| static const log_flash_t * | log_flash (void) |
| static uint16_t | log_sensors (void) |
| void | app_start (void) |
Variables | |
| I2C_HandleTypeDef | hi2c1 |
| UART_HandleTypeDef | huart1 |
| static osThreadId_t | s_imuTask |
| static osMessageQueueId_t | qTlm |
| static volatile uint32_t | s_irqCyc |
| static volatile uint32_t | s_irqCount |
| static volatile uint32_t | s_tlmDrop |
| static volatile uint32_t | s_busErr |
| static volatile uint32_t | s_fifoOvr |
| static volatile uint32_t | s_procCycMax |
| static volatile uint32_t | s_procCycLast |
| static volatile uint32_t | s_wordsMax |
| static volatile uint32_t | s_drainReads |
| static volatile float | s_odrHz |
| static volatile int8_t | s_imuInit = 1 |
| static imu_parse_stats_t | s_psTot |
| static imu_t | s_imu |
| static est_t | s_est |
| static uint8_t | s_fifo [IMU_FIFO_MAX_WORDS *IMU_FIFO_WORD_BYTES] |
| static imu_event_t | s_ev [IMU_FIFO_MAX_WORDS] |
| uint8_t | g_initFrame [TLM_FRAME_MAX] |
| volatile uint16_t | g_initLen |
| static cal_set_t | s_calset |
| static est_config_t | s_ec |
| static volatile float | g_level_trim_deg [2] |
| static volatile uint8_t | s_est_restart |
| static alt_est_t | s_alt |
| static uint8_t | s_flow_q = 0xFFu |
| static float | s_flow_v [2] |
| static uint32_t | s_flow_ms |
| static uint8_t | s_flow_ok |
| static uint8_t | s_flow_usable |
| static uint16_t | s_tof_mm = 0xFFFFu |
| static uint32_t | s_tof_ms |
| static float | s_vib2 |
| static uint16_t | s_acc_clip |
| static float | s_loop_hz |
| static float | s_jitter_us |
| static float | s_cpu_pct |
| static uint32_t | s_cpu_cyc |
| static float | s_baro_temp |
| static uint16_t | s_imu_sensors |
| static volatile uint32_t | s_lastGyroMs |
| static tlm_msg_t | s_msg |
Phase 1: multi-rate attitude estimation on STM32WB55, control hooks in place.
INT1 LSM6DSV16X (FIFO threshold, 960 Hz) | EXTI ISR: timestamp DWT + osThreadFlagsSet v ImuTask (osPriorityRealtime) FIFO status (2 bytes) -> DMA burst of N words -> parse -> est_process(): propagation 960 Hz, ctrl_rate_step, acc update 240 Hz, ctrl_attitude_step, update mag ~50 Hz | qTlm (240 Hz snapshots, never blocking) v TelemetryTask (osPriorityBelowNormal) UART DMA -> VCP ST-LINK | (APP_TLM_BLE = 1) same frames, STATE decimated, no REPLAY v tlm_ble ring -> BleTxThread (ble_tlm_svc.c) -> GATT notifications (BLE stack on the Cortex-M0+, IPCC)
ImuTask remains the only high-priority task: BLE never touches it.
Protocol v2 (ACwing Pilot app, HANDOVER_FW.md): ImuTask also runs the flight core (fc_core: arming, modes, TAKEOFF/LAND/ HOLD, failsafes) at ~250 Hz and the vertical estimator (alt_est: baro on the sensor hub, ToF from the ExtTask), and it is the only task that feeds the independent watchdog. AppTask (app_task.c) handles commands, parameters, flash, calibrations and the v2 frames. ExtTask (ext_sens.c) polls the ACwing Flow board on the Qwiic bus.
Definition in file app_main.c.
| #define FLAG_IMU_IRQ 0x0001u |
Definition at line 69 of file app_main.c.
| #define APP_IWDG_RELOAD 500u |
Definition at line 75 of file app_main.c.
| #define FC_SERVICE_MS 4u /* flight core ~250 Hz */ |
Definition at line 76 of file app_main.c.
| #define IMU_STALE_MS 50u /* no gyro for 50 ms: estimate not usable */ |
Definition at line 126 of file app_main.c.
|
static |
Definition at line 129 of file app_main.c.
Referenced by app_start().
| void app_exti | ( | uint16_t | pin | ) |
Definition at line 136 of file app_main.c.
References FLAG_IMU_IRQ, s_imuTask, s_irqCount, and s_irqCyc.
| void app_i2c_rx_cplt | ( | I2C_HandleTypeDef * | h | ) |
Definition at line 144 of file app_main.c.
References imu_i2c_rx_cplt_isr().
| void app_i2c_error | ( | I2C_HandleTypeDef * | h | ) |
Definition at line 145 of file app_main.c.
References imu_i2c_error_isr().
| void app_uart_tx_cplt | ( | UART_HandleTypeDef * | h | ) |
Definition at line 146 of file app_main.c.
References tlm_uart_tx_cplt_isr().
|
static |
Definition at line 151 of file app_main.c.
References tlm_msg_t::len, qTlm, s_msg, and s_tlmDrop.
Referenced by on_init(), and on_state().
|
static |
Definition at line 158 of file app_main.c.
References cal_sample_t::acc, cal_set_t::accel, alt_est_accel_up(), alt_est_predict(), APP_STEP_HZ, APP_STEPS_PER_ACC, APP_TLM_DECIM_REPLAY, APP_TLM_DECIMATION, cal_gyro_t::bias, tlm_msg_t::bytes, cal_set_t::compass, EST_F_MAG_NEW, fsh_cal_push(), g_fsh_cal_want, cal_set_t::gyro, cal_sample_t::gyro, cal_compass_t::hi, iekf_get_quat(), cal_sample_t::mag, cal_sample_t::mag_new, ms, n, cal_accel_t::off, push_frame(), cal_sample_t::q, cal_sample_t::running, s_alt, s_calset, s_msg, s_vib2, cal_accel_t::scale, cal_compass_t::scale, tlm_build_replay(), and tlm_build_state().
Referenced by est_init(), ImuTask(), and take_calibrations().
|
static |
Definition at line 201 of file app_main.c.
References APP_STEP_HZ, APP_STEPS_PER_ACC, tlm_msg_t::bytes, g_initFrame, g_initLen, push_frame(), s_msg, and tlm_build_init().
Referenced by est_init(), ImuTask(), and take_calibrations().
|
static |
Definition at line 212 of file app_main.c.
References APP_IWDG_RELOAD.
Referenced by ImuTask().
|
static |
Definition at line 227 of file app_main.c.
References cal_set_t::accel, alt_est_baro(), cal_gyro_t::bias, cal_set_t::gyro, IMU_EV_ACC, IMU_EV_BARO, IMU_EV_GYRO, n, cal_accel_t::off, s_acc_clip, s_alt, s_baro_temp, s_calset, cal_accel_t::scale, and imu_event_t::v.
Referenced by ImuTask().
|
static |
Definition at line 255 of file app_main.c.
References cal_set_t::accel, APP_TLM_REPLAY, cal_gyro_t::bias, cal_copy_type(), cal_set_t::compass, est_init(), fsh_apply_take(), g_level_trim_deg, cal_set_t::gyro, cal_compass_t::hi, cal_set_t::level, est_config_t::mag_hard_iron, est_config_t::mag_scale, on_init(), on_state(), cal_level_t::pitch_deg, PV2_CAL_ACCEL, PV2_CAL_COMPASS, PV2_CAL_GYRO, PV2_CAL_LEVEL, cal_level_t::roll_deg, s_calset, s_ec, s_est, s_est_restart, cal_compass_t::scale, fsh_apply_t::set, and fsh_apply_t::type.
Referenced by flight_service().
|
static |
Definition at line 295 of file app_main.c.
References alt_est_tof(), APP_FLOW_MIN_H, APP_FLOW_SIGN_X, APP_FLOW_SIGN_Y, ext_sample_t::dt_s, ext_sample_t::dx, ext_sample_t::dy, EXT_FLOW, ext_sensors(), ext_take(), EXT_TOF, ext_sample_t::kind, PV2_S_FLOW, s_alt, s_flow_ms, s_flow_ok, s_flow_q, s_flow_usable, s_flow_v, s_tof_mm, s_tof_ms, ext_sample_t::squal, ext_sample_t::status, alt_est_t::tof_ground, alt_est_t::tof_h, ext_sample_t::tof_mm, and alt_est_t::tof_valid.
Referenced by flight_service().
|
static |
Definition at line 335 of file app_main.c.
References fsh_est_t::acc_clip, fsh_est_t::acc_gated, alt_est_h(), alt_est_vz(), fsh_est_t::alt_valid, fsh_est_t::att_unc_deg, alt_est_t::baro_ok, fsh_est_t::baro_pa, fsh_est_t::cpu_pct, fsh_est_t::flow_ok, fsh_est_t::flow_q, fsh_est_t::flow_usable, fsh_est_write(), fsh_est_t::h, iekf_get_quat(), IEKF_NX, fsh_est_t::imu_ok, fsh_est_t::imu_temp_c, fsh_est_t::jitter_us, fsh_est_t::loop_hz, fsh_est_t::mag_gated, fsh_est_t::mag_interf_pct, alt_est_t::pressure, fsh_est_t::q, fsh_est_t::running, s_acc_clip, s_alt, s_baro_temp, s_cpu_cyc, s_cpu_pct, s_est, s_flow_ok, s_flow_q, s_flow_usable, s_flow_v, s_imu_sensors, s_imuInit, s_irqCount, s_jitter_us, s_loop_hz, s_tof_mm, s_vib2, fsh_est_t::sensors, fsh_est_t::tof_ground, alt_est_t::tof_ground, fsh_est_t::tof_h, alt_est_t::tof_h, fsh_est_t::tof_mm, fsh_est_t::tof_valid, alt_est_t::tof_valid, alt_est_t::valid, fsh_est_t::vel_body, fsh_est_t::vib_mg, and fsh_est_t::vz.
Referenced by flight_service().
|
static |
Definition at line 391 of file app_main.c.
References alt_est_t::baro_ok, fc_snapshot(), IEKF_NX, log_q_active(), logf_att(), logf_est(), logf_flight(), logf_vert(), alt_est_t::pressure, s_alt, s_cpu_pct, s_est, s_jitter_us, s_loop_hz, s_tof_mm, s_vib2, and sp.
Referenced by flight_service().
|
static |
Definition at line 416 of file app_main.c.
References alt_est_h(), alt_est_vz(), fc_vert_t::cos_tilt, fc_service(), FC_SERVICE_MS, fc_setpoint(), fc_vert_t::flow_q, fc_vert_t::flow_usable, fc_vert_t::h, iekf_get_quat(), IMU_STALE_MS, log_records(), publish_est(), s_acc_clip, s_alt, s_est, s_flow_q, s_flow_usable, s_lastGyroMs, sp, take_calibrations(), take_ext(), fc_vert_t::tof_ground, alt_est_t::tof_ground, fc_vert_t::tof_h, alt_est_t::tof_h, fc_vert_t::tof_valid, alt_est_t::tof_valid, fc_vert_t::valid, alt_est_t::valid, and fc_vert_t::vz.
Referenced by ImuTask().
|
static |
Definition at line 455 of file app_main.c.
References imu_parse_stats_t::acc, alt_est_default_cfg(), alt_est_init(), app_est_config(), APP_GY_LPF1_BW, APP_IMU_ODR, APP_IMU_WATERMARK, APP_MAG_FAST, APP_TLM_REPLAY, APP_XL_LPF2_BW, APP_XL_LPF2_ENABLE, imu_t::baro_enabled, cal_copy_type(), cal_set_t::compass, correct_events(), ctrl_init(), est_init(), est_process(), fc_service(), FLAG_IMU_IRQ, flight_service(), fsh_apply_take(), g_level_trim_deg, imu_parse_stats_t::gyro, cal_compass_t::hi, hi2c1, imu_fifo_level(), IMU_FIFO_MAX_WORDS, imu_fifo_parse(), IMU_FIFO_REG, IMU_FIFO_WORD_BYTES, IMU_I2C_ADDR_7BIT, imu_i2c_make_bus(), imu_i2c_read_dma(), imu_init(), IMU_OK, iwdg_start(), cal_set_t::level, imu_parse_stats_t::mag, MAG_AXIS_MAP, imu_parse_stats_t::mag_dup, imu_t::mag_enabled, est_config_t::mag_hard_iron, est_config_t::mag_map, est_config_t::mag_scale, n, imu_config_t::odr, imu_t::odr_hz, on_init(), on_state(), cal_level_t::pitch_deg, PV2_S_BARO, PV2_S_IMU, PV2_S_MAG_FC, cal_level_t::roll_deg, s_alt, s_busErr, s_calset, s_cpu_cyc, s_drainReads, s_ec, s_est, s_ev, s_fifo, s_fifoOvr, s_imu, s_imu_sensors, s_imuInit, s_irqCyc, s_jitter_us, s_lastGyroMs, s_odrHz, s_procCycLast, s_procCycMax, s_psTot, s_wordsMax, cal_compass_t::scale, fsh_apply_t::set, imu_parse_stats_t::sh_nack, st(), and fsh_apply_t::type.
Referenced by app_start().
|
static |
Definition at line 600 of file app_main.c.
References APP_TLM_TX_TIMEOUT_MS, seq, tlm_ble_push(), tlm_ble_want_state(), TLM_OVERHEAD, TLM_TYPE_REPLAY, TLM_TYPE_STATE, tlm_uart_send(), usbl_open(), usbl_put_frame(), and USBL_SRC_TLM.
Referenced by send_status(), and TelemetryTask().
|
static |
Definition at line 623 of file app_main.c.
References imu_parse_stats_t::acc, cap, imu_parse_stats_t::gyro, imu_parse_stats_t::mag, imu_parse_stats_t::mag_dup, s_busErr, s_est, s_fifoOvr, s_imuInit, s_irqCount, s_procCycMax, s_psTot, s_tlmDrop, s_wordsMax, imu_parse_stats_t::sh_nack, st(), tlm_ble_dropped(), tlm_ble_error(), tlm_ble_mtu(), tlm_ble_state(), tlm_encode(), TLM_TYPE_STATUS, tlm_uart_dropped(), tx_frame(), and tlm_status_t::uptime_ms.
Referenced by TelemetryTask().
|
static |
Definition at line 647 of file app_main.c.
References APP_INIT_EVERY_N, tlm_msg_t::bytes, g_initFrame, g_initLen, huart1, tlm_msg_t::len, n, qTlm, send_status(), tlm_ble_push(), tlm_ble_take_init_request(), tlm_uart_init(), and tx_frame().
Referenced by app_start().
|
static |
Definition at line 681 of file app_main.c.
References cap, g_initFrame, g_initLen, and n.
Referenced by app_start().
|
static |
Definition at line 689 of file app_main.c.
References motors_test().
Referenced by app_start().
|
static |
Definition at line 692 of file app_main.c.
References APP_LOG_RAM_KB, and log_flash_ram_init().
Referenced by app_start().
|
static |
Definition at line 708 of file app_main.c.
References ext_sensors(), fsh_est_read(), and fsh_est_t::sensors.
Referenced by app_start().
| void app_start | ( | void | ) |
Definition at line 716 of file app_main.c.
References batt_task_cfg_t::adc_read, APP_BATT_ADC, APP_BATT_CURRENT, APP_BATT_SENS_V_A, APP_FW_MAJOR, APP_FW_MINOR, APP_FW_PATCH, APP_HW_REV, app_task_boot(), app_task_start(), batt_task_cfg_t::batt, batt_adc_init(), batt_adc_read(), app_task_cfg_t::batt_monitor, batt_task_start(), cal_set_t::battery, app_task_cfg_t::board_sensors, dwt_init(), ext_sens_start(), fc_init(), log_task_cfg_t::flash, log_task_cfg_t::fw, get_init(), app_task_cfg_t::get_init, batt_cfg_t::has_current, hi2c1, app_task_cfg_t::hw_rev, log_task_cfg_t::hw_rev, imu_i2c_bus_init(), ImuTask(), log_flash(), app_task_cfg_t::log_pct, log_sensors(), log_task_pct(), log_task_start(), logf_event(), logf_set_clock(), MAG_HARD_IRON, motor_test_hook(), motors_arm(), app_task_cfg_t::motors_built, MOTORS_ENABLE, motors_init(), app_task_cfg_t::n_motors, PV2_S_CURRENT, PV2_S_FLASH_LOG, qTlm, s_imuTask, batt_cfg_t::scale_i, cal_battery_t::scale_i, batt_cfg_t::scale_v, cal_battery_t::scale_v, batt_cfg_t::sens_v_per_a, log_task_cfg_t::sensors, fc_config_t::sensors, TelemetryTask(), app_task_cfg_t::uid, usb_cdc_init(), usb_cdc_pump(), log_task_cfg_t::usb_pump, and usbl_host().
|
extern |
Referenced by app_start(), and ImuTask().
|
extern |
Referenced by TelemetryTask().
|
static |
Definition at line 79 of file app_main.c.
Referenced by app_exti(), and app_start().
|
static |
Definition at line 80 of file app_main.c.
Referenced by app_start(), push_frame(), and TelemetryTask().
|
static |
Definition at line 81 of file app_main.c.
Referenced by app_exti(), and ImuTask().
|
static |
Definition at line 82 of file app_main.c.
Referenced by app_exti(), publish_est(), and send_status().
|
static |
Definition at line 82 of file app_main.c.
Referenced by push_frame(), and send_status().
|
static |
Definition at line 82 of file app_main.c.
Referenced by ImuTask(), and send_status().
|
static |
Definition at line 82 of file app_main.c.
Referenced by ImuTask(), and send_status().
|
static |
Definition at line 84 of file app_main.c.
Referenced by ImuTask(), and send_status().
|
static |
Definition at line 84 of file app_main.c.
Referenced by ImuTask().
|
static |
Definition at line 85 of file app_main.c.
Referenced by ImuTask(), and send_status().
|
static |
Definition at line 86 of file app_main.c.
Referenced by ImuTask().
|
static |
Definition at line 87 of file app_main.c.
Referenced by ImuTask().
|
static |
Definition at line 88 of file app_main.c.
Referenced by ImuTask(), publish_est(), and send_status().
|
static |
Definition at line 89 of file app_main.c.
Referenced by ImuTask(), and send_status().
|
static |
Definition at line 91 of file app_main.c.
Referenced by ImuTask().
|
static |
Definition at line 92 of file app_main.c.
Referenced by flight_service(), ImuTask(), log_records(), publish_est(), send_status(), and take_calibrations().
|
static |
Definition at line 93 of file app_main.c.
Referenced by ImuTask().
|
static |
Definition at line 94 of file app_main.c.
Referenced by ImuTask().
| uint8_t g_initFrame[TLM_FRAME_MAX] |
Definition at line 101 of file app_main.c.
Referenced by get_init(), on_init(), and TelemetryTask().
| volatile uint16_t g_initLen |
Definition at line 102 of file app_main.c.
Referenced by get_init(), on_init(), and TelemetryTask().
|
static |
Definition at line 105 of file app_main.c.
Referenced by correct_events(), ImuTask(), on_state(), and take_calibrations().
|
static |
Definition at line 106 of file app_main.c.
Referenced by ImuTask(), and take_calibrations().
|
static |
Definition at line 107 of file app_main.c.
Referenced by ImuTask(), and take_calibrations().
|
static |
Definition at line 108 of file app_main.c.
Referenced by take_calibrations().
|
static |
Definition at line 111 of file app_main.c.
Referenced by correct_events(), flight_service(), ImuTask(), log_records(), on_state(), publish_est(), and take_ext().
|
static |
Definition at line 112 of file app_main.c.
Referenced by flight_service(), publish_est(), and take_ext().
|
static |
Definition at line 113 of file app_main.c.
Referenced by publish_est(), and take_ext().
|
static |
Definition at line 114 of file app_main.c.
Referenced by take_ext().
|
static |
Definition at line 115 of file app_main.c.
Referenced by publish_est(), and take_ext().
|
static |
Definition at line 116 of file app_main.c.
Referenced by flight_service(), publish_est(), and take_ext().
|
static |
Definition at line 117 of file app_main.c.
Referenced by log_records(), publish_est(), and take_ext().
|
static |
Definition at line 118 of file app_main.c.
Referenced by take_ext().
|
static |
Definition at line 119 of file app_main.c.
Referenced by log_records(), on_state(), and publish_est().
|
static |
Definition at line 120 of file app_main.c.
Referenced by correct_events(), flight_service(), and publish_est().
|
static |
Definition at line 121 of file app_main.c.
Referenced by log_records(), and publish_est().
|
static |
Definition at line 121 of file app_main.c.
Referenced by ImuTask(), log_records(), and publish_est().
|
static |
Definition at line 121 of file app_main.c.
Referenced by log_records(), and publish_est().
|
static |
Definition at line 122 of file app_main.c.
Referenced by ImuTask(), and publish_est().
|
static |
Definition at line 123 of file app_main.c.
Referenced by correct_events(), and publish_est().
|
static |
Definition at line 124 of file app_main.c.
Referenced by ImuTask(), and publish_est().
|
static |
Definition at line 125 of file app_main.c.
Referenced by flight_service(), and ImuTask().
|
static |
Definition at line 149 of file app_main.c.
Referenced by on_init(), on_state(), and push_frame().