ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
iekf.h
Go to the documentation of this file.
1
20#ifndef IEKF_H
21#define IEKF_H
22
23#include <stdint.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#define IEKF_NX 6
30#define IEKF_P_ATT_MIN 1e-12f
31#define IEKF_P_BIAS_MIN 1e-16f
32typedef struct {
33 float sigma_gyro;
34 float sigma_bias;
35 float sigma_acc;
36 float sigma_mag;
37 float acc_tol;
38 float mag_tol;
39 float p0_att;
40 float p0_bias;
42 uint8_t adaptive;
45 float acc_nis_gate; /* NIS gate (chi^2, 2 dof) above which R_acc is
46 inflated by NIS/gate. 0 = off. 9.21 = 99% */
47 float mag_nis_gate; /* same for the magnetometer. NOT RECOMMENDED: a high
48 mag NIS is often a true yaw error, and de-weighting
49 it slows recovery. Prefer dip_tol. */
50 float dip_tol; /* rad: the mag is de-weighted when the angle between
51 the MEASURED field and gravity departs from the
52 calibration value (magnetic disturbance). Independent
53 of the estimate. 0 = off */
54 float infl_decay; /* per-update decay of the inflation (memory):
55 exp(-dt_update/tau), e.g. tau = 0.25 s */
56 uint8_t decouple;
58 uint8_t tangent_innov; /* 1: keep only the part of the vector innovation
59 tangent to the predicted direction (default). The
60 radial part (1 - cos e along yhat) is outside the
61 range of H: no attitude error explains it, so in S
62 it is weighted by R alone and inflates the NIS of
63 large errors for nothing (c4dynamics review, 2026-11) */
77 float infl_max;
78 uint16_t lockout_updates;
80 uint16_t grace_updates;
85
87typedef struct {
88 float R0[9];
89 float b0[3];
90 float g_ref[3];
91 float m_ref[3];
92 float nom_acc;
93 float nom_mag;
95
96typedef struct {
98 float R[9];
99 float b[3];
100 float P[IEKF_NX * IEKF_NX];
101 float g_ref[3], m_ref[3];
102 float nom_acc, nom_mag;
103 float last_acc_dev, last_mag_dev;
104 float acc_infl, mag_infl;
105 float last_acc_nis, last_mag_nis;
106 float last_acc_dir[3];
107 float dip_ref;
109 uint8_t acc_trusted; /* set from outside (stillness detector):
110 no NIS inflation on the next acc update */
111 uint16_t acc_gated_run;
112 uint16_t acc_grace;
113 uint32_t lockouts;
114 float innov_w_mean[3];
116 float last_acc_innov_b[3];
117 float E_last[9];
119 float wF[36], wA[36], wB[36];
120 float wH[18], wHt[18], wPHt[18], wK[18];
121} iekf_t;
122
124void iekf_init(iekf_t *f, const iekf_config_t *cfg, const iekf_seed_t *seed);
127void iekf_propagate_dtheta(iekf_t *f, const float dtheta[3], float dt);
129void iekf_propagate(iekf_t *f, const float gyro[3], float dt);
131int iekf_update_acc(iekf_t *f, const float acc[3]);
132int iekf_update_mag(iekf_t *f, const float mag[3]);
135int iekf_update_mag_ex(iekf_t *f, const float mag[3], float extra_var);
136void iekf_get_quat(const iekf_t *f, float q[4]);
137
145void iekf_reset_attitude(iekf_t *f, const float R0[9], float p0_att);
146
147#ifdef __cplusplus
148}
149#endif
150#endif /* IEKF_H */
double b[6]
Definition calib_mgr.c:53
#define IEKF_NX
Definition iekf.h:29
int iekf_update_mag_ex(iekf_t *f, const float mag[3], float extra_var)
As above, with an additional per-component variance (e.g.
Definition iekf.c:371
int iekf_update_mag(iekf_t *f, const float mag[3])
Definition iekf.c:366
void iekf_propagate(iekf_t *f, const float gyro[3], float dt)
Single-sample case: dtheta = gyro*dt (compatible with the previous port)
Definition iekf.c:134
void iekf_init(iekf_t *f, const iekf_config_t *cfg, const iekf_seed_t *seed)
Definition iekf.c:74
void iekf_default_config(iekf_config_t *cfg)
Definition iekf.c:45
void iekf_get_quat(const iekf_t *f, float q[4])
Definition iekf.c:408
int iekf_update_acc(iekf_t *f, const float acc[3])
Return 1 if the measurement was "gated" (de-weighted), 0 otherwise.
Definition iekf.c:315
void iekf_reset_attitude(iekf_t *f, const float R0[9], float p0_att)
Re-initialisation of the attitude away from rest (estimator reset after a failsafe,...
Definition iekf.c:413
void iekf_propagate_dtheta(iekf_t *f, const float dtheta[3], float dt)
Propagation with a measured angle increment (bias NOT yet removed): phi = dtheta - b*dt.
Definition iekf.c:99
float mag_tol
Definition iekf.h:38
float sigma_mag
Definition iekf.h:36
uint8_t adaptive
Definition iekf.h:42
uint8_t decouple
1: acc corrects only roll/pitch, mag only yaw
Definition iekf.h:56
float infl_max
— protection against "lockout" of the NIS inflation — If the estimate is truly wrong,...
Definition iekf.h:77
uint8_t reset_jacobian
1: transport P after the retraction (default)
Definition iekf.h:57
float sigma_bias
rad/s^2, bias random walk
Definition iekf.h:34
float p0_att
initial attitude variance [rad^2]
Definition iekf.h:39
uint16_t grace_updates
e.g.
Definition iekf.h:80
float acc_nis_gate
— robustness to external accelerations / magnetic disturbances —
Definition iekf.h:45
float dip_tol
Definition iekf.h:50
float persist_ratio
|mean|^2 / mean(|e|^2) above which it is persistent, e.g.
Definition iekf.h:82
float sigma_acc
per-component std of the normalised vector
Definition iekf.h:35
uint8_t tilt_comp_mag
Definition iekf.h:43
float p0_bias
initial bias variance [(rad/s)^2]
Definition iekf.h:40
float sigma_gyro
rad/s, gyro white noise
Definition iekf.h:33
float persist_decay
moving mean of the innovation: exp(-dt/tau), tau ~ 1 s
Definition iekf.h:81
uint16_t lockout_updates
e.g.
Definition iekf.h:78
uint8_t estimate_bias
Definition iekf.h:41
uint8_t tangent_innov
Definition iekf.h:58
float acc_tol
relative tolerance on the magnitude (gating)
Definition iekf.h:37
float mag_nis_gate
Definition iekf.h:47
float lockout_p_bump
rad^2, e.g.
Definition iekf.h:79
float infl_decay
Definition iekf.h:54
float persist_min
minimum |mean| (sine of the error), e.g.
Definition iekf.h:83
Initial conditions, typically from calib_finish()
Definition iekf.h:87
float nom_acc
nominal magnitudes for the adaptive gating
Definition iekf.h:92
float nom_mag
Definition iekf.h:93
Definition iekf.h:96
float nom_acc
Definition iekf.h:102
float last_dip_dev
Definition iekf.h:108
uint16_t acc_grace
remaining acc updates without inflation
Definition iekf.h:112
uint16_t acc_gated_run
consecutive acc updates with inflation > 2
Definition iekf.h:111
float last_acc_dev
Definition iekf.h:103
uint8_t acc_trusted
Definition iekf.h:109
float last_acc_nis
Definition iekf.h:105
float acc_infl
Definition iekf.h:104
iekf_config_t cfg
Definition iekf.h:97
float innov_w_pow
moving mean of |innovation|^2
Definition iekf.h:115
uint32_t lockouts
how many times the protection fired
Definition iekf.h:113
float dip_ref
g_ref-m_ref angle, rad
Definition iekf.h:107