diff options
| author | 2021-01-10 22:09:56 -0700 | |
|---|---|---|
| committer | 2021-01-10 22:09:56 -0700 | |
| commit | 7a3c884e39fccfbb498b855080bffabc9ce2e7f1 (patch) | |
| tree | 5056f9406dec188439cb0deb87603498243a9412 /src/input_common/motion_input.cpp | |
| parent | More forgetting... duh (diff) | |
| parent | Merge pull request #5229 from Morph1984/fullscreen-opt (diff) | |
| download | yuzu-7a3c884e39fccfbb498b855080bffabc9ce2e7f1.tar.gz yuzu-7a3c884e39fccfbb498b855080bffabc9ce2e7f1.tar.xz yuzu-7a3c884e39fccfbb498b855080bffabc9ce2e7f1.zip | |
Merge remote-tracking branch 'upstream/master' into int-flags
Diffstat (limited to 'src/input_common/motion_input.cpp')
| -rw-r--r-- | src/input_common/motion_input.cpp | 144 |
1 files changed, 132 insertions, 12 deletions
diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp index 22a849866..6a65f175e 100644 --- a/src/input_common/motion_input.cpp +++ b/src/input_common/motion_input.cpp | |||
| @@ -2,13 +2,13 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included | 3 | // Refer to the license.txt file included |
| 4 | 4 | ||
| 5 | #include <random> | ||
| 5 | #include "common/math_util.h" | 6 | #include "common/math_util.h" |
| 6 | #include "input_common/motion_input.h" | 7 | #include "input_common/motion_input.h" |
| 7 | 8 | ||
| 8 | namespace InputCommon { | 9 | namespace InputCommon { |
| 9 | 10 | ||
| 10 | MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) | 11 | MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) : kp(new_kp), ki(new_ki), kd(new_kd) {} |
| 11 | : kp(new_kp), ki(new_ki), kd(new_kd), quat{{0, 0, -1}, 0} {} | ||
| 12 | 12 | ||
| 13 | void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { | 13 | void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { |
| 14 | accel = acceleration; | 14 | accel = acceleration; |
| @@ -16,8 +16,16 @@ void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { | |||
| 16 | 16 | ||
| 17 | void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { | 17 | void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { |
| 18 | gyro = gyroscope - gyro_drift; | 18 | gyro = gyroscope - gyro_drift; |
| 19 | |||
| 20 | // Auto adjust drift to minimize drift | ||
| 21 | if (!IsMoving(0.1f)) { | ||
| 22 | gyro_drift = (gyro_drift * 0.9999f) + (gyroscope * 0.0001f); | ||
| 23 | } | ||
| 24 | |||
| 19 | if (gyro.Length2() < gyro_threshold) { | 25 | if (gyro.Length2() < gyro_threshold) { |
| 20 | gyro = {}; | 26 | gyro = {}; |
| 27 | } else { | ||
| 28 | only_accelerometer = false; | ||
| 21 | } | 29 | } |
| 22 | } | 30 | } |
| 23 | 31 | ||
| @@ -50,7 +58,7 @@ bool MotionInput::IsCalibrated(f32 sensitivity) const { | |||
| 50 | } | 58 | } |
| 51 | 59 | ||
| 52 | void MotionInput::UpdateRotation(u64 elapsed_time) { | 60 | void MotionInput::UpdateRotation(u64 elapsed_time) { |
| 53 | const f32 sample_period = elapsed_time / 1000000.0f; | 61 | const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f; |
| 54 | if (sample_period > 0.1f) { | 62 | if (sample_period > 0.1f) { |
| 55 | return; | 63 | return; |
| 56 | } | 64 | } |
| @@ -66,9 +74,9 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 66 | f32 q2 = quat.xyz[0]; | 74 | f32 q2 = quat.xyz[0]; |
| 67 | f32 q3 = quat.xyz[1]; | 75 | f32 q3 = quat.xyz[1]; |
| 68 | f32 q4 = quat.xyz[2]; | 76 | f32 q4 = quat.xyz[2]; |
| 69 | const f32 sample_period = elapsed_time / 1000000.0f; | 77 | const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f; |
| 70 | 78 | ||
| 71 | // ignore invalid elapsed time | 79 | // Ignore invalid elapsed time |
| 72 | if (sample_period > 0.1f) { | 80 | if (sample_period > 0.1f) { |
| 73 | return; | 81 | return; |
| 74 | } | 82 | } |
| @@ -80,6 +88,13 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 80 | rad_gyro.y = -swap; | 88 | rad_gyro.y = -swap; |
| 81 | rad_gyro.z = -rad_gyro.z; | 89 | rad_gyro.z = -rad_gyro.z; |
| 82 | 90 | ||
| 91 | // Clear gyro values if there is no gyro present | ||
| 92 | if (only_accelerometer) { | ||
| 93 | rad_gyro.x = 0; | ||
| 94 | rad_gyro.y = 0; | ||
| 95 | rad_gyro.z = 0; | ||
| 96 | } | ||
| 97 | |||
| 83 | // Ignore drift correction if acceleration is not reliable | 98 | // Ignore drift correction if acceleration is not reliable |
| 84 | if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { | 99 | if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { |
| 85 | const f32 ax = -normal_accel.x; | 100 | const f32 ax = -normal_accel.x; |
| @@ -92,8 +107,11 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 92 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; | 107 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; |
| 93 | 108 | ||
| 94 | // Error is cross product between estimated direction and measured direction of gravity | 109 | // Error is cross product between estimated direction and measured direction of gravity |
| 95 | const Common::Vec3f new_real_error = {az * vx - ax * vz, ay * vz - az * vy, | 110 | const Common::Vec3f new_real_error = { |
| 96 | ax * vy - ay * vx}; | 111 | az * vx - ax * vz, |
| 112 | ay * vz - az * vy, | ||
| 113 | ax * vy - ay * vx, | ||
| 114 | }; | ||
| 97 | 115 | ||
| 98 | derivative_error = new_real_error - real_error; | 116 | derivative_error = new_real_error - real_error; |
| 99 | real_error = new_real_error; | 117 | real_error = new_real_error; |
| @@ -106,9 +124,22 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 106 | } | 124 | } |
| 107 | 125 | ||
| 108 | // Apply feedback terms | 126 | // Apply feedback terms |
| 109 | rad_gyro += kp * real_error; | 127 | if (!only_accelerometer) { |
| 110 | rad_gyro += ki * integral_error; | 128 | rad_gyro += kp * real_error; |
| 111 | rad_gyro += kd * derivative_error; | 129 | rad_gyro += ki * integral_error; |
| 130 | rad_gyro += kd * derivative_error; | ||
| 131 | } else { | ||
| 132 | // Give more weight to accelerometer values to compensate for the lack of gyro | ||
| 133 | rad_gyro += 35.0f * kp * real_error; | ||
| 134 | rad_gyro += 10.0f * ki * integral_error; | ||
| 135 | rad_gyro += 10.0f * kd * derivative_error; | ||
| 136 | |||
| 137 | // Emulate gyro values for games that need them | ||
| 138 | gyro.x = -rad_gyro.y; | ||
| 139 | gyro.y = rad_gyro.x; | ||
| 140 | gyro.z = -rad_gyro.z; | ||
| 141 | UpdateRotation(elapsed_time); | ||
| 142 | } | ||
| 112 | } | 143 | } |
| 113 | 144 | ||
| 114 | const f32 gx = rad_gyro.y; | 145 | const f32 gx = rad_gyro.y; |
| @@ -159,18 +190,49 @@ Common::Vec3f MotionInput::GetRotations() const { | |||
| 159 | return rotations; | 190 | return rotations; |
| 160 | } | 191 | } |
| 161 | 192 | ||
| 193 | Input::MotionStatus MotionInput::GetMotion() const { | ||
| 194 | const Common::Vec3f gyroscope = GetGyroscope(); | ||
| 195 | const Common::Vec3f accelerometer = GetAcceleration(); | ||
| 196 | const Common::Vec3f rotation = GetRotations(); | ||
| 197 | const std::array<Common::Vec3f, 3> orientation = GetOrientation(); | ||
| 198 | return {accelerometer, gyroscope, rotation, orientation}; | ||
| 199 | } | ||
| 200 | |||
| 201 | Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_magnitude) const { | ||
| 202 | std::random_device device; | ||
| 203 | std::mt19937 gen(device()); | ||
| 204 | std::uniform_int_distribution<s16> distribution(-1000, 1000); | ||
| 205 | const Common::Vec3f gyroscope{ | ||
| 206 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 207 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 208 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 209 | }; | ||
| 210 | const Common::Vec3f accelerometer{ | ||
| 211 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 212 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 213 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 214 | }; | ||
| 215 | constexpr Common::Vec3f rotation; | ||
| 216 | constexpr std::array orientation{ | ||
| 217 | Common::Vec3f{1.0f, 0.0f, 0.0f}, | ||
| 218 | Common::Vec3f{0.0f, 1.0f, 0.0f}, | ||
| 219 | Common::Vec3f{0.0f, 0.0f, 1.0f}, | ||
| 220 | }; | ||
| 221 | return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation}; | ||
| 222 | } | ||
| 223 | |||
| 162 | void MotionInput::ResetOrientation() { | 224 | void MotionInput::ResetOrientation() { |
| 163 | if (!reset_enabled) { | 225 | if (!reset_enabled || only_accelerometer) { |
| 164 | return; | 226 | return; |
| 165 | } | 227 | } |
| 166 | if (!IsMoving(0.5f) && accel.z <= -0.9f) { | 228 | if (!IsMoving(0.5f) && accel.z <= -0.9f) { |
| 167 | ++reset_counter; | 229 | ++reset_counter; |
| 168 | if (reset_counter > 900) { | 230 | if (reset_counter > 900) { |
| 169 | // TODO: calculate quaternion from gravity vector | ||
| 170 | quat.w = 0; | 231 | quat.w = 0; |
| 171 | quat.xyz[0] = 0; | 232 | quat.xyz[0] = 0; |
| 172 | quat.xyz[1] = 0; | 233 | quat.xyz[1] = 0; |
| 173 | quat.xyz[2] = -1; | 234 | quat.xyz[2] = -1; |
| 235 | SetOrientationFromAccelerometer(); | ||
| 174 | integral_error = {}; | 236 | integral_error = {}; |
| 175 | reset_counter = 0; | 237 | reset_counter = 0; |
| 176 | } | 238 | } |
| @@ -178,4 +240,62 @@ void MotionInput::ResetOrientation() { | |||
| 178 | reset_counter = 0; | 240 | reset_counter = 0; |
| 179 | } | 241 | } |
| 180 | } | 242 | } |
| 243 | |||
| 244 | void MotionInput::SetOrientationFromAccelerometer() { | ||
| 245 | int iterations = 0; | ||
| 246 | const f32 sample_period = 0.015f; | ||
| 247 | |||
| 248 | const auto normal_accel = accel.Normalized(); | ||
| 249 | |||
| 250 | while (!IsCalibrated(0.01f) && ++iterations < 100) { | ||
| 251 | // Short name local variable for readability | ||
| 252 | f32 q1 = quat.w; | ||
| 253 | f32 q2 = quat.xyz[0]; | ||
| 254 | f32 q3 = quat.xyz[1]; | ||
| 255 | f32 q4 = quat.xyz[2]; | ||
| 256 | |||
| 257 | Common::Vec3f rad_gyro; | ||
| 258 | const f32 ax = -normal_accel.x; | ||
| 259 | const f32 ay = normal_accel.y; | ||
| 260 | const f32 az = -normal_accel.z; | ||
| 261 | |||
| 262 | // Estimated direction of gravity | ||
| 263 | const f32 vx = 2.0f * (q2 * q4 - q1 * q3); | ||
| 264 | const f32 vy = 2.0f * (q1 * q2 + q3 * q4); | ||
| 265 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; | ||
| 266 | |||
| 267 | // Error is cross product between estimated direction and measured direction of gravity | ||
| 268 | const Common::Vec3f new_real_error = { | ||
| 269 | az * vx - ax * vz, | ||
| 270 | ay * vz - az * vy, | ||
| 271 | ax * vy - ay * vx, | ||
| 272 | }; | ||
| 273 | |||
| 274 | derivative_error = new_real_error - real_error; | ||
| 275 | real_error = new_real_error; | ||
| 276 | |||
| 277 | rad_gyro += 10.0f * kp * real_error; | ||
| 278 | rad_gyro += 5.0f * ki * integral_error; | ||
| 279 | rad_gyro += 10.0f * kd * derivative_error; | ||
| 280 | |||
| 281 | const f32 gx = rad_gyro.y; | ||
| 282 | const f32 gy = rad_gyro.x; | ||
| 283 | const f32 gz = rad_gyro.z; | ||
| 284 | |||
| 285 | // Integrate rate of change of quaternion | ||
| 286 | const f32 pa = q2; | ||
| 287 | const f32 pb = q3; | ||
| 288 | const f32 pc = q4; | ||
| 289 | q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period); | ||
| 290 | q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period); | ||
| 291 | q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period); | ||
| 292 | q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period); | ||
| 293 | |||
| 294 | quat.w = q1; | ||
| 295 | quat.xyz[0] = q2; | ||
| 296 | quat.xyz[1] = q3; | ||
| 297 | quat.xyz[2] = q4; | ||
| 298 | quat = quat.Normalized(); | ||
| 299 | } | ||
| 300 | } | ||
| 181 | } // namespace InputCommon | 301 | } // namespace InputCommon |