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 | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 38 | constexpr auto 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 | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 41 | constexpr auto kMaxRotationalVelocity = 4 * M_PI; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 42 | |
| 43 | // This should be set to the typical time scale that the translation sensors used drift in. This |
| 44 | // means, loosely, for how long we can trust the reading to be "accurate enough". This would |
| 45 | // determine the time constants used for high-pass filtering those readings. If the value is set |
| 46 | // too high, we may experience drift. If it is set too low, we may experience poses tending toward |
| 47 | // identity too fast. |
| 48 | constexpr auto kTranslationalDriftTimeConstant = 20s; |
| 49 | |
| 50 | // This should be set to the typical time scale that the rotation sensors used drift in. This |
| 51 | // means, loosely, for how long we can trust the reading to be "accurate enough". This would |
| 52 | // determine the time constants used for high-pass filtering those readings. If the value is set |
| 53 | // too high, we may experience drift. If it is set too low, we may experience poses tending toward |
| 54 | // identity too fast. |
| 55 | constexpr auto kRotationalDriftTimeConstant = 20s; |
| 56 | |
| 57 | // This is how far into the future we predict the head pose, using linear extrapolation based on |
| 58 | // twist (velocity). It should be set to a value that matches the characteristic durations of moving |
| 59 | // one's head. The higher we set this, the more latency we are able to reduce, but setting this too |
| 60 | // high will result in high prediction errors whenever the head accelerates (changes velocity). |
| 61 | constexpr auto kPredictionDuration = 10ms; |
| 62 | |
| 63 | // After losing this many consecutive samples from either sensor, we would treat the measurement as |
| 64 | // stale; |
| 65 | constexpr auto kMaxLostSamples = 4; |
| 66 | |
| 67 | // Time units for system clock ticks. This is what the Sensor Framework timestamps represent and |
| 68 | // what we use for pose filtering. |
| 69 | using Ticks = std::chrono::nanoseconds; |
| 70 | |
| 71 | // How many ticks in a second. |
| 72 | constexpr auto kTicksPerSecond = Ticks::period::den; |
| 73 | |
| 74 | } // namespace |
| 75 | |
| 76 | SpatializerPoseController::SpatializerPoseController(Listener* listener, |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 77 | std::chrono::microseconds sensorPeriod, |
| 78 | std::chrono::microseconds maxUpdatePeriod) |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 79 | : mListener(listener), |
| 80 | mSensorPeriod(sensorPeriod), |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 81 | mProcessor(createHeadTrackingProcessor(HeadTrackingProcessor::Options{ |
| 82 | .maxTranslationalVelocity = kMaxTranslationalVelocity / kTicksPerSecond, |
| 83 | .maxRotationalVelocity = kMaxRotationalVelocity / kTicksPerSecond, |
| 84 | .translationalDriftTimeConstant = Ticks(kTranslationalDriftTimeConstant).count(), |
| 85 | .rotationalDriftTimeConstant = Ticks(kRotationalDriftTimeConstant).count(), |
| 86 | .freshnessTimeout = Ticks(sensorPeriod * kMaxLostSamples).count(), |
| 87 | .predictionDuration = Ticks(kPredictionDuration).count(), |
| 88 | })), |
Ytai Ben-Tsvi | 8b6fe3a | 2021-09-13 15:55:44 -0700 | [diff] [blame] | 89 | mPoseProvider(SensorPoseProvider::create("headtracker", this)), |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 90 | mThread([this, maxUpdatePeriod] { |
| 91 | while (true) { |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 92 | Pose3f headToStage; |
| 93 | std::optional<HeadTrackingMode> modeIfChanged; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 94 | { |
| 95 | std::unique_lock lock(mMutex); |
| 96 | mCondVar.wait_for(lock, maxUpdatePeriod, |
| 97 | [this] { return mShouldExit || mShouldCalculate; }); |
| 98 | if (mShouldExit) { |
| 99 | ALOGV("Exiting thread"); |
| 100 | return; |
| 101 | } |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 102 | |
| 103 | // Calculate. |
| 104 | std::tie(headToStage, modeIfChanged) = calculate_l(); |
| 105 | } |
| 106 | |
| 107 | // Invoke the callbacks outside the lock. |
| 108 | mListener->onHeadToStagePose(headToStage); |
| 109 | if (modeIfChanged) { |
| 110 | mListener->onActualModeChange(modeIfChanged.value()); |
| 111 | } |
| 112 | |
| 113 | { |
| 114 | std::lock_guard lock(mMutex); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 115 | if (!mCalculated) { |
| 116 | mCalculated = true; |
| 117 | mCondVar.notify_all(); |
| 118 | } |
| 119 | mShouldCalculate = false; |
| 120 | } |
| 121 | } |
| 122 | }) {} |
| 123 | |
| 124 | SpatializerPoseController::~SpatializerPoseController() { |
| 125 | { |
| 126 | std::unique_lock lock(mMutex); |
| 127 | mShouldExit = true; |
| 128 | mCondVar.notify_all(); |
| 129 | } |
| 130 | mThread.join(); |
| 131 | } |
| 132 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 133 | void SpatializerPoseController::setHeadSensor(int32_t sensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 134 | std::lock_guard lock(mMutex); |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 135 | // Stop current sensor, if valid and different from the other sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 136 | if (mHeadSensor != INVALID_SENSOR && mHeadSensor != mScreenSensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 137 | mPoseProvider->stopSensor(mHeadSensor); |
| 138 | } |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 139 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 140 | if (sensor != INVALID_SENSOR) { |
| 141 | if (sensor != mScreenSensor) { |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 142 | // Start new sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 143 | mHeadSensor = |
| 144 | mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR; |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 145 | } else { |
| 146 | // Sensor is already enabled. |
| 147 | mHeadSensor = mScreenSensor; |
| 148 | } |
| 149 | } else { |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 150 | mHeadSensor = INVALID_SENSOR; |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Ytai Ben-Tsvi | 95b00c8 | 2021-08-27 15:27:08 -0700 | [diff] [blame] | 153 | mProcessor->recenter(true, false); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 156 | void SpatializerPoseController::setScreenSensor(int32_t sensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 157 | std::lock_guard lock(mMutex); |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 158 | // Stop current sensor, if valid and different from the other sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 159 | if (mScreenSensor != INVALID_SENSOR && mScreenSensor != mHeadSensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 160 | mPoseProvider->stopSensor(mScreenSensor); |
| 161 | } |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 162 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 163 | if (sensor != INVALID_SENSOR) { |
| 164 | if (sensor != mHeadSensor) { |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 165 | // Start new sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 166 | mScreenSensor = |
| 167 | mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR; |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 168 | } else { |
| 169 | // Sensor is already enabled. |
| 170 | mScreenSensor = mHeadSensor; |
| 171 | } |
| 172 | } else { |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 173 | mScreenSensor = INVALID_SENSOR; |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Ytai Ben-Tsvi | 95b00c8 | 2021-08-27 15:27:08 -0700 | [diff] [blame] | 176 | mProcessor->recenter(false, true); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void SpatializerPoseController::setDesiredMode(HeadTrackingMode mode) { |
| 180 | std::lock_guard lock(mMutex); |
| 181 | mProcessor->setDesiredMode(mode); |
| 182 | } |
| 183 | |
| 184 | void SpatializerPoseController::setScreenToStagePose(const Pose3f& screenToStage) { |
| 185 | std::lock_guard lock(mMutex); |
| 186 | mProcessor->setScreenToStagePose(screenToStage); |
| 187 | } |
| 188 | |
| 189 | void SpatializerPoseController::setDisplayOrientation(float physicalToLogicalAngle) { |
| 190 | std::lock_guard lock(mMutex); |
| 191 | mProcessor->setDisplayOrientation(physicalToLogicalAngle); |
| 192 | } |
| 193 | |
| 194 | void SpatializerPoseController::calculateAsync() { |
| 195 | std::lock_guard lock(mMutex); |
| 196 | mShouldCalculate = true; |
| 197 | mCondVar.notify_all(); |
| 198 | } |
| 199 | |
| 200 | void SpatializerPoseController::waitUntilCalculated() { |
| 201 | std::unique_lock lock(mMutex); |
| 202 | mCondVar.wait(lock, [this] { return mCalculated; }); |
| 203 | } |
| 204 | |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 205 | std::tuple<media::Pose3f, std::optional<media::HeadTrackingMode>> |
| 206 | SpatializerPoseController::calculate_l() { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 207 | Pose3f headToStage; |
| 208 | HeadTrackingMode mode; |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 209 | std::optional<media::HeadTrackingMode> modeIfChanged; |
| 210 | |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 211 | mProcessor->calculate(elapsedRealtimeNano()); |
| 212 | headToStage = mProcessor->getHeadToStagePose(); |
| 213 | mode = mProcessor->getActualMode(); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 214 | if (!mActualMode.has_value() || mActualMode.value() != mode) { |
| 215 | mActualMode = mode; |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 216 | modeIfChanged = mode; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 217 | } |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 218 | return std::make_tuple(headToStage, modeIfChanged); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void SpatializerPoseController::recenter() { |
| 222 | std::lock_guard lock(mMutex); |
| 223 | mProcessor->recenter(); |
| 224 | } |
| 225 | |
| 226 | 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^] | 227 | const std::optional<Twist3f>& twist, bool isNewReference) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 228 | std::lock_guard lock(mMutex); |
| 229 | if (sensor == mHeadSensor) { |
| 230 | mProcessor->setWorldToHeadPose(timestamp, pose, twist.value_or(Twist3f())); |
Ytai Ben-Tsvi | 2c694be | 2021-10-06 17:12:49 -0700 | [diff] [blame^] | 231 | if (isNewReference) { |
| 232 | mProcessor->recenter(true, false); |
| 233 | } |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 234 | } |
| 235 | if (sensor == mScreenSensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 236 | mProcessor->setWorldToScreenPose(timestamp, pose); |
Ytai Ben-Tsvi | 2c694be | 2021-10-06 17:12:49 -0700 | [diff] [blame^] | 237 | if (isNewReference) { |
| 238 | mProcessor->recenter(false, true); |
| 239 | } |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | } // namespace android |