Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | */ |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 16 | #include <inttypes.h> |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 17 | |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 18 | #include <android-base/stringprintf.h> |
| 19 | #include <audio_utils/SimpleLog.h> |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 20 | #include "media/HeadTrackingProcessor.h" |
| 21 | |
| 22 | #include "ModeSelector.h" |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 23 | #include "PoseBias.h" |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 24 | #include "QuaternionUtil.h" |
| 25 | #include "ScreenHeadFusion.h" |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 26 | #include "StillnessDetector.h" |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace media { |
| 30 | namespace { |
| 31 | |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 32 | using android::base::StringAppendF; |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 33 | using Eigen::Quaternionf; |
| 34 | using Eigen::Vector3f; |
| 35 | |
| 36 | class HeadTrackingProcessorImpl : public HeadTrackingProcessor { |
| 37 | public: |
| 38 | HeadTrackingProcessorImpl(const Options& options, HeadTrackingMode initialMode) |
| 39 | : mOptions(options), |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 40 | mHeadStillnessDetector(StillnessDetector::Options{ |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 41 | .defaultValue = false, |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 42 | .windowDuration = options.autoRecenterWindowDuration, |
| 43 | .translationalThreshold = options.autoRecenterTranslationalThreshold, |
| 44 | .rotationalThreshold = options.autoRecenterRotationalThreshold, |
| 45 | }), |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 46 | mScreenStillnessDetector(StillnessDetector::Options{ |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 47 | .defaultValue = true, |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 48 | .windowDuration = options.screenStillnessWindowDuration, |
| 49 | .translationalThreshold = options.screenStillnessTranslationalThreshold, |
| 50 | .rotationalThreshold = options.screenStillnessRotationalThreshold, |
| 51 | }), |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 52 | mModeSelector(ModeSelector::Options{.freshnessTimeout = options.freshnessTimeout}, |
| 53 | initialMode), |
| 54 | mRateLimiter(PoseRateLimiter::Options{ |
| 55 | .maxTranslationalVelocity = options.maxTranslationalVelocity, |
| 56 | .maxRotationalVelocity = options.maxRotationalVelocity}) {} |
| 57 | |
| 58 | void setDesiredMode(HeadTrackingMode mode) override { mModeSelector.setDesiredMode(mode); } |
| 59 | |
| 60 | void setWorldToHeadPose(int64_t timestamp, const Pose3f& worldToHead, |
| 61 | const Twist3f& headTwist) override { |
| 62 | Pose3f predictedWorldToHead = |
| 63 | worldToHead * integrate(headTwist, mOptions.predictionDuration); |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 64 | mHeadPoseBias.setInput(predictedWorldToHead); |
Ytai Ben-Tsvi | 5e6c0f3 | 2022-01-19 08:20:39 -0800 | [diff] [blame] | 65 | mHeadStillnessDetector.setInput(timestamp, predictedWorldToHead); |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 66 | mWorldToHeadTimestamp = timestamp; |
| 67 | } |
| 68 | |
| 69 | void setWorldToScreenPose(int64_t timestamp, const Pose3f& worldToScreen) override { |
Ytai Ben-Tsvi | 7901bdd | 2021-09-08 16:11:07 -0700 | [diff] [blame] | 70 | if (mPhysicalToLogicalAngle != mPendingPhysicalToLogicalAngle) { |
| 71 | // We're introducing an artificial discontinuity. Enable the rate limiter. |
| 72 | mRateLimiter.enable(); |
| 73 | mPhysicalToLogicalAngle = mPendingPhysicalToLogicalAngle; |
| 74 | } |
| 75 | |
Ytai Ben-Tsvi | 5e6c0f3 | 2022-01-19 08:20:39 -0800 | [diff] [blame] | 76 | Pose3f worldToLogicalScreen = worldToScreen * Pose3f(rotateY(-mPhysicalToLogicalAngle)); |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 77 | mScreenPoseBias.setInput(worldToLogicalScreen); |
Ytai Ben-Tsvi | 5e6c0f3 | 2022-01-19 08:20:39 -0800 | [diff] [blame] | 78 | mScreenStillnessDetector.setInput(timestamp, worldToLogicalScreen); |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 79 | mWorldToScreenTimestamp = timestamp; |
| 80 | } |
| 81 | |
| 82 | void setScreenToStagePose(const Pose3f& screenToStage) override { |
| 83 | mModeSelector.setScreenToStagePose(screenToStage); |
| 84 | } |
| 85 | |
| 86 | void setDisplayOrientation(float physicalToLogicalAngle) override { |
Ytai Ben-Tsvi | 7901bdd | 2021-09-08 16:11:07 -0700 | [diff] [blame] | 87 | mPendingPhysicalToLogicalAngle = physicalToLogicalAngle; |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void calculate(int64_t timestamp) override { |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 91 | // Handle the screen first, since it might trigger a recentering of the head. |
| 92 | if (mWorldToScreenTimestamp.has_value()) { |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 93 | const Pose3f worldToLogicalScreen = mScreenPoseBias.getOutput(); |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 94 | bool screenStable = mScreenStillnessDetector.calculate(timestamp); |
| 95 | mModeSelector.setScreenStable(mWorldToScreenTimestamp.value(), screenStable); |
| 96 | // Whenever the screen is unstable, recenter the head pose. |
| 97 | if (!screenStable) { |
| 98 | recenter(true, false); |
| 99 | } |
| 100 | mScreenHeadFusion.setWorldToScreenPose(mWorldToScreenTimestamp.value(), |
| 101 | worldToLogicalScreen); |
| 102 | } |
| 103 | |
| 104 | // Handle head. |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 105 | if (mWorldToHeadTimestamp.has_value()) { |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 106 | Pose3f worldToHead = mHeadPoseBias.getOutput(); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 107 | // Auto-recenter. |
| 108 | if (mHeadStillnessDetector.calculate(timestamp)) { |
| 109 | recenter(true, false); |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 110 | worldToHead = mHeadPoseBias.getOutput(); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 113 | mScreenHeadFusion.setWorldToHeadPose(mWorldToHeadTimestamp.value(), worldToHead); |
| 114 | mModeSelector.setWorldToHeadPose(mWorldToHeadTimestamp.value(), worldToHead); |
| 115 | } |
| 116 | |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 117 | auto maybeScreenToHead = mScreenHeadFusion.calculate(); |
| 118 | if (maybeScreenToHead.has_value()) { |
| 119 | mModeSelector.setScreenToHeadPose(maybeScreenToHead->timestamp, |
| 120 | maybeScreenToHead->pose); |
| 121 | } else { |
| 122 | mModeSelector.setScreenToHeadPose(timestamp, std::nullopt); |
| 123 | } |
| 124 | |
| 125 | HeadTrackingMode prevMode = mModeSelector.getActualMode(); |
| 126 | mModeSelector.calculate(timestamp); |
| 127 | if (mModeSelector.getActualMode() != prevMode) { |
| 128 | // Mode has changed, enable rate limiting. |
| 129 | mRateLimiter.enable(); |
| 130 | } |
| 131 | mRateLimiter.setTarget(mModeSelector.getHeadToStagePose()); |
| 132 | mHeadToStagePose = mRateLimiter.calculatePose(timestamp); |
| 133 | } |
| 134 | |
| 135 | Pose3f getHeadToStagePose() const override { return mHeadToStagePose; } |
| 136 | |
| 137 | HeadTrackingMode getActualMode() const override { return mModeSelector.getActualMode(); } |
| 138 | |
Ytai Ben-Tsvi | 95b00c8 | 2021-08-27 15:27:08 -0700 | [diff] [blame] | 139 | void recenter(bool recenterHead, bool recenterScreen) override { |
| 140 | if (recenterHead) { |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 141 | mHeadPoseBias.recenter(); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 142 | mHeadStillnessDetector.reset(); |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 143 | mLocalLog.log("recenter Head"); |
Ytai Ben-Tsvi | 95b00c8 | 2021-08-27 15:27:08 -0700 | [diff] [blame] | 144 | } |
| 145 | if (recenterScreen) { |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 146 | mScreenPoseBias.recenter(); |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 147 | mScreenStillnessDetector.reset(); |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 148 | mLocalLog.log("recenter Screen"); |
Ytai Ben-Tsvi | 95b00c8 | 2021-08-27 15:27:08 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // If a sensor being recentered is included in the current mode, apply rate limiting to |
| 152 | // avoid discontinuities. |
| 153 | HeadTrackingMode mode = mModeSelector.getActualMode(); |
| 154 | if ((recenterHead && (mode == HeadTrackingMode::WORLD_RELATIVE || |
| 155 | mode == HeadTrackingMode::SCREEN_RELATIVE)) || |
| 156 | (recenterScreen && mode == HeadTrackingMode::SCREEN_RELATIVE)) { |
| 157 | mRateLimiter.enable(); |
| 158 | } |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 161 | std::string toString_l(unsigned level) const override { |
| 162 | std::string prefixSpace(level, ' '); |
| 163 | std::string ss = prefixSpace + "HeadTrackingProcessor:\n"; |
| 164 | StringAppendF(&ss, "%smaxTranslationalVelocity: %f\n", prefixSpace.c_str(), |
| 165 | mOptions.maxTranslationalVelocity); |
| 166 | StringAppendF(&ss, "%smaxRotationalVelocity: %f\n", prefixSpace.c_str(), |
| 167 | mOptions.maxRotationalVelocity); |
| 168 | StringAppendF(&ss, "%sfreshnessTimeout: %" PRId64 "\n", prefixSpace.c_str(), |
| 169 | mOptions.freshnessTimeout); |
| 170 | StringAppendF(&ss, "%spredictionDuration: %f\n", prefixSpace.c_str(), |
| 171 | mOptions.predictionDuration); |
| 172 | StringAppendF(&ss, "%sautoRecenterWindowDuration: %" PRId64 "\n", prefixSpace.c_str(), |
| 173 | mOptions.autoRecenterWindowDuration); |
| 174 | StringAppendF(&ss, "%sautoRecenterTranslationalThreshold: %f\n", prefixSpace.c_str(), |
| 175 | mOptions.autoRecenterTranslationalThreshold); |
| 176 | StringAppendF(&ss, "%sautoRecenterRotationalThreshold: %f\n", prefixSpace.c_str(), |
| 177 | mOptions.autoRecenterRotationalThreshold); |
| 178 | StringAppendF(&ss, "%sscreenStillnessWindowDuration: %" PRId64 "\n", prefixSpace.c_str(), |
| 179 | mOptions.screenStillnessWindowDuration); |
| 180 | StringAppendF(&ss, "%sscreenStillnessTranslationalThreshold: %f\n", prefixSpace.c_str(), |
| 181 | mOptions.screenStillnessTranslationalThreshold); |
| 182 | StringAppendF(&ss, "%sscreenStillnessRotationalThreshold: %f\n", prefixSpace.c_str(), |
| 183 | mOptions.screenStillnessRotationalThreshold); |
| 184 | ss.append(prefixSpace + "ReCenterHistory:\n"); |
| 185 | ss += mLocalLog.dumpToString((prefixSpace + " ").c_str(), mMaxLocalLogLine); |
| 186 | // TODO: 233092747 add string from PoseRateLimiter/PoseRateLimiter etc... |
| 187 | return ss; |
| 188 | } |
| 189 | |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 190 | private: |
| 191 | const Options mOptions; |
| 192 | float mPhysicalToLogicalAngle = 0; |
Ytai Ben-Tsvi | 7901bdd | 2021-09-08 16:11:07 -0700 | [diff] [blame] | 193 | // We store the physical to logical angle as "pending" until the next world-to-screen sample it |
| 194 | // applies to arrives. |
| 195 | float mPendingPhysicalToLogicalAngle = 0; |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 196 | std::optional<int64_t> mWorldToHeadTimestamp; |
| 197 | std::optional<int64_t> mWorldToScreenTimestamp; |
| 198 | Pose3f mHeadToStagePose; |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 199 | PoseBias mHeadPoseBias; |
| 200 | PoseBias mScreenPoseBias; |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 201 | StillnessDetector mHeadStillnessDetector; |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 202 | StillnessDetector mScreenStillnessDetector; |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 203 | ScreenHeadFusion mScreenHeadFusion; |
| 204 | ModeSelector mModeSelector; |
| 205 | PoseRateLimiter mRateLimiter; |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 206 | static constexpr std::size_t mMaxLocalLogLine = 10; |
| 207 | SimpleLog mLocalLog{mMaxLocalLogLine}; |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | } // namespace |
| 211 | |
Ytai Ben-Tsvi | edbab3d | 2021-08-16 11:27:52 -0700 | [diff] [blame] | 212 | std::unique_ptr<HeadTrackingProcessor> createHeadTrackingProcessor( |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 213 | const HeadTrackingProcessor::Options& options, HeadTrackingMode initialMode) { |
| 214 | return std::make_unique<HeadTrackingProcessorImpl>(options, initialMode); |
| 215 | } |
| 216 | |
| 217 | } // namespace media |
| 218 | } // namespace android |