STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sensors_core.h
Go to the documentation of this file.
1#ifndef STAR_SENSORS_CORE_H
2#define STAR_SENSORS_CORE_H
3
4/*
5 * The SPI3 sensors (IMU, high-g, barometer) as one state machine. Portable:
6 * no HAL, no RTOS; time and events are passed in, so the same code runs in
7 * sensors_task on the board and in the host tests on a laptop.
8 *
9 * IMU read on its data-ready edge (EV_IMU), stamped with the edge time
10 * high-g read on its data-ready edge (EV_HIGHG); its DRDY is a level, so
11 * if no edge for 2 periods it is read anyway (missed_drdy++)
12 * baro D1 -> 10 ms -> D2 -> 10 ms -> compensate -> D1 ... (no DRDY pin)
13 *
14 * A sensor whose init fails, or that fails SENSORS_MAX_ERRORS reads in a
15 * row, is re-initialized every SENSORS_RETRY_US until it answers.
16 */
17
18#include <stdbool.h>
19#include <stdint.h>
20
21#include "dev_bus.h"
22#include "dev_ms5611.h"
23#include "records.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#define SENSORS_EV_IMU (1U << 0)
30#define SENSORS_EV_HIGHG (1U << 1)
31
32#define SENSORS_IMU_PERIOD_US 1200U /* 833 Hz (ISM330DHCX ODR) */
33#define SENSORS_HIGHG_PERIOD_US 2500U /* 400 Hz (H3LIS100DL ODR) */
34#define SENSORS_BARO_STEP_US (MS5611_CONVERSION_MS * 1000U)
35#define SENSORS_RETRY_US 1000000U
36#define SENSORS_MAX_ERRORS 3U
37
39
40typedef struct {
41 bool ok;
42 int last_error; /* DEV_* of the last failure */
43 uint32_t samples; /* records produced */
44 uint32_t missed_drdy; /* reads done on timeout, not on an edge */
45 uint32_t errors; /* failed reads */
46 uint32_t inits; /* init attempts */
47 uint32_t error_streak; /* consecutive failed reads */
48 uint32_t retry_at_us;
50
51typedef struct {
57 uint8_t baro_phase; /* 0 start D1, 1 D1 converting, 2 D2 converting */
58 uint32_t baro_due_us;
59 uint32_t baro_d1;
60 uint32_t baro_d1_us;
61 /* Latest sample of each, for low-rate readers (telemetry). */
66
67/* Buses and sink must be set before init. Initializes all three sensors. */
68void sensors_core_init(sensors_core_t* core, uint32_t now_us);
69
70/* Idle: power the IMU and high-g down (DRDY stops) and drop any barometer
71 * conversion; the MS5611 is in standby whenever it is not converting.
72 * sensors_core_init() starts everything again. */
74
75/* Handle events (SENSORS_EV_*) with their edge times, then any timeouts. */
76void sensors_core_step(sensors_core_t* core, uint32_t events,
77 uint32_t imu_edge_us, uint32_t highg_edge_us,
78 uint32_t now_us);
79
80/* Microseconds until the next deadline (baro step, DRDY timeout, retry). */
81uint32_t sensors_core_wait_us(const sensors_core_t* core, uint32_t now_us);
82
83#ifdef __cplusplus
84}
85#endif
86#endif
static gnss_core_t core
Definition gnss_task.c:12
rec_baro_t
Definition records.h:51
rec_highg_t
Definition records.h:45
rec_imu_t
Definition records.h:40
void sensors_core_init(sensors_core_t *core, uint32_t now_us)
uint32_t sensors_core_wait_us(const sensors_core_t *core, uint32_t now_us)
void sensors_core_stop(sensors_core_t *core)
void sensors_core_step(sensors_core_t *core, uint32_t events, uint32_t imu_edge_us, uint32_t highg_edge_us, uint32_t now_us)
record_sink_t sensors_sink_t
rec_highg_t highg_latest
dev_bus_t imu_bus
dev_bus_t baro_bus
uint32_t baro_d1
uint32_t baro_d1_us
dev_bus_t highg_bus
uint32_t imu_last_us
sensors_device_t highg
uint8_t baro_phase
sensors_device_t imu
uint32_t highg_last_us
rec_imu_t imu_latest
uint32_t baro_due_us
sensors_device_t baro
rec_baro_t baro_latest
ms5611_prom_t prom
sensors_sink_t sink
uint32_t retry_at_us
uint32_t error_streak
uint32_t missed_drdy