23#define T_LED_SYNC_DEFAULT_RESYNC_TIME (time_duration_ns)(U_TIME_1S_IN_NS * 5LL)
25#define T_LED_SYNC_SELECTED_LEDS 2
28#define T_LED_SYNC_SEEN_LEDS_ACCUMULATION_BUFFER_SIZE 5
30#define T_LED_SYNC_GRAY_CODE_SIZE_BITS 11
32#define T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES 20
36#define T_LED_SYNC_SEEN_LED_BUFFER_INDEX(sequence_id) ((sequence_id) % T_LED_SYNC_SEEN_LEDS_ACCUMULATION_BUFFER_SIZE)
37#define T_LED_SYNC_GRAY_CODE_BUFFER_INDEX(camera_index, sequence_id, led_idx) \
38 (((((camera_index) * T_LED_SYNC_GRAY_CODE_SIZE_BITS) + ((sequence_id) % T_LED_SYNC_GRAY_CODE_SIZE_BITS)) * \
39 T_LED_SYNC_SELECTED_LEDS) + \
46 T_LED_SYNC_REFINEMENT_FLAGS_NONE = 0,
95 T_LED_SYNC_SAMPLE_TIMESTAMP_MODE_INVALID = 0,
192 uint64_t sequence_id;
193 float brightness[T_LED_SYNC_SELECTED_LEDS];
194 float facing_dot[T_LED_SYNC_SELECTED_LEDS];
200 uint64_t anchor_sequence_id;
244 double sum_t, sum_tt, sum_d, sum_td;
329 } binary_search_state;
339 uint32_t num_samples;
362 T_LED_SYNC_SELECTED_LEDS];
371 T_LED_SYNC_SELECTED_LEDS];
384 uint8_t
leds[T_LED_SYNC_SELECTED_LEDS];
389 } optimize_brightness_curve;
400 } blink_time_refinement_state;
407 bool has_saved_offset;
409 } optical_driven_offset;
u_logging_level
Logging level enum.
Definition u_logging.h:45
int64_t timepoint_ns
Integer timestamp type.
Definition u_time.h:77
int64_t time_duration_ns
Integer duration type in nanoseconds.
Definition u_time.h:88
Wrapper around OS threading native functions.
A wrapper around a native mutex.
Definition os_threading.h:69
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
Frame period measured over a long run of frames, so it's steady enough to base the LED cycle on.
Definition t_led_sync_refinement.h:199
double period_ns
The last trusted estimate, 0 until there is one.
Definition t_led_sync_refinement.h:205
double candidate_ns
The previous run's estimate, the next one has to agree with it to be trusted.
Definition t_led_sync_refinement.h:203
Options for the LED sync refinement.
Definition t_led_sync_refinement.h:72
time_duration_ns time_to_resync_ns
Time to wait after the last visually seen sample before resyncing.
Definition t_led_sync_refinement.h:85
time_duration_ns min_blink_duration_ns
The minimum duration to use for the LED blinks, if using reduction.
Definition t_led_sync_refinement.h:80
uint32_t settle_frames
Frames to wait after the sample was applied to let the device settle.
Definition t_led_sync_refinement.h:87
time_duration_ns max_blink_duration_ns
The maximum duration to use for the LED blinks, if using reduction.
Definition t_led_sync_refinement.h:82
enum t_led_sync_refinement_flags flags
The flags to use when refining.
Definition t_led_sync_refinement.h:74
time_duration_ns initial_blink_duration_ns
How long the LEDs should be initially blinking for, in nanoseconds.
Definition t_led_sync_refinement.h:77
time_duration_ns latency_cap_ns
The maximum latency offset to use, if using a latency cap.
Definition t_led_sync_refinement.h:90
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
time_duration_ns found_left_edge_ns
The found left edge of the exposure, -1 if not found yet.
Definition t_led_sync_refinement.h:319
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
time_duration_ns found_right_edge_ns
The found right edge of the exposure, -1 if not found yet.
Definition t_led_sync_refinement.h:321
time_duration_ns right_bound_ns
The current right bound of the binary search, if we're in a find edge phase.
Definition t_led_sync_refinement.h:328
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
time_duration_ns left_bound_ns
The current left bound of the binary search, if we're in a find edge phase.
Definition t_led_sync_refinement.h:326
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
float led_facing_dots[11 *XRT_TRACKING_MAX_FRAME_SOURCES *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
bool backing_off
Whether we're currently trying to back off a lower blink duration.
Definition t_led_sync_refinement.h:399
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
float led_brightnesses[11 *XRT_TRACKING_MAX_FRAME_SOURCES *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
uint64_t current_sequence_id
The sequence ID of the latest timing event we processed.
Definition t_led_sync_refinement.h:313
uint8_t leds[2]
The LEDs frames were recorded for.
Definition t_led_sync_refinement.h:384
time_duration_ns last_good_blink_duration_ns
The last blink duration that didn't cause the device to become unstable.
Definition t_led_sync_refinement.h:396
time_duration_ns exposure_time_ns
The known time the frame was exposed for.
Definition t_led_sync_refinement.h:269
time_duration_ns current_blink_duration_ns
How long the LEDs blink for, each frame.
Definition t_led_sync_refinement.h:307
enum t_led_sync_phase phase
The current search phase.
Definition t_led_sync_refinement.h:293
struct t_led_sync_frame frames[XRT_TRACKING_MAX_FRAME_SOURCES][20]
Ring buffer of the last T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES frames per camera.
Definition t_led_sync_refinement.h:388
bool initialized
Whether the structure has been fully initialized.
Definition t_led_sync_refinement.h:250
time_duration_ns current_blink_fudge_ns
The amount of fudge between the latency offset and the actual blink start time, in nanoseconds,...
Definition t_led_sync_refinement.h:304
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 driver_host_device_clock_offset_ns
The latest estimated host->device clock offset from the driver.
Definition t_led_sync_refinement.h:405
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
timepoint_ns last_sample_apply_time_ns
The time the latest sample was applied to the driver/device.
Definition t_led_sync_refinement.h:316
bool has_exposure_time
Whether we know the actual exposure time of the frames.
Definition t_led_sync_refinement.h:267
time_duration_ns exposure_interval_ns
The known interval between frames.
Definition t_led_sync_refinement.h:271
uint32_t cur_sequence_id
The index into the seen LEDs ring buffer.
Definition t_led_sync_refinement.h:334
uint8_t decoded_gray_code
The gray code we're trying.
Definition t_led_sync_refinement.h:376
struct t_led_sync_refinement_options options
The options to use for refinement.
Definition t_led_sync_refinement.h:255
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
Definition t_led_sync_refinement.h:121
time_duration_ns base_time_offset_ns
The base time offset to send to the controller (in nanoseconds).
Definition t_led_sync_refinement.h:123
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
double cycle_period_ns
The LED cycle period in device ns, already trimmed for the skew. 0 lets the driver use its own estima...
Definition t_led_sync_refinement.h:148
time_duration_ns fudge_offset_ns
The latency offset to apply to fudge the blink to line up as best as possible with the exposure.
Definition t_led_sync_refinement.h:151
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
double clock_skew
Clock skew (device vs host, in ns per ns) and its host-time epoch.
Definition t_led_sync_refinement.h:144
time_duration_ns device_host_latency_ns
The latency offset from the device timestamps to the host.
Definition t_led_sync_refinement.h:133
time_duration_ns blink_duration_ns
The duration to make the LEDs blink for, in nanoseconds.
Definition t_led_sync_refinement.h:153
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
Marks beginning of a camera exposure, pushed from the source to the sink.
Definition t_time_sync.h:29
Header defining the tracking system integration in Monado.
#define XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE
The maximum number of LEDs that a single device can have.
Definition t_constellation.h:37
void t_led_sync_push_host_device_clock_offset(struct t_led_sync_refinement *refinement, time_duration_ns clock_offset_ns)
Pushes a clock offset to the refinement routine, this is the offset from the host to the device.
Definition t_led_sync_refinement.c:680
t_led_sync_sample_timestamp_mode
Definition t_led_sync_refinement.h:94
@ 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_TIMESTAMP_MODE_DEVICE_HOST_LATENCY
Used when the LED sync refinement is just computing a basic latency offset.
Definition t_led_sync_refinement.h:97
t_led_sync_sample_pssense_mode
Definition t_led_sync_refinement.h:103
@ 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
@ T_LED_SYNC_SAMPLE_PSSENSE_MODE_BG
"BG", blink certain LEDs with a brightness curve over a set of frames, then fit a curve to that measu...
Definition t_led_sync_refinement.h:115
@ T_LED_SYNC_SAMPLE_PSSENSE_MODE_STABLE
"STABLE", all LEDs blink narrowly on the trimmed cycle, holding sync with nothing measured.
Definition t_led_sync_refinement.h:117
#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_refinement_flags
Flags to control the behavior of the LED sync refinement routine.
Definition t_led_sync_refinement.h:45
@ T_LED_SYNC_REFINEMENT_FLAGS_PS_SENSE
Whether the device we're syncing is specifically a PS VR2 controller ("pssense"), and so has access t...
Definition t_led_sync_refinement.h:67
@ T_LED_SYNC_REFINEMENT_FLAGS_BLINK_DURATION
Whether to try to optimize the blink duration after finding an offset, to lessen power usage.
Definition t_led_sync_refinement.h:48
@ T_LED_SYNC_REFINEMENT_FLAGS_OPTICAL_DRIVEN_OFFSET
Whether to lock in a clock offset and have it be driven entirely by the sync refinement,...
Definition t_led_sync_refinement.h:57
@ T_LED_SYNC_REFINEMENT_FLAGS_HAS_LATENCY_CAP
Whether the device has specified a maximum latency cap.
Definition t_led_sync_refinement.h:62
t_led_sync_phase
Definition t_led_sync_refinement.h:160
@ T_LED_SYNC_PHASE_MAINTAIN_OFFSET
We've found an offset and optimized the center of the LED blink to the center of the exposure.
Definition t_led_sync_refinement.h:176
@ T_LED_SYNC_PHASE_REFINE_BLINK_DURATION
We've found an offset, now we can optimize the blink duration to something that keeps it tracking.
Definition t_led_sync_refinement.h:174
@ 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_FIND_LEFT_EDGE
Trying to align the falling edge of the LED blinks with the rising edge of the camera exposure.
Definition t_led_sync_refinement.h:168
@ 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
@ T_LED_SYNC_PHASE_DECODE_GRAY
Trying to decode the Gray codes present in the LEDs we've selected.
Definition t_led_sync_refinement.h:170
@ T_LED_SYNC_PHASE_INIT
The initial phase, where we haven't made any adjustments yet.
Definition t_led_sync_refinement.h:162
@ T_LED_SYNC_PHASE_FIND_RIGHT_EDGE
Trying to align the rising edge of the LED blinks with the falling edge of the camera exposure.
Definition t_led_sync_refinement.h:166
#define T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES
20 frames per sample
Definition t_led_sync_refinement.h:32
#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
Header defining interfaces for time synchronization in Monado.
Basic logging functionality.
Time-keeping: a clock that is steady, convertible to system time, and ideally high-resolution.