|
Monado OpenXR Runtime
|
Routines to help decoding PS VR2 controller Gray codes + brightness curves. More...
#include "constellation/t_led_sync_refinement.h"#include "util/u_time.h"#include "math/m_api.h"#include "led_sync_internal.h"#include <float.h>
Go to the source code of this file.
Macros | |
| #define | GRAY_SPEW 0 |
| Whether to enable the "spew" for brightness-decoding related routines. | |
| #define | LOG_GRAY_SPEW(refinement, ...) |
| #define | TIME_BETWEEN_LED_SELECTIONS_NS (U_TIME_1MS_IN_NS * 2500LL) |
| #define | INVALID_GRAY_CODE_JUMP INT64_MAX |
| The jump amount for an invalid code. | |
| #define | INVALID_GRAY_CODE (0x4) |
| This gray code isn't in the official table. | |
| #define | GRAY_CODE_MATCHES_NEEDED 3 |
| How many identical gray codes do we need to decode in a row to accept it. | |
| #define | GRAY_CODE_TIMEOUT_NS (U_TIME_1S_IN_NS * 10LL) |
| How long to wait without an accepted gray code before resetting back to phase 1. | |
| #define | BG_NUM_FRAMES T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES |
| #define | BG_STEP_NS (U_TIME_1US_IN_NS * 50) |
| Each frame of the BG curve is shifted this much further than the last. | |
| #define | BG_TARGET_STEP 10.0f |
| The frame the dip of the BG curve should be centered on when in sync. | |
| #define | BG_OFFSET_GAIN 0.5f |
| Proportional gain for the BG clock-offset servo. | |
| #define | BG_DEADBAND_STEPS 0.25f |
| Errors within this are left to the cycle trim, wide enough that measurement noise doesn't trigger sends. | |
| #define | BG_SKEW_WINDOW_SAMPLES 20 |
| BG measurements per skew least-squares window. | |
| #define | BG_MAX_SKEW 1e-3 |
| Clamp on the estimated clock skew, in ns per ns. | |
| #define | BG_NO_MEASUREMENT_TIMEOUT_NS (U_TIME_1S_IN_NS * 13LL) |
| How long BG may go without an accepted measurement before the lock is given up. | |
| #define | BG_LED_ERROR_SMOOTHING 0.2 |
| Smoothing gain on the LEDs' measured offset from the exposure, trading measurement noise against lag. | |
| #define | BG_MODEL_PUBLISH_THRESHOLD_NS (U_TIME_1US_IN_NS * 3) |
| How far the clock model may move before it's republished, every publish resends the LED settings. | |
| #define | BG_STABLE_MAX_RATE 2e-6 |
| The fitted rate has to stay under this to enter STABLE, in ns per ns. | |
| #define | BG_STABLE_DWELL_NS (U_TIME_1S_IN_NS * 15LL) |
| How long the rate has to stay converged before STABLE, first time and coming back from it. | |
| #define | BG_STABLE_DWELL_WARM_NS (U_TIME_1S_IN_NS * 3LL) |
| #define | STABLE_RECHECK_NS (U_TIME_1S_IN_NS * 100LL) |
| How long STABLE runs unmeasured before BG checks the LEDs again. | |
| #define | STABLE_NOT_SEEN_TIMEOUT_NS (U_TIME_1S_IN_NS * 15LL) |
| How long STABLE can go without seeing the controller before starting over. | |
Functions | |
| static void | handle_successful_gray_decode (struct t_led_sync_refinement *refinement, uint16_t gray_code) |
| static void | handle_decoded_gray_code (struct t_led_sync_refinement *refinement, uint16_t decoded_gray_code) |
| static void | handle_leds_seen (struct t_led_sync_refinement *refinement, const struct t_constellation_tracker_sample *sample) |
| static void | handle_decode_gray_constellation_sample (struct t_led_sync_refinement *refinement, const struct t_constellation_tracker_sample *sample) |
| static struct t_led_sync_frame * | bg_frame (struct t_led_sync_refinement *refinement, uint32_t camera_index, uint64_t sequence_id) |
| 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. | |
| static void | bg_store_frame (struct t_led_sync_refinement *refinement, const struct t_constellation_tracker_sample *sample) |
| 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. | |
| 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. | |
| 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. -1 if they don't agree. | |
| 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 midpoint. | |
| 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 they're right. | |
| 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. | |
| 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 is unchanged. | |
| 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. | |
| 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. | |
| 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. | |
| 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 sample yet. | |
| static void | handle_optimize_brightness_curve_sample (struct t_led_sync_refinement *refinement, const struct t_constellation_tracker_sample *sample) |
| 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. | |
| static void | handle_decode_gray_tick (struct t_led_sync_refinement *refinement) |
| static void | handle_optimize_brightness_curve (struct t_led_sync_refinement *refinement) |
| static void | handle_stable (struct t_led_sync_refinement *refinement) |
Variables | |
| static const time_duration_ns | gray_code_jumps_ns [] |
| A table of how much time to jump for each gray code. | |
Routines to help decoding PS VR2 controller Gray codes + brightness curves.
| #define BG_DEADBAND_STEPS 0.25f |
Errors within this are left to the cycle trim, wide enough that measurement noise doesn't trigger sends.
| #define BG_LED_ERROR_SMOOTHING 0.2 |
Smoothing gain on the LEDs' measured offset from the exposure, trading measurement noise against lag.
| #define BG_MAX_SKEW 1e-3 |
Clamp on the estimated clock skew, in ns per ns.
| #define BG_MODEL_PUBLISH_THRESHOLD_NS (U_TIME_1US_IN_NS * 3) |
How far the clock model may move before it's republished, every publish resends the LED settings.
| #define BG_NO_MEASUREMENT_TIMEOUT_NS (U_TIME_1S_IN_NS * 13LL) |
How long BG may go without an accepted measurement before the lock is given up.
| #define BG_OFFSET_GAIN 0.5f |
Proportional gain for the BG clock-offset servo.
| #define BG_SKEW_WINDOW_SAMPLES 20 |
BG measurements per skew least-squares window.
| #define BG_STABLE_DWELL_NS (U_TIME_1S_IN_NS * 15LL) |
How long the rate has to stay converged before STABLE, first time and coming back from it.
| #define BG_STABLE_MAX_RATE 2e-6 |
The fitted rate has to stay under this to enter STABLE, in ns per ns.
| #define BG_STEP_NS (U_TIME_1US_IN_NS * 50) |
Each frame of the BG curve is shifted this much further than the last.
| #define BG_TARGET_STEP 10.0f |
The frame the dip of the BG curve should be centered on when in sync.
| #define GRAY_CODE_MATCHES_NEEDED 3 |
How many identical gray codes do we need to decode in a row to accept it.
| #define GRAY_CODE_TIMEOUT_NS (U_TIME_1S_IN_NS * 10LL) |
How long to wait without an accepted gray code before resetting back to phase 1.
| #define GRAY_SPEW 0 |
Whether to enable the "spew" for brightness-decoding related routines.
1! | #define INVALID_GRAY_CODE (0x4) |
This gray code isn't in the official table.
| #define INVALID_GRAY_CODE_JUMP INT64_MAX |
The jump amount for an invalid code.
| #define STABLE_NOT_SEEN_TIMEOUT_NS (U_TIME_1S_IN_NS * 15LL) |
How long STABLE can go without seeing the controller before starting over.
| #define STABLE_RECHECK_NS (U_TIME_1S_IN_NS * 100LL) |
How long STABLE runs unmeasured before BG checks the LEDs again.
|
static |
Finds where the cycle starts in the window, by both LEDs showing the marker there. -1 if they don't agree.
References bg_marker_ok(), MAX, and MIN.
|
static |
Whether frame was recorded for sequence_id and saw all the selected LEDs.
Referenced by bg_read_window().
|
static |
Whether the BG marker (1001 at the start of the cycle, 101 at the end) is at cycle_start.
References ARRAY_SIZE.
Referenced by bg_find_cycle_start().
|
static |
How far the dip of the curve is from BG_TARGET_STEP, from the centroid of the frames below the midpoint.
References BG_TARGET_STEP, MAX, and MIN.
|
static |
Whether the clock model has moved far enough from what the driver has to be worth republishing.
References BG_MODEL_PUBLISH_THRESHOLD_NS, t_led_sync_refinement::current_clock_offset_ns, t_led_sync_sample::host_device_clock_offset_ns, model_clock_offset_ns(), t_led_sync_refinement::sample_for_driver, and T_LED_SYNC_SAMPLE_TIMESTAMP_MODE_HOST_DEVICE_CLOCK_OFFSET.
Referenced by delta_sync_maintain_clock().
|
static |
Reads the window ending at last_sequence_id, oldest first, from the camera facing the LEDs best.
Only cameras that saw both LEDs in every frame are used, a missed LED would read as a dark frame.
References bg_frame_usable(), and t_led_sync_refinement::window_start_sequence_id.
|
static |
Sends a servo correction along with the current cycle trim, unless the driver hasn't taken the last sample yet.
References t_led_sync_refinement::has_sample_for_driver, t_led_sync_delta_sync::led_error_ns, and t_led_sync_delta_sync::servo_integral_ns.
Referenced by delta_sync_maintain_clock().
|
static |
Smooths the LEDs' measured offset from the exposure, which the published clock model takes out.
References BG_LED_ERROR_SMOOTHING, and t_led_sync_delta_sync::led_error_ns.
|
static |
Fits the phase deviation against time over a window of measurements and adds the slope to the skew.
References t_led_sync_delta_sync::been_stable, BG_MAX_SKEW, BG_SKEW_WINDOW_SAMPLES, BG_STABLE_MAX_RATE, CLAMP, t_led_sync_delta_sync::converged_since_ns, delta_sync_set_skew(), t_led_sync_delta_sync::num_samples, t_led_sync_delta_sync::num_windows, t_led_sync_delta_sync::servo_integral_ns, and t_led_sync_delta_sync::skew.
|
static |
Keeps the clock model on the latest period estimate and republishes it, needs no measurements.
References bg_model_stale(), bg_send_correction(), delta_sync_rebase(), and t_led_sync_delta_sync::trim_pending.
|
static |
Corrects the clock model's drift from the LED cycle since the anchor, the LEDs follow the camera so they're right.
References t_led_sync_delta_sync::anchor_sequence_id, t_led_sync_delta_sync::base_period_ns, t_led_sync_refinement::current_clock_offset_ns, and t_led_sync_refinement::last_exposure_sequence_id.
Referenced by delta_sync_set_skew().
|
static |
Moves the base onto the latest trusted period estimate, the skew takes up the difference so the cycle is unchanged.
References t_led_sync_delta_sync::base_period_ns, delta_sync_set_skew(), t_led_sync_period_estimate::period_ns, and t_led_sync_delta_sync::skew.
Referenced by delta_sync_maintain_clock().
|
static |
Moves to a new skew, re-basing the clock model at now_ns so host->device conversion stays continuous.
References t_led_sync_refinement::current_clock_offset_ns, delta_sync_reanchor(), t_led_sync_delta_sync::epoch_ns, t_led_sync_delta_sync::skew, and t_led_sync_delta_sync::trim_pending.
Referenced by bg_update_skew(), and delta_sync_rebase().
|
static |
A table of how much time to jump for each gray code.