ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
checks.c
Go to the documentation of this file.
1
5#include "checks.h"
6#include <string.h>
7
8static void put(pv2_check_t *it, uint8_t id, uint8_t st, int32_t v)
9{
10 it->id = id;
11 it->state = st;
12 const int16_t s = (int16_t)(v > 32767 ? 32767 : (v < -32768 ? -32768 : v));
13 memcpy(&it->value, &s, sizeof s);
14}
15
16uint8_t checks_build(const checks_in_t *in, pv2_check_t *it, uint8_t *ready, uint8_t *battery_fail)
17{
18 const uint16_t S = in->sensors;
19 const uint32_t done = in->cal_done_mask;
20 uint8_t n = 0;
21
22 /* 1 battery: average cell mV; FAIL < battCrit, WARN < battWarn (§9) */
23 if (!in->batt_monitor) put(&it[n++], PV2_CHK_BATTERY, PV2_CK_NA, 0);
24 else if (in->batt_stale) put(&it[n++], PV2_CHK_BATTERY, PV2_CK_FAIL, 0); /* monitor stopped */
25 else if (!in->batt_present) put(&it[n++], PV2_CHK_BATTERY, PV2_CK_WARN, 0); /* USB only: motors unpowered */
26 else {
27 const float c = in->batt_cell_v;
28 uint8_t st = PV2_CK_OK;
29 if (c < in->batt_warn || in->batt_level >= PV2_BATT_LOW) st = PV2_CK_WARN;
30 if (c < in->batt_crit || in->batt_level >= PV2_BATT_CRITICAL) st = PV2_CK_FAIL;
31 put(&it[n++], PV2_CHK_BATTERY, st, (int32_t)(c * 1000.0f + 0.5f));
32 }
33
34 /* 2 IMU & level: FAIL if the IMU is missing or the accelerometer was never calibrated */
35 put(&it[n++], PV2_CHK_IMU,
36 (!in->imu_ok || !(done & (1u << (PV2_CAL_ACCEL - 1u)))) ? PV2_CK_FAIL : PV2_CK_OK, 0);
37
38 /* 3 compass: WARN above 15 % interference or if a calibration is recommended */
39 if (!(S & PV2_S_MAG_FC)) put(&it[n++], PV2_CHK_COMPASS, PV2_CK_NA, 0);
40 else {
41 const int32_t i = (int32_t)(in->mag_interf_pct + 0.5f);
42 const uint8_t never = !(done & (1u << (PV2_CAL_COMPASS - 1u)));
43 uint8_t st = (i > 15 || never) ? PV2_CK_WARN : PV2_CK_OK;
44 if (never && (S & PV2_S_GNSS)) st = PV2_CK_FAIL; /* GNSS models need it */
45 put(&it[n++], PV2_CHK_COMPASS, st, i);
46 }
47
48 /* 4 GNSS, 5 home, 6 ELRS: not fitted */
49 put(&it[n++], PV2_CHK_GNSS, PV2_CK_NA, 0);
50 put(&it[n++], PV2_CHK_HOME, PV2_CK_NA, 0);
51 put(&it[n++], PV2_CHK_RADIO, PV2_CK_NA, 0);
52
53 /* 7 EKF */
54 put(&it[n++], PV2_CHK_EKF,
55 !in->est_running ? PV2_CK_FAIL : (in->att_unc_deg > 3.0f ? PV2_CK_WARN : PV2_CK_OK), 0);
56
57 /* 8 geofence / failsafe: without an altitude estimate the altitude fence
58 * cannot be enforced -> WARN */
59 {
60 const uint8_t alt = (S & (PV2_S_BARO | PV2_S_TOF)) != 0u && in->alt_valid;
61 const int32_t v = (S & PV2_S_GNSS) ? (int32_t)in->gf_max_dist : (int32_t)in->gf_max_alt;
62 put(&it[n++], PV2_CHK_FAILSAFE, alt ? PV2_CK_OK : PV2_CK_WARN, v);
63 }
64
65 /* 9 optical flow. On the ground the sensor is below its 80 mm minimum
66 * height, so the quality is low by design: only the sensor is checked.
67 * In the air: WARN below 50 % quality. */
68 if (!(S & PV2_S_FLOW)) put(&it[n++], PV2_CHK_FLOW, PV2_CK_NA, 0);
69 else {
70 const uint8_t q = (in->flow_q == 0xFFu) ? 0u : in->flow_q;
71 uint8_t st;
72 if (!in->flow_ok) st = PV2_CK_WARN;
73 else if (!in->in_air) st = PV2_CK_OK;
74 else st = (q < 50u) ? PV2_CK_WARN : PV2_CK_OK;
75 put(&it[n++], PV2_CHK_FLOW, st, q);
76 }
77
78 /* 10 throttle low on the active input (§7.1) */
79 {
80 const uint8_t high = in->rc_fresh && in->rc_thr > (in->hold_mode ? 100 : -900);
81 put(&it[n++], PV2_CHK_THROTTLE, high ? PV2_CK_FAIL : PV2_CK_OK, 0);
82 }
83
84 /* 11 calibration: low byte required, high byte recommended (bit = type - 1) */
85 {
86 uint8_t req = 0, rec = 0;
87 if (!(done & (1u << (PV2_CAL_ACCEL - 1u)))) req |= 1u << (PV2_CAL_ACCEL - 1u);
88 if (!(done & (1u << (PV2_CAL_LEVEL - 1u)))) rec |= 1u << (PV2_CAL_LEVEL - 1u);
89 if (!(done & (1u << (PV2_CAL_GYRO - 1u)))) rec |= 1u << (PV2_CAL_GYRO - 1u);
90 if ((S & PV2_S_MAG_FC) &&
91 (!(done & (1u << (PV2_CAL_COMPASS - 1u))) || in->mag_interf_pct > 30.0f))
92 rec |= 1u << (PV2_CAL_COMPASS - 1u);
93 put(&it[n++], PV2_CHK_CALIBRATION, req ? PV2_CK_FAIL : (rec ? PV2_CK_WARN : PV2_CK_OK),
94 (int32_t)((uint16_t)req | ((uint16_t)rec << 8)));
95 }
96
97 /* 12 USB: a cable to a PC must not be in the drone when it arms (ADR 0009) */
98 put(&it[n++], PV2_CHK_USB, in->usb_host ? PV2_CK_FAIL : PV2_CK_OK, 0);
99
100 uint8_t rdy = 1, bf = 0;
101 for (uint8_t k = 0; k < n; ++k) {
102 if (it[k].state == PV2_CK_FAIL) rdy = 0;
103 if (it[k].id == PV2_CHK_BATTERY && it[k].state == PV2_CK_FAIL) bf = 1;
104 }
105 *ready = rdy;
106 *battery_fail = bf;
107 return n;
108}
static batt_state_t s
Definition battery.c:21
static void done(uint8_t step)
Definition calib_mgr.c:122
uint32_t n
Definition calib_mgr.c:35
static void put(pv2_check_t *it, uint8_t id, uint8_t st, int32_t v)
Definition checks.c:8
uint8_t checks_build(const checks_in_t *in, pv2_check_t *it, uint8_t *ready, uint8_t *battery_fail)
fills items (PV2_CHECKS_MAX), returns the count; *ready = no FAIL; *battery_fail = the battery item i...
Definition checks.c:16
Pre-flight checks (CHECKS frame, HANDOVER_FW.md §9).
uint16_t id
Definition log_task.c:49
#define PV2_S_MAG_FC
Definition proto_v2.h:123
#define PV2_S_TOF
Definition proto_v2.h:127
#define PV2_S_FLOW
Definition proto_v2.h:126
#define PV2_S_BARO
Definition proto_v2.h:122
@ PV2_CK_FAIL
Definition proto_v2.h:161
@ PV2_CK_WARN
Definition proto_v2.h:161
@ PV2_CK_OK
Definition proto_v2.h:161
@ PV2_CK_NA
Definition proto_v2.h:161
@ PV2_CAL_COMPASS
Definition proto_v2.h:152
@ PV2_CAL_ACCEL
Definition proto_v2.h:152
@ PV2_CAL_GYRO
Definition proto_v2.h:152
@ PV2_CAL_LEVEL
Definition proto_v2.h:152
#define PV2_S_GNSS
Definition proto_v2.h:125
@ PV2_BATT_LOW
Definition proto_v2.h:136
@ PV2_BATT_CRITICAL
Definition proto_v2.h:136
@ PV2_CHK_CALIBRATION
Definition proto_v2.h:159
@ PV2_CHK_BATTERY
Definition proto_v2.h:157
@ PV2_CHK_HOME
Definition proto_v2.h:158
@ PV2_CHK_FLOW
Definition proto_v2.h:159
@ PV2_CHK_THROTTLE
Definition proto_v2.h:159
@ PV2_CHK_COMPASS
Definition proto_v2.h:157
@ PV2_CHK_EKF
Definition proto_v2.h:158
@ PV2_CHK_FAILSAFE
Definition proto_v2.h:158
@ PV2_CHK_IMU
Definition proto_v2.h:157
@ PV2_CHK_GNSS
Definition proto_v2.h:157
@ PV2_CHK_RADIO
Definition proto_v2.h:158
@ PV2_CHK_USB
FAIL while a USB host is connected: no ARM.
Definition proto_v2.h:160
float gf_max_alt
param gfMaxAlt
Definition checks.h:24
uint8_t usb_host
a USB host is connected (item 12, FAIL)
Definition checks.h:37
uint8_t flow_ok
bridge alive, PMW3901 OK
Definition checks.h:28
uint8_t hold_mode
ALT_HOLD / POS_HOLD: centre stick = hold
Definition checks.h:23
uint8_t batt_monitor
battery measured (BattTask runs)
Definition checks.h:31
uint16_t sensors
CAPS bits
Definition checks.h:15
float gf_max_dist
param gfMaxDist
Definition checks.h:25
uint8_t rc_fresh
RC from the app in the last 0.5 s
Definition checks.h:21
int16_t rc_thr
Definition checks.h:22
uint32_t cal_done_mask
bit (type-1): calibration done and saved
Definition checks.h:20
uint8_t batt_present
Definition checks.h:33
uint8_t imu_ok
IMU initialised
Definition checks.h:16
uint8_t batt_stale
BattTask silent
Definition checks.h:32
uint8_t in_air
Definition checks.h:29
uint8_t batt_level
latched level from the flight core
Definition checks.h:34
uint8_t est_running
attitude estimate converged and running
Definition checks.h:17
float mag_interf_pct
Definition checks.h:19
uint8_t flow_q
0..100, 0xFF n/a
Definition checks.h:27
float att_unc_deg
1 sigma
Definition checks.h:18
float batt_cell_v
average cell, sagged and filtered
Definition checks.h:35
uint8_t alt_valid
vertical estimate running (baro / ToF)
Definition checks.h:26
uint8_t id
< 0x0F CHECKS item (after ready u8, n u8)
Definition proto_v2.h:285
uint8_t state
Definition proto_v2.h:285
int16_t value
Definition proto_v2.h:286
static USBD_StatusTypeDef st(HAL_StatusTypeDef h)
Definition usbd_conf.c:86