Monado OpenXR Runtime
psvr2.h
Go to the documentation of this file.
1// Copyright 2020-2021, Collabora, Ltd.
2// Copyright 2023, Jan Schmidt
3// Copyright 2024, Joel Valenciano
4// Copyright 2025, Beyley Cardellio
5// SPDX-License-Identifier: BSL-1.0
6/*!
7 * @file
8 * @brief PSVR2 HMD device
9 *
10 * @author Jan Schmidt <jan@centricular.com>
11 * @author Jakob Bornecrantz <jakob@collabora.com>
12 * @author Ryan Pavlik <ryan.pavlik@collabora.com>
13 * @author Joel Valenciano <joelv1907@gmail.com>
14 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
15 * @ingroup drv_psvr2
16 */
17#pragma once
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#include <xrt/xrt_defines.h>
24#include <xrt/xrt_byte_order.h>
25#include "xrt/xrt_device.h"
26#include "xrt/xrt_prober.h"
27#include "xrt/xrt_tracking.h"
28
29#include "os/os_threading.h"
30#include "os/os_time.h"
31
32#include "math/m_api.h"
34#include "math/m_mathinclude.h"
37#include "math/m_filter_fifo.h"
38
39#include "util/u_misc.h"
40#include "util/u_debug.h"
41#include "util/u_device.h"
43#include "util/u_frame.h"
44#include "util/u_logging.h"
45#include "util/u_sink.h"
46#include "util/u_time.h"
47#include "util/u_trace_marker.h"
48#include "util/u_var.h"
49#include "util/u_debug.h"
50
51#include "psvr2_protocol.h"
52
53#include <stdbool.h>
54#include <stdint.h>
55#include <stdio.h>
56#include <assert.h>
57#include <libusb.h>
58
59
60#define NUM_CAM_XFERS 1
61
62#define PSVR2_TRACE(p, ...) U_LOG_XDEV_IFL_T(&p->base, p->log_level, __VA_ARGS__)
63#define PSVR2_TRACE_HEX(p, data, data_size) U_LOG_XDEV_IFL_T_HEX(&p->base, p->log_level, data, data_size)
64#define PSVR2_DEBUG(p, ...) U_LOG_XDEV_IFL_D(&p->base, p->log_level, __VA_ARGS__)
65#define PSVR2_DEBUG_HEX(p, data, data_size) U_LOG_XDEV_IFL_D_HEX(&p->base, p->log_level, data, data_size)
66#define PSVR2_WARN(p, ...) U_LOG_XDEV_IFL_W(&p->base, p->log_level, __VA_ARGS__)
67#define PSVR2_ERROR(p, ...) U_LOG_XDEV_IFL_E(&p->base, p->log_level, __VA_ARGS__)
68
69#define TIMESTAMP_SAMPLES 100
70
71/*!
72 * PSVR2 HMD device
73 *
74 * @implements xrt_device
75 */
77{
78 struct xrt_device base;
79
80 struct xrt_pose pose;
81
82 enum u_logging_level log_level;
83
84 struct os_mutex data_lock;
85
86 /* Device status */
87 uint8_t dprx_status; //< DisplayPort receiver status
88 xrt_atomic_s32_t proximity_sensor; //< Atomic state for whether the proximity sensor is triggered
89 bool function_button; //< Boolean state for whether the function button is pressed
90
91 bool ipd_updated; //< Whether the IPD has been updated, and an HMD info refresh is needed
92 uint8_t ipd_mm; //< IPD dial value in mm, from 59 to 72mm
93
94 bool camera_enable; //< Whether the camera is enabled
95 enum psvr2_camera_mode camera_mode; //< The current camera mode
96 struct u_var_button camera_enable_btn;
97 struct u_var_button camera_mode_btn;
98
99 struct u_var_button brightness_btn;
100 float brightness;
101
102 /* IMU input data */
103 uint32_t last_imu_vts_us; //< Last VTS timestamp, in microseconds
104 uint16_t last_imu_ts; //< Last IMU timestamp, in microseconds
105 struct xrt_vec3 last_gyro; //< Last gyro reading, in rad/s
106 struct xrt_vec3 last_accel; //< Last accel reading, in m/s²
107
108 /* SLAM input data */
109 uint32_t last_slam_vts_us; //< Last slam timestamp, in microseconds
110 struct xrt_pose last_slam_pose; //< Last SLAM pose reading
111
112 struct xrt_pose slam_correction_pose;
113 struct u_var_button slam_correction_set_btn;
114 struct u_var_button slam_correction_reset_btn;
115
116 struct xrt_pose T_imu_head; //< Constant transform from SLAM tracker pose to head pose
117
118 /* Display parameters */
119 struct u_device_simple_info info;
120
121 /* Camera debug sinks */
122 struct u_sink_debug debug_sinks[4];
123
124 /* USB communication */
125 libusb_context *ctx;
126 libusb_device_handle *dev;
127
128 struct os_thread_helper usb_thread;
129 int usb_complete;
130 int usb_active_xfers;
131
132 /* Status report */
133 struct libusb_transfer *status_xfer;
134 /* SLAM (bulk) transfer */
135 struct libusb_transfer *slam_xfer;
136 /* Camera (bulk) transfers */
137 struct libusb_transfer *camera_xfers[NUM_CAM_XFERS];
138 /* LD EP9 (bulk) transfer */
139 struct libusb_transfer *led_detector_xfer;
140 /* RP EP10 (bulk) transfer */
141 struct libusb_transfer *relocalizer_xfer;
142 /* VD EP11 (bulk) transfer */
143 struct libusb_transfer *vd_xfer;
144 /* Gaze transfer */
145 struct libusb_transfer *gaze_xfer;
146
147 /* Distortion calibration parameters, to be used with
148 * psvr2_compute_distortion_asymmetric. Very specific to
149 * PS VR2. */
150 float distortion_calibration[8];
151
152 /* Timing data */
153 int timestamp_samples;
154
155 timepoint_ns last_imu_vts_ns;
156 timepoint_ns last_slam_vts_ns;
157 timepoint_ns system_zero_ns;
158 timepoint_ns last_imu_ns;
159
160 time_duration_ns hw2mono_vts;
161 time_duration_ns hw2mono_imu;
162
163 /* Tracking state */
164 struct m_relation_history *slam_relation_history;
165 struct m_ff_vec3_f32 *ff_gyro;
166};
167
168/// Casting helper function
169static inline struct psvr2_hmd *
171{
172 return (struct psvr2_hmd *)xdev;
173}
174
175enum psvr2_hmd_input_name
176{
177 PSVR2_HMD_INPUT_HEAD_POSE,
178 PSVR2_HMD_INPUT_FUNCTION_BUTTON,
179 PSVR2_HMD_INPUT_COUNT,
180};
181
182void
183psvr2_compute_distortion_asymmetric(
184 float *calibration, struct xrt_uv_triplet *distCoords, int eEye, float fU, float fV);
185
186bool
187psvr2_usb_xfer_continue(struct libusb_transfer *xfer, const char *type);
188
189bool
190send_psvr2_control(struct psvr2_hmd *hmd, uint16_t report_id, uint8_t subcmd, uint8_t *pkt_data, uint32_t pkt_len);
191
192#ifdef __cplusplus
193}
194#endif
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
C interface to math library.
Helpers to estimate offsets between clocks.
A fifo that also lets you dynamically filter.
Header for a "One Euro Filter" implementation.
Wrapper header for <math.h> to ensure pi-related math constants are defined.
Small utility for keeping track of the history of an xrt_space_relation, ie.
Wrapper around OS threading native functions.
Wrapper around OS native time functions.
static struct psvr2_hmd * psvr2_hmd(struct xrt_device *xdev)
Casting helper function.
Definition: psvr2.h:170
PSVR2 HMD protocol defines.
Definition: m_filter_fifo.c:23
Definition: m_relation_history.cpp:49
A wrapper around a native mutex.
Definition: os_threading.h:69
All in one helper that handles locking, waiting for change and starting a thread.
Definition: os_threading.h:499
PSVR2 HMD device.
Definition: psvr2.h:77
Info to describe a very simple headset with diffractive lens optics.
Definition: u_device.h:55
Allows more safely to debug sink inputs and outputs.
Definition: u_sink.h:211
Simple pushable button.
Definition: u_var.h:80
A single HMD or input device.
Definition: xrt_device.h:310
struct xrt_hmd_parts * hmd
Null if this device does not interface with the users head.
Definition: xrt_device.h:322
A pose composed of a position and orientation.
Definition: xrt_defines.h:479
Represents a uv triplet for distortion, basically just three xrt_vec2.
Definition: xrt_defines.h:279
A 3 element vector with single floats.
Definition: xrt_defines.h:289
Small debug helpers.
Misc helpers for device drivers.
Code to generate disortion meshes.
xrt_frame helpers.
Basic logging functionality.
Very small misc utils.
xrt_frame_sink converters and other helpers.
Time-keeping: a clock that is steady, convertible to system time, and ideally high-resolution.
Tracing support code, see Tracing support.
Variable tracking code.
Endian-specific byte order defines.
Common defines and enums for XRT.
Header defining an xrt display or controller device.
Common interface to probe for devices.
Header defining the tracking system integration in Monado.