Monado OpenXR Runtime
Loading...
Searching...
No Matches
pssense_gray_code.h File Reference

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>
Include dependency graph for pssense_gray_code.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.
 

Detailed Description

Routines to help decoding PS VR2 controller Gray codes + brightness curves.

Author
Beyley Cardellio ep1cm.nosp@m.1n10.nosp@m.n123@.nosp@m.gmai.nosp@m.l.com

Macro Definition Documentation

◆ BG_DEADBAND_STEPS

#define BG_DEADBAND_STEPS   0.25f

Errors within this are left to the cycle trim, wide enough that measurement noise doesn't trigger sends.

◆ BG_LED_ERROR_SMOOTHING

#define BG_LED_ERROR_SMOOTHING   0.2

Smoothing gain on the LEDs' measured offset from the exposure, trading measurement noise against lag.

◆ BG_MAX_SKEW

#define BG_MAX_SKEW   1e-3

Clamp on the estimated clock skew, in ns per ns.

◆ BG_MODEL_PUBLISH_THRESHOLD_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.

◆ BG_NO_MEASUREMENT_TIMEOUT_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.

◆ BG_OFFSET_GAIN

#define BG_OFFSET_GAIN   0.5f

Proportional gain for the BG clock-offset servo.

◆ BG_SKEW_WINDOW_SAMPLES

#define BG_SKEW_WINDOW_SAMPLES   20

BG measurements per skew least-squares window.

◆ BG_STABLE_DWELL_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.

◆ BG_STABLE_MAX_RATE

#define BG_STABLE_MAX_RATE   2e-6

The fitted rate has to stay under this to enter STABLE, in ns per ns.

◆ BG_STEP_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.

◆ BG_TARGET_STEP

#define BG_TARGET_STEP   10.0f

The frame the dip of the BG curve should be centered on when in sync.

◆ GRAY_CODE_MATCHES_NEEDED

#define GRAY_CODE_MATCHES_NEEDED   3

How many identical gray codes do we need to decode in a row to accept it.

◆ GRAY_CODE_TIMEOUT_NS

#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.

◆ GRAY_SPEW

#define GRAY_SPEW   0

Whether to enable the "spew" for brightness-decoding related routines.

Note
This should never be upstreamed set to 1!

◆ INVALID_GRAY_CODE

#define INVALID_GRAY_CODE   (0x4)

This gray code isn't in the official table.

◆ INVALID_GRAY_CODE_JUMP

#define INVALID_GRAY_CODE_JUMP   INT64_MAX

The jump amount for an invalid code.

◆ STABLE_NOT_SEEN_TIMEOUT_NS

#define STABLE_NOT_SEEN_TIMEOUT_NS   (U_TIME_1S_IN_NS * 15LL)

How long STABLE can go without seeing the controller before starting over.

◆ STABLE_RECHECK_NS

#define STABLE_RECHECK_NS   (U_TIME_1S_IN_NS * 100LL)

How long STABLE runs unmeasured before BG checks the LEDs again.

Function Documentation

◆ bg_find_cycle_start()

static int bg_find_cycle_start ( float  brightness[T_LED_SYNC_SELECTED_LEDS][T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES])
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.

◆ bg_frame_usable()

static bool bg_frame_usable ( const struct t_led_sync_frame *  frame,
uint64_t  sequence_id 
)
static

Whether frame was recorded for sequence_id and saw all the selected LEDs.

Referenced by bg_read_window().

◆ bg_marker_ok()

static bool bg_marker_ok ( const bool  bits[T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES],
int  cycle_start 
)
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().

◆ bg_measure_error_steps()

static bool bg_measure_error_steps ( const float  brightness[T_LED_SYNC_BRIGHTNESS_CURVE_NUM_SAMPLES],
int  cycle_start,
float *  out_error_steps 
)
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.

◆ bg_model_stale()

static bool bg_model_stale ( struct t_led_sync_refinement *  refinement)
static

◆ bg_read_window()

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] 
)
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.

◆ bg_send_correction()

static void bg_send_correction ( struct t_led_sync_refinement *  refinement,
time_duration_ns  servo_ns 
)
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().

◆ bg_update_led_error()

static void bg_update_led_error ( struct t_led_sync_refinement *  refinement,
time_duration_ns  error_ns 
)
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.

◆ bg_update_skew()

static void bg_update_skew ( struct t_led_sync_refinement *  refinement,
timepoint_ns  now_ns,
time_duration_ns  error_ns 
)
static

◆ delta_sync_maintain_clock()

static void delta_sync_maintain_clock ( struct t_led_sync_refinement *  refinement,
timepoint_ns  now_ns 
)
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.

◆ delta_sync_reanchor()

static void delta_sync_reanchor ( struct t_led_sync_refinement *  refinement,
bool  reanchor_period 
)
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().

◆ delta_sync_rebase()

static void delta_sync_rebase ( struct t_led_sync_refinement *  refinement,
timepoint_ns  now_ns 
)
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().

◆ delta_sync_set_skew()

static void delta_sync_set_skew ( struct t_led_sync_refinement *  refinement,
timepoint_ns  now_ns,
double  skew 
)
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().

Variable Documentation

◆ gray_code_jumps_ns

const time_duration_ns gray_code_jumps_ns[]
static
Initial value:
= {
0,
INT64_MAX ,
}
#define U_TIME_1US_IN_NS
The number of nanoseconds in a microsecond.
Definition u_time.h:61

A table of how much time to jump for each gray code.