rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
openyge.h
Go to the documentation of this file.
1
30
31#ifndef RCBENCH_OPENYGE_H
32#define RCBENCH_OPENYGE_H
33
34#include <stdbool.h>
35#include <stddef.h>
36#include <stdint.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/* ------------------------------------------------------------- the wire */
43
44#define OPENYGE_SYNC 0xA5u
45#define OPENYGE_VERSION 3u
48
49#define OPENYGE_HDR_V3 6u
50#define OPENYGE_HDR_LEGACY 4u
51#define OPENYGE_CRC_BYTES 2u
52
53#define OPENYGE_TELEMETRY_BYTES 26u
54#define OPENYGE_CONTROL_BYTES 4u
55
60#define OPENYGE_MIN_FRAME (OPENYGE_HDR_LEGACY + OPENYGE_CRC_BYTES)
61#define OPENYGE_MAX_FRAME 140u
62
70#define OPENYGE_CRC_INIT 0x0000u
71#define OPENYGE_CRC_CHECK 0x31C3u
72
87uint16_t openyge_crc(uint16_t crc, const void *data, size_t len);
88
90#define OPENYGE_DEV_MASTER 0x80u
91#define OPENYGE_DEV_ADDR_MASK 0x7Fu
92#define OPENYGE_DEV_BROADCAST 0x00u
93
97#define OPENYGE_TEMP_OFFSET 40
98
107
108/* ------------------------------------------------------------ decoding */
109
110typedef struct {
111 uint8_t version;
112 uint8_t type;
113 uint8_t seq;
114 uint8_t device;
115 bool legacy;
117
119typedef struct {
122 uint8_t payload_len;
124
126typedef struct {
127 uint8_t buf[OPENYGE_MAX_FRAME];
128 uint8_t len;
129 /* Counters for the analyser screen: a quiet line, a noisy one and a
130 * wrong baud rate are distinguished here. crc_errors and rejected count
131 * a failing sync candidate once, when it reaches the front of the
132 * buffer; bytes dropped ahead of a candidate count under resyncs. */
133 uint32_t frames;
134 uint32_t crc_errors;
135 uint32_t resyncs;
136 uint32_t rejected;
138
145
159bool openyge_decode_byte(openyge_decoder_t *d, uint8_t byte,
160 openyge_frame_t *out);
161
162/* ------------------------------------------------------------ telemetry */
163
164typedef struct {
165 float volts;
166 float amps;
168 uint32_t erpm;
169 int8_t pwm_pct;
171 float bec_volts;
172 float bec_amps;
173 int16_t temp_c;
174 int16_t bec_temp_c;
175 int16_t cap_temp_c;
176 int16_t aux_temp_c;
177 uint8_t status1;
178 uint8_t status2;
179 uint16_t pidx;
180 uint16_t pdata;
182
190
203
213bool openyge_control_parse(const openyge_frame_t *f, uint16_t *index,
214 uint16_t *param);
215
216/* ------------------------------------------------------------- encoding */
217
237size_t openyge_encode(uint8_t *out, size_t cap, uint8_t type, uint8_t device,
238 uint8_t seq, const void *payload, size_t payload_len);
239
249size_t openyge_build_telemetry_request(uint8_t *out, size_t cap,
250 uint8_t device, uint8_t seq);
251
265size_t openyge_build_param_write(uint8_t *out, size_t cap, uint8_t device,
266 uint8_t seq, uint16_t index, uint16_t value);
267
268/* ---------------------------------------------------------------- status */
269
271typedef enum {
272 OPENYGE_SUBJECT_ESC = 0,
273 OPENYGE_SUBJECT_BEC,
275
282typedef enum {
283 OPENYGE_ST_DISARMED = 0x0,
284 OPENYGE_ST_POWER_CUT = 0x1,
285 OPENYGE_ST_FAST_START = 0x2,
286 OPENYGE_ST_ALIGN_FOR_POS = 0x4,
287 OPENYGE_ST_BRAKING_NORM_FINI = 0x6,
288 OPENYGE_ST_BRAKING_SYNC_FINI = 0x7,
289 OPENYGE_ST_STARTING = 0x8,
290 OPENYGE_ST_BRAKING_NORM = 0x9,
291 OPENYGE_ST_BRAKING_SYNC = 0xA,
292 OPENYGE_ST_WINDMILLING = 0xC,
293 OPENYGE_ST_RUNNING_NORM = 0xE,
295
297typedef struct {
298 uint8_t state;
300
301 /*
302 * The overloaded combination. 0x80|0x40 would read as a BEC (battery
303 * eliminator circuit) over-current, which cannot occur, so that exact
304 * value means the ESC reports a dirty input signal instead. While this
305 * is set, the subject and the warning flags below carry no meaning.
306 */
307 bool setpoint_noise;
308
310 bool warn_undervoltage;
311 bool warn_overtemp;
312 bool warn_overcurrent;
313
314 /*
315 * A warning bit alone is a caution; it is a fault only in combination
316 * with the state. Over-voltage has no bit of its own: it is the absence
317 * of warnings while the power is cut. A decoder that reports flags
318 * without the state misses that condition.
319 */
320 bool fault_overvoltage;
321 bool fault_undervoltage;
322 bool fault_overtemp;
323 bool fault_overcurrent;
324 bool any_fault;
326
333void openyge_status_decode(uint8_t status1, openyge_status_t *out);
334
341const char *openyge_state_name(uint8_t state);
342
343/* ------------------------------------------------------------ parameters */
344
348#define OPENYGE_MAX_PARAMS 64
349
356#define OPENYGE_P_COUNT 0
357#define OPENYGE_P_MOTOR_POLES 20
358#define OPENYGE_P_PINION_TEETH 21
359#define OPENYGE_P_MAIN_TEETH 22
360
361typedef struct {
363 uint64_t seen;
364 /* Parameter 0. Zero means the count has not arrived: a table of zero
365 * parameters cannot exist, so no separate flag is needed. */
366 uint16_t count;
369
376
388void openyge_params_observe(openyge_params_t *p, uint16_t index,
389 uint16_t value);
390
404
413
425bool openyge_params_get(const openyge_params_t *p, uint16_t index,
426 uint16_t *out);
427
438
452bool openyge_motor_rpm(const openyge_params_t *p, uint32_t erpm,
453 float *motor_rpm);
462bool openyge_head_rpm(const openyge_params_t *p, uint32_t erpm,
463 float *head_rpm);
464
465#ifdef __cplusplus
466}
467#endif
468
469#endif /* RCBENCH_OPENYGE_H */
bool openyge_telemetry_parse(const openyge_frame_t *f, openyge_telemetry_t *out)
Parse the telemetry payload.
Definition openyge_frame.c:301
void openyge_params_observe(openyge_params_t *p, uint16_t index, uint16_t value)
Record one (index, value) pair from a telemetry frame.
Definition openyge_params.c:17
size_t openyge_build_param_write(uint8_t *out, size_t cap, uint8_t device, uint8_t seq, uint16_t index, uint16_t value)
A single parameter write.
Definition openyge_frame.c:395
float openyge_params_progress(const openyge_params_t *p)
0..1, for a progress bar.
Definition openyge_params.c:92
bool openyge_params_complete(const openyge_params_t *p)
True once every index 0..count-1 has been seen and no write is pending.
Definition openyge_params.c:59
bool openyge_motor_rpm(const openyge_params_t *p, uint32_t erpm, float *motor_rpm)
Mechanical and head RPM (revolutions per minute) from eRPM (electrical RPM) and the gear train.
Definition openyge_params.c:107
bool openyge_params_get(const openyge_params_t *p, uint16_t index, uint16_t *out)
Read one.
Definition openyge_params.c:80
size_t openyge_build_telemetry_request(uint8_t *out, size_t cap, uint8_t device, uint8_t seq)
A telemetry request: the control payload, both words zero.
Definition openyge_frame.c:387
size_t openyge_encode(uint8_t *out, size_t cap, uint8_t type, uint8_t device, uint8_t seq, const void *payload, size_t payload_len)
Build a frame.
Definition openyge_frame.c:355
void openyge_params_reset(openyge_params_t *p)
Clear the parameter cache.
Definition openyge_params.c:10
const char * openyge_state_name(uint8_t state)
A short name for a motor state, or "?" for a reserved value.
Definition openyge_status.c:21
uint16_t openyge_crc(uint16_t crc, const void *data, size_t len)
Fold len bytes at data into crc and return the new accumulator.
Definition openyge_frame.c:88
openyge_state_t
The motor states.
Definition openyge.h:282
void openyge_decoder_reset(openyge_decoder_t *d)
Clear a decoder, buffer and counters.
Definition openyge_frame.c:104
bool openyge_decode_byte(openyge_decoder_t *d, uint8_t byte, openyge_frame_t *out)
Feed one received byte; returns true when out holds a whole good frame.
Definition openyge_frame.c:219
void openyge_status_decode(uint8_t status1, openyge_status_t *out)
Decode the status byte of a telemetry frame.
Definition openyge_status.c:39
openyge_subject_t
Which device the warning bits are talking about.
Definition openyge.h:271
void openyge_params_begin_writes(openyge_params_t *p)
Mark writes outstanding, which withdraws the table until it is re-read.
Definition openyge_params.c:37
bool openyge_head_rpm(const openyge_params_t *p, uint32_t erpm, float *head_rpm)
Head rpm from eRPM and the gear train.
Definition openyge_params.c:120
bool openyge_frame_has_telemetry(const openyge_frame_t *f)
True if this frame is one an ESC sends, i.e. one carrying telemetry.
Definition openyge_frame.c:295
openyge_frame_type_t
Frame type codes, the type byte of the header.
Definition openyge.h:100
@ OPENYGE_FT_TELE_REQ
Definition openyge.h:103
@ OPENYGE_FT_TELE_RESP
Definition openyge.h:102
@ OPENYGE_FT_TELE_AUTO
Definition openyge.h:101
@ OPENYGE_FT_WRITE_PARAM_REQ
Definition openyge.h:105
@ OPENYGE_FT_WRITE_PARAM_RESP
Definition openyge.h:104
void openyge_params_end_writes(openyge_params_t *p)
Clear the outstanding-writes mark.
Definition openyge_params.c:52
#define OPENYGE_MAX_FRAME
Definition openyge.h:61
#define OPENYGE_MAX_PARAMS
The bitmap that tracks which indices have been seen is 64 bits wide.
Definition openyge.h:348
bool openyge_control_parse(const openyge_frame_t *f, uint16_t *index, uint16_t *param)
Parse the 4-byte control payload of a request.
Definition openyge_frame.c:337
Byte buffer and counters of the resynchronising decoder.
Definition openyge.h:126
uint32_t rejected
Definition openyge.h:136
One decoded frame: header and payload.
Definition openyge.h:119
uint8_t payload[OPENYGE_MAX_FRAME]
Definition openyge.h:121
uint8_t payload_len
Definition openyge.h:122
Definition openyge.h:110
uint8_t type
Definition openyge.h:112
uint8_t device
Definition openyge.h:114
bool legacy
Definition openyge.h:115
uint8_t version
Definition openyge.h:111
uint8_t seq
Definition openyge.h:113
Definition openyge.h:361
uint16_t value[OPENYGE_MAX_PARAMS]
Definition openyge.h:362
uint64_t seen
Definition openyge.h:363
bool writes_pending
Definition openyge.h:367
Decoded status byte.
Definition openyge.h:297
bool state_known
Definition openyge.h:299
openyge_subject_t subject
Definition openyge.h:309
uint8_t state
Definition openyge.h:298
Definition openyge.h:164
uint8_t status1
Definition openyge.h:177
uint8_t status2
Definition openyge.h:178
float amps
Definition openyge.h:166
float bec_volts
Definition openyge.h:171
uint16_t pidx
Definition openyge.h:179
uint16_t pdata
Definition openyge.h:180
int16_t temp_c
Definition openyge.h:173
int8_t throttle_pct
Definition openyge.h:170
int16_t cap_temp_c
Definition openyge.h:175
uint16_t consumption_mah
Definition openyge.h:167
int16_t bec_temp_c
Definition openyge.h:174
float volts
Definition openyge.h:165
int8_t pwm_pct
Definition openyge.h:169
float bec_amps
Definition openyge.h:172
uint32_t erpm
Definition openyge.h:168
int16_t aux_temp_c
Definition openyge.h:176