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