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
26
extern
"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
38
typedef
record_sink_t
sensors_sink_t
;
39
40
typedef
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
;
49
}
sensors_device_t
;
50
51
typedef
struct
{
52
dev_bus_t
imu_bus
,
highg_bus
,
baro_bus
;
53
sensors_sink_t
sink
;
54
sensors_device_t
imu
,
highg
,
baro
;
55
uint32_t
imu_last_us
,
highg_last_us
;
56
ms5611_prom_t
prom
;
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). */
62
rec_imu_t
imu_latest
;
63
rec_highg_t
highg_latest
;
64
rec_baro_t
baro_latest
;
65
}
sensors_core_t
;
66
67
/* Buses and sink must be set before init. Initializes all three sensors. */
68
void
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. */
73
void
sensors_core_stop
(
sensors_core_t
*
core
);
74
75
/* Handle events (SENSORS_EV_*) with their edge times, then any timeouts. */
76
void
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). */
81
uint32_t
sensors_core_wait_us
(
const
sensors_core_t
*
core
, uint32_t now_us);
82
83
#ifdef __cplusplus
84
}
85
#endif
86
#endif
dev_bus.h
dev_ms5611.h
core
static gnss_core_t core
Definition
gnss_task.c:12
records.h
rec_baro_t
rec_baro_t
Definition
records.h:51
rec_highg_t
rec_highg_t
Definition
records.h:45
rec_imu_t
rec_imu_t
Definition
records.h:40
sensors_core_init
void sensors_core_init(sensors_core_t *core, uint32_t now_us)
Definition
sensors_core.c:63
sensors_core_wait_us
uint32_t sensors_core_wait_us(const sensors_core_t *core, uint32_t now_us)
Definition
sensors_core.c:202
sensors_core_stop
void sensors_core_stop(sensors_core_t *core)
Definition
sensors_core.c:69
sensors_core_step
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)
Definition
sensors_core.c:157
sensors_sink_t
record_sink_t sensors_sink_t
Definition
sensors_core.h:38
dev_bus_t
Definition
dev_bus.h:26
ms5611_prom_t
Definition
dev_ms5611.h:31
record_sink_t
Definition
records.h:30
sensors_core_t
Definition
sensors_core.h:51
sensors_core_t::highg_latest
rec_highg_t highg_latest
Definition
sensors_core.h:63
sensors_core_t::imu_bus
dev_bus_t imu_bus
Definition
sensors_core.h:52
sensors_core_t::baro_bus
dev_bus_t baro_bus
Definition
sensors_core.h:52
sensors_core_t::baro_d1
uint32_t baro_d1
Definition
sensors_core.h:59
sensors_core_t::baro_d1_us
uint32_t baro_d1_us
Definition
sensors_core.h:60
sensors_core_t::highg_bus
dev_bus_t highg_bus
Definition
sensors_core.h:52
sensors_core_t::imu_last_us
uint32_t imu_last_us
Definition
sensors_core.h:55
sensors_core_t::highg
sensors_device_t highg
Definition
sensors_core.h:54
sensors_core_t::baro_phase
uint8_t baro_phase
Definition
sensors_core.h:57
sensors_core_t::imu
sensors_device_t imu
Definition
sensors_core.h:54
sensors_core_t::highg_last_us
uint32_t highg_last_us
Definition
sensors_core.h:55
sensors_core_t::imu_latest
rec_imu_t imu_latest
Definition
sensors_core.h:62
sensors_core_t::baro_due_us
uint32_t baro_due_us
Definition
sensors_core.h:58
sensors_core_t::baro
sensors_device_t baro
Definition
sensors_core.h:54
sensors_core_t::baro_latest
rec_baro_t baro_latest
Definition
sensors_core.h:64
sensors_core_t::prom
ms5611_prom_t prom
Definition
sensors_core.h:56
sensors_core_t::sink
sensors_sink_t sink
Definition
sensors_core.h:53
sensors_device_t
Definition
sensors_core.h:40
sensors_device_t::errors
uint32_t errors
Definition
sensors_core.h:45
sensors_device_t::ok
bool ok
Definition
sensors_core.h:41
sensors_device_t::retry_at_us
uint32_t retry_at_us
Definition
sensors_core.h:48
sensors_device_t::inits
uint32_t inits
Definition
sensors_core.h:46
sensors_device_t::samples
uint32_t samples
Definition
sensors_core.h:43
sensors_device_t::error_streak
uint32_t error_streak
Definition
sensors_core.h:47
sensors_device_t::last_error
int last_error
Definition
sensors_core.h:42
sensors_device_t::missed_drdy
uint32_t missed_drdy
Definition
sensors_core.h:44
firmware
Flight_Computer
src
app
sensors_core.h
Generated by
1.15.0