10#define BARO_INIT_N 25u
11#define TOF_GATE_S 3.0f
12#define TOF_REANCHOR 15u
29 memset(e, 0,
sizeof *e);
31 e->
P[0][0] = 0.01f; e->
P[1][1] = 0.01f; e->
P[2][2] = 1.0f; e->
P[3][3] = 0.04f;
38 const float w = q[0], x = q[1], y = q[2], z = q[3];
39 const float r20 = 2.0f * (x*z - w*y);
40 const float r21 = 2.0f * (y*z + w*x);
41 const float r22 = 1.0f - 2.0f * (x*x + y*y);
42 const float fz = r20*f[0] + r21*f[1] + r22*f[2];
43 const float n = (nom_g > 0.5f) ? nom_g : 1.0f;
44 return (fz /
n - 1.0f) *
G0;
49 if (dt <= 0.0f || dt > 0.1f)
return;
51 const float u = a - x[3];
52 x[0] += x[1]*dt + 0.5f*u*dt*dt;
56 const float h2 = 0.5f*dt*dt;
57 float F[4][4] = { { 1, dt, 0, -h2 }, { 0, 1, 0, -dt }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };
58 float T[4][4],
N[4][4];
59 for (
int i = 0; i < 4; ++i)
60 for (
int j = 0; j < 4; ++j) {
62 for (
int k = 0; k < 4; ++k)
s +=
F[i][k] * e->
P[k][j];
65 for (
int i = 0; i < 4; ++i)
66 for (
int j = 0; j < 4; ++j) {
68 for (
int k = 0; k < 4; ++k)
s += T[i][k] *
F[j][k];
72 const float G[2] = { h2, dt };
73 for (
int i = 0; i < 2; ++i)
for (
int j = 0; j < 2; ++j)
N[i][j] +=
G[i]*
G[j]*qa;
77 for (
int i = 0; i < 4; ++i)
78 for (
int j = 0; j < 4; ++j) e->
P[i][j] = 0.5f * (
N[i][j] +
N[j][i]);
85 const float pred = h0*e->
x[0] + h2*e->
x[2];
87 for (
int i = 0; i < 4; ++i) PH[i] = e->
P[i][0]*h0 + e->
P[i][2]*h2;
88 const float S = h0*PH[0] + h2*PH[2] + r;
89 if (S <= 0.0f)
return -1;
90 const float y = z - pred;
91 if (gate > 0.0f && y*y > gate*gate*S)
return -1;
93 for (
int i = 0; i < 4; ++i) K[i] = PH[i] / S;
94 for (
int i = 0; i < 4; ++i) e->
x[i] += K[i] * y;
95 for (
int i = 0; i < 4; ++i)
96 for (
int j = 0; j < 4; ++j) e->
P[i][j] -= K[i] * PH[j];
97 for (
int i = 0; i < 4; ++i)
98 for (
int j = i + 1; j < 4; ++j) {
const float m = 0.5f*(e->
P[i][j] + e->
P[j][i]); e->
P[i][j] = e->
P[j][i] = m; }
104 if (!(pa > 30000.0f && pa < 120000.0f))
return;
116 e->
baro_alt = 44330.0f * (1.0f - powf(pa / e->
p_ref, 0.190295f));
133 if (!ok || range > c->
tof_max_m || ct < c->tof_max_tilt_cos)
return;
161 const float h0 = e->
x[0];
void alt_est_baro(alt_est_t *e, float pa)
void alt_est_tof(alt_est_t *e, float range, uint8_t ok, float ct)
range_m: raw ToF range; status_ok: range status valid; cos_tilt: R33
void alt_est_default_cfg(alt_est_cfg_t *c)
static int update(alt_est_t *e, float h0, float h2, float z, float r, float gate)
void alt_est_init(alt_est_t *e, const alt_est_cfg_t *c)
float alt_est_accel_up(const float q[4], const float f[3], float nom_g)
vertical acceleration (m/s^2, + up, gravity removed) from the attitude and the specific force in body...
void alt_est_zero(alt_est_t *e)
void alt_est_predict(alt_est_t *e, float a, float dt)
Vertical estimator: height, climb rate, baro bias, accelerometer bias.
static const log_flash_t * F
float tof_offset_m
lens height above the floor when landed
float tof_max_tilt_cos
cos of the largest usable tilt
float sigma_acc_bias
m/s^2/sqrt(s), random walk
float sigma_acc
m/s^2, vertical acceleration noise
float sigma_baro
m, baro altitude noise
float sigma_tof
m, ToF noise at 0 m (+1 % of the range)
float sigma_baro_bias
m/sqrt(s), random walk (while ToF is valid)
uint8_t tof_valid
last ToF sample used
uint8_t valid
an altitude source is initialised
float baro_alt
last baro altitude (m, p_ref based)
uint8_t have_tof
a ToF sample was used at least once
uint8_t tof_ground
below the minimum range: on the floor
float tof_h
last height above the floor (m)
float tof_age_s
since the last valid ToF sample