rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
sense_sched.h
Go to the documentation of this file.
1
172
173#pragma once
174
175#include <stdbool.h>
176#include <stdint.h>
177
178#include "as5600.h"
179#include "ina228.h"
180#include "ina3221.h"
181#include "sense_bus.h"
182#include "servo_move.h"
183
184#ifdef __cplusplus
185extern "C" {
186#endif
187
188#define SENSE_WINDOW_MS 50u
194#define SENSE_ROTATION 20u
196#define SENSE_WIN_RING 4u
204#define SENSE_CAP_FILTER_N 4u
208#define SENSE_ENC_STILL_TOL 12u
210#define SENSE_ENC_MAG_EVERY 25u
214#define SENSE_CAP_SETTLE_N 10u
219#define SENSE_CAP_LAG_MS 5u
221#define SENSE_CAP_T_PER_MS 10u
229#define SENSE_CAP_EDGE_WAIT_MS SERVO_MOVE_TIMEOUT_MS
234#define SENSE_CAP_RISE_AUTO INT32_MIN
236#define SENSE_FAST_HISTORY SENSE_WINDOW_MS
237
239#define SENSE_I3221_PARTS 4u
240#define SENSE_CHANNELS 12u
241_Static_assert(SENSE_CHANNELS == SENSE_I3221_PARTS * INA3221_CHANNELS,
242 "three channels a part");
250static inline unsigned sense_ch(unsigned a, unsigned ch)
251{
252 return a * INA3221_CHANNELS + ch - 1u;
253}
254#define SENSE_CH(a, ch) sense_ch((unsigned)(a), (unsigned)(ch))
256#define SENSE_FAST_NONE 0xFFu
257
262#define SENSE_SRC_INA228 SENSE_CHANNELS
263#define SENSE_SRC_COUNT (SENSE_CHANNELS + 1u)
264
266typedef struct {
267 sense_i2c_t i2c;
268 uint64_t (*now_us)(void *ctx);
269 void (*recover)(void *ctx);
271 void *ctx;
273
275typedef struct {
276 uint8_t fitted;
277 uint32_t shunt_uohm[SENSE_CHANNELS];
278 uint8_t fast;
280
282typedef struct {
283 bool ina228_en;
284 uint8_t ina228_addr;
285 uint32_t ina228_shunt_uohm;
286 uint32_t ina228_max_ma;
287 sense_i3221_cfg_t i3221;
290
305void sense_i3221_one(sense_i3221_cfg_t *m, bool en, uint8_t addr,
306 uint32_t shunt_uohm, uint8_t channels);
307
309typedef struct {
310 uint16_t number;
311 uint16_t n_i;
312 uint16_t n_v;
313 bool clip_hi;
314 bool clip_lo;
315 int32_t i_mean_ua,
318 int32_t v_mean_uv,
321
323typedef struct {
324 int64_t i_sum, v_sum;
325 uint16_t n_i, n_v;
326 uint16_t n_hi, n_lo;
327 bool clip_hi,
329 int32_t i_min,
333
335typedef struct {
336 bool closed;
337 uint16_t n_i;
338 uint16_t n_clip;
339 uint16_t n_v;
340 bool clip_hi;
341 bool clip_lo;
342 int32_t i_mean_ua,
345 int32_t v_mean_uv,
348
350typedef struct {
351 bool have_v;
352 bool have_i;
353 bool have_p;
355 int32_t v_min_uv;
356 int32_t i_max_ua;
357 int64_t p_max_uw;
360 uint64_t energy_mj;
361 int64_t charge_uc;
363
368typedef enum {
369 SENSE_CAP_IDLE = 0,
372 SENSE_CAP_MOVING,
373 SENSE_CAP_ARRIVED,
379
381typedef struct {
382 int32_t rise_ua;
384 int32_t hold_ua;
385 int32_t move_ua;
386 int32_t band_ua;
388
390typedef struct {
391 float a;
392 int8_t clip;
394
396typedef struct {
397 uint32_t t;
398 int32_t ua;
400
404typedef struct {
405 sense_cap_state_t state;
406 uint16_t seq;
408 uint32_t edge_t;
409 uint32_t arm_t;
412 /* The last SENSE_CAP_FILTER_N FAST samples while armed. */
414 uint8_t pre_n, pre_head;
415 /* Once over: */
416 uint32_t move_t;
418 uint32_t arrive_t;
420 int32_t peak_ua;
421 int32_t mean_ua;
422 uint32_t samples;
423 bool clipped;
424 servo_move_t mv;
426
428typedef struct {
429 as5600_t dev;
431 uint8_t status;
432 uint16_t raw;
433 uint16_t samples;
434 bool have_mag;
435 uint8_t agc;
436 uint16_t magnitude;
437 uint32_t slots;
438 uint16_t anchor;
439 uint64_t anchor_ms;
440 bool anchored;
442
446typedef struct {
449 sense_bus_t bus;
450 ina228_t i228;
455 /* Per channel: a clipped sample is at least clip, the top and bottom
456 * codes of the range stand for top and bottom; 0 for a channel off. */
457 int32_t ch_clip_ua[SENSE_CHANNELS];
458 int32_t ch_top_ua[SENSE_CHANNELS];
459 int32_t ch_bottom_ua[SENSE_CHANNELS];
460 /* The channels read in turn beside FAST, one a tick. */
461 uint8_t others[SENSE_CHANNELS];
462 uint8_t n_others, other_at;
463 uint8_t volt_at;
464 uint32_t ticks;
465 uint32_t gen;
468 uint32_t rot;
469 bool started;
470 uint64_t t0_ms;
471 uint64_t win;
472 sense_acc_t acc[SENSE_SRC_COUNT];
473 sense_window_t last[SENSE_SRC_COUNT];
474 bool have_last;
475 uint8_t last_online;
476 /* FAST's last windows, newest first; ring_at numbers entry 0. */
478 uint64_t ring_at;
479 uint8_t i228_resets;
482 int32_t vbus_uv;
483 bool have_temp, have_diag;
484 uint8_t have_flags;
485 int32_t temp_mdegc;
486 uint16_t diag;
488 sense_run_t run;
489 sense_cap_t cap;
490 sense_enc_t enc;
491 /* The last SENSE_FAST_HISTORY FAST samples, oldest overwritten. */
493 uint8_t hist_n, hist_head;
495
508 const sense_sched_cfg_t *cfg);
509
516
524
534bool sense_sched_window(const sense_sched_t *s, unsigned src,
535 sense_window_t *out);
536
548bool sense_sched_ring(const sense_sched_t *s, unsigned back,
549 sense_ring_win_t *out);
550
563
574void sense_sched_cap_edge(sense_sched_t *s, uint64_t edge_us);
575
582
590
600
601#ifdef __cplusplus
602}
603#endif
The ams OSRAM AS5600: a 12-bit magnetic rotary position sensor on I2C (Inter-Integrated Circuit).
#define s
Short name for esc_stick_s inside the five stick-class files.
Definition esc_stick_panel_int.h:295
The TI INA228: 85V, 20-bit current, voltage, power, energy and charge monitor on I2C (Inter-Integrate...
ina228_setup_err_t
Definition ina228.h:203
The TI INA3221: three channels of 13-bit shunt and bus voltage, 0 to 26V, on I2C (Inter-Integrated Ci...
ina3221_setup_err_t
Definition ina3221.h:218
#define INA3221_CHANNELS
Definition ina3221.h:64
The sensor bus: one I2C (Inter-Integrated Circuit) bus with current monitors on it,...
void sense_sched_tick(sense_sched_t *s)
One tick: every 1ms.
Definition sense_sched.c:759
void sense_i3221_one(sense_i3221_cfg_t *m, bool en, uint8_t addr, uint32_t shunt_uohm, uint8_t channels)
One INA3221 as the SENSE page sets it up before protocol 4.25: the part at addr fitted when en and ad...
Definition sense_i3221_cfg.c:15
bool sense_sched_window(const sense_sched_t *s, unsigned src, sense_window_t *out)
The last complete window of source src (a channel, or SENSE_SRC_INA228); false before one has closed.
Definition sense_sched.c:289
sense_cap_state_t
The move capture's state, in the order a capture passes through; the numbers are link_cap_state_t's.
Definition sense_sched.h:368
@ SENSE_CAP_ARMED
Definition sense_sched.h:370
@ SENSE_CAP_LATE
Definition sense_sched.h:375
@ SENSE_CAP_LOST
Definition sense_sched.h:377
@ SENSE_CAP_WAITING
Definition sense_sched.h:371
@ SENSE_CAP_SETTLED
Definition sense_sched.h:374
@ SENSE_CAP_UNSEEN
Definition sense_sched.h:376
void sense_sched_arm(sense_sched_t *s)
The edge into driving: the run's peaks start again and the INA228's totals are cleared on the next ti...
Definition sense_sched.c:283
static unsigned sense_ch(unsigned a, unsigned ch)
Channel ch (1 to 3) of part a (0 to 3): 0 to 11.
Definition sense_sched.h:250
void sense_sched_init(sense_sched_t *s, const sense_sched_io_t *io, const sense_sched_cfg_t *cfg)
A schedule over io for the parts cfg enables.
Definition sense_sched.c:245
#define SENSE_WIN_RING
FAST windows the ring keeps: 200ms.
Definition sense_sched.h:196
void sense_sched_cap_edge(sense_sched_t *s, uint64_t edge_us)
The PWM frame carrying the new pulse starts at edge_us, on the clock now_us() reads; it may lie ahead...
Definition sense_sched_cap.c:59
bool sense_sched_ring(const sense_sched_t *s, unsigned back, sense_ring_win_t *out)
FAST's window numbered back before the newest complete one, back under SENSE_WIN_RING; false,...
Definition sense_sched.c:299
#define SENSE_I3221_PARTS
INA3221 parts, at 0x40 to 0x43, and their channels.
Definition sense_sched.h:239
void sense_sched_cap_disarm(sense_sched_t *s)
Stop a capture; its count stays.
Definition sense_sched_cap.c:91
#define SENSE_FAST_HISTORY
FAST samples kept for that level: one window at 1kHz.
Definition sense_sched.h:236
const ina3221_t * sense_sched_fast_part(const sense_sched_t *s)
FAST's INA3221, or NULL with no FAST channel.
Definition sense_sched.c:277
uint16_t sense_sched_enc_still_ms(const sense_sched_t *s)
How long the encoder's angle has stayed within SENSE_ENC_STILL_TOL counts of its anchor,...
Definition sense_sched.c:594
bool sense_sched_cap_arm(sense_sched_t *s, const sense_cap_arm_t *levels)
Arm a capture on FAST with levels; rise_ua SENSE_CAP_RISE_AUTO takes the level before the command fro...
Definition sense_sched_cap.c:21
#define SENSE_CAP_FILTER_N
The capture's filter: a moving mean of 4 samples, 4ms.
Definition sense_sched.h:204
One servo move judged from the servo's current, sample by sample: when it started moving and when it ...
Definition as5600.h:189
Definition ina228.h:316
One INA3221 on a sensor bus: its part record, shunts and Configuration.
Definition ina3221.h:228
A window being filled.
Definition sense_sched.h:323
int32_t i_max
Definition sense_sched.h:330
bool clip_lo
Definition sense_sched.h:328
bool clip_hi
Definition sense_sched.h:327
uint16_t n_lo
Definition sense_sched.h:326
int32_t i_min
Definition sense_sched.h:329
int32_t v_min
Definition sense_sched.h:331
Definition sense_bus.h:91
A capture's levels, µA.
Definition sense_sched.h:381
int32_t hold_ua
Definition sense_sched.h:384
int32_t move_ua
Definition sense_sched.h:385
int32_t band_ua
Definition sense_sched.h:386
int32_t rise_ua
Definition sense_sched.h:382
A FAST sample held while armed, for the filter.
Definition sense_sched.h:390
int8_t clip
Definition sense_sched.h:392
The state of a capture of a FAST move.
Definition sense_sched.h:404
uint32_t move_t
Definition sense_sched.h:416
int32_t peak_ua
Definition sense_sched.h:420
uint32_t edge_t
Definition sense_sched.h:408
sense_cap_arm_t arm
Definition sense_sched.h:407
bool clipped
Definition sense_sched.h:423
bool rise_owed
Definition sense_sched.h:410
uint32_t arrive_t
Definition sense_sched.h:418
int32_t mean_ua
Definition sense_sched.h:421
uint16_t seq
Definition sense_sched.h:406
uint32_t samples
Definition sense_sched.h:422
uint32_t arm_t
Definition sense_sched.h:409
The output encoder as last read.
Definition sense_sched.h:428
uint8_t agc
Definition sense_sched.h:435
uint16_t magnitude
Definition sense_sched.h:436
uint32_t slots
Definition sense_sched.h:437
uint8_t status
Definition sense_sched.h:431
uint16_t samples
Definition sense_sched.h:433
bool have_mag
Definition sense_sched.h:434
uint16_t raw
Definition sense_sched.h:432
uint64_t anchor_ms
Definition sense_sched.h:439
uint16_t anchor
Definition sense_sched.h:438
bool anchored
Definition sense_sched.h:440
bool have_angle
Definition sense_sched.h:430
A FAST sample kept for the level before a command.
Definition sense_sched.h:396
uint32_t t
Definition sense_sched.h:397
int32_t ua
Definition sense_sched.h:398
The wire. n is the register width in bytes: 2, 3 or 5 for a read (INA228 SLYS021A Table 7-3,...
Definition sense_bus.h:77
The INA3221s: which are fitted, each channel's shunt, and FAST.
Definition sense_sched.h:275
uint32_t shunt_uohm[SENSE_CHANNELS]
Definition sense_sched.h:277
uint8_t fast
Definition sense_sched.h:278
uint8_t fitted
Definition sense_sched.h:276
One FAST window as the ring keeps it.
Definition sense_sched.h:335
bool clip_hi
Definition sense_sched.h:340
int32_t v_mean_uv
Definition sense_sched.h:345
int32_t v_min_uv
Definition sense_sched.h:346
uint16_t n_v
Definition sense_sched.h:339
bool clip_lo
Definition sense_sched.h:341
int32_t i_min_ua
Definition sense_sched.h:343
uint16_t n_clip
Definition sense_sched.h:338
bool closed
Definition sense_sched.h:336
int32_t i_max_ua
Definition sense_sched.h:344
int32_t i_mean_ua
Definition sense_sched.h:342
uint16_t n_i
Definition sense_sched.h:337
The run since sense_sched_arm(), from the INA228.
Definition sense_sched.h:350
uint64_t energy_mj
Definition sense_sched.h:360
bool i_clipped
Definition sense_sched.h:354
bool have_v
Definition sense_sched.h:351
bool have_p
Definition sense_sched.h:353
int32_t v_min_uv
Definition sense_sched.h:355
int64_t charge_uc
Definition sense_sched.h:361
int32_t i_max_ua
Definition sense_sched.h:356
bool clear_owed
Definition sense_sched.h:358
int64_t p_max_uw
Definition sense_sched.h:357
bool have_i
Definition sense_sched.h:352
bool totals_ok
Definition sense_sched.h:359
What is fitted and how; a part not enabled is never addressed.
Definition sense_sched.h:282
bool as5600_en
Definition sense_sched.h:288
The wire, the clock and the bus recovery.
Definition sense_sched.h:266
uint64_t(*) now_us(void *ctx)
Definition sense_sched.h:268
void(*) recover(void *ctx)
Definition sense_sched.h:269
void * ctx
Definition sense_sched.h:271
The sampling schedule of the sensor bus: its parts, windows and captures.
Definition sense_sched.h:446
uint8_t have_flags
Definition sense_sched.h:484
ina3221_setup_err_t i3221_setup[SENSE_I3221_PARTS]
Definition sense_sched.h:453
uint8_t i228_resets
Definition sense_sched.h:479
uint8_t last_online
Definition sense_sched.h:475
uint64_t t0_ms
Definition sense_sched.h:470
int32_t temp_mdegc
Definition sense_sched.h:485
ina228_setup_err_t i228_setup
Definition sense_sched.h:452
uint8_t i3221_resets[SENSE_I3221_PARTS]
Definition sense_sched.h:480
uint32_t rot
Definition sense_sched.h:468
uint16_t diag
Definition sense_sched.h:486
uint8_t volt_at
Definition sense_sched.h:463
uint16_t flags[SENSE_I3221_PARTS]
Definition sense_sched.h:487
uint32_t gen
Definition sense_sched.h:465
bool have_vbus
Definition sense_sched.h:481
uint64_t win
Definition sense_sched.h:471
One complete window of one source.
Definition sense_sched.h:309
uint16_t n_v
Definition sense_sched.h:312
bool clip_hi
Definition sense_sched.h:313
uint16_t n_i
Definition sense_sched.h:311
int32_t v_mean_uv
Definition sense_sched.h:318
bool clip_lo
Definition sense_sched.h:314
uint16_t number
Definition sense_sched.h:310
int32_t v_min_uv
Definition sense_sched.h:319
int32_t i_min_ua
Definition sense_sched.h:316
int32_t i_mean_ua
Definition sense_sched.h:315
int32_t i_max_ua
Definition sense_sched.h:317
Definition servo_move.h:118