blob: 0a9f4d9b5f37128902a8328c290a73def15916f2 [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
Ytai Ben-Tsvie2356842022-04-14 11:30:32 -070049// After not getting a pose sample for this long, we would treat the measurement as stale.
50constexpr auto kFreshnessTimeout = 50ms;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070051
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080052// Auto-recenter kicks in after the head has been still for this long.
Ytai Ben-Tsvicdc03062022-01-10 15:57:52 -080053constexpr auto kAutoRecenterWindowDuration = 6s;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080054
55// Auto-recenter considers head not still if translated by this much (in meters, approx).
56constexpr float kAutoRecenterTranslationThreshold = 0.1f;
57
58// Auto-recenter considers head not still if rotated by this much (in radians, approx).
Ytai Ben-Tsvi237012c2022-02-16 15:54:31 -080059constexpr float kAutoRecenterRotationThreshold = 10.5f / 180 * M_PI;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080060
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080061// Screen is considered to be unstable (not still) if it has moved significantly within the last
62// time window of this duration.
Ytai Ben-Tsvicdc03062022-01-10 15:57:52 -080063constexpr auto kScreenStillnessWindowDuration = 3s;
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080064
65// Screen is considered to have moved significantly if translated by this much (in meter, approx).
66constexpr float kScreenStillnessTranslationThreshold = 0.1f;
67
68// Screen is considered to have moved significantly if rotated by this much (in radians, approx).
Ytai Ben-Tsvicdc03062022-01-10 15:57:52 -080069constexpr float kScreenStillnessRotationThreshold = 7.0f / 180 * M_PI;
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080070
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070071// Time units for system clock ticks. This is what the Sensor Framework timestamps represent and
72// what we use for pose filtering.
73using Ticks = std::chrono::nanoseconds;
74
75// How many ticks in a second.
76constexpr auto kTicksPerSecond = Ticks::period::den;
77
78} // namespace
79
80SpatializerPoseController::SpatializerPoseController(Listener* listener,
Eric Laurente51f80e2022-04-14 10:20:38 +020081 std::chrono::microseconds sensorPeriod,
82 std::optional<std::chrono::microseconds> maxUpdatePeriod)
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070083 : mListener(listener),
84 mSensorPeriod(sensorPeriod),
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070085 mProcessor(createHeadTrackingProcessor(HeadTrackingProcessor::Options{
86 .maxTranslationalVelocity = kMaxTranslationalVelocity / kTicksPerSecond,
87 .maxRotationalVelocity = kMaxRotationalVelocity / kTicksPerSecond,
Ytai Ben-Tsvie2356842022-04-14 11:30:32 -070088 .freshnessTimeout = Ticks(kFreshnessTimeout).count(),
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070089 .predictionDuration = Ticks(kPredictionDuration).count(),
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080090 .autoRecenterWindowDuration = Ticks(kAutoRecenterWindowDuration).count(),
91 .autoRecenterTranslationalThreshold = kAutoRecenterTranslationThreshold,
92 .autoRecenterRotationalThreshold = kAutoRecenterRotationThreshold,
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080093 .screenStillnessWindowDuration = Ticks(kScreenStillnessWindowDuration).count(),
94 .screenStillnessTranslationalThreshold = kScreenStillnessTranslationThreshold,
95 .screenStillnessRotationalThreshold = kScreenStillnessRotationThreshold,
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070096 })),
Ytai Ben-Tsvi8b6fe3a2021-09-13 15:55:44 -070097 mPoseProvider(SensorPoseProvider::create("headtracker", this)),
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070098 mThread([this, maxUpdatePeriod] {
99 while (true) {
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700100 Pose3f headToStage;
101 std::optional<HeadTrackingMode> modeIfChanged;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700102 {
103 std::unique_lock lock(mMutex);
Eric Laurente51f80e2022-04-14 10:20:38 +0200104 if (maxUpdatePeriod.has_value()) {
105 mCondVar.wait_for(lock, maxUpdatePeriod.value(),
106 [this] { return mShouldExit || mShouldCalculate; });
107 } else {
108 mCondVar.wait(lock, [this] { return mShouldExit || mShouldCalculate; });
109 }
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700110 if (mShouldExit) {
111 ALOGV("Exiting thread");
112 return;
113 }
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700114
115 // Calculate.
116 std::tie(headToStage, modeIfChanged) = calculate_l();
117 }
118
119 // Invoke the callbacks outside the lock.
120 mListener->onHeadToStagePose(headToStage);
121 if (modeIfChanged) {
122 mListener->onActualModeChange(modeIfChanged.value());
123 }
124
125 {
126 std::lock_guard lock(mMutex);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700127 if (!mCalculated) {
128 mCalculated = true;
129 mCondVar.notify_all();
130 }
131 mShouldCalculate = false;
132 }
133 }
134 }) {}
135
136SpatializerPoseController::~SpatializerPoseController() {
137 {
138 std::unique_lock lock(mMutex);
139 mShouldExit = true;
140 mCondVar.notify_all();
141 }
142 mThread.join();
143}
144
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700145void SpatializerPoseController::setHeadSensor(int32_t sensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700146 std::lock_guard lock(mMutex);
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700147 // Stop current sensor, if valid and different from the other sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700148 if (mHeadSensor != INVALID_SENSOR && mHeadSensor != mScreenSensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700149 mPoseProvider->stopSensor(mHeadSensor);
150 }
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700151
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700152 if (sensor != INVALID_SENSOR) {
153 if (sensor != mScreenSensor) {
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700154 // Start new sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700155 mHeadSensor =
156 mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR;
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700157 } else {
158 // Sensor is already enabled.
159 mHeadSensor = mScreenSensor;
160 }
161 } else {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700162 mHeadSensor = INVALID_SENSOR;
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700163 }
164
Ytai Ben-Tsvi95b00c82021-08-27 15:27:08 -0700165 mProcessor->recenter(true, false);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700166}
167
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700168void SpatializerPoseController::setScreenSensor(int32_t sensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700169 std::lock_guard lock(mMutex);
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700170 // Stop current sensor, if valid and different from the other sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700171 if (mScreenSensor != INVALID_SENSOR && mScreenSensor != mHeadSensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700172 mPoseProvider->stopSensor(mScreenSensor);
173 }
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700174
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700175 if (sensor != INVALID_SENSOR) {
176 if (sensor != mHeadSensor) {
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700177 // Start new sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700178 mScreenSensor =
179 mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR;
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700180 } else {
181 // Sensor is already enabled.
182 mScreenSensor = mHeadSensor;
183 }
184 } else {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700185 mScreenSensor = INVALID_SENSOR;
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700186 }
187
Ytai Ben-Tsvi95b00c82021-08-27 15:27:08 -0700188 mProcessor->recenter(false, true);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700189}
190
191void SpatializerPoseController::setDesiredMode(HeadTrackingMode mode) {
192 std::lock_guard lock(mMutex);
193 mProcessor->setDesiredMode(mode);
194}
195
196void SpatializerPoseController::setScreenToStagePose(const Pose3f& screenToStage) {
197 std::lock_guard lock(mMutex);
198 mProcessor->setScreenToStagePose(screenToStage);
199}
200
201void SpatializerPoseController::setDisplayOrientation(float physicalToLogicalAngle) {
202 std::lock_guard lock(mMutex);
203 mProcessor->setDisplayOrientation(physicalToLogicalAngle);
204}
205
206void SpatializerPoseController::calculateAsync() {
207 std::lock_guard lock(mMutex);
208 mShouldCalculate = true;
209 mCondVar.notify_all();
210}
211
212void SpatializerPoseController::waitUntilCalculated() {
213 std::unique_lock lock(mMutex);
214 mCondVar.wait(lock, [this] { return mCalculated; });
215}
216
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700217std::tuple<media::Pose3f, std::optional<media::HeadTrackingMode>>
218SpatializerPoseController::calculate_l() {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700219 Pose3f headToStage;
220 HeadTrackingMode mode;
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700221 std::optional<media::HeadTrackingMode> modeIfChanged;
222
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700223 mProcessor->calculate(elapsedRealtimeNano());
224 headToStage = mProcessor->getHeadToStagePose();
225 mode = mProcessor->getActualMode();
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700226 if (!mActualMode.has_value() || mActualMode.value() != mode) {
227 mActualMode = mode;
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700228 modeIfChanged = mode;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700229 }
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700230 return std::make_tuple(headToStage, modeIfChanged);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700231}
232
233void SpatializerPoseController::recenter() {
234 std::lock_guard lock(mMutex);
235 mProcessor->recenter();
236}
237
238void SpatializerPoseController::onPose(int64_t timestamp, int32_t sensor, const Pose3f& pose,
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700239 const std::optional<Twist3f>& twist, bool isNewReference) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700240 std::lock_guard lock(mMutex);
241 if (sensor == mHeadSensor) {
Ytai Ben-Tsvi3c234b12022-01-31 11:15:17 -0800242 mProcessor->setWorldToHeadPose(timestamp, pose,
243 twist.value_or(Twist3f()) / kTicksPerSecond);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700244 if (isNewReference) {
245 mProcessor->recenter(true, false);
246 }
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700247 }
248 if (sensor == mScreenSensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700249 mProcessor->setWorldToScreenPose(timestamp, pose);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700250 if (isNewReference) {
251 mProcessor->recenter(false, true);
252 }
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700253 }
254}
255
256} // namespace android