ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
log_feed.c
Go to the documentation of this file.
1
5#include "log_feed.h"
6#include "log_queue.h"
7#include "log_format.h"
8#include <math.h>
9#include <string.h>
10
11static uint32_t (*s_clock)(void);
12
13void logf_set_clock(uint32_t (*now_ms)(void)) { s_clock = now_ms; }
14
15static int16_t i16(float v) { return (int16_t)(v > 32767.0f ? 32767 : (v < -32768.0f ? -32768 : lrintf(v))); }
16static uint16_t u16(float v) { return (uint16_t)(v > 65535.0f ? 65535 : (v < 0.0f ? 0 : lrintf(v))); }
17static int32_t i32(float v) { return (int32_t)(v > 2.0e9f ? 2000000000 : (v < -2.0e9f ? -2000000000 : lrintf(v))); }
18static int8_t i8(float v) { return (int8_t)(v > 127.0f ? 127 : (v < -128.0f ? -128 : lrintf(v))); }
19
20void logf_att(uint32_t t, const float q[4], const float w[3], const float a[3])
21{
22 if (!log_q_active()) return;
23 log_att_t r;
24 r.t_ms = t;
25 for (int i = 0; i < 4; ++i) r.q[i] = i16(q[i] * 32767.0f);
26 for (int i = 0; i < 3; ++i) { r.gyro[i] = i16(w[i] * 1000.0f); r.acc[i] = i16(a[i] * 1000.0f); }
27 (void)log_q_put(LOG_SRC_IMU, LR_ATT, &r, sizeof r);
28}
29
30void logf_vert(uint32_t t, const fc_vert_t *v, const fc_setpoint_t *sp, uint16_t tof_mm, uint32_t baro_pa)
31{
32 if (!log_q_active()) return;
33 log_vert_t r;
34 memset(&r, 0, sizeof r);
35 r.t_ms = t;
36 r.h_mm = i32(v->h * 1000.0f);
37 r.vz_mms = i16(v->vz * 1000.0f);
38 r.h_sp_mm = i32(sp->h_sp * 1000.0f);
39 r.vz_sp_mms = i16(sp->vz_sp * 1000.0f);
40 const float th = sp->thrust * 255.0f;
41 r.thrust = (uint8_t)(th > 255.0f ? 255 : (th < 0.0f ? 0 : lrintf(th)));
42 r.tof_mm = tof_mm;
43 r.flow_q = v->flow_q;
44 r.baro_pa = baro_pa;
45 r.flags = (uint8_t)((v->valid ? 1u : 0u) | (v->tof_valid ? 2u : 0u) | (v->tof_ground ? 4u : 0u) |
46 (v->flow_usable ? 8u : 0u) | (sp->alt_ctrl ? 16u : 0u));
47 (void)log_q_put(LOG_SRC_IMU, LR_VERT, &r, sizeof r);
48}
49
50void logf_flight(uint32_t t, const fc_state_t *s)
51{
52 if (!log_q_active()) return;
54 r.t_ms = t;
55 r.armed = s->armed; r.mode = s->mode; r.autom = s->autom; r.failsafe = s->failsafe;
56 r.flags = (uint8_t)((s->in_air ? 1u : 0u) | (s->landed ? 2u : 0u) | (s->rc_stale ? 4u : 0u) |
57 (s->geofence ? 8u : 0u));
58 for (int i = 0; i < 4; ++i) r.stick[i] = i8(s->stick[i] * 100.0f);
59 r.silence_ms = (uint16_t)(s->silence_ms > 65535u ? 65535u : s->silence_ms);
60 (void)log_q_put(LOG_SRC_IMU, LR_FLIGHT, &r, sizeof r);
61}
62
63void logf_est(uint32_t t, const float b[3], float unc, float vib, float hz, float jit, float cpu, float mi)
64{
65 if (!log_q_active()) return;
66 log_est_t r;
67 r.t_ms = t;
68 for (int i = 0; i < 3; ++i) r.gyro_bias[i] = i16(b[i] * 10000.0f);
69 r.att_unc_cdeg = u16(unc * 100.0f);
70 r.vib_mg = u16(vib);
71 r.loop_hz = u16(hz);
72 r.jitter_us = u16(jit);
73 r.cpu_pct = (uint8_t)u16(cpu > 255.0f ? 255.0f : cpu);
74 r.mag_interf_pct = (uint8_t)u16(mi > 255.0f ? 255.0f : mi);
75 (void)log_q_put(LOG_SRC_IMU, LR_EST, &r, sizeof r);
76}
77
78void logf_event(const fc_event_t *e)
79{
80 if (!log_q_active()) return;
82 r.t_ms = s_clock ? s_clock() : 0u;
83 r.level = e->level; r.code = e->code; r.arg = e->arg;
84 (void)log_q_put(LOG_SRC_IMU, LR_EVENT, &r, sizeof r);
85}
86
87void logf_power(uint32_t t, const batt_state_t *b)
88{
89 if (!log_q_active()) return;
91 r.t_ms = t;
92 r.vbat_mv = u16(b->vbat * 1000.0f);
93 r.current_ca = i16(b->current * 100.0f);
94 r.used_mah = u16(b->used_mah);
95 r.cell_mv = u16(b->cell_v * 1000.0f);
96 r.cell_rest_mv = u16(b->cell_rest_v * 1000.0f);
97 r.pct = b->pct;
98 r.level = b->level;
99 r.r_cell_mohm = u16(b->r_cell * 1000.0f);
100 r.cells = (uint8_t)((b->cells & 0x7Fu) | (b->present ? 0x80u : 0u));
101 (void)log_q_put(LOG_SRC_BATT, LR_POWER, &r, sizeof r);
102}
static batt_state_t s
Definition battery.c:21
double sp
Definition calib_mgr.c:42
double b[6]
Definition calib_mgr.c:53
void logf_vert(uint32_t t, const fc_vert_t *v, const fc_setpoint_t *sp, uint16_t tof_mm, uint32_t baro_pa)
Definition log_feed.c:30
void logf_est(uint32_t t, const float b[3], float unc, float vib, float hz, float jit, float cpu, float mi)
Definition log_feed.c:63
void logf_power(uint32_t t, const batt_state_t *b)
Definition log_feed.c:87
static int16_t i16(float v)
Definition log_feed.c:15
static int8_t i8(float v)
Definition log_feed.c:18
void logf_att(uint32_t t, const float q[4], const float w[3], const float a[3])
Definition log_feed.c:20
void logf_set_clock(uint32_t(*now_ms)(void))
clock for the event records (ms)
Definition log_feed.c:13
static uint16_t u16(float v)
Definition log_feed.c:16
static int32_t i32(float v)
Definition log_feed.c:17
static uint32_t(* s_clock)(void)
Definition log_feed.c:11
void logf_event(const fc_event_t *e)
fc_config_t.on_event
Definition log_feed.c:78
void logf_flight(uint32_t t, const fc_state_t *s)
Definition log_feed.c:50
Producers' side of the flight log: build a record from the values the task already has and queue it (...
On-flash format of the ACwing flight log (format 1).
@ LR_POWER
battery, 10 Hz (BattTask)
Definition log_format.h:58
@ LR_EST
estimator health, 10 Hz
Definition log_format.h:57
@ LR_EVENT
every EVENT of the flight core
Definition log_format.h:56
@ LR_ATT
attitude and IMU, 50 Hz
Definition log_format.h:53
@ LR_VERT
vertical estimate and loop, 50 Hz
Definition log_format.h:54
@ LR_FLIGHT
flight core state, 10 Hz
Definition log_format.h:55
int log_q_put(uint8_t src, uint8_t type, const void *p, uint8_t len)
0 = queued
Definition log_queue.c:33
uint8_t log_q_active(void)
Definition log_queue.c:24
Record queues from the producing tasks to the LogTask.
@ LOG_SRC_BATT
Definition log_queue.h:18
@ LOG_SRC_IMU
Definition log_queue.h:18
published state
Definition battery.h:51
uint8_t level
Definition fc_core.h:60
int32_t arg
Definition fc_core.h:60
uint16_t code
Definition fc_core.h:60
setpoints for the attitude / rate loops (phase 3) and the motor mixer
Definition fc_core.h:115
published state (AppTask builds FLIGHT / MOTORS from it)
Definition fc_core.h:85
vertical state from the estimator (flight-control context)
Definition fc_core.h:103
float h
Definition fc_core.h:105
uint8_t tof_valid
Definition fc_core.h:106
uint8_t valid
an altitude source is running (baro and/or ToF)
Definition fc_core.h:104
float vz
m, m/s, + up (alt_est)
Definition fc_core.h:105
uint8_t flow_usable
Definition fc_core.h:110
uint8_t tof_ground
ToF used / ToF below its minimum range
Definition fc_core.h:106
uint8_t flow_q
0..100, 0xFF n/a
Definition fc_core.h:109
int16_t gyro[3]
mrad/s, body (calibrated)
Definition log_format.h:81
int16_t q[4]
×32767, body -> world NWU [w x y z]
Definition log_format.h:80
int16_t acc[3]
mg, body (calibrated)
Definition log_format.h:82
uint32_t t_ms
< LR_ATT
Definition log_format.h:79
uint16_t att_unc_cdeg
Definition log_format.h:116
uint16_t vib_mg
Definition log_format.h:117
uint8_t mag_interf_pct
Definition log_format.h:121
uint8_t cpu_pct
Definition log_format.h:120
uint16_t loop_hz
Definition log_format.h:118
uint16_t jitter_us
Definition log_format.h:119
uint32_t t_ms
< LR_EST
Definition log_format.h:114
int16_t gyro_bias[3]
0.1 mrad/s
Definition log_format.h:115
uint32_t t_ms
< LR_EVENT
Definition log_format.h:107
int32_t arg
Definition log_format.h:110
uint8_t level
Definition log_format.h:108
uint16_t code
Definition log_format.h:109
uint32_t t_ms
< LR_FLIGHT
Definition log_format.h:99
uint8_t flags
bit0 in air, 1 landed, 2 RC stale, 3 geofence
Definition log_format.h:101
int8_t stick[4]
×100: thr, yaw, pitch, roll
Definition log_format.h:102
uint8_t mode
Definition log_format.h:100
uint8_t autom
Definition log_format.h:100
uint16_t silence_ms
app link
Definition log_format.h:103
uint8_t armed
Definition log_format.h:100
uint8_t failsafe
Definition log_format.h:100
uint32_t t_ms
< LR_POWER
Definition log_format.h:125
uint8_t level
PV2_BATT_*
Definition log_format.h:132
uint8_t pct
Definition log_format.h:131
uint8_t cells
bit7 = battery present
Definition log_format.h:134
uint16_t used_mah
Definition log_format.h:128
uint16_t vbat_mv
Definition log_format.h:126
uint16_t r_cell_mohm
Definition log_format.h:133
uint16_t cell_mv
sagged, filtered
Definition log_format.h:129
uint16_t cell_rest_mv
Definition log_format.h:130
int16_t current_ca
Definition log_format.h:127
uint8_t thrust
collective ×255
Definition log_format.h:91
uint8_t flags
bit0 alt valid, 1 ToF valid, 2 ToF ground, 3 flow usable, 4 alt loop
Definition log_format.h:95
uint32_t t_ms
< LR_VERT
Definition log_format.h:86
int32_t h_mm
altitude estimate
Definition log_format.h:87
int16_t vz_mms
Definition log_format.h:88
uint16_t tof_mm
0xFFFF n/a
Definition log_format.h:92
uint32_t baro_pa
0 n/a
Definition log_format.h:94
uint8_t flow_q
0xFF n/a
Definition log_format.h:93
int16_t vz_sp_mms
Definition log_format.h:90
int32_t h_sp_mm
height set point (when the altitude loop runs)
Definition log_format.h:89