blob: 58a57ac5dd694df2320a6945f9e9958ad0222262 [file] [log] [blame]
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -07001/*
2 * Copyright 2021, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "SpatializerPoseController.h"
17
Ytai Ben-Tsvie9b34952021-09-10 12:48:27 -070018#define LOG_TAG "SpatializerPoseController"
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070019//#define LOG_NDEBUG 0
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070020#include <sensor/Sensor.h>
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070021#include <utils/Log.h>
22#include <utils/SystemClock.h>
23
24namespace android {
25
26using media::createHeadTrackingProcessor;
27using media::HeadTrackingMode;
28using media::HeadTrackingProcessor;
29using media::Pose3f;
30using media::SensorPoseProvider;
31using media::Twist3f;
32
33using namespace std::chrono_literals;
34
35namespace {
36
37// This is how fast, in m/s, we allow position to shift during rate-limiting.
Ytai Ben-Tsvic2d28402022-01-14 14:55:16 -080038constexpr float kMaxTranslationalVelocity = 2;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070039
40// This is how fast, in rad/s, we allow rotation angle to shift during rate-limiting.
Ytai Ben-Tsvic2d28402022-01-14 14:55:16 -080041constexpr float kMaxRotationalVelocity = 8;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070042
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070043// This is how far into the future we predict the head pose, using linear extrapolation based on
44// twist (velocity). It should be set to a value that matches the characteristic durations of moving
45// one's head. The higher we set this, the more latency we are able to reduce, but setting this too
46// high will result in high prediction errors whenever the head accelerates (changes velocity).
Ytai Ben-Tsvi3c234b12022-01-31 11:15:17 -080047constexpr auto kPredictionDuration = 50ms;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070048
49// After losing this many consecutive samples from either sensor, we would treat the measurement as
50// stale;
51constexpr auto kMaxLostSamples = 4;
52
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080053// Auto-recenter kicks in after the head has been still for this long.
Ytai Ben-Tsvicdc03062022-01-10 15:57:52 -080054constexpr auto kAutoRecenterWindowDuration = 6s;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080055
56// Auto-recenter considers head not still if translated by this much (in meters, approx).
57constexpr float kAutoRecenterTranslationThreshold = 0.1f;
58
59// Auto-recenter considers head not still if rotated by this much (in radians, approx).
Ytai Ben-Tsvi237012c2022-02-16 15:54:31 -080060constexpr float kAutoRecenterRotationThreshold = 10.5f / 180 * M_PI;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080061
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080062// Screen is considered to be unstable (not still) if it has moved significantly within the last
63// time window of this duration.
Ytai Ben-Tsvicdc03062022-01-10 15:57:52 -080064constexpr auto kScreenStillnessWindowDuration = 3s;
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080065
66// Screen is considered to have moved significantly if translated by this much (in meter, approx).
67constexpr float kScreenStillnessTranslationThreshold = 0.1f;
68
69// Screen is considered to have moved significantly if rotated by this much (in radians, approx).
Ytai Ben-Tsvicdc03062022-01-10 15:57:52 -080070constexpr float kScreenStillnessRotationThreshold = 7.0f / 180 * M_PI;
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080071
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070072// Time units for system clock ticks. This is what the Sensor Framework timestamps represent and
73// what we use for pose filtering.
74using Ticks = std::chrono::nanoseconds;
75
76// How many ticks in a second.
77constexpr auto kTicksPerSecond = Ticks::period::den;
78
79} // namespace
80
81SpatializerPoseController::SpatializerPoseController(Listener* listener,
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -070082 std::chrono::microseconds sensorPeriod,
83 std::chrono::microseconds maxUpdatePeriod)
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070084 : mListener(listener),
85 mSensorPeriod(sensorPeriod),
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070086 mProcessor(createHeadTrackingProcessor(HeadTrackingProcessor::Options{
87 .maxTranslationalVelocity = kMaxTranslationalVelocity / kTicksPerSecond,
88 .maxRotationalVelocity = kMaxRotationalVelocity / kTicksPerSecond,
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070089 .freshnessTimeout = Ticks(sensorPeriod * kMaxLostSamples).count(),
90 .predictionDuration = Ticks(kPredictionDuration).count(),
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080091 .autoRecenterWindowDuration = Ticks(kAutoRecenterWindowDuration).count(),
92 .autoRecenterTranslationalThreshold = kAutoRecenterTranslationThreshold,
93 .autoRecenterRotationalThreshold = kAutoRecenterRotationThreshold,
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080094 .screenStillnessWindowDuration = Ticks(kScreenStillnessWindowDuration).count(),
95 .screenStillnessTranslationalThreshold = kScreenStillnessTranslationThreshold,
96 .screenStillnessRotationalThreshold = kScreenStillnessRotationThreshold,
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070097 })),
Ytai Ben-Tsvi8b6fe3a2021-09-13 15:55:44 -070098 mPoseProvider(SensorPoseProvider::create("headtracker", this)),
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070099 mThread([this, maxUpdatePeriod] {
100 while (true) {
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700101 Pose3f headToStage;
102 std::optional<HeadTrackingMode> modeIfChanged;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700103 {
104 std::unique_lock lock(mMutex);
105 mCondVar.wait_for(lock, maxUpdatePeriod,
106 [this] { return mShouldExit || mShouldCalculate; });
107 if (mShouldExit) {
108 ALOGV("Exiting thread");
109 return;
110 }
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700111
112 // Calculate.
113 std::tie(headToStage, modeIfChanged) = calculate_l();
114 }
115
116 // Invoke the callbacks outside the lock.
117 mListener->onHeadToStagePose(headToStage);
118 if (modeIfChanged) {
119 mListener->onActualModeChange(modeIfChanged.value());
120 }
121
122 {
123 std::lock_guard lock(mMutex);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700124 if (!mCalculated) {
125 mCalculated = true;
126 mCondVar.notify_all();
127 }
128 mShouldCalculate = false;
129 }
130 }
131 }) {}
132
133SpatializerPoseController::~SpatializerPoseController() {
134 {
135 std::unique_lock lock(mMutex);
136 mShouldExit = true;
137 mCondVar.notify_all();
138 }
139 mThread.join();
140}
141
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700142void SpatializerPoseController::setHeadSensor(int32_t sensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700143 std::lock_guard lock(mMutex);
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700144 // Stop current sensor, if valid and different from the other sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700145 if (mHeadSensor != INVALID_SENSOR && mHeadSensor != mScreenSensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700146 mPoseProvider->stopSensor(mHeadSensor);
147 }
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700148
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700149 if (sensor != INVALID_SENSOR) {
150 if (sensor != mScreenSensor) {
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700151 // Start new sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700152 mHeadSensor =
153 mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR;
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700154 } else {
155 // Sensor is already enabled.
156 mHeadSensor = mScreenSensor;
157 }
158 } else {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700159 mHeadSensor = INVALID_SENSOR;
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700160 }
161
Ytai Ben-Tsvi95b00c82021-08-27 15:27:08 -0700162 mProcessor->recenter(true, false);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700163}
164
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700165void SpatializerPoseController::setScreenSensor(int32_t sensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700166 std::lock_guard lock(mMutex);
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700167 // Stop current sensor, if valid and different from the other sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700168 if (mScreenSensor != INVALID_SENSOR && mScreenSensor != mHeadSensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700169 mPoseProvider->stopSensor(mScreenSensor);
170 }
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700171
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700172 if (sensor != INVALID_SENSOR) {
173 if (sensor != mHeadSensor) {
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700174 // Start new sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700175 mScreenSensor =
176 mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR;
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700177 } else {
178 // Sensor is already enabled.
179 mScreenSensor = mHeadSensor;
180 }
181 } else {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700182 mScreenSensor = INVALID_SENSOR;
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700183 }
184
Ytai Ben-Tsvi95b00c82021-08-27 15:27:08 -0700185 mProcessor->recenter(false, true);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700186}
187
188void SpatializerPoseController::setDesiredMode(HeadTrackingMode mode) {
189 std::lock_guard lock(mMutex);
190 mProcessor->setDesiredMode(mode);
191}
192
193void SpatializerPoseController::setScreenToStagePose(const Pose3f& screenToStage) {
194 std::lock_guard lock(mMutex);
195 mProcessor->setScreenToStagePose(screenToStage);
196}
197
198void SpatializerPoseController::setDisplayOrientation(float physicalToLogicalAngle) {
199 std::lock_guard lock(mMutex);
200 mProcessor->setDisplayOrientation(physicalToLogicalAngle);
201}
202
203void SpatializerPoseController::calculateAsync() {
204 std::lock_guard lock(mMutex);
205 mShouldCalculate = true;
206 mCondVar.notify_all();
207}
208
209void SpatializerPoseController::waitUntilCalculated() {
210 std::unique_lock lock(mMutex);
211 mCondVar.wait(lock, [this] { return mCalculated; });
212}
213
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700214std::tuple<media::Pose3f, std::optional<media::HeadTrackingMode>>
215SpatializerPoseController::calculate_l() {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700216 Pose3f headToStage;
217 HeadTrackingMode mode;
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700218 std::optional<media::HeadTrackingMode> modeIfChanged;
219
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700220 mProcessor->calculate(elapsedRealtimeNano());
221 headToStage = mProcessor->getHeadToStagePose();
222 mode = mProcessor->getActualMode();
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700223 if (!mActualMode.has_value() || mActualMode.value() != mode) {
224 mActualMode = mode;
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700225 modeIfChanged = mode;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700226 }
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700227 return std::make_tuple(headToStage, modeIfChanged);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700228}
229
230void SpatializerPoseController::recenter() {
231 std::lock_guard lock(mMutex);
232 mProcessor->recenter();
233}
234
235void SpatializerPoseController::onPose(int64_t timestamp, int32_t sensor, const Pose3f& pose,
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700236 const std::optional<Twist3f>& twist, bool isNewReference) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700237 std::lock_guard lock(mMutex);
238 if (sensor == mHeadSensor) {
Ytai Ben-Tsvi3c234b12022-01-31 11:15:17 -0800239 mProcessor->setWorldToHeadPose(timestamp, pose,
240 twist.value_or(Twist3f()) / kTicksPerSecond);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700241 if (isNewReference) {
242 mProcessor->recenter(true, false);
243 }
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700244 }
245 if (sensor == mScreenSensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700246 mProcessor->setWorldToScreenPose(timestamp, pose);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700247 if (isNewReference) {
248 mProcessor->recenter(false, true);
249 }
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700250 }
251}
252
253} // namespace android