Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 1 | /* |
| 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-Tsvi | e9b3495 | 2021-09-10 12:48:27 -0700 | [diff] [blame] | 18 | #define LOG_TAG "SpatializerPoseController" |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 20 | #include <sensor/Sensor.h> |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | #include <utils/SystemClock.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | using media::createHeadTrackingProcessor; |
| 27 | using media::HeadTrackingMode; |
| 28 | using media::HeadTrackingProcessor; |
| 29 | using media::Pose3f; |
| 30 | using media::SensorPoseProvider; |
| 31 | using media::Twist3f; |
| 32 | |
| 33 | using namespace std::chrono_literals; |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | // This is how fast, in m/s, we allow position to shift during rate-limiting. |
Ytai Ben-Tsvi | c2d2840 | 2022-01-14 14:55:16 -0800 | [diff] [blame] | 38 | constexpr float kMaxTranslationalVelocity = 2; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 39 | |
| 40 | // This is how fast, in rad/s, we allow rotation angle to shift during rate-limiting. |
Ytai Ben-Tsvi | c2d2840 | 2022-01-14 14:55:16 -0800 | [diff] [blame] | 41 | constexpr float kMaxRotationalVelocity = 8; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 42 | |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 43 | // 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-Tsvi | 3c234b1 | 2022-01-31 11:15:17 -0800 | [diff] [blame] | 47 | constexpr auto kPredictionDuration = 50ms; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 48 | |
Ytai Ben-Tsvi | e235684 | 2022-04-14 11:30:32 -0700 | [diff] [blame^] | 49 | // After not getting a pose sample for this long, we would treat the measurement as stale. |
| 50 | constexpr auto kFreshnessTimeout = 50ms; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 51 | |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 52 | // Auto-recenter kicks in after the head has been still for this long. |
Ytai Ben-Tsvi | cdc0306 | 2022-01-10 15:57:52 -0800 | [diff] [blame] | 53 | constexpr auto kAutoRecenterWindowDuration = 6s; |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 54 | |
| 55 | // Auto-recenter considers head not still if translated by this much (in meters, approx). |
| 56 | constexpr float kAutoRecenterTranslationThreshold = 0.1f; |
| 57 | |
| 58 | // Auto-recenter considers head not still if rotated by this much (in radians, approx). |
Ytai Ben-Tsvi | 237012c | 2022-02-16 15:54:31 -0800 | [diff] [blame] | 59 | constexpr float kAutoRecenterRotationThreshold = 10.5f / 180 * M_PI; |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 60 | |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 61 | // Screen is considered to be unstable (not still) if it has moved significantly within the last |
| 62 | // time window of this duration. |
Ytai Ben-Tsvi | cdc0306 | 2022-01-10 15:57:52 -0800 | [diff] [blame] | 63 | constexpr auto kScreenStillnessWindowDuration = 3s; |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 64 | |
| 65 | // Screen is considered to have moved significantly if translated by this much (in meter, approx). |
| 66 | constexpr float kScreenStillnessTranslationThreshold = 0.1f; |
| 67 | |
| 68 | // Screen is considered to have moved significantly if rotated by this much (in radians, approx). |
Ytai Ben-Tsvi | cdc0306 | 2022-01-10 15:57:52 -0800 | [diff] [blame] | 69 | constexpr float kScreenStillnessRotationThreshold = 7.0f / 180 * M_PI; |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 70 | |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 71 | // Time units for system clock ticks. This is what the Sensor Framework timestamps represent and |
| 72 | // what we use for pose filtering. |
| 73 | using Ticks = std::chrono::nanoseconds; |
| 74 | |
| 75 | // How many ticks in a second. |
| 76 | constexpr auto kTicksPerSecond = Ticks::period::den; |
| 77 | |
| 78 | } // namespace |
| 79 | |
| 80 | SpatializerPoseController::SpatializerPoseController(Listener* listener, |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 81 | std::chrono::microseconds sensorPeriod, |
| 82 | std::chrono::microseconds maxUpdatePeriod) |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 83 | : mListener(listener), |
| 84 | mSensorPeriod(sensorPeriod), |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 85 | mProcessor(createHeadTrackingProcessor(HeadTrackingProcessor::Options{ |
| 86 | .maxTranslationalVelocity = kMaxTranslationalVelocity / kTicksPerSecond, |
| 87 | .maxRotationalVelocity = kMaxRotationalVelocity / kTicksPerSecond, |
Ytai Ben-Tsvi | e235684 | 2022-04-14 11:30:32 -0700 | [diff] [blame^] | 88 | .freshnessTimeout = Ticks(kFreshnessTimeout).count(), |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 89 | .predictionDuration = Ticks(kPredictionDuration).count(), |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 90 | .autoRecenterWindowDuration = Ticks(kAutoRecenterWindowDuration).count(), |
| 91 | .autoRecenterTranslationalThreshold = kAutoRecenterTranslationThreshold, |
| 92 | .autoRecenterRotationalThreshold = kAutoRecenterRotationThreshold, |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 93 | .screenStillnessWindowDuration = Ticks(kScreenStillnessWindowDuration).count(), |
| 94 | .screenStillnessTranslationalThreshold = kScreenStillnessTranslationThreshold, |
| 95 | .screenStillnessRotationalThreshold = kScreenStillnessRotationThreshold, |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 96 | })), |
Ytai Ben-Tsvi | 8b6fe3a | 2021-09-13 15:55:44 -0700 | [diff] [blame] | 97 | mPoseProvider(SensorPoseProvider::create("headtracker", this)), |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 98 | mThread([this, maxUpdatePeriod] { |
| 99 | while (true) { |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 100 | Pose3f headToStage; |
| 101 | std::optional<HeadTrackingMode> modeIfChanged; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 102 | { |
| 103 | std::unique_lock lock(mMutex); |
| 104 | mCondVar.wait_for(lock, maxUpdatePeriod, |
| 105 | [this] { return mShouldExit || mShouldCalculate; }); |
| 106 | if (mShouldExit) { |
| 107 | ALOGV("Exiting thread"); |
| 108 | return; |
| 109 | } |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 110 | |
| 111 | // Calculate. |
| 112 | std::tie(headToStage, modeIfChanged) = calculate_l(); |
| 113 | } |
| 114 | |
| 115 | // Invoke the callbacks outside the lock. |
| 116 | mListener->onHeadToStagePose(headToStage); |
| 117 | if (modeIfChanged) { |
| 118 | mListener->onActualModeChange(modeIfChanged.value()); |
| 119 | } |
| 120 | |
| 121 | { |
| 122 | std::lock_guard lock(mMutex); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 123 | if (!mCalculated) { |
| 124 | mCalculated = true; |
| 125 | mCondVar.notify_all(); |
| 126 | } |
| 127 | mShouldCalculate = false; |
| 128 | } |
| 129 | } |
| 130 | }) {} |
| 131 | |
| 132 | SpatializerPoseController::~SpatializerPoseController() { |
| 133 | { |
| 134 | std::unique_lock lock(mMutex); |
| 135 | mShouldExit = true; |
| 136 | mCondVar.notify_all(); |
| 137 | } |
| 138 | mThread.join(); |
| 139 | } |
| 140 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 141 | void SpatializerPoseController::setHeadSensor(int32_t sensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 142 | std::lock_guard lock(mMutex); |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 143 | // Stop current sensor, if valid and different from the other sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 144 | if (mHeadSensor != INVALID_SENSOR && mHeadSensor != mScreenSensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 145 | mPoseProvider->stopSensor(mHeadSensor); |
| 146 | } |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 147 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 148 | if (sensor != INVALID_SENSOR) { |
| 149 | if (sensor != mScreenSensor) { |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 150 | // Start new sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 151 | mHeadSensor = |
| 152 | mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR; |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 153 | } else { |
| 154 | // Sensor is already enabled. |
| 155 | mHeadSensor = mScreenSensor; |
| 156 | } |
| 157 | } else { |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 158 | mHeadSensor = INVALID_SENSOR; |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Ytai Ben-Tsvi | 95b00c8 | 2021-08-27 15:27:08 -0700 | [diff] [blame] | 161 | mProcessor->recenter(true, false); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 164 | void SpatializerPoseController::setScreenSensor(int32_t sensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 165 | std::lock_guard lock(mMutex); |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 166 | // Stop current sensor, if valid and different from the other sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 167 | if (mScreenSensor != INVALID_SENSOR && mScreenSensor != mHeadSensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 168 | mPoseProvider->stopSensor(mScreenSensor); |
| 169 | } |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 170 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 171 | if (sensor != INVALID_SENSOR) { |
| 172 | if (sensor != mHeadSensor) { |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 173 | // Start new sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 174 | mScreenSensor = |
| 175 | mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR; |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 176 | } else { |
| 177 | // Sensor is already enabled. |
| 178 | mScreenSensor = mHeadSensor; |
| 179 | } |
| 180 | } else { |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 181 | mScreenSensor = INVALID_SENSOR; |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Ytai Ben-Tsvi | 95b00c8 | 2021-08-27 15:27:08 -0700 | [diff] [blame] | 184 | mProcessor->recenter(false, true); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void SpatializerPoseController::setDesiredMode(HeadTrackingMode mode) { |
| 188 | std::lock_guard lock(mMutex); |
| 189 | mProcessor->setDesiredMode(mode); |
| 190 | } |
| 191 | |
| 192 | void SpatializerPoseController::setScreenToStagePose(const Pose3f& screenToStage) { |
| 193 | std::lock_guard lock(mMutex); |
| 194 | mProcessor->setScreenToStagePose(screenToStage); |
| 195 | } |
| 196 | |
| 197 | void SpatializerPoseController::setDisplayOrientation(float physicalToLogicalAngle) { |
| 198 | std::lock_guard lock(mMutex); |
| 199 | mProcessor->setDisplayOrientation(physicalToLogicalAngle); |
| 200 | } |
| 201 | |
| 202 | void SpatializerPoseController::calculateAsync() { |
| 203 | std::lock_guard lock(mMutex); |
| 204 | mShouldCalculate = true; |
| 205 | mCondVar.notify_all(); |
| 206 | } |
| 207 | |
| 208 | void SpatializerPoseController::waitUntilCalculated() { |
| 209 | std::unique_lock lock(mMutex); |
| 210 | mCondVar.wait(lock, [this] { return mCalculated; }); |
| 211 | } |
| 212 | |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 213 | std::tuple<media::Pose3f, std::optional<media::HeadTrackingMode>> |
| 214 | SpatializerPoseController::calculate_l() { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 215 | Pose3f headToStage; |
| 216 | HeadTrackingMode mode; |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 217 | std::optional<media::HeadTrackingMode> modeIfChanged; |
| 218 | |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 219 | mProcessor->calculate(elapsedRealtimeNano()); |
| 220 | headToStage = mProcessor->getHeadToStagePose(); |
| 221 | mode = mProcessor->getActualMode(); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 222 | if (!mActualMode.has_value() || mActualMode.value() != mode) { |
| 223 | mActualMode = mode; |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 224 | modeIfChanged = mode; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 225 | } |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 226 | return std::make_tuple(headToStage, modeIfChanged); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void SpatializerPoseController::recenter() { |
| 230 | std::lock_guard lock(mMutex); |
| 231 | mProcessor->recenter(); |
| 232 | } |
| 233 | |
| 234 | void SpatializerPoseController::onPose(int64_t timestamp, int32_t sensor, const Pose3f& pose, |
Ytai Ben-Tsvi | 2c694be | 2021-10-06 17:12:49 -0700 | [diff] [blame] | 235 | const std::optional<Twist3f>& twist, bool isNewReference) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 236 | std::lock_guard lock(mMutex); |
| 237 | if (sensor == mHeadSensor) { |
Ytai Ben-Tsvi | 3c234b1 | 2022-01-31 11:15:17 -0800 | [diff] [blame] | 238 | mProcessor->setWorldToHeadPose(timestamp, pose, |
| 239 | twist.value_or(Twist3f()) / kTicksPerSecond); |
Ytai Ben-Tsvi | 2c694be | 2021-10-06 17:12:49 -0700 | [diff] [blame] | 240 | if (isNewReference) { |
| 241 | mProcessor->recenter(true, false); |
| 242 | } |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 243 | } |
| 244 | if (sensor == mScreenSensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 245 | mProcessor->setWorldToScreenPose(timestamp, pose); |
Ytai Ben-Tsvi | 2c694be | 2021-10-06 17:12:49 -0700 | [diff] [blame] | 246 | if (isNewReference) { |
| 247 | mProcessor->recenter(false, true); |
| 248 | } |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | } // namespace android |