The open source OpenXR runtime
1/*
2 * Copyright 2013, Fredrik Hultin.
3 * Copyright 2013, Jakob Bornecrantz.
4 * Copyright 2016 Philipp Zabel
5 * Copyright 2019-2022 Jan Schmidt
6 * SPDX-License-Identifier: BSL-1.0
7 */
8
9/*!
10 * @file
11 * @brief HMD tracker handling
12 * @author Jan Schmidt <jan@centricular.com>
13 * @ingroup drv_rift_s
14 */
15
16#pragma once
17
18#include "math/m_imu_3dof.h"
19#include "os/os_threading.h"
20#include "util/u_var.h"
21#include "xrt/xrt_defines.h"
22#include "xrt/xrt_device.h"
23
24#include "tracking/t_tracking.h"
25
26#include "rift_s_firmware.h"
27
28/* Oculus Rift S HMD Tracking */
29#ifndef RIFT_S_TRACKER_H
30#define RIFT_S_TRACKER_H
31
32struct rift_s_hmd_config;
33
34enum rift_s_tracker_pose
35{
36 RIFT_S_TRACKER_POSE_IMU,
37 RIFT_S_TRACKER_POSE_LEFT_CAMERA,
38 RIFT_S_TRACKER_POSE_DEVICE,
39};
40
41struct rift_s_tracker
42{
43 struct xrt_device base;
44
45 //! Protects shared access to 3dof and pose storage
46 struct os_mutex mutex;
47
48 //! Don't process IMU / video until started
49 bool ready_for_data;
50
51 struct
52 {
53 //! Main fusion calculator.
54 struct m_imu_3dof i3dof;
55
56 //! The last angular velocity from the IMU, for prediction.
57 struct xrt_vec3 last_angular_velocity;
58
59 //! When did we get the last IMU sample, device clock
60 uint64_t last_imu_timestamp_ns;
61
62 //! Last IMU sample local system clock
63 timepoint_ns last_imu_local_timestamp_ns;
64 } fusion;
65
66 //! Fields related to camera-based tracking (SLAM and hand tracking)
67 struct
68 {
69 //! SLAM tracker.
70 //! @todo Right now, we are not consistent in how we interface with
71 //! trackers. In particular, we have a @ref xrt_tracked_slam field but not
72 //! an equivalent for hand tracking.
73 struct xrt_tracked_slam *slam;
74
75 //! Set at start. Whether the SLAM tracker was initialized.
76 bool slam_enabled;
77
78 //! Set at start. Whether the hand tracker was initialized.
79 bool hand_enabled;
80 } tracking;
81
82 // Correction offset poses from firmware
83 struct xrt_pose device_from_imu;
84 struct xrt_pose left_cam_from_imu;
85
86 //!< Estimated offset from HMD device timestamp to local monotonic clock
87 uint64_t seen_clock_observations;
88 bool have_hw2mono;
89 time_duration_ns hw2mono;
90 timepoint_ns last_frame_time;
91
92 //! Adjustment to apply to camera timestamps to bring them into the
93 // same 32-bit range as the IMU times
94 int64_t camera_ts_offset;
95
96 //! Whether to track the HMD with 6dof SLAM or fallback to the `fusion` 3dof tracker
97 bool slam_over_3dof;
98
99 //! Last tracked pose
100 struct xrt_pose pose;
101
102 /* Stereo calibration for the front 2 cameras */
103 struct t_stereo_camera_calibration *stereo_calib;
104 struct t_slam_calibration slam_calib;
105
106 /* Input sinks that the camera delivers SLAM frames to */
107 struct xrt_slam_sinks in_slam_sinks;
108
109 /* SLAM/HT sinks we deliver imu and frame data to */
110 struct xrt_slam_sinks slam_sinks;
111
112 struct xrt_device *handtracker;
113
114 struct
115 {
116 struct u_var_button hmd_screen_enable_btn;
117 struct u_var_button switch_tracker_btn;
118 char hand_status[128];
119 char slam_status[128];
120 } gui;
121};
122
123struct rift_s_tracker *
124rift_s_tracker_create(struct xrt_tracking_origin *origin,
125 struct xrt_frame_context *xfctx,
126 struct rift_s_hmd_config *hmd_config);
127void
128rift_s_tracker_start(struct rift_s_tracker *t);
129void
130rift_s_tracker_destroy(struct rift_s_tracker *t);
131void
132rift_s_tracker_add_debug_ui(struct rift_s_tracker *t, void *root);
133
134struct xrt_slam_sinks *
135rift_s_tracker_get_slam_sinks(struct rift_s_tracker *t);
136struct xrt_device *
137rift_s_tracker_get_hand_tracking_device(struct rift_s_tracker *t);
138
139void
140rift_s_tracker_clock_update(struct rift_s_tracker *t, uint64_t device_timestamp_ns, timepoint_ns local_timestamp_ns);
141
142void
143rift_s_tracker_imu_update(struct rift_s_tracker *t,
144 uint64_t device_timestamp_ns,
145 const struct xrt_vec3 *accel,
146 const struct xrt_vec3 *gyro);
147
148void
149rift_s_tracker_push_slam_frames(struct rift_s_tracker *t,
150 uint64_t frame_ts_ns,
151 struct xrt_frame *frames[RIFT_S_CAMERA_COUNT]);
152void
153rift_s_tracker_get_tracked_pose(struct rift_s_tracker *t,
154 enum rift_s_tracker_pose pose,
155 uint64_t at_timestamp_ns,
156 struct xrt_space_relation *out_relation);
157
158#endif