ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
telemetry.h
Go to the documentation of this file.
1
20#ifndef TELEMETRY_H
21#define TELEMETRY_H
22
23#include <stdint.h>
24#include <stddef.h>
25
26#define TLM_SYNC0 0xA5u
27#define TLM_SYNC1 0x5Au
28#define TLM_TYPE_STATE 0x01u
29#define TLM_TYPE_INIT 0x02u
30#define TLM_TYPE_STATUS 0x03u
31#define TLM_TYPE_REPLAY 0x04u
32#define TLM_OVERHEAD 6u
33
34/* STATE packet flags */
35#define TLM_F_RUNNING 0x01u
36#define TLM_F_MAG_NEW 0x02u
37#define TLM_F_ACC_GATED 0x04u
38#define TLM_F_MAG_GATED 0x08u
39#define TLM_F_OVERRUN 0x10u
40#define TLM_F_STILL 0x20u
41/* INIT packet flags */
42#define TLM_I_EST_BIAS 0x01u
43#define TLM_I_ADAPTIVE 0x02u
44#define TLM_I_TILT_COMP 0x04u
45#define TLM_I_DECOUPLE 0x08u
46#define TLM_I_RESET_JAC 0x10u
47#define TLM_I_TANGENT 0x20u
49/* WARNING: "packed" structs = float fields NOT 4-byte aligned
50 * (e.g. tlm_state_t.q is at offset 6). Never pass the address of a field to a
51 * function that writes floats through a pointer: on Cortex-M4F the compiler uses
52 * VSTR, which on an unaligned address raises UsageFault -> HardFault.
53 * Always write into a local variable first, then copy with memcpy. */
54#pragma pack(push, 1)
55typedef struct {
56 uint16_t seq;
57 uint32_t t_us;
58 float q[4];
59 float bias[3];
60 float acc[3];
61 float gyro[3];
62 float mag[3];
63 uint8_t flags;
66typedef struct {
67 float q0[4];
68 float g_ref[3];
69 float m_ref[3];
70 float b0[3];
71 float sigma_acc, sigma_mag, sigma_gyro, sigma_bias;
72 float nom_acc, nom_mag;
73 float fs_hz;
74 float acc_tol, mag_tol;
75 float p0_att, p0_bias;
76 float acc_nis_gate, mag_nis_gate, infl_decay, dip_tol;
77 float infl_max, lockout_p_bump, persist_decay, persist_ratio, persist_min;
78 uint16_t lockout_updates, grace_updates;
79 uint8_t flags;
80} tlm_init_t;
82typedef struct {
83 uint32_t uptime_ms;
84 uint32_t irq_count;
85 uint32_t bus_err;
86 uint32_t fifo_ovr;
87 uint32_t tlm_drop;
88 uint32_t uart_drop;
89 uint32_t proc_cyc_max;
90 uint32_t n_gyro, n_acc, n_mag, n_mag_dup, n_sh_nack;
91 uint16_t words_max;
92 uint16_t calib_count;
93 int8_t imu_init;
94 uint8_t running;
95 /* ---- phase 2: BLE link (all 0 with APP_TLM_BLE = 0) ---- */
96 uint32_t ble_drop;
97 uint16_t ble_mtu;
98 uint8_t ble_state; /* tlm_ble_state_t: 0 off, 1 advertising,
99 2 connected, 3 streaming */
100 uint8_t ble_err;
101} tlm_status_t;
102#pragma pack(pop)
103
104#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
105_Static_assert(sizeof(tlm_state_t) == 71, "tlm_state_t layout");
106_Static_assert(sizeof(tlm_init_t) == 137, "tlm_init_t layout");
107_Static_assert(sizeof(tlm_status_t) == 62, "tlm_status_t layout");
108#endif
109
110/* REPLAY (variable length, max TLM_REPLAY_MAX payload bytes):
111 * uint32 win | uint8 nops | uint8 flags(bit0 = trace truncated)
112 * ops[...] (encoding in est_pipeline.h)
113 * float q[4] b[3] Pdiag[6] acc_infl mag_infl (state AFTER the window) */
114#define TLM_REPLAY_TAIL 60u
115#define TLM_REPLAY_MAX (6u + 140u + TLM_REPLAY_TAIL)
116#define TLM_FRAME_MAX (TLM_REPLAY_MAX + TLM_OVERHEAD)
117
118uint16_t tlm_crc16(const uint8_t *data, size_t len);
120size_t tlm_encode(uint8_t type, const void *payload, uint8_t len, uint8_t *out, size_t cap);
121
122#endif /* TELEMETRY_H */
float bias[3]
Definition calib_mgr.c:47
float cap[6][3]
Definition calib_mgr.c:36
float nom_acc
Definition telemetry.h:72
float acc_tol
Definition telemetry.h:74
uint8_t flags
Definition telemetry.h:79
float sigma_acc
Definition telemetry.h:71
float fs_hz
Definition telemetry.h:73
float p0_att
Definition telemetry.h:75
float infl_max
Definition telemetry.h:77
float acc_nis_gate
Definition telemetry.h:76
uint16_t grace_updates
Definition telemetry.h:78
uint8_t flags
Definition telemetry.h:63
uint16_t seq
filter sample index (0 = initial state R0)
Definition telemetry.h:56
uint32_t t_us
filter time: sum of the dt actually used
Definition telemetry.h:57
uint32_t uptime_ms
Definition telemetry.h:83
uint8_t running
1 = filter running
Definition telemetry.h:94
uint8_t ble_state
Definition telemetry.h:98
uint32_t proc_cyc_max
max CPU cycles of est_process
Definition telemetry.h:89
uint32_t irq_count
INT1 interrupts received
Definition telemetry.h:84
uint16_t ble_mtu
ATT_MTU in use
Definition telemetry.h:97
uint32_t n_acc
Definition telemetry.h:90
uint32_t uart_drop
frames not sent (UART DMA busy)
Definition telemetry.h:88
uint16_t words_max
max words read in one interrupt
Definition telemetry.h:91
uint32_t tlm_drop
frames lost on the telemetry queue
Definition telemetry.h:87
uint16_t calib_count
calibration samples collected
Definition telemetry.h:92
uint32_t ble_drop
frames not queued for BLE (link too slow)
Definition telemetry.h:96
uint32_t fifo_ovr
reads truncated to IMU_FIFO_MAX_WORDS
Definition telemetry.h:86
uint32_t bus_err
I2C errors (FIFO status or DMA burst)
Definition telemetry.h:85
int8_t imu_init
1 = in progress, 0 = OK, <0 = imu_status_t
Definition telemetry.h:93
uint8_t ble_err
last BLE stack error code (0 = none)
Definition telemetry.h:100
size_t tlm_encode(uint8_t type, const void *payload, uint8_t len, uint8_t *out, size_t cap)
Writes the complete frame into out; returns its length, 0 if cap is too small.
Definition telemetry.c:18
uint16_t tlm_crc16(const uint8_t *data, size_t len)
Definition telemetry.c:7