Monado OpenXR Runtime
Loading...
Searching...
No Matches
pssense_gray_code.h
Go to the documentation of this file.
1// Copyright 2026, Beyley Cardellio
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Routines to help decoding PS VR2 controller Gray codes + brightness curves
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup tracking
8 */
9
10#pragma once
11
13
14#include "util/u_time.h"
15
16#include "math/m_api.h"
17
18#include "led_sync_internal.h"
19
20#include <float.h>
21
22
23/*!
24 * Whether to enable the "spew" for brightness-decoding related routines.
25 *
26 * @note This should never be upstreamed set to `1`!
27 */
28#define GRAY_SPEW 0
29
30#if GRAY_SPEW
31#define LOG_GRAY_SPEW(refinement, ...) LOG_DEBUG(refinement, __VA_ARGS__)
32#else
33#define LOG_GRAY_SPEW(refinement, ...)
34#endif
35
36#define TIME_BETWEEN_LED_SELECTIONS_NS (U_TIME_1MS_IN_NS * 2500LL)
37
38//! The jump amount for an invalid code
39#define INVALID_GRAY_CODE_JUMP INT64_MAX
40//! This gray code isn't in the official table.
41#define INVALID_GRAY_CODE (0x4)
42//! How many identical gray codes do we need to decode in a row to accept it.
43#define GRAY_CODE_MATCHES_NEEDED 3
44//! How long to wait without an accepted gray code before resetting back to phase 1
45#define GRAY_CODE_TIMEOUT_NS (U_TIME_1S_IN_NS * 10LL)
46
47#define BG_NUM_FRAMES T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES
48//! Each frame of the BG curve is shifted this much further than the last.
49#define BG_STEP_NS (U_TIME_1US_IN_NS * 50)
50//! The frame the dip of the BG curve should be centered on when in sync.
51#define BG_TARGET_STEP 10.0f
52//! Proportional gain for the BG clock-offset servo.
53#define BG_OFFSET_GAIN 0.5f
54//! Errors within this are left to the cycle trim, wide enough that measurement noise doesn't trigger sends.
55#define BG_DEADBAND_STEPS 0.25f
56//! BG measurements per skew least-squares window.
57#define BG_SKEW_WINDOW_SAMPLES 20
58//! Clamp on the estimated clock skew, in ns per ns.
59#define BG_MAX_SKEW 1e-3
60//! How long BG may go without an accepted measurement before the lock is given up.
61#define BG_NO_MEASUREMENT_TIMEOUT_NS (U_TIME_1S_IN_NS * 13LL)
62//! Smoothing gain on the LEDs' measured offset from the exposure, trading measurement noise against lag.
63#define BG_LED_ERROR_SMOOTHING 0.2
64//! How far the clock model may move before it's republished, every publish resends the LED settings.
65#define BG_MODEL_PUBLISH_THRESHOLD_NS (U_TIME_1US_IN_NS * 3)
66//! The fitted rate has to stay under this to enter STABLE, in ns per ns.
67#define BG_STABLE_MAX_RATE 2e-6
68//! How long the rate has to stay converged before STABLE, first time and coming back from it.
69#define BG_STABLE_DWELL_NS (U_TIME_1S_IN_NS * 15LL)
70#define BG_STABLE_DWELL_WARM_NS (U_TIME_1S_IN_NS * 3LL)
71//! How long STABLE runs unmeasured before BG checks the LEDs again.
72#define STABLE_RECHECK_NS (U_TIME_1S_IN_NS * 100LL)
73//! How long STABLE can go without seeing the controller before starting over.
74#define STABLE_NOT_SEEN_TIMEOUT_NS (U_TIME_1S_IN_NS * 15LL)
75
76//! A table of how much time to jump for each gray code
78 U_TIME_1US_IN_NS * -500, // 000
79 U_TIME_1US_IN_NS * -250, // 001
80 U_TIME_1US_IN_NS * 250, // 010
81 0, // 011 <- Target code
82 INVALID_GRAY_CODE_JUMP, // 100 <- Invalid, never emit.
83 U_TIME_1US_IN_NS * 750, // 101
84 U_TIME_1US_IN_NS * 500, // 110
85 U_TIME_1US_IN_NS * 750, // 111
86};
87
88static void
89handle_successful_gray_decode(struct t_led_sync_refinement *refinement, uint16_t gray_code)
90{
91 timepoint_ns jump_amount_ns = gray_code_jumps_ns[gray_code];
92 assert(jump_amount_ns != INVALID_GRAY_CODE_JUMP);
93
94 refinement->decode_gray.last_accepted_gray_code_ns = os_monotonic_get_ns();
95
96 if (jump_amount_ns == 0) {
97 LOG_DEBUG(refinement, "Got the \"done\" gray code (%d), moving to next phase.", gray_code);
98
99 // We're done!
100 set_phase_locked(refinement, T_LED_SYNC_PHASE_OPTIMIZE_BRIGHTNESS_CURVE);
101
102 return;
103 }
104
105 LOG_DEBUG(refinement, "Accepting gray code %x after %d identical matches, moving %" PRIi64 "us", gray_code,
106 refinement->decode_gray.matching_decodes, (jump_amount_ns / 1000L));
107
108 bool success = advance_clock_offset_pssense_locked(refinement, jump_amount_ns);
109 // @todo should we have a "busy" recovery strategy here?
110 (void)success;
111}
112
113static void
114handle_decoded_gray_code(struct t_led_sync_refinement *refinement, uint16_t decoded_gray_code)
115{
116 LOG_GRAY_SPEW(refinement, "Decoded gray code! Got %x", decoded_gray_code);
117
118 // The code is invalid if it doesn't match the current and isn't the invalid gray code
119 bool invalid_code = decoded_gray_code != refinement->decode_gray.decoded_gray_code || //
120 decoded_gray_code == INVALID_GRAY_CODE;
121
122 if (invalid_code) {
123 // Reset since the code didn't match.
124 refinement->decode_gray.decoded_gray_code = decoded_gray_code;
125 refinement->decode_gray.matching_decodes = 1;
126 } else {
127 refinement->decode_gray.matching_decodes += 1;
128
129 if (refinement->decode_gray.matching_decodes == GRAY_CODE_MATCHES_NEEDED) {
130 // Success!
131 handle_successful_gray_decode(refinement, decoded_gray_code);
132
133 // Re-arm, so a stable code keeps being accepted instead of only once.
134 refinement->decode_gray.matching_decodes = 0;
135 }
136 }
137}
138
139static void
140handle_leds_seen(struct t_led_sync_refinement *refinement, const struct t_constellation_tracker_sample *sample)
141{
142 // Whether the sequence ID is being incremented or not
143 bool clear_slot = sample->sequence_id > refinement->leds_seen.cur_sequence_id;
144
145 // Clear the brightnesses for all cameras in the old slot.
146 if (clear_slot) {
147 for (uint32_t camera_idx = 0; camera_idx < XRT_TRACKING_MAX_CAMS; camera_idx++) {
148 for (uint32_t led_idx = 0; led_idx < T_LED_SYNC_SELECTED_LEDS; led_idx++) {
149 size_t led_buffer_index =
150 T_LED_SYNC_GRAY_CODE_BUFFER_INDEX(camera_idx, sample->sequence_id, led_idx);
151
152 refinement->decode_gray.led_brightnesses[led_buffer_index] = 0.0f;
153 // 1.0 would be "facing the same direction as the camera", so the worst case, we
154 // want facing *toward* the camera, which will be -1.
155 refinement->decode_gray.led_facing_dots[led_buffer_index] = 1.0f;
156 }
157 }
158
159 refinement->leds_seen.num_samples += 1;
160 }
161
162 uint32_t seen_led_idx =
163 T_LED_SYNC_SEEN_LED_BUFFER_INDEX(refinement->leds_seen.cur_sequence_id = sample->sequence_id);
164
165 struct t_led_sync_seen_led *led_buf =
166 refinement->leds_seen.buf + (seen_led_idx * XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE);
167
168 static_assert(ARRAY_SIZE(sample->leds) == XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE,
169 "LED sync depends on these being equal.");
170
171 for (uint32_t i = 0; i < ARRAY_SIZE(sample->leds); i++) {
172 if (clear_slot) {
173 led_buf[i] = (struct t_led_sync_seen_led){0};
174 }
175
176 if (!sample->leds[i].observed) {
177 continue;
178 }
179
180 led_buf[i].accum_seen_count += 1;
181 led_buf[i].accum_facing_dot += sample->leds[i].facing_dot;
182 }
183
184 // Wait until we have enough samples to actually make a selection
185 if (refinement->leds_seen.num_samples < T_LED_SYNC_SEEN_LEDS_ACCUMULATION_BUFFER_SIZE) {
186 return;
187 }
188
189 bool need_new_led_selection =
190 (sample->timestamp_ns - refinement->leds_seen.last_selection_ns) > TIME_BETWEEN_LED_SELECTIONS_NS &&
192
193 // If we don't have a sample outgoing, then compute a new one for the new "most-seen" LEDs.
194 if (!refinement->has_sample_for_driver && need_new_led_selection) {
196
197 for (uint32_t window_idx = 0; window_idx < T_LED_SYNC_SEEN_LEDS_ACCUMULATION_BUFFER_SIZE;
198 window_idx++) {
199 struct t_led_sync_seen_led *window_led_buf =
200 refinement->leds_seen.buf + (window_idx * XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE);
201
202 for (uint32_t led_idx = 0; led_idx < XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE; led_idx++) {
203 leds_seen[led_idx].accum_seen_count += window_led_buf[led_idx].accum_seen_count;
204 leds_seen[led_idx].accum_facing_dot += window_led_buf[led_idx].accum_facing_dot;
205 }
206 }
207
208 size_t most_seen_index = 0;
209 float most_seen_value = -INFINITY;
210 size_t second_most_seen_index = 0;
211 float second_most_seen_value = -INFINITY;
212 for (uint32_t i = 0; i < ARRAY_SIZE(leds_seen); i++) {
213 struct t_led_sync_seen_led seen = leds_seen[i];
214
215 // Seen more times == good, facing more towards camera == good
216 float value = seen.accum_seen_count + -(seen.accum_facing_dot / seen.accum_seen_count);
217
218 if (value > most_seen_value) {
219 second_most_seen_index = most_seen_index;
220 second_most_seen_value = most_seen_value;
221
222 most_seen_index = i;
223 most_seen_value = value;
224 } else if (value > second_most_seen_value) {
225 second_most_seen_index = i;
226 second_most_seen_value = value;
227 }
228 }
229
230 uint8_t leds[4] = {0xFF, 0xFF, 0xFF, 0xFF};
231 if (most_seen_value > -INFINITY) {
232 leds[0] = most_seen_index;
233 }
234 if (second_most_seen_value > -INFINITY) {
235 leds[1] = second_most_seen_index;
236 }
237
238 LOG_GRAY_SPEW(refinement, "Selected LEDs %d,%d", leds[0], leds[1]);
239
240 // Only set the new one if there isn't an existing sample ready
241 if (!refinement->has_sample_for_driver) {
242 set_pssense_mode(refinement, refinement->sample_for_driver.pssense_mode, leds);
243
244 refinement->leds_seen.last_selection_ns = sample->timestamp_ns;
245 }
246 }
247}
248
249static void
250handle_decode_gray_constellation_sample(struct t_led_sync_refinement *refinement,
251 const struct t_constellation_tracker_sample *sample)
252{
253 XRT_MAYBE_UNUSED const uint16_t decoding_mask = 0x7FF; // 0b11111111111
254 const uint16_t header_mask = 0x7F8; // 0b11111111XXX
255 const uint16_t gray_code_mask = 0x7; // 0bXXXXXXXX111
256 const uint16_t expected_header = 0x38; // 0b00000111XXX
257
258 // Update the ring buffer with the new values
259 for (int i = 0; i < T_LED_SYNC_SELECTED_LEDS; i++) {
260 uint8_t led = refinement->sample_for_driver.pssense.leds[i];
261 if (led == 0xFF) {
262 continue;
263 }
264
265 size_t led_buffer_index =
266 T_LED_SYNC_GRAY_CODE_BUFFER_INDEX(sample->camera_index, refinement->leds_seen.cur_sequence_id, i);
267
268 // Total light, for the same contrast as BG.
269 refinement->decode_gray.led_brightnesses[led_buffer_index] = sample->leds[led].greysum;
270 refinement->decode_gray.led_facing_dots[led_buffer_index] = sample->leds[led].facing_dot;
271 }
272
273 // We have enough info in theory
274 // @todo this should be an actual "num samples" check
275 if (refinement->leds_seen.cur_sequence_id > T_LED_SYNC_GRAY_CODE_SIZE_BITS) {
276 uint16_t decoded_values[T_LED_SYNC_SELECTED_LEDS] = {0};
277
278 for (int led = 0; led < T_LED_SYNC_SELECTED_LEDS; led++) {
279 float min_observed_brightness[XRT_TRACKING_MAX_CAMS];
280 float max_observed_brightness[XRT_TRACKING_MAX_CAMS] = {0};
281 for (uint32_t i = 0; i < ARRAY_SIZE(min_observed_brightness); i++) {
282 min_observed_brightness[i] = INFINITY;
283 }
284
285 float brightnesses[T_LED_SYNC_GRAY_CODE_SIZE_BITS] = {0};
286 int best_camera_indices[T_LED_SYNC_GRAY_CODE_SIZE_BITS];
287 for (size_t i = 0; i < ARRAY_SIZE(best_camera_indices); i++) {
288 best_camera_indices[i] = -1;
289 }
290
291 // Collect the brightnesses and the thresholds
292 for (int age = 0; age < T_LED_SYNC_GRAY_CODE_SIZE_BITS; age++) {
293 float best_facing_dot = 1.0f;
294 float best_brightness = 0.0f;
295
296 for (int camera_index = 0; camera_index < XRT_TRACKING_MAX_CAMS; camera_index++) {
297 // Pick which camera's data to use
298 size_t led_buffer_index = T_LED_SYNC_GRAY_CODE_BUFFER_INDEX(
299 camera_index, (refinement->leds_seen.cur_sequence_id - age), led);
300
301 float brightness = refinement->decode_gray.led_brightnesses[led_buffer_index];
302 float facing_dot = refinement->decode_gray.led_facing_dots[led_buffer_index];
303
304 if (brightness > 0.0f && facing_dot < 1) {
305 // Update the min/max observed brightnesses, but only for valid
306 // observations.
307 min_observed_brightness[camera_index] =
308 MIN(min_observed_brightness[camera_index], brightness);
309 max_observed_brightness[camera_index] =
310 MAX(max_observed_brightness[camera_index], brightness);
311 }
312
313 if (brightness > 0 && facing_dot < best_facing_dot) {
314 best_brightness = brightness;
315 best_facing_dot = facing_dot;
316 best_camera_indices[age] = camera_index;
317 }
318 }
319
320 brightnesses[age] = best_brightness;
321 }
322
323 // Decode the brightnesses
324 for (int age = 0; age < T_LED_SYNC_GRAY_CODE_SIZE_BITS; age++) {
325 if (age != 0) {
326 decoded_values[led] >>= 1;
327 }
328
329 float threshold = 0.5f;
330 if (best_camera_indices[age] != -1) {
331 threshold = (min_observed_brightness[best_camera_indices[age]] +
332 max_observed_brightness[best_camera_indices[age]]) /
333 2.0f;
334 }
335
336 bool bit = brightnesses[age] > threshold;
337
338 LOG_GRAY_SPEW(refinement, "brightness[%d][%d]: %f", led, age, brightnesses[age]);
339
340 decoded_values[led] |= (bit ? 1 : 0) << (T_LED_SYNC_GRAY_CODE_SIZE_BITS - 1);
341 }
342 }
343
344 if (decoded_values[0] == decoded_values[1]) {
345 uint16_t decoded_value = decoded_values[0];
346
347 // No bits outside the decoded value
348 assert((decoded_value & (~decoding_mask)) == 0);
349
350 uint16_t decoded_header = decoded_value & header_mask;
351 uint16_t decoded_gray_code = decoded_value & gray_code_mask;
352
353 if (decoded_header == expected_header) {
354 handle_decoded_gray_code(refinement, decoded_gray_code);
355 } else {
356 LOG_GRAY_SPEW(refinement, "Decoding gray code failed! Got header %x and data %x",
357 decoded_header, decoded_gray_code);
358 }
359 } else {
360 LOG_GRAY_SPEW(refinement, "Decoding gray code failed, the two LEDs didn't match, %d\t%d",
361 decoded_values[0], decoded_values[1]);
362 }
363 }
364}
365
366/*
367 *
368 * BG brightness curve decoding and clock sync.
369 *
370 */
371
372static struct t_led_sync_frame *
373bg_frame(struct t_led_sync_refinement *refinement, uint32_t camera_index, uint64_t sequence_id)
374{
375 return &refinement->optimize_brightness_curve.frames[camera_index][sequence_id % BG_NUM_FRAMES];
376}
377
378//! Whether @p frame was recorded for @p sequence_id and saw all the selected LEDs.
379static bool
380bg_frame_usable(const struct t_led_sync_frame *frame, uint64_t sequence_id)
381{
382 if (frame->sequence_id != sequence_id) {
383 return false;
384 }
385
386 for (int led = 0; led < T_LED_SYNC_SELECTED_LEDS; led++) {
387 if (frame->brightness[led] <= 0.0f) {
388 return false;
389 }
390 }
391
392 return true;
393}
394
395static void
396bg_store_frame(struct t_led_sync_refinement *refinement, const struct t_constellation_tracker_sample *sample)
397{
398 const uint8_t *leds = refinement->sample_for_driver.pssense.leds;
399
400 // Frames recorded for other LEDs can't be used, start the window over.
401 if (memcmp(refinement->optimize_brightness_curve.leds, leds, T_LED_SYNC_SELECTED_LEDS) != 0) {
402 memcpy(refinement->optimize_brightness_curve.leds, leds, T_LED_SYNC_SELECTED_LEDS);
403 refinement->optimize_brightness_curve.window_start_sequence_id = sample->sequence_id + 1;
404 }
405
406 assert(sample->camera_index < XRT_TRACKING_MAX_CAMS);
407 struct t_led_sync_frame *frame = bg_frame(refinement, sample->camera_index, sample->sequence_id);
408
409 frame->sequence_id = sample->sequence_id;
410 for (int i = 0; i < T_LED_SYNC_SELECTED_LEDS; i++) {
411 bool seen = leds[i] != 0xFF && sample->leds[leds[i]].observed;
412
413 // The total light, a partly exposed LED's saturated core mostly shrinks, which dividing by area hides.
414 frame->brightness[i] = seen ? sample->leds[leds[i]].greysum : 0.0f;
415 frame->facing_dot[i] = seen ? sample->leds[leds[i]].facing_dot : 1.0f;
416 }
417}
418
419/*!
420 * Reads the window ending at @p last_sequence_id, oldest first, from the camera facing the LEDs best. Only cameras
421 * that saw both LEDs in every frame are used, a missed LED would read as a dark frame.
422 */
423static bool
425 uint64_t last_sequence_id,
426 float out_brightness[T_LED_SYNC_SELECTED_LEDS][BG_NUM_FRAMES])
427{
428 if (last_sequence_id + 1 < refinement->optimize_brightness_curve.window_start_sequence_id + BG_NUM_FRAMES) {
429 return false;
430 }
431
432 uint64_t first_sequence_id = last_sequence_id + 1 - BG_NUM_FRAMES;
433
434 int best_camera = -1;
435 float best_facing_dot = INFINITY;
436
437 for (uint32_t camera = 0; camera < XRT_TRACKING_MAX_CAMS; camera++) {
438 bool complete = true;
439 float facing_dot = 0.0f;
440
441 for (uint64_t seq = first_sequence_id; seq <= last_sequence_id && complete; seq++) {
442 const struct t_led_sync_frame *frame = bg_frame(refinement, camera, seq);
443
444 complete = bg_frame_usable(frame, seq);
445 facing_dot += frame->facing_dot[0] + frame->facing_dot[1];
446 }
447
448 if (complete && facing_dot < best_facing_dot) {
449 best_camera = (int)camera;
450 best_facing_dot = facing_dot;
451 }
452 }
453
454 if (best_camera < 0) {
455 return false;
456 }
457
458 for (int i = 0; i < BG_NUM_FRAMES; i++) {
459 const struct t_led_sync_frame *frame = bg_frame(refinement, best_camera, first_sequence_id + i);
460
461 for (int led = 0; led < T_LED_SYNC_SELECTED_LEDS; led++) {
462 out_brightness[led][i] = frame->brightness[led];
463 }
464 }
465
466 return true;
467}
468
469//! Whether the BG marker (1001 at the start of the cycle, 101 at the end) is at @p cycle_start.
470static bool
471bg_marker_ok(const bool bits[BG_NUM_FRAMES], int cycle_start)
472{
473 static const int positions[] = {0, 1, 2, 3, 17, 18, 19};
474 static const bool expected[] = {true, false, false, true, true, false, true};
475
476 for (size_t i = 0; i < ARRAY_SIZE(positions); i++) {
477 if (bits[(cycle_start + positions[i]) % BG_NUM_FRAMES] != expected[i]) {
478 return false;
479 }
480 }
481
482 return true;
483}
484
485//! Finds where the cycle starts in the window, by both LEDs showing the marker there. -1 if they don't agree.
486static int
487bg_find_cycle_start(float brightness[T_LED_SYNC_SELECTED_LEDS][BG_NUM_FRAMES])
488{
489 bool bits[T_LED_SYNC_SELECTED_LEDS][BG_NUM_FRAMES];
490
491 for (int led = 0; led < T_LED_SYNC_SELECTED_LEDS; led++) {
492 float min = brightness[led][0];
493 float max = brightness[led][0];
494 for (int i = 1; i < BG_NUM_FRAMES; i++) {
495 min = MIN(min, brightness[led][i]);
496 max = MAX(max, brightness[led][i]);
497 }
498
499 // @todo Reject windows with too little contrast to threshold reliably.
500 float threshold = (min + max) / 2.0f;
501 for (int i = 0; i < BG_NUM_FRAMES; i++) {
502 bits[led][i] = brightness[led][i] > threshold;
503 }
504 }
505
506 for (int cycle_start = 0; cycle_start < BG_NUM_FRAMES; cycle_start++) {
507 bool all_ok = true;
508 for (int led = 0; led < T_LED_SYNC_SELECTED_LEDS; led++) {
509 all_ok = all_ok && bg_marker_ok(bits[led], cycle_start);
510 }
511
512 if (all_ok) {
513 return cycle_start;
514 }
515 }
516
517 return -1;
518}
519
520//! How far the dip of the curve is from @ref BG_TARGET_STEP, from the centroid of the frames below the midpoint.
521static bool
522bg_measure_error_steps(const float brightness[BG_NUM_FRAMES], int cycle_start, float *out_error_steps)
523{
524 const int first = 4;
525 const int last = 16;
526
527 float min = INFINITY;
528 float max = -INFINITY;
529 for (int k = first; k <= last; k++) {
530 float value = brightness[(cycle_start + k) % BG_NUM_FRAMES];
531 min = MIN(min, value);
532 max = MAX(max, value);
533 }
534 float mid = (min + max) / 2.0f;
535
536 float weighted_sum = 0.0f;
537 float total_weight = 0.0f;
538 for (int k = first; k <= last; k++) {
539 float weight = fmaxf(mid - brightness[(cycle_start + k) % BG_NUM_FRAMES], 0.0f);
540 weighted_sum += (float)k * weight;
541 total_weight += weight;
542 }
543
544 // A flat curve has no dip to measure.
545 if (total_weight <= 0.0f) {
546 return false;
547 }
548
549 *out_error_steps = BG_TARGET_STEP - weighted_sum / total_weight;
550 return true;
551}
552
553//! Corrects the clock model's drift from the LED cycle since the anchor, the LEDs follow the camera so they're right.
554static void
555delta_sync_reanchor(struct t_led_sync_refinement *refinement, bool reanchor_period)
556{
557 struct t_led_sync_delta_sync *delta = &refinement->delta_sync;
558
559 if (refinement->last_exposure_sequence_id > delta->anchor_sequence_id) {
560 // The LEDs advanced a base period per frame, the model advanced by the camera's timestamps.
561 uint64_t frames = refinement->last_exposure_sequence_id - delta->anchor_sequence_id;
562 double elapsed_ns = (double)(refinement->last_exposure_ns - delta->anchor_ns);
563 double drift_ns = elapsed_ns - (double)frames * delta->base_period_ns;
564
565 refinement->current_clock_offset_ns -= (time_duration_ns)drift_ns;
566
567 if (reanchor_period) {
568 delta->base_period_ns += drift_ns / (double)frames;
569 }
570
571 LOG_DEBUG(refinement, "Clock model drifted %.0fns from the LEDs over %" PRIu64 " frames", drift_ns,
572 frames);
573 }
574
576 delta->anchor_ns = refinement->last_exposure_ns;
577}
578
579//! Moves to a new skew, re-basing the clock model at @p now_ns so host->device conversion stays continuous.
580static void
582{
583 struct t_led_sync_delta_sync *delta = &refinement->delta_sync;
584
585 delta_sync_reanchor(refinement, false);
586
587 refinement->current_clock_offset_ns += (time_duration_ns)(delta->skew * (double)(now_ns - delta->epoch_ns));
588 delta->epoch_ns = now_ns;
589 delta->skew = skew;
590 delta->trim_pending = true;
591}
592
593//! Moves the base onto the latest trusted period estimate, the skew takes up the difference so the cycle is unchanged.
594static void
596{
597 struct t_led_sync_delta_sync *delta = &refinement->delta_sync;
598 double period_ns = refinement->period_estimate.period_ns;
599
600 if (period_ns <= 0.0 || period_ns == delta->base_period_ns) {
601 return;
602 }
603
604 delta_sync_set_skew(refinement, now_ns, delta->base_period_ns * (1.0 + delta->skew) / period_ns - 1.0);
605 delta->base_period_ns = period_ns;
606
607 LOG_DEBUG(refinement, "BG rebased onto %.1fns, skew %f ppm", period_ns, delta->skew * 1e6);
608}
609
610//! Fits the phase deviation against time over a window of measurements and adds the slope to the skew.
611static void
613{
614 struct t_led_sync_delta_sync *delta = &refinement->delta_sync;
615
616 if (delta->num_samples == 0) {
617 delta->window_start_ns = now_ns;
618 }
619
620 // The servo corrections plus the remaining error is the drift the cycle trim didn't cover.
621 double t = (double)(now_ns - delta->window_start_ns);
622 double d = (double)(delta->servo_integral_ns + error_ns);
623
624 delta->num_samples++;
625 delta->sum_t += t;
626 delta->sum_tt += t * t;
627 delta->sum_d += d;
628 delta->sum_td += t * d;
629
630 if (delta->num_samples < BG_SKEW_WINDOW_SAMPLES) {
631 return;
632 }
633
634 double n = delta->num_samples;
635 double denom = n * delta->sum_tt - delta->sum_t * delta->sum_t;
636 bool converged = false;
637 if (denom != 0.0) {
638 double slope = (n * delta->sum_td - delta->sum_t * delta->sum_d) / denom;
639 delta_sync_set_skew(refinement, now_ns, CLAMP(delta->skew + slope, -BG_MAX_SKEW, BG_MAX_SKEW));
640 converged = fabs(slope) < BG_STABLE_MAX_RATE;
641 }
642
643 // STABLE has nothing to correct the LEDs with, so the skew has to have stopped moving first.
644 delta->num_windows++;
645 uint32_t windows_needed = delta->been_stable ? 1 : 2;
646 if (!converged || delta->num_windows < windows_needed) {
647 delta->converged_since_ns = 0;
648 } else if (delta->converged_since_ns == 0) {
649 delta->converged_since_ns = now_ns;
650 }
651
652 delta->num_samples = 0;
653 delta->sum_t = delta->sum_tt = delta->sum_d = delta->sum_td = 0.0;
654}
655
656//! Smooths the LEDs' measured offset from the exposure, which the published clock model takes out.
657static void
659{
660 struct t_led_sync_delta_sync *delta = &refinement->delta_sync;
661
662 if (!delta->has_led_error) {
663 delta->led_error_ns = (double)error_ns;
664 delta->has_led_error = true;
665 return;
666 }
667
668 delta->led_error_ns += BG_LED_ERROR_SMOOTHING * ((double)error_ns - delta->led_error_ns);
669}
670
671//! Whether the clock model has moved far enough from what the driver has to be worth republishing.
672static bool
674{
675 const struct t_led_sync_sample *published = &refinement->sample_for_driver;
676 if (published->timestamp_mode != T_LED_SYNC_SAMPLE_TIMESTAMP_MODE_HOST_DEVICE_CLOCK_OFFSET) {
677 return false;
678 }
679
680 time_duration_ns diff_ns = model_clock_offset_ns(refinement, refinement->current_clock_offset_ns) -
681 published->timestamp.host_device_clock_offset_ns;
682
684}
685
686//! Sends a servo correction along with the current cycle trim, unless the driver hasn't taken the last sample yet.
687static void
689{
690 if (refinement->has_sample_for_driver) {
691 return;
692 }
693
694 // The LEDs move toward the exposure, the published model stays where it was.
695 refinement->delta_sync.servo_integral_ns += servo_ns;
696 refinement->delta_sync.led_error_ns -= (double)servo_ns;
697
698 bool sent = advance_clock_offset_pssense_locked(refinement, servo_ns);
699 assert(sent);
700 (void)sent;
701}
702
703static void
704handle_optimize_brightness_curve_sample(struct t_led_sync_refinement *refinement,
705 const struct t_constellation_tracker_sample *sample)
706{
707 bg_store_frame(refinement, sample);
708
709 float brightness[T_LED_SYNC_SELECTED_LEDS][BG_NUM_FRAMES];
710 if (!bg_read_window(refinement, sample->sequence_id, brightness)) {
711 return;
712 }
713
714 for (int i = 0; i < BG_NUM_FRAMES; i++) {
715 LOG_GRAY_SPEW(refinement, "BG [%d] %f %f", i, brightness[0][i], brightness[1][i]);
716 }
717
718 int cycle_start = bg_find_cycle_start(brightness);
719 if (cycle_start < 0) {
720 return;
721 }
722
723 // These frames are used up, the next measurement needs a fresh window.
724 refinement->optimize_brightness_curve.window_start_sequence_id = sample->sequence_id + 1;
725
726 float error_steps;
727 if (!bg_measure_error_steps(brightness[0], cycle_start, &error_steps)) {
728 return;
729 }
730
732 refinement->delta_sync.last_measurement_ns = now_ns;
733
734 time_duration_ns error_ns = (time_duration_ns)(BG_STEP_NS * error_steps);
735 bg_update_skew(refinement, now_ns, error_ns);
736 bg_update_led_error(refinement, error_ns);
737
738 LOG_GRAY_SPEW(refinement, "BG error %f steps, skew %f ppm", error_steps, refinement->delta_sync.skew * 1e6);
739
740 if (fabsf(error_steps) > BG_DEADBAND_STEPS) {
741 bg_send_correction(refinement, (time_duration_ns)(error_ns * BG_OFFSET_GAIN));
742 }
743}
744
745//! Keeps the clock model on the latest period estimate and republishes it, needs no measurements.
746static void
748{
749 // IMU timestamps need the skew against host time, which only a host-measured base gives.
750 delta_sync_rebase(refinement, now_ns);
751
752 // A new skew goes out as a cycle trim with no base-time change, a moved clock model the same way.
753 if (refinement->delta_sync.trim_pending || bg_model_stale(refinement)) {
754 bg_send_correction(refinement, 0);
755 }
756}
757
758static void
759handle_decode_gray_tick(struct t_led_sync_refinement *refinement)
760{
761 if ((os_monotonic_get_ns() - refinement->decode_gray.last_accepted_gray_code_ns) > GRAY_CODE_TIMEOUT_NS) {
762 LOG_DEBUG(refinement, "Waited %lf seconds without valid gray decode, resetting.",
764 set_phase_locked(refinement, T_LED_SYNC_PHASE_FIND_INITIAL_OFFSET);
765 return;
766 }
767
768 // If the final one hasn't been marked as applied, but it has been applied, send the new broad mode
769 if (!refinement->decode_gray.applied_gray_start && refinement->sample_applied) {
770 set_pssense_mode(refinement, T_LED_SYNC_SAMPLE_PSSENSE_MODE_BROAD, NULL);
771
772 refinement->decode_gray.applied_gray_start = true;
773 }
774
775 // Every 10 sequence events reanchor the period
776 if (refinement->last_exposure_sequence_id > refinement->delta_sync.anchor_sequence_id &&
777 refinement->last_exposure_sequence_id - refinement->delta_sync.anchor_sequence_id > 10) {
778 delta_sync_reanchor(refinement, true);
779 bool applied = advance_clock_offset_pssense_locked(refinement, 0);
780 (void)applied; // ignore errors, it's fine
781 }
782}
783
784static void
785handle_optimize_brightness_curve(struct t_led_sync_refinement *refinement)
786{
787 // Seen but never measurable means the lock is bad, waiting longer only lets the LEDs drift off.
789 time_duration_ns since_measurement_ns = now_ns - refinement->delta_sync.last_measurement_ns;
790 if (since_measurement_ns > BG_NO_MEASUREMENT_TIMEOUT_NS) {
791 LOG_DEBUG(refinement, "No BG measurement for %fms, resyncing", time_ns_to_ms_f(since_measurement_ns));
792 set_phase_locked(refinement, T_LED_SYNC_PHASE_FIND_INITIAL_OFFSET);
793 return;
794 }
795
796 // Converged for long enough, a pending sample has to go out before the phase can change.
797 time_duration_ns dwell_ns = refinement->delta_sync.been_stable ? BG_STABLE_DWELL_WARM_NS : BG_STABLE_DWELL_NS;
798 timepoint_ns converged_since_ns = refinement->delta_sync.converged_since_ns;
799 if (converged_since_ns != 0 && now_ns - converged_since_ns > dwell_ns && !refinement->has_sample_for_driver) {
800 set_phase_locked(refinement, T_LED_SYNC_PHASE_STABLE);
801 return;
802 }
803
804 delta_sync_maintain_clock(refinement, now_ns);
805}
806
807static void
808handle_stable(struct t_led_sync_refinement *refinement)
809{
811
812 // Nothing corrects the LEDs here, so BG checks on them every so often, once they're in view to measure.
813 bool seen = refinement->frames_since_last_visually_seen <= MAX_SAMPLE_LATENCY;
814 if (now_ns - refinement->delta_sync.stable_entry_ns > STABLE_RECHECK_NS && seen &&
815 !refinement->has_sample_for_driver) {
816 set_phase_locked(refinement, T_LED_SYNC_PHASE_OPTIMIZE_BRIGHTNESS_CURVE);
817 return;
818 }
819
820 delta_sync_maintain_clock(refinement, now_ns);
821}
#define CLAMP(X, A, B)
X clamped to the range [A, B].
Definition m_api.h:78
#define MAX(A, B)
Maximum of A and B.
Definition m_api.h:70
#define MIN(A, B)
Minimum of A and B.
Definition m_api.h:61
static int64_t os_monotonic_get_ns(void)
Return a monotonic clock in nanoseconds.
Definition os_time.h:317
int64_t timepoint_ns
Integer timestamp type.
Definition u_time.h:77
static double time_ns_to_ms_f(time_duration_ns ns)
Convert nanoseconds to double float milliseconds, useful for printing.
Definition u_time.h:121
static double time_ns_to_s(time_duration_ns ns)
Convert nanoseconds duration to double seconds.
Definition u_time.h:97
int64_t time_duration_ns
Integer duration type in nanoseconds.
Definition u_time.h:88
Internal routines for LED sync code.
static time_duration_ns model_clock_offset_ns(struct t_led_sync_refinement *refinement, time_duration_ns clock_offset_ns)
The host->device offset to publish, corrected by the LEDs' offset from the exposure so it follows the...
Definition led_sync_internal.h:63
#define MAX_SAMPLE_LATENCY
The amount of frames to not see it for consider a device "not found" during an active search.
Definition led_sync_internal.h:32
C interface to math library.
#define INVALID_GRAY_CODE
This gray code isn't in the official table.
Definition pssense_gray_code.h:41
#define BG_DEADBAND_STEPS
Errors within this are left to the cycle trim, wide enough that measurement noise doesn't trigger sen...
Definition pssense_gray_code.h:55
#define BG_MODEL_PUBLISH_THRESHOLD_NS
How far the clock model may move before it's republished, every publish resends the LED settings.
Definition pssense_gray_code.h:65
static void delta_sync_rebase(struct t_led_sync_refinement *refinement, timepoint_ns now_ns)
Moves the base onto the latest trusted period estimate, the skew takes up the difference so the cycle...
Definition pssense_gray_code.h:595
static void delta_sync_set_skew(struct t_led_sync_refinement *refinement, timepoint_ns now_ns, double skew)
Moves to a new skew, re-basing the clock model at now_ns so host->device conversion stays continuous.
Definition pssense_gray_code.h:581
static void bg_update_led_error(struct t_led_sync_refinement *refinement, time_duration_ns error_ns)
Smooths the LEDs' measured offset from the exposure, which the published clock model takes out.
Definition pssense_gray_code.h:658
static void bg_update_skew(struct t_led_sync_refinement *refinement, timepoint_ns now_ns, time_duration_ns error_ns)
Fits the phase deviation against time over a window of measurements and adds the slope to the skew.
Definition pssense_gray_code.h:612
static void bg_send_correction(struct t_led_sync_refinement *refinement, time_duration_ns servo_ns)
Sends a servo correction along with the current cycle trim, unless the driver hasn't taken the last s...
Definition pssense_gray_code.h:688
#define BG_STABLE_MAX_RATE
The fitted rate has to stay under this to enter STABLE, in ns per ns.
Definition pssense_gray_code.h:67
static int bg_find_cycle_start(float brightness[T_LED_SYNC_SELECTED_LEDS][T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES])
Finds where the cycle starts in the window, by both LEDs showing the marker there....
Definition pssense_gray_code.h:487
static void delta_sync_maintain_clock(struct t_led_sync_refinement *refinement, timepoint_ns now_ns)
Keeps the clock model on the latest period estimate and republishes it, needs no measurements.
Definition pssense_gray_code.h:747
#define BG_MAX_SKEW
Clamp on the estimated clock skew, in ns per ns.
Definition pssense_gray_code.h:59
static bool bg_read_window(struct t_led_sync_refinement *refinement, uint64_t last_sequence_id, float out_brightness[T_LED_SYNC_SELECTED_LEDS][T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES])
Reads the window ending at last_sequence_id, oldest first, from the camera facing the LEDs best.
Definition pssense_gray_code.h:424
#define BG_SKEW_WINDOW_SAMPLES
BG measurements per skew least-squares window.
Definition pssense_gray_code.h:57
#define GRAY_CODE_MATCHES_NEEDED
How many identical gray codes do we need to decode in a row to accept it.
Definition pssense_gray_code.h:43
#define BG_OFFSET_GAIN
Proportional gain for the BG clock-offset servo.
Definition pssense_gray_code.h:53
#define STABLE_RECHECK_NS
How long STABLE runs unmeasured before BG checks the LEDs again.
Definition pssense_gray_code.h:72
static bool bg_frame_usable(const struct t_led_sync_frame *frame, uint64_t sequence_id)
Whether frame was recorded for sequence_id and saw all the selected LEDs.
Definition pssense_gray_code.h:380
static bool bg_marker_ok(const bool bits[T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES], int cycle_start)
Whether the BG marker (1001 at the start of the cycle, 101 at the end) is at cycle_start.
Definition pssense_gray_code.h:471
#define BG_TARGET_STEP
The frame the dip of the BG curve should be centered on when in sync.
Definition pssense_gray_code.h:51
#define INVALID_GRAY_CODE_JUMP
The jump amount for an invalid code.
Definition pssense_gray_code.h:39
#define BG_NO_MEASUREMENT_TIMEOUT_NS
How long BG may go without an accepted measurement before the lock is given up.
Definition pssense_gray_code.h:61
static bool bg_measure_error_steps(const float brightness[T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES], int cycle_start, float *out_error_steps)
How far the dip of the curve is from BG_TARGET_STEP, from the centroid of the frames below the midpoi...
Definition pssense_gray_code.h:522
#define BG_LED_ERROR_SMOOTHING
Smoothing gain on the LEDs' measured offset from the exposure, trading measurement noise against lag.
Definition pssense_gray_code.h:63
#define BG_STEP_NS
Each frame of the BG curve is shifted this much further than the last.
Definition pssense_gray_code.h:49
static const time_duration_ns gray_code_jumps_ns[]
A table of how much time to jump for each gray code.
Definition pssense_gray_code.h:77
static void delta_sync_reanchor(struct t_led_sync_refinement *refinement, bool reanchor_period)
Corrects the clock model's drift from the LED cycle since the anchor, the LEDs follow the camera so t...
Definition pssense_gray_code.h:555
#define BG_STABLE_DWELL_NS
How long the rate has to stay converged before STABLE, first time and coming back from it.
Definition pssense_gray_code.h:69
#define GRAY_CODE_TIMEOUT_NS
How long to wait without an accepted gray code before resetting back to phase 1.
Definition pssense_gray_code.h:45
static bool bg_model_stale(struct t_led_sync_refinement *refinement)
Whether the clock model has moved far enough from what the driver has to be worth republishing.
Definition pssense_gray_code.h:673
Definition u_pacing_compositor.c:59
Definition t_constellation.h:378
Clock sync from BROAD on: the LED cycle, the clock model kept on it, and BG's offset servo and skew f...
Definition t_led_sync_refinement.h:210
uint32_t num_windows
Skew windows fitted since BG was entered.
Definition t_led_sync_refinement.h:233
timepoint_ns converged_since_ns
When the fitted rate last started staying under the STABLE gate, 0 if it isn't.
Definition t_led_sync_refinement.h:235
bool trim_pending
The skew changed and the new cycle still has to be sent.
Definition t_led_sync_refinement.h:228
timepoint_ns epoch_ns
Host time the published offset is valid at, the skew extrapolates from here.
Definition t_led_sync_refinement.h:216
uint32_t num_samples
Least-squares window of the phase deviation against time.
Definition t_led_sync_refinement.h:242
uint64_t anchor_sequence_id
The camera frame the clock model last matched the LED cycle at, its drift is measured from here.
Definition t_led_sync_refinement.h:212
double base_period_ns
The frame period the LED cycle is trimmed from, set when BROAD starts and moved onto each trusted est...
Definition t_led_sync_refinement.h:226
double skew
Estimated clock skew, device vs host, in ns per ns.
Definition t_led_sync_refinement.h:218
double led_error_ns
Smoothed offset of the LEDs from the exposure, as the servo correction that would center them.
Definition t_led_sync_refinement.h:222
bool been_stable
Whether this sync has reached STABLE, BG comes back from it warm.
Definition t_led_sync_refinement.h:237
timepoint_ns stable_entry_ns
Host time STABLE was entered.
Definition t_led_sync_refinement.h:239
timepoint_ns last_measurement_ns
Host time of the last accepted measurement, for the no-data watchdog.
Definition t_led_sync_refinement.h:230
time_duration_ns servo_integral_ns
Sum of all servo corrections this BG session.
Definition t_led_sync_refinement.h:220
One camera's view of the selected LEDs in one frame, brightness is the blob's greysum,...
Definition t_led_sync_refinement.h:191
double period_ns
The last trusted estimate, 0 until there is one.
Definition t_led_sync_refinement.h:205
Definition t_led_sync_refinement.h:248
uint64_t last_exposure_sequence_id
The latest timing event's frame and exposure time.
Definition t_led_sync_refinement.h:274
timepoint_ns last_selection_ns
The time when we last selected the set of LEDs.
Definition t_led_sync_refinement.h:337
uint32_t frames_since_last_visually_seen
The amount of frames since the controller was last visually seen.
Definition t_led_sync_refinement.h:310
struct t_led_sync_seen_led buf[XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE *5]
The seen LEDs ring buffer.
Definition t_led_sync_refinement.h:346
timepoint_ns last_accepted_gray_code_ns
The last time we had an accepted gray code (or the start of this phase).
Definition t_led_sync_refinement.h:378
bool has_sample_for_driver
Whether we have a sample ready to be sent to the driver/device.
Definition t_led_sync_refinement.h:282
bool sample_applied
Whether the sample has been applied.
Definition t_led_sync_refinement.h:286
uint64_t window_start_sequence_id
The first frame the next measurement may use.
Definition t_led_sync_refinement.h:386
struct t_led_sync_sample sample_for_driver
The sample to be sent to the driver/device on it's next convenience.
Definition t_led_sync_refinement.h:284
uint8_t leds[2]
The LEDs frames were recorded for.
Definition t_led_sync_refinement.h:384
struct t_led_sync_frame frames[XRT_TRACKING_MAX_CAMS][20]
Ring buffer of the last T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES frames per camera.
Definition t_led_sync_refinement.h:388
float led_brightnesses[11 *XRT_TRACKING_MAX_CAMS *2]
A ring buffer storing the last T_LED_SYNC_GRAY_CODE_SIZE_BITS bits of the brightness sequence per cam...
Definition t_led_sync_refinement.h:362
bool applied_gray_start
Whether or not we have applied the final cycle position or not.
Definition t_led_sync_refinement.h:353
time_duration_ns current_clock_offset_ns
The current estimated latency offset between the device and the host, in nanoseconds.
Definition t_led_sync_refinement.h:299
uint32_t cur_sequence_id
The index into the seen LEDs ring buffer.
Definition t_led_sync_refinement.h:334
float led_facing_dots[11 *XRT_TRACKING_MAX_CAMS *2]
A ring buffer storing the last T_LED_SYNC_GRAY_CODE_SIZE_BITS bits of the sequence,...
Definition t_led_sync_refinement.h:371
uint8_t decoded_gray_code
The gray code we're trying.
Definition t_led_sync_refinement.h:376
uint32_t matching_decodes
How many decodes we've had that match in a row, see GRAY_CODE_MATCHES_NEEDED.
Definition t_led_sync_refinement.h:374
uint8_t leds[4]
The LEDs to enable, or 0xff if we don't have a selection.
Definition t_led_sync_refinement.h:125
A sample read out from the driver, to pass to the device in question.
Definition t_led_sync_refinement.h:130
time_duration_ns host_device_clock_offset_ns
The clock offset from the host timestamp to the device.
Definition t_led_sync_refinement.h:136
Definition t_led_sync_refinement.h:182
float accum_facing_dot
How "facing" the camera it was.
Definition t_led_sync_refinement.h:186
uint8_t accum_seen_count
How many times it's been seen.
Definition t_led_sync_refinement.h:184
#define XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE
The maximum number of LEDs that a single device can have.
Definition t_constellation.h:37
A routine to automatically refine latency offsets of LED blink times using constellation samples.
@ T_LED_SYNC_SAMPLE_TIMESTAMP_MODE_HOST_DEVICE_CLOCK_OFFSET
Used when the LED sync refinement is fully deriving the clock offset, latency included.
Definition t_led_sync_refinement.h:99
@ T_LED_SYNC_SAMPLE_PSSENSE_MODE_BROAD
"BROAD", blink certain LEDs in an 11-frame Gray code to refine where we are in the offset faster than...
Definition t_led_sync_refinement.h:110
@ T_LED_SYNC_SAMPLE_PSSENSE_MODE_PRESCAN
"PRESCAN", used to scan for an initial offset to then refine.
Definition t_led_sync_refinement.h:105
#define T_LED_SYNC_SEEN_LEDS_ACCUMULATION_BUFFER_SIZE
How many elements are in the seen LEDs ring buffer.
Definition t_led_sync_refinement.h:28
@ T_LED_SYNC_PHASE_OPTIMIZE_BRIGHTNESS_CURVE
Trying to optimize the brightness curve for the correct time offset.
Definition t_led_sync_refinement.h:172
@ T_LED_SYNC_PHASE_FIND_INITIAL_OFFSET
Trying to find some offset that gets us in sync at all.
Definition t_led_sync_refinement.h:164
@ T_LED_SYNC_PHASE_STABLE
PS Sense only, BG has converged so the LEDs hold sync on the trimmed cycle until BG checks them again...
Definition t_led_sync_refinement.h:178
#define T_LED_SYNC_GRAY_CODE_SIZE_BITS
The amount of bits in the full gray code we're decoding.
Definition t_led_sync_refinement.h:30
Time-keeping: a clock that is steady, convertible to system time, and ideally high-resolution.
#define U_TIME_1US_IN_NS
The number of nanoseconds in a microsecond.
Definition u_time.h:61
#define ARRAY_SIZE(a)
Array size helper.
Definition xrt_compiler.h:59