rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
servo_sync.h
Go to the documentation of this file.
1
34
35#pragma once
36
37#include <stdbool.h>
38#include <stdint.h>
39
40typedef enum {
41 SERVO_SYNC_RUNNING = 0,
42 SERVO_SYNC_DONE,
43 SERVO_SYNC_FAULT,
44} servo_sync_state_t;
45
46typedef enum {
47 SERVO_SYNC_FAULT_NONE = 0,
66
68typedef enum {
69 SERVO_SYNC_STAGE_CENTRE = 0,
70 SERVO_SYNC_STAGE_HIGH,
71 SERVO_SYNC_STAGE_LOW,
72 SERVO_SYNC_STAGE_COUNT,
74
76#define SERVO_SYNC_MAX_POINTS 9
77
78typedef struct {
79 uint16_t centre_us;
80 uint16_t deflection_us;
81
82 int16_t window_us;
83 uint8_t points;
84 uint8_t rounds;
85
101 uint16_t settle_ms;
102 float slew_us_per_ms;
103 uint16_t measure_ms;
104
114
115 float hard_limit_a;
117
119#define SERVO_SYNC_MIN_US 500u
120#define SERVO_SYNC_MAX_US 2500u
121
122typedef struct {
124 servo_sync_state_t state;
125 servo_sync_fault_t fault;
126
127 /* The answers. All three are what the *second* servo should be commanded
128 * to, given the first is at centre, at +deflection and at -deflection. */
129 int16_t trim_us;
130 int16_t travel_hi_us;
131 int16_t travel_lo_us;
132
137 float best_a[SERVO_SYNC_STAGE_COUNT];
138 float worst_a[SERVO_SYNC_STAGE_COUNT];
139
140 /* Internals. */
141 servo_sync_stage_t stage;
142 uint8_t round;
143 uint8_t point;
144 int32_t win_centre_us;
145 int32_t win_half_us;
146 float scan_a[SERVO_SYNC_MAX_POINTS];
147 uint32_t phase_start_ms;
148 uint32_t travel_ms;
149 bool measuring;
150 double sum_a;
151 uint32_t samples;
152 bool started;
153 uint16_t cmd_a_us;
154 uint16_t cmd_b_us;
156
163void servo_sync_defaults(servo_sync_cfg_t *cfg, uint16_t centre_us);
164
171void servo_sync_init(servo_sync_t *sy, const servo_sync_cfg_t *cfg);
172
187void servo_sync_step(servo_sync_t *sy, float total_a, uint32_t now_ms,
188 uint16_t *cmd_a, uint16_t *cmd_b);
189
196float servo_sync_progress(const servo_sync_t *sy);
void servo_sync_step(servo_sync_t *sy, float total_a, uint32_t now_ms, uint16_t *cmd_a, uint16_t *cmd_b)
Advance the search with one reading of the pair's total current.
Definition servo_sync.c:277
#define SERVO_SYNC_MAX_POINTS
The most points one round of a scan may have.
Definition servo_sync.h:76
void servo_sync_defaults(servo_sync_cfg_t *cfg, uint16_t centre_us)
Defaults for a sync search.
Definition servo_sync.c:25
servo_sync_stage_t
Which of the three measurements the search is on.
Definition servo_sync.h:68
servo_sync_fault_t
Definition servo_sync.h:46
@ SERVO_SYNC_FAULT_NO_MINIMUM
The scan found no minimum distinguishable from the noise, or none inside its window.
Definition servo_sync.h:64
@ SERVO_SYNC_FAULT_OVERCURRENT
Current crossed the hard ceiling.
Definition servo_sync.h:51
void servo_sync_init(servo_sync_t *sy, const servo_sync_cfg_t *cfg)
Start a sync search at the centre stage.
Definition servo_sync.c:135
float servo_sync_progress(const servo_sync_t *sy)
0..1 across all three stages.
Definition servo_sync.c:330
Definition servo_sync.h:78
int16_t window_us
Definition servo_sync.h:82
uint16_t settle_ms
How long to wait after a move before a reading counts.
Definition servo_sync.h:101
uint16_t deflection_us
Definition servo_sync.h:80
uint8_t rounds
Definition servo_sync.h:84
float min_depth_a
How far the best reading of a scan must sit below the worst before the minimum counts.
Definition servo_sync.h:113
uint8_t points
Definition servo_sync.h:83
Definition servo_sync.h:122
uint32_t travel_ms
Definition servo_sync.h:148
float best_a[SERVO_SYNC_STAGE_COUNT]
What the pair draws at the best and the worst point of each stage.
Definition servo_sync.h:137
int16_t travel_lo_us
Definition servo_sync.h:131
int16_t travel_hi_us
Definition servo_sync.h:130
int16_t trim_us
Definition servo_sync.h:129