forked from cntools/libsurvive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors-readout.c
383 lines (307 loc) · 9.68 KB
/
sensors-readout.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <survive.h>
static volatile int keepRunning = 1;
static void redraw(SurviveContext *ctx);
#include "math.h"
#include <os_generic.h>
#include <stdlib.h>
#include <variance.h>
#ifndef _WIN32
#include <sys/ioctl.h>
#include <unistd.h>
#include <assert.h>
#include <ctype.h>
#include <os_generic.h>
#include <signal.h>
void intHandler(int dummy) {
if (keepRunning == 0)
exit(-1);
keepRunning = 0;
}
#endif
bool needsRedraw = false;
bool surviveIsDone = false;
struct variance_measure imu_variance = {.size = 6};
struct sensor_stats {
double MN, MX;
struct variance_measure variance;
};
struct sensor_time_stats {
size_t hit_count;
double hz;
size_t hz_count;
survive_timecode hz_start;
};
struct sensor_stats stats[32][NUM_GEN2_LIGHTHOUSES][SENSORS_PER_OBJECT][2] = {0};
struct sensor_time_stats time_stats[32][NUM_GEN2_LIGHTHOUSES][SENSORS_PER_OBJECT] = {0};
struct sensor_time_stats imu_time_stats[32];
void process_reading(int i, int lh, int sensor, int axis, FLT angle) {
struct sensor_stats *s = &stats[i][lh][sensor][axis];
if (isnan(angle))
return;
variance_measure_add(&stats[i][lh][sensor][axis].variance, &angle);
s->MN = fmin(angle, s->MN);
s->MX = fmax(angle, s->MX);
}
static void record_data(SurviveObject *so, int sensor_id, survive_timecode timecode, uint32_t lh) {
size_t idx = 0;
for (idx = 0; idx < so->ctx->objs_ct && so->ctx->objs[idx] != so; idx++)
;
time_stats[idx][lh][sensor_id].hit_count++;
double time_since_start =
survive_timecode_difference(timecode, time_stats[idx][lh][sensor_id].hz_start) / (double)so->timebase_hz;
struct SurviveContext *ctx = so->ctx;
time_stats[idx][lh][sensor_id].hz_count++;
if (time_since_start > 3. || time_stats[idx][lh][sensor_id].hz_start == 0) {
if (time_stats[idx][lh][sensor_id].hz_start != 0)
time_stats[idx][lh][sensor_id].hz = time_stats[idx][lh][sensor_id].hz_count / time_since_start;
time_stats[idx][lh][sensor_id].hz_count = 0;
time_stats[idx][lh][sensor_id].hz_start = timecode;
variance_measure_reset(&stats[idx][lh][sensor_id]->variance);
}
}
void angle_fn(SurviveObject *so, int sensor_id, int acode, survive_timecode timecode,
FLT length, FLT angle, uint32_t lh) {
record_data(so, sensor_id, timecode, lh);
survive_default_angle_process(so, sensor_id, acode, timecode, length, angle, lh);
}
void sweep_fn(SurviveObject *so, survive_channel channel, int sensor_id, survive_timecode timecode, int8_t plane,
FLT angle) {
record_data(so, sensor_id, timecode, survive_get_bsd_idx(so->ctx, channel));
survive_default_sweep_angle_process(so, channel, sensor_id, timecode, plane, angle);
if (needsRedraw)
redraw(so->ctx);
}
const char *column_width = " ";
static void print_int(int i) { printf("%9d |", i); }
static void print_small(float f) { printf("%+3.2f ", f); }
static void print(float f) {
if (isnan(f)) {
printf("%s|", column_width);
} else if (fabs(f) > 0 && fabs(f) < 1e-4) {
printf("%+9.2e |", f);
} else if (fabs(f) < 10.) {
printf("%+9.6f |", f);
} else {
printf("%+9.4f |", f);
}
}
static void print_label(const char *l) { printf("%*s|", 10, l); }
int printf_fn(SurviveContext *ctx, const char *fault, ...) { return 0; }
int lh = -1;
bool useRawSensorId = false;
static uint8_t get_raw_sensor_id(SurviveObject *so, uint8_t sensor_id) {
if (so->channel_map) {
for (int i = 0; i < 32; i++) {
if (so->channel_map[i] == sensor_id) {
return i;
}
}
return -1;
}
return sensor_id;
}
char *new_str(const char *s) {
char *rtn = calloc(strlen(s) + 1, sizeof(char));
strcpy(rtn, s);
return rtn;
}
char *lines[10] = {0};
size_t lines_idx = 0;
int window_rows = -1, window_cols = -1;
#define gotoxy(x, y) printf("\033[%d;%dH", (y), (x))
static void redraw(SurviveContext *ctx) {
printf("\033[;H");
for (int i = 0; i < ctx->objs_ct; i++) {
SurviveObject *so = ctx->objs[i];
printf("%s (%+5.2fs still): ", so->codename,
SurviveSensorActivations_stationary_time(&so->activations) / 48000000.);
if (lh >= 0) {
double v[2] = {0, 0};
int v_cnt[2] = {0};
for (int sensor = 0; sensor < so->sensor_ct; sensor++) {
for (int axis = 0; axis < 2; axis++) {
FLT f = so->activations.angles[sensor][lh][axis];
if (!isnan(f)) {
v_cnt[axis]++;
v[axis] += f;
}
}
}
for (int axis = 0; axis < 2; axis++) {
printf("%1.6f ", v[axis] / (v_cnt[axis] == 0 ? 1 : (double)v_cnt[axis]));
}
}
FLT calc_imu_var[6];
variance_measure_calc(&imu_variance, calc_imu_var);
printf("IMU: %5.1fhz ", imu_time_stats[i].hz);
for (int i = 0; i < 3; i++)
print_small(so->activations.accel[i]);
for (int i = 0; i < 3; i++)
print_small(so->activations.gyro[i]);
printf("Var: ");
for (int i = 0; i < 6; i++)
print_small(calc_imu_var[i]);
printf("\n");
printf("|\x1B[4m");
const char *labels[] = {"ch.sensor", "Hits", "Hits/sec", "X", "Y", "min X", "max X",
"width X", "var X", "min Y", "max Y", "width Y", "var Y", 0};
for (const char **l = labels; *l; l++) {
print_label(*l);
}
printf("\x1B[0m\n");
int lh_start = lh == -1 ? 0 : lh;
int lh_end = lh == -1 ? NUM_GEN2_LIGHTHOUSES : (lh + 1);
for (int lh = lh_start; lh < lh_end; lh++) {
for (int sensor = 0; sensor < so->sensor_ct; sensor++) {
struct sensor_stats *s = &stats[i][lh][sensor][0];
bool allNans = true;
for (int axis = 0; axis < 2 && allNans; axis++) {
FLT f = so->activations.angles[sensor][lh][axis];
allNans &= isnan(f);
}
if (allNans)
continue;
if (sensor % 2 == 0)
printf("\x1B[2m");
if (sensor == so->sensor_ct - 1)
printf("\x1B[4m");
uint8_t displaySensor = useRawSensorId ? get_raw_sensor_id(so, sensor) : sensor;
printf("| %2d.%02d |", ctx->bsd[lh].mode, displaySensor);
print_int(time_stats[i][lh][sensor].hit_count);
print(time_stats[i][lh][sensor].hz);
for (int axis = 0; axis < 2; axis++) {
FLT f = so->activations.angles[sensor][lh][axis];
process_reading(i, lh, sensor, axis, f);
print(f);
}
for (int axis = 0; axis < 2; axis++) {
print(s[axis].MN);
print(s[axis].MX);
print(s[axis].MX - s[axis].MN);
FLT var;
variance_measure_calc(&s[axis].variance, &var);
print(var);
}
printf("\x1B[0m");
printf("\r\n\33[2K");
}
printf("\33[2K\r\n");
}
}
if (window_cols != -1 && false) {
gotoxy(0, window_rows - 10 - 1);
printf("=== Log ===\n");
for (int i = 0; i < 10; i++) {
char *line = lines[(lines_idx + i) % 10];
if (line != 0)
printf("\33[2K\r %s\n", line);
}
}
needsRedraw = false;
}
void light_fn(SurviveObject *so, int sensor_id, int acode, int timeinsweep, survive_timecode timecode,
survive_timecode length, uint32_t lh) {
survive_default_light_process(so, sensor_id, acode, timeinsweep, timecode, length, lh);
if (needsRedraw)
redraw(so->ctx);
}
void imu_fn(SurviveObject *so, int mode, const FLT *accelgyro, survive_timecode timecode, int id) {
variance_measure_add(&imu_variance, accelgyro);
size_t idx = 0;
for (idx = 0; idx < so->ctx->objs_ct && so->ctx->objs[idx] != so; idx++)
;
imu_time_stats[idx].hit_count++;
double time_since_start =
survive_timecode_difference(timecode, imu_time_stats[idx].hz_start) / (double)so->timebase_hz;
struct SurviveContext *ctx = so->ctx;
imu_time_stats[idx].hz_count++;
if (time_since_start > 3. || imu_time_stats[idx].hz_start == 0) {
if (imu_time_stats[idx].hz_start != 0)
imu_time_stats[idx].hz = imu_time_stats[idx].hz_count / time_since_start;
imu_time_stats[idx].hz_count = 0;
imu_time_stats[idx].hz_start = timecode;
variance_measure_reset(&imu_variance);
}
survive_default_imu_process(so, mode, accelgyro, timecode, id);
}
void info_fn(SurviveContext *ctx, SurviveLogLevel logLevel, const char *fault) {
free(lines[lines_idx % 10]);
lines[lines_idx % 10] = new_str(fault);
lines_idx++;
redraw(ctx);
}
static void inc_lh(SurviveContext *ctx) {
do {
lh++;
} while (lh < NUM_GEN2_LIGHTHOUSES && !ctx->bsd[lh].OOTXSet);
if (lh == NUM_GEN2_LIGHTHOUSES)
lh = -1;
}
void *KBThread(void *user) {
SurviveContext *ctx = user;
while (keepRunning) {
int c = tolower(getchar());
int err_clear = system("clear");
(void)err_clear;
if (c == 'l') {
inc_lh(ctx);
} else if (c == 'q') {
keepRunning = false;
} else if (c == 'r') {
useRawSensorId = !useRawSensorId;
}
if (surviveIsDone) {
redraw(ctx);
} else {
needsRedraw = true;
if (c == 10) {
inc_lh(ctx);
}
}
}
return 0;
}
int main(int argc, char **argv) {
#ifdef __linux__
signal(SIGINT, intHandler);
signal(SIGTERM, intHandler);
signal(SIGKILL, intHandler);
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
window_cols = w.ws_col;
window_rows = w.ws_row;
#endif
struct sensor_stats *s = &stats[0][0][0][0];
for (int i = 0; i < 32 * NUM_GEN2_LIGHTHOUSES * SENSORS_PER_OBJECT * 2; i++) {
s[i].MX = s[i].MN = NAN;
}
SurviveContext *ctx = survive_init(argc, argv);
if (ctx == 0) // implies -help or similiar
return 0;
FLT last_redraw = OGGetAbsoluteTime();
survive_install_sweep_angle_fn(ctx, sweep_fn);
survive_install_angle_fn(ctx, angle_fn);
survive_install_printf_fn(ctx, printf_fn);
survive_install_log_fn(ctx, info_fn);
survive_install_imu_fn(ctx, imu_fn);
survive_install_light_fn(ctx, light_fn);
survive_startup(ctx);
int clear_err = system("clear");
(void)clear_err;
og_thread_t kbThread = OGCreateThread(KBThread, "kb-thread", ctx);
while (keepRunning && survive_poll(ctx) == 0) {
FLT this_time = OGGetAbsoluteTime();
if (this_time > last_redraw + .03) {
needsRedraw = true;
last_redraw = this_time;
redraw(ctx);
}
}
surviveIsDone = true;
if (keepRunning) {
printf("Survive done, type 'q <enter>' to exit...\n");
}
OGJoinThread(kbThread);
survive_close(ctx);
return 0;
}