ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
ext_sens.c
Go to the documentation of this file.
1
5#include "ext_sens.h"
6#include "flow_bridge.h"
7#include "proto_v2.h"
8#include "spsc.h"
9
10#define EXT_TOF_BUDGET_MS 33u
11
13
18static uint8_t s_probed;
19static volatile uint16_t s_sens;
20static volatile uint32_t s_err;
21
23{
24 s_bus = *b;
25 s_tof.ok = 0; s_flow.ok = 0;
26 s_tof_err = s_flow_err = 0; s_err = 0;
27 s_probed = 0; s_sens = 0;
29}
30
31static void publish(void)
32{
33 s_sens = (uint16_t)((s_tof.ok ? PV2_S_TOF : 0u) | (s_flow.ok ? PV2_S_FLOW : 0u));
34}
35
36void ext_step(uint32_t now)
37{
38 /* probe the missing devices at the first step and every 5 s */
39 if ((!s_probed || (int32_t)(now - s_next_probe) >= 0) && (!s_tof.ok || !s_flow.ok)) {
40 s_probed = 1;
43 if (!s_flow.ok && flow_init(&s_flow, &s_bus.flow) == 0) s_flow_err = 0;
44 publish();
45 }
46
47 if (s_tof.ok) {
49 const int32_t r = vl53_poll(&s_tof, &v);
50 if (r < 0) {
51 s_err++;
52 if (++s_tof_err > EXT_ERR_LIMIT) { s_tof.ok = 0; s_next_probe = now + EXT_PROBE_MS; }
53 } else {
54 s_tof_err = 0;
55 if (r > 0) {
56 const ext_sample_t s = { .kind = EXT_TOF, .status = v.status, .tof_mm = v.mm, .t_ms = now };
57 (void)spsc_push(&s_q, &s);
58 }
59 }
60 }
61 if (s_flow.ok) {
63 const int32_t r = flow_read(&s_flow, &f);
64 if (r < 0) {
65 s_err++;
67 } else {
68 /* every frame goes up, also without new motion: the FC needs the
69 * sensor status and SQUAL on the ground too (below the PMW3901
70 * minimum height every sample is rejected by the bridge) */
71 s_flow_err = 0;
72 const ext_sample_t s = { .kind = EXT_FLOW, .status = f.status,
73 .dx = r > 0 ? f.dx : 0, .dy = r > 0 ? f.dy : 0,
74 .squal = f.squal, .dt_s = r > 0 ? f.dt_s : 0.0f, .t_ms = now };
75 (void)spsc_push(&s_q, &s);
76 }
77 }
78 publish();
79}
80
81int ext_take(ext_sample_t *s) { return spsc_pop(&s_q, s); }
82uint16_t ext_sensors(void) { return s_sens; }
83uint32_t ext_errors(void) { return s_err; }
84
85/* ------------------------------------------------------------------ RTOS shell */
86#ifndef HOST_TEST
87#include "cmsis_os2.h"
88#include "ext_i2c.h"
89
90static void ExtTask(void *arg)
91{
92 (void)arg;
93 osDelay(500); /* let the IMU start first */
94 for (;;) {
95 ext_step(osKernelGetTickCount());
96 osDelay(EXT_PERIOD_MS);
97 }
98}
99
101{
105 ext_init(&b);
106 static const osThreadAttr_t a = { .name = "ext", .priority = osPriorityBelowNormal,
107 .stack_size = 768 };
108 (void)osThreadNew(ExtTask, NULL, &a);
109}
110#endif
static batt_state_t s
Definition battery.c:21
double b[6]
Definition calib_mgr.c:53
void ext_i2c_flow_bus(sensor_bus_t *b, uint8_t addr7)
8-bit registers
Definition ext_i2c.c:34
void ext_i2c_tof_bus(vl53_bus_t *b, uint8_t addr7)
16-bit registers
Definition ext_i2c.c:29
HAL binding of the expansion (Qwiic) devices on I2C1.
static uint8_t s_probed
Definition ext_sens.c:18
static vl53_t s_tof
Definition ext_sens.c:15
static flow_t s_flow
Definition ext_sens.c:16
void ext_step(uint32_t now)
one 10 ms cycle
Definition ext_sens.c:36
static void publish(void)
Definition ext_sens.c:31
int ext_take(ext_sample_t *s)
flight context: 1 if a sample was taken
Definition ext_sens.c:81
static volatile uint32_t s_err
Definition ext_sens.c:20
static uint32_t s_flow_err
Definition ext_sens.c:17
static uint32_t s_next_probe
Definition ext_sens.c:17
uint32_t ext_errors(void)
Definition ext_sens.c:83
static ext_buses_t s_bus
Definition ext_sens.c:14
static uint32_t s_tof_err
Definition ext_sens.c:17
uint16_t ext_sensors(void)
PV2_S_TOF | PV2_S_FLOW currently detected.
Definition ext_sens.c:82
void ext_init(const ext_buses_t *b)
Definition ext_sens.c:22
static void ExtTask(void *arg)
Definition ext_sens.c:90
#define EXT_TOF_BUDGET_MS
Definition ext_sens.c:10
static volatile uint16_t s_sens
Definition ext_sens.c:19
void ext_sens_start(void)
creates the task (after imu_i2c_bus_init)
Definition ext_sens.c:100
Expansion sensors on the Qwiic bus (ACwing Flow rev A): VL53L1X ToF and the optical-flow bridge.
#define EXT_PERIOD_MS
Definition ext_sens.h:20
#define EXT_ERR_LIMIT
consecutive errors: device lost, probe again
Definition ext_sens.h:22
@ EXT_FLOW
Definition ext_sens.h:23
@ EXT_TOF
Definition ext_sens.h:23
#define EXT_PROBE_MS
Definition ext_sens.h:21
int32_t flow_read(flow_t *f, flow_frame_t *fr)
1 = new data, 0 = none, <0 error
Definition flow_bridge.c:31
int32_t flow_init(flow_t *f, const sensor_bus_t *bus)
0 = bridge found
Definition flow_bridge.c:8
ACwing Flow rev A optical-flow bridge (STM32C011 at 0x3B, PMW3901 behind it).
#define FLOW_BRIDGE_ADDR_7BIT
Definition flow_bridge.h:39
ACwing Pilot app protocol v2: identifiers and frame layouts.
#define PV2_S_TOF
Definition proto_v2.h:127
#define PV2_S_FLOW
Definition proto_v2.h:126
Lock-free single-producer single-consumer queue of fixed-size items.
#define SPSC_DEFINE(name, type, n)
Definition spsc.h:29
static int spsc_push(spsc_t *q, const void *it)
Definition spsc.h:35
static void spsc_reset(spsc_t *q)
Definition spsc.h:33
static int spsc_pop(spsc_t *q, void *it)
Definition spsc.h:45
vl53_bus_t tof
Definition ext_sens.h:36
sensor_bus_t flow
Definition ext_sens.h:37
uint8_t status
Definition flow_bridge.h:59
uint8_t squal
Definition flow_bridge.h:59
uint8_t ok
Definition flow_bridge.h:55
Range status after the ULD mapping: 0 valid, 1 sigma fail, 2 signal fail, 4 out of bounds,...
Definition vl53l1x.h:36
uint8_t status
Definition vl53l1x.h:38
uint16_t mm
Definition vl53l1x.h:37
uint8_t ok
Definition vl53l1x.h:31
int32_t vl53_init(vl53_t *d, const vl53_bus_t *bus, uint16_t budget_ms)
0 = sensor found and ranging; < 0 = absent or error (the board works without it)
Definition vl53l1x.c:86
int32_t vl53_poll(vl53_t *d, vl53_sample_t *s)
1 = new sample in *s, 0 = none yet, < 0 = bus error
Definition vl53l1x.c:137
#define VL53L1X_ADDR_7BIT
Definition vl53l1x.h:18