Monado OpenXR Runtime
Loading...
Searching...
No Matches
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 Rylie Pavlik <rylie.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
72{
73 uint32_t vts_us;
74 int16_t accel[3];
75 int16_t gyro[3];
76 uint16_t dp_frame_cnt;
77 uint16_t dp_line_cnt;
78 uint16_t imu_ts_us;
79 uint16_t status;
80};
81
83{
84 bool gaze_point_valid;
85 // gaze point in meters
86 struct xrt_vec3 gaze_point;
87
88 bool gaze_direction_valid;
89 // gaze direction (normalized vector)
90 struct xrt_vec3 gaze_direction;
91
92 struct m_filter_euro_vec3 gaze_direction_filter;
93 struct xrt_vec3 filtered_gaze_direction;
94
95 // whether the pupil diameter is valid
96 bool pupil_diameter_valid;
97 // pupil diameter in meters
98 float pupil_diameter;
99
100 bool unk_float_2_valid;
101 struct xrt_vec2 unk_float_2;
102
103 bool unk_float_4_valid;
104 struct xrt_vec2 unk_float_4;
105
106 // whether the blink state is valid
107 bool blink_valid;
108 // whether the user is blinking
109 bool blink;
110
111 float blink_interp;
112};
113
115{
116 bool gaze_point_valid;
117 // gaze point in meters
118 struct xrt_vec3 gaze_point;
119
120 bool gaze_direction_valid;
121 // gaze direction (normalized vector)
122 struct xrt_vec3 gaze_direction;
123
124 struct m_filter_euro_vec3 gaze_direction_filter;
125 struct xrt_vec3 filtered_gaze_direction;
126
127 bool is_valid;
128
129 bool unk_float_8_valid;
130 float unk_float_8;
131
132 bool unk_float3_pair_valid;
133 struct xrt_vec3 unk_float_12;
134 struct xrt_vec3 unk_float_15;
135 struct xrt_vec3 unk_float_18;
136};
137
139{
140 struct os_thread_helper eye_tracking_thread;
141
142 //! Whether eye tracking is currently enabled
144 bool force_enable;
145
146 //! Whether the eye tracking enable command has been sent
148
149 struct m_relation_history *gaze_relation_history;
150
151 struct os_mutex data_mutex;
152 bool data_mutex_created;
153
154 struct psvr2_et_eye_data eyes[2];
155 struct psvr2_et_combined_data combined;
156
157 bool processed_sample_packet;
158
159 uint32_t last_remote_report_sample_time_us;
160 timepoint_ns last_remote_report_sample_time_ns;
161
162 bool unk_float_4_valid;
163 float unk_float_4;
164
165 bool unk_float_5_valid;
166 float unk_float_5;
167};
168
169/*!
170 * PSVR2 HMD device
171 *
172 * @implements xrt_device
173 */
175{
176 struct xrt_device base;
177
178 struct xrt_pose pose;
179
180 enum u_logging_level log_level;
181
182 struct os_mutex data_lock;
183
184 /* Device status */
185 uint8_t dprx_status; //< DisplayPort receiver status
186 xrt_atomic_s32_t proximity_sensor; //< Atomic state for whether the proximity sensor is triggered
187 bool function_button; //< Boolean state for whether the function button is pressed
188
189 bool ipd_updated; //< Whether the IPD has been updated, and an HMD info refresh is needed
190 uint8_t ipd_mm; //< IPD dial value in mm, from 59 to 72mm
191
192 bool camera_enable; //< Whether the camera is enabled
193 enum psvr2_camera_mode camera_mode; //< The current camera mode
194 struct u_var_button camera_enable_btn;
195 struct u_var_button camera_mode_btn;
196
197 struct u_var_button brightness_btn;
198 float brightness;
199
200 /* IMU input data */
201 uint32_t last_imu_vts_us; //< Last VTS timestamp, in microseconds
202 uint16_t last_imu_ts; //< Last IMU timestamp, in microseconds
203 struct xrt_vec3 last_gyro; //< Last gyro reading, in rad/s
204 struct xrt_vec3 last_accel; //< Last accel reading, in m/s²
205
206 /* SLAM input data */
207 uint32_t last_slam_vts_us; //< Last slam timestamp, in microseconds
208 struct xrt_pose last_slam_pose; //< Last SLAM pose reading
209
210 struct xrt_pose slam_correction_pose;
211 struct u_var_button slam_correction_set_btn;
212 struct u_var_button slam_correction_reset_btn;
213
214 struct xrt_pose T_imu_head; //< Constant transform from SLAM tracker pose to head pose
215
216 /* Display parameters */
217 struct u_device_simple_info info;
218
219 /* Camera debug sinks */
220 struct u_sink_debug debug_sinks[4];
221
222 /* USB communication */
223 libusb_context *ctx;
224 libusb_device_handle *dev;
225
226 struct os_thread_helper usb_thread;
227 int usb_complete;
228 int usb_active_xfers;
229
230 /* Status report */
231 struct libusb_transfer *status_xfer;
232 /* SLAM (bulk) transfer */
233 struct libusb_transfer *slam_xfer;
234 /* Camera (bulk) transfers */
235 struct libusb_transfer *camera_xfers[NUM_CAM_XFERS];
236 /* LD EP9 (bulk) transfer */
237 struct libusb_transfer *led_detector_xfer;
238 /* RP EP10 (bulk) transfer */
239 struct libusb_transfer *relocalizer_xfer;
240 /* VD EP11 (bulk) transfer */
241 struct libusb_transfer *vd_xfer;
242 /* Gaze transfer */
243 struct libusb_transfer *gaze_xfer;
244
245 /* Distortion calibration parameters, to be used with
246 * psvr2_compute_distortion_asymmetric. Very specific to
247 * PS VR2. */
248 float distortion_calibration[8];
249
250 /* Timing data */
251 int timestamp_samples;
252
253 timepoint_ns last_imu_vts_ns;
254 timepoint_ns last_slam_vts_ns;
255 timepoint_ns system_zero_ns;
256 timepoint_ns last_imu_ns;
257
258 time_duration_ns hw2mono_vts;
259 time_duration_ns hw2mono_imu;
260
261 /* Tracking state */
262 struct m_relation_history *slam_relation_history;
263 struct m_ff_vec3_f32 *ff_gyro;
264
265 /* Eye State */
266 bool eye_feature_enabled;
267 bool face_feature_enabled;
268
269 struct psvr2_et_data et_data;
270};
271
272/// Casting helper function
273static inline struct psvr2_hmd *
275{
276 return (struct psvr2_hmd *)xdev;
277}
278
279enum psvr2_hmd_input_name
280{
281 PSVR2_HMD_INPUT_HEAD_POSE,
282 PSVR2_HMD_INPUT_FUNCTION_BUTTON,
283 PSVR2_HMD_INPUT_EYE_GAZE_POSE,
284 PSVR2_HMD_INPUT_FB_FACE_TRACKING2_VISUAL,
285 PSVR2_HMD_INPUT_HTC_EYE_FACE_TRACKING,
286 PSVR2_HMD_INPUT_ANDROID_FACE_TRACKING,
287 PSVR2_HMD_INPUT_COUNT,
288};
289
290void
291psvr2_compute_distortion_asymmetric(
292 float *calibration, struct xrt_uv_triplet *distCoords, int eEye, float fU, float fV);
293
294bool
295psvr2_usb_xfer_continue(struct libusb_transfer *xfer, const char *type);
296
297bool
298send_psvr2_control(struct psvr2_hmd *hmd, uint16_t report_id, uint8_t subcmd, uint8_t *pkt_data, uint32_t pkt_len);
299
300void
301psvr2_free_et_data(struct psvr2_hmd *hmd);
302
303int
304psvr2_start_gaze_tracking(struct psvr2_hmd *hmd);
305
307psvr2_get_face_tracking(struct xrt_device *xdev,
308 enum xrt_input_name facial_expression_type,
309 int64_t at_timestamp_ns,
310 struct xrt_facial_expression_set *out_value);
311
312#ifdef __cplusplus
313}
314#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
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition xrt_defines.h:915
enum xrt_result xrt_result_t
Result type used across Monado.
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.
PSVR2 HMD protocol defines.
Definition psvr2.h:72
Definition m_filter_fifo.c:23
One Euro filter for a 3D float measurement.
Definition m_filter_one_euro.h:104
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
Definition psvr2.h:115
Definition psvr2.h:139
bool want_enabled
Whether eye tracking is currently enabled.
Definition psvr2.h:143
bool enabled
Whether the eye tracking enable command has been sent.
Definition psvr2.h:147
Definition psvr2.h:83
PSVR2 HMD device.
Definition psvr2.h:175
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
Definition xrt_defines.h:1934
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 2 element vector with single floats.
Definition xrt_defines.h:268
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.