Monado OpenXR Runtime
Loading...
Searching...
No Matches
t_led_sync_refinement.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 A routine to automatically refine latency offsets of LED blink times using constellation samples.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup tracking
8 */
9
10#pragma once
11
12#include "util/u_time.h"
13#include "util/u_logging.h"
14
15#include "os/os_threading.h"
16
19
20
21//! 10 seconds after last visually seen sample to resync
22#define T_LED_SYNC_DEFAULT_RESYNC_TIME (time_duration_ns)(U_TIME_1S_IN_NS * 5LL)
23
24//! Flags to control the behavior of the LED sync refinement routine.
26{
27 T_LED_SYNC_REFINEMENT_FLAGS_NONE = 0,
28 //! Whether to try to optimize the blink duration after finding an offset, to lessen power usage.
30};
31
32//! Options for the LED sync refinement.
34{
35 //! The flags to use when refining.
37
38 //! How long the LEDs should be initially blinking for, in nanoseconds.
40
41 //! The minimum duration to use for the LED blinks, if using reduction.
43 //! The maximum duration to use for the LED blinks, if using reduction.
45
46 //! Time to wait after the last visually seen sample before resyncing.
48 //! Frames to wait after the sample was applied to let the device settle.
49 uint32_t settle_frames;
50};
51
52//! A sample read out from the driver, to pass to the device in question.
54{
55 //! The latency offset from the device timestamps to the host.
57
58 //! The latency offset to apply to fudge the blink to line up as best as possible with the exposure.
60 //! The duration to make the LEDs blink for, in nanoseconds.
62};
63
65{
66 //! The initial phase, where we haven't made any adjustments yet.
68 //! Trying to find some offset that gets us in sync at all.
70 //! Trying to align the rising edge of the LED blinks with the falling edge of the camera exposure.
72 //! Trying to align the falling edge of the LED blinks with the rising edge of the camera exposure.
74 //! We've found an offset, now we can optimize the blink duration to something that keeps it tracking.
76 //! We've found an offset and optimized the center of the LED blink to the center of the exposure.
78};
79
81{
82 //! Whether the structure has been fully initialized.
84
85 enum u_logging_level log_level;
86
87 //! The options to use for refinement
89
90 struct os_mutex lock;
91
92 /*!
93 * Whether we know the actual exposure time of the frames. We may not, so we have to work without it in some
94 * cases.
95 */
97 //! The known time the frame was exposed for.
99
100 //! The known interval between frames.
102
103 //! Whether we have a sample ready to be sent to the driver/device.
105 //! The sample to be sent to the driver/device on it's next convenience.
107 //! Whether the sample has been applied
109
110 //! The current search phase
112
113 /*!
114 * The current estimated latency offset between the device and the host, in nanoseconds. This is what we are
115 * trying to refine.
116 */
118 /*!
119 * The amount of fudge between the latency offset and the actual blink start time, in nanoseconds, so we can
120 * place the blink at the optimal time for the exposure.
121 */
123
124 //! How long the LEDs blink for, each frame.
126
127 //! The amount of frames since the controller was last visually seen.
129
130 //! The sequence ID of the latest timing event we processed.
132
133 //! The time the latest sample was applied to the driver/device
135
136 //! The found left edge of the exposure, -1 if not found yet.
138 //! The found right edge of the exposure, -1 if not found yet.
140
141 struct
142 {
143 //! The current left bound of the binary search, if we're in a find edge phase.
145 //! The current right bound of the binary search, if we're in a find edge phase.
147 } binary_search_state;
148
149 struct
150 {
151 //! The last blink duration that didn't cause the device to become unstable.
153
154 //! Whether we're currently trying to back off a lower blink duration.
156 } blink_time_refinement_state;
157};
158
159int
160t_led_sync_refinement_init(struct t_led_sync_refinement *refinement,
161 const struct t_led_sync_refinement_options *options);
162
163void
164t_led_sync_refinement_destroy(struct t_led_sync_refinement *refinement);
165
166void
167t_led_sync_push_timing_event(struct t_led_sync_refinement *refinement,
168 const struct t_timing_event_camera_exposure_start *event);
169
170void
171t_led_sync_push_constellation_sample(struct t_led_sync_refinement *refinement,
172 const struct t_constellation_tracker_sample *sample);
173
174bool
175t_led_sync_get_sample(struct t_led_sync_refinement *refinement, struct t_led_sync_sample *out_sample);
176
177void
178t_led_sync_mark_latest_sample_applied(struct t_led_sync_refinement *refinement, timepoint_ns apply_time_ns);
179
180void
181t_led_sync_update_minimum_blink_time(struct t_led_sync_refinement *refinement, time_duration_ns new_minimum_ns);
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:306
Options for the LED sync refinement.
Definition t_led_sync_refinement.h:34
time_duration_ns time_to_resync_ns
Time to wait after the last visually seen sample before resyncing.
Definition t_led_sync_refinement.h:47
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:42
uint32_t settle_frames
Frames to wait after the sample was applied to let the device settle.
Definition t_led_sync_refinement.h:49
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:44
enum t_led_sync_refinement_flags flags
The flags to use when refining.
Definition t_led_sync_refinement.h:36
time_duration_ns initial_blink_duration_ns
How long the LEDs should be initially blinking for, in nanoseconds.
Definition t_led_sync_refinement.h:39
Definition t_led_sync_refinement.h:81
uint32_t frames_since_last_visually_seen
The amount of frames since the controller was last visually seen.
Definition t_led_sync_refinement.h:128
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:137
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:139
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:146
bool has_sample_for_driver
Whether we have a sample ready to be sent to the driver/device.
Definition t_led_sync_refinement.h:104
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:144
time_duration_ns current_latency_offset_ns
The current estimated latency offset between the device and the host, in nanoseconds.
Definition t_led_sync_refinement.h:117
uint32_t current_sequence_id
The sequence ID of the latest timing event we processed.
Definition t_led_sync_refinement.h:131
bool sample_applied
Whether the sample has been applied.
Definition t_led_sync_refinement.h:108
bool backing_off
Whether we're currently trying to back off a lower blink duration.
Definition t_led_sync_refinement.h:155
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:106
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:152
time_duration_ns exposure_time_ns
The known time the frame was exposed for.
Definition t_led_sync_refinement.h:98
time_duration_ns current_blink_duration_ns
How long the LEDs blink for, each frame.
Definition t_led_sync_refinement.h:125
enum t_led_sync_phase phase
The current search phase.
Definition t_led_sync_refinement.h:111
bool initialized
Whether the structure has been fully initialized.
Definition t_led_sync_refinement.h:83
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:122
timepoint_ns last_sample_apply_time_ns
The time the latest sample was applied to the driver/device.
Definition t_led_sync_refinement.h:134
bool has_exposure_time
Whether we know the actual exposure time of the frames.
Definition t_led_sync_refinement.h:96
time_duration_ns exposure_interval_ns
The known interval between frames.
Definition t_led_sync_refinement.h:101
struct t_led_sync_refinement_options options
The options to use for refinement.
Definition t_led_sync_refinement.h:88
A sample read out from the driver, to pass to the device in question.
Definition t_led_sync_refinement.h:54
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:59
time_duration_ns device_host_latency_ns
The latency offset from the device timestamps to the host.
Definition t_led_sync_refinement.h:56
time_duration_ns blink_duration_ns
The duration to make the LEDs blink for, in nanoseconds.
Definition t_led_sync_refinement.h:61
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.
t_led_sync_refinement_flags
Flags to control the behavior of the LED sync refinement routine.
Definition t_led_sync_refinement.h:26
@ 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:29
t_led_sync_phase
Definition t_led_sync_refinement.h:65
@ T_LED_SYNC_SEARCH_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:75
@ T_LED_SYNC_SEARCH_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:73
@ T_LED_SYNC_SEARCH_PHASE_INIT
The initial phase, where we haven't made any adjustments yet.
Definition t_led_sync_refinement.h:67
@ T_LED_SYNC_SEARCH_PHASE_FIND_INITIAL_OFFSET
Trying to find some offset that gets us in sync at all.
Definition t_led_sync_refinement.h:69
@ T_LED_SYNC_SEARCH_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:71
@ T_LED_SYNC_SEARCH_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:77
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.