diff options
Diffstat (limited to 'src/core/hid/motion_input.cpp')
| -rw-r--r-- | src/core/hid/motion_input.cpp | 357 |
1 files changed, 0 insertions, 357 deletions
diff --git a/src/core/hid/motion_input.cpp b/src/core/hid/motion_input.cpp deleted file mode 100644 index f56f2ae1d..000000000 --- a/src/core/hid/motion_input.cpp +++ /dev/null | |||
| @@ -1,357 +0,0 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <cmath> | ||
| 5 | |||
| 6 | #include "common/math_util.h" | ||
| 7 | #include "core/hid/motion_input.h" | ||
| 8 | |||
| 9 | namespace Core::HID { | ||
| 10 | |||
| 11 | MotionInput::MotionInput() { | ||
| 12 | // Initialize PID constants with default values | ||
| 13 | SetPID(0.3f, 0.005f, 0.0f); | ||
| 14 | SetGyroThreshold(ThresholdStandard); | ||
| 15 | ResetQuaternion(); | ||
| 16 | ResetRotations(); | ||
| 17 | } | ||
| 18 | |||
| 19 | void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) { | ||
| 20 | kp = new_kp; | ||
| 21 | ki = new_ki; | ||
| 22 | kd = new_kd; | ||
| 23 | } | ||
| 24 | |||
| 25 | void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { | ||
| 26 | accel = acceleration; | ||
| 27 | |||
| 28 | accel.x = std::clamp(accel.x, -AccelMaxValue, AccelMaxValue); | ||
| 29 | accel.y = std::clamp(accel.y, -AccelMaxValue, AccelMaxValue); | ||
| 30 | accel.z = std::clamp(accel.z, -AccelMaxValue, AccelMaxValue); | ||
| 31 | } | ||
| 32 | |||
| 33 | void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { | ||
| 34 | gyro = gyroscope - gyro_bias; | ||
| 35 | |||
| 36 | gyro.x = std::clamp(gyro.x, -GyroMaxValue, GyroMaxValue); | ||
| 37 | gyro.y = std::clamp(gyro.y, -GyroMaxValue, GyroMaxValue); | ||
| 38 | gyro.z = std::clamp(gyro.z, -GyroMaxValue, GyroMaxValue); | ||
| 39 | |||
| 40 | // Auto adjust gyro_bias to minimize drift | ||
| 41 | if (!IsMoving(IsAtRestRelaxed)) { | ||
| 42 | gyro_bias = (gyro_bias * 0.9999f) + (gyroscope * 0.0001f); | ||
| 43 | } | ||
| 44 | |||
| 45 | // Adjust drift when calibration mode is enabled | ||
| 46 | if (calibration_mode) { | ||
| 47 | gyro_bias = (gyro_bias * 0.99f) + (gyroscope * 0.01f); | ||
| 48 | StopCalibration(); | ||
| 49 | } | ||
| 50 | |||
| 51 | if (gyro.Length() < gyro_threshold * user_gyro_threshold) { | ||
| 52 | gyro = {}; | ||
| 53 | } else { | ||
| 54 | only_accelerometer = false; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | void MotionInput::SetQuaternion(const Common::Quaternion<f32>& quaternion) { | ||
| 59 | quat = quaternion; | ||
| 60 | } | ||
| 61 | |||
| 62 | void MotionInput::SetEulerAngles(const Common::Vec3f& euler_angles) { | ||
| 63 | const float cr = std::cos(euler_angles.x * 0.5f); | ||
| 64 | const float sr = std::sin(euler_angles.x * 0.5f); | ||
| 65 | const float cp = std::cos(euler_angles.y * 0.5f); | ||
| 66 | const float sp = std::sin(euler_angles.y * 0.5f); | ||
| 67 | const float cy = std::cos(euler_angles.z * 0.5f); | ||
| 68 | const float sy = std::sin(euler_angles.z * 0.5f); | ||
| 69 | |||
| 70 | quat.w = cr * cp * cy + sr * sp * sy; | ||
| 71 | quat.xyz.x = sr * cp * cy - cr * sp * sy; | ||
| 72 | quat.xyz.y = cr * sp * cy + sr * cp * sy; | ||
| 73 | quat.xyz.z = cr * cp * sy - sr * sp * cy; | ||
| 74 | } | ||
| 75 | |||
| 76 | void MotionInput::SetGyroBias(const Common::Vec3f& bias) { | ||
| 77 | gyro_bias = bias; | ||
| 78 | } | ||
| 79 | |||
| 80 | void MotionInput::SetGyroThreshold(f32 threshold) { | ||
| 81 | gyro_threshold = threshold; | ||
| 82 | } | ||
| 83 | |||
| 84 | void MotionInput::SetUserGyroThreshold(f32 threshold) { | ||
| 85 | user_gyro_threshold = threshold / ThresholdStandard; | ||
| 86 | } | ||
| 87 | |||
| 88 | void MotionInput::EnableReset(bool reset) { | ||
| 89 | reset_enabled = reset; | ||
| 90 | } | ||
| 91 | |||
| 92 | void MotionInput::ResetRotations() { | ||
| 93 | rotations = {}; | ||
| 94 | } | ||
| 95 | |||
| 96 | void MotionInput::ResetQuaternion() { | ||
| 97 | quat = {{0.0f, 0.0f, -1.0f}, 0.0f}; | ||
| 98 | } | ||
| 99 | |||
| 100 | bool MotionInput::IsMoving(f32 sensitivity) const { | ||
| 101 | return gyro.Length() >= sensitivity || accel.Length() <= 0.9f || accel.Length() >= 1.1f; | ||
| 102 | } | ||
| 103 | |||
| 104 | bool MotionInput::IsCalibrated(f32 sensitivity) const { | ||
| 105 | return real_error.Length() < sensitivity; | ||
| 106 | } | ||
| 107 | |||
| 108 | void MotionInput::UpdateRotation(u64 elapsed_time) { | ||
| 109 | const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f; | ||
| 110 | if (sample_period > 0.1f) { | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | rotations += gyro * sample_period; | ||
| 114 | } | ||
| 115 | |||
| 116 | void MotionInput::Calibrate() { | ||
| 117 | calibration_mode = true; | ||
| 118 | calibration_counter = 0; | ||
| 119 | } | ||
| 120 | |||
| 121 | void MotionInput::StopCalibration() { | ||
| 122 | if (calibration_counter++ > CalibrationSamples) { | ||
| 123 | calibration_mode = false; | ||
| 124 | ResetQuaternion(); | ||
| 125 | ResetRotations(); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | // Based on Madgwick's implementation of Mayhony's AHRS algorithm. | ||
| 130 | // https://github.com/xioTechnologies/Open-Source-AHRS-With-x-IMU/blob/master/x-IMU%20IMU%20and%20AHRS%20Algorithms/x-IMU%20IMU%20and%20AHRS%20Algorithms/AHRS/MahonyAHRS.cs | ||
| 131 | void MotionInput::UpdateOrientation(u64 elapsed_time) { | ||
| 132 | if (!IsCalibrated(0.1f)) { | ||
| 133 | ResetOrientation(); | ||
| 134 | } | ||
| 135 | // Short name local variable for readability | ||
| 136 | f32 q1 = quat.w; | ||
| 137 | f32 q2 = quat.xyz[0]; | ||
| 138 | f32 q3 = quat.xyz[1]; | ||
| 139 | f32 q4 = quat.xyz[2]; | ||
| 140 | const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f; | ||
| 141 | |||
| 142 | // Ignore invalid elapsed time | ||
| 143 | if (sample_period > 0.1f) { | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | |||
| 147 | const auto normal_accel = accel.Normalized(); | ||
| 148 | auto rad_gyro = gyro * Common::PI * 2; | ||
| 149 | const f32 swap = rad_gyro.x; | ||
| 150 | rad_gyro.x = rad_gyro.y; | ||
| 151 | rad_gyro.y = -swap; | ||
| 152 | rad_gyro.z = -rad_gyro.z; | ||
| 153 | |||
| 154 | // Clear gyro values if there is no gyro present | ||
| 155 | if (only_accelerometer) { | ||
| 156 | rad_gyro.x = 0; | ||
| 157 | rad_gyro.y = 0; | ||
| 158 | rad_gyro.z = 0; | ||
| 159 | } | ||
| 160 | |||
| 161 | // Ignore drift correction if acceleration is not reliable | ||
| 162 | if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { | ||
| 163 | const f32 ax = -normal_accel.x; | ||
| 164 | const f32 ay = normal_accel.y; | ||
| 165 | const f32 az = -normal_accel.z; | ||
| 166 | |||
| 167 | // Estimated direction of gravity | ||
| 168 | const f32 vx = 2.0f * (q2 * q4 - q1 * q3); | ||
| 169 | const f32 vy = 2.0f * (q1 * q2 + q3 * q4); | ||
| 170 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; | ||
| 171 | |||
| 172 | // Error is cross product between estimated direction and measured direction of gravity | ||
| 173 | const Common::Vec3f new_real_error = { | ||
| 174 | az * vx - ax * vz, | ||
| 175 | ay * vz - az * vy, | ||
| 176 | ax * vy - ay * vx, | ||
| 177 | }; | ||
| 178 | |||
| 179 | derivative_error = new_real_error - real_error; | ||
| 180 | real_error = new_real_error; | ||
| 181 | |||
| 182 | // Prevent integral windup | ||
| 183 | if (ki != 0.0f && !IsCalibrated(0.05f)) { | ||
| 184 | integral_error += real_error; | ||
| 185 | } else { | ||
| 186 | integral_error = {}; | ||
| 187 | } | ||
| 188 | |||
| 189 | // Apply feedback terms | ||
| 190 | if (!only_accelerometer) { | ||
| 191 | rad_gyro += kp * real_error; | ||
| 192 | rad_gyro += ki * integral_error; | ||
| 193 | rad_gyro += kd * derivative_error; | ||
| 194 | } else { | ||
| 195 | // Give more weight to accelerometer values to compensate for the lack of gyro | ||
| 196 | rad_gyro += 35.0f * kp * real_error; | ||
| 197 | rad_gyro += 10.0f * ki * integral_error; | ||
| 198 | rad_gyro += 10.0f * kd * derivative_error; | ||
| 199 | |||
| 200 | // Emulate gyro values for games that need them | ||
| 201 | gyro.x = -rad_gyro.y; | ||
| 202 | gyro.y = rad_gyro.x; | ||
| 203 | gyro.z = -rad_gyro.z; | ||
| 204 | UpdateRotation(elapsed_time); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | const f32 gx = rad_gyro.y; | ||
| 209 | const f32 gy = rad_gyro.x; | ||
| 210 | const f32 gz = rad_gyro.z; | ||
| 211 | |||
| 212 | // Integrate rate of change of quaternion | ||
| 213 | const f32 pa = q2; | ||
| 214 | const f32 pb = q3; | ||
| 215 | const f32 pc = q4; | ||
| 216 | q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period); | ||
| 217 | q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period); | ||
| 218 | q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period); | ||
| 219 | q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period); | ||
| 220 | |||
| 221 | quat.w = q1; | ||
| 222 | quat.xyz[0] = q2; | ||
| 223 | quat.xyz[1] = q3; | ||
| 224 | quat.xyz[2] = q4; | ||
| 225 | quat = quat.Normalized(); | ||
| 226 | } | ||
| 227 | |||
| 228 | std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const { | ||
| 229 | const Common::Quaternion<float> quad{ | ||
| 230 | .xyz = {-quat.xyz[1], -quat.xyz[0], -quat.w}, | ||
| 231 | .w = -quat.xyz[2], | ||
| 232 | }; | ||
| 233 | const std::array<float, 16> matrix4x4 = quad.ToMatrix(); | ||
| 234 | |||
| 235 | return {Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]), | ||
| 236 | Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]), | ||
| 237 | Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])}; | ||
| 238 | } | ||
| 239 | |||
| 240 | Common::Vec3f MotionInput::GetAcceleration() const { | ||
| 241 | return accel; | ||
| 242 | } | ||
| 243 | |||
| 244 | Common::Vec3f MotionInput::GetGyroscope() const { | ||
| 245 | return gyro; | ||
| 246 | } | ||
| 247 | |||
| 248 | Common::Vec3f MotionInput::GetGyroBias() const { | ||
| 249 | return gyro_bias; | ||
| 250 | } | ||
| 251 | |||
| 252 | Common::Quaternion<f32> MotionInput::GetQuaternion() const { | ||
| 253 | return quat; | ||
| 254 | } | ||
| 255 | |||
| 256 | Common::Vec3f MotionInput::GetRotations() const { | ||
| 257 | return rotations; | ||
| 258 | } | ||
| 259 | |||
| 260 | Common::Vec3f MotionInput::GetEulerAngles() const { | ||
| 261 | // roll (x-axis rotation) | ||
| 262 | const float sinr_cosp = 2 * (quat.w * quat.xyz.x + quat.xyz.y * quat.xyz.z); | ||
| 263 | const float cosr_cosp = 1 - 2 * (quat.xyz.x * quat.xyz.x + quat.xyz.y * quat.xyz.y); | ||
| 264 | |||
| 265 | // pitch (y-axis rotation) | ||
| 266 | const float sinp = std::sqrt(1 + 2 * (quat.w * quat.xyz.y - quat.xyz.x * quat.xyz.z)); | ||
| 267 | const float cosp = std::sqrt(1 - 2 * (quat.w * quat.xyz.y - quat.xyz.x * quat.xyz.z)); | ||
| 268 | |||
| 269 | // yaw (z-axis rotation) | ||
| 270 | const float siny_cosp = 2 * (quat.w * quat.xyz.z + quat.xyz.x * quat.xyz.y); | ||
| 271 | const float cosy_cosp = 1 - 2 * (quat.xyz.y * quat.xyz.y + quat.xyz.z * quat.xyz.z); | ||
| 272 | |||
| 273 | return { | ||
| 274 | std::atan2(sinr_cosp, cosr_cosp), | ||
| 275 | 2 * std::atan2(sinp, cosp) - Common::PI / 2, | ||
| 276 | std::atan2(siny_cosp, cosy_cosp), | ||
| 277 | }; | ||
| 278 | } | ||
| 279 | |||
| 280 | void MotionInput::ResetOrientation() { | ||
| 281 | if (!reset_enabled || only_accelerometer) { | ||
| 282 | return; | ||
| 283 | } | ||
| 284 | if (!IsMoving(IsAtRestRelaxed) && accel.z <= -0.9f) { | ||
| 285 | ++reset_counter; | ||
| 286 | if (reset_counter > 900) { | ||
| 287 | quat.w = 0; | ||
| 288 | quat.xyz[0] = 0; | ||
| 289 | quat.xyz[1] = 0; | ||
| 290 | quat.xyz[2] = -1; | ||
| 291 | SetOrientationFromAccelerometer(); | ||
| 292 | integral_error = {}; | ||
| 293 | reset_counter = 0; | ||
| 294 | } | ||
| 295 | } else { | ||
| 296 | reset_counter = 0; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | void MotionInput::SetOrientationFromAccelerometer() { | ||
| 301 | int iterations = 0; | ||
| 302 | const f32 sample_period = 0.015f; | ||
| 303 | |||
| 304 | const auto normal_accel = accel.Normalized(); | ||
| 305 | |||
| 306 | while (!IsCalibrated(0.01f) && ++iterations < 100) { | ||
| 307 | // Short name local variable for readability | ||
| 308 | f32 q1 = quat.w; | ||
| 309 | f32 q2 = quat.xyz[0]; | ||
| 310 | f32 q3 = quat.xyz[1]; | ||
| 311 | f32 q4 = quat.xyz[2]; | ||
| 312 | |||
| 313 | Common::Vec3f rad_gyro; | ||
| 314 | const f32 ax = -normal_accel.x; | ||
| 315 | const f32 ay = normal_accel.y; | ||
| 316 | const f32 az = -normal_accel.z; | ||
| 317 | |||
| 318 | // Estimated direction of gravity | ||
| 319 | const f32 vx = 2.0f * (q2 * q4 - q1 * q3); | ||
| 320 | const f32 vy = 2.0f * (q1 * q2 + q3 * q4); | ||
| 321 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; | ||
| 322 | |||
| 323 | // Error is cross product between estimated direction and measured direction of gravity | ||
| 324 | const Common::Vec3f new_real_error = { | ||
| 325 | az * vx - ax * vz, | ||
| 326 | ay * vz - az * vy, | ||
| 327 | ax * vy - ay * vx, | ||
| 328 | }; | ||
| 329 | |||
| 330 | derivative_error = new_real_error - real_error; | ||
| 331 | real_error = new_real_error; | ||
| 332 | |||
| 333 | rad_gyro += 10.0f * kp * real_error; | ||
| 334 | rad_gyro += 5.0f * ki * integral_error; | ||
| 335 | rad_gyro += 10.0f * kd * derivative_error; | ||
| 336 | |||
| 337 | const f32 gx = rad_gyro.y; | ||
| 338 | const f32 gy = rad_gyro.x; | ||
| 339 | const f32 gz = rad_gyro.z; | ||
| 340 | |||
| 341 | // Integrate rate of change of quaternion | ||
| 342 | const f32 pa = q2; | ||
| 343 | const f32 pb = q3; | ||
| 344 | const f32 pc = q4; | ||
| 345 | q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period); | ||
| 346 | q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period); | ||
| 347 | q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period); | ||
| 348 | q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period); | ||
| 349 | |||
| 350 | quat.w = q1; | ||
| 351 | quat.xyz[0] = q2; | ||
| 352 | quat.xyz[1] = q3; | ||
| 353 | quat.xyz[2] = q4; | ||
| 354 | quat = quat.Normalized(); | ||
| 355 | } | ||
| 356 | } | ||
| 357 | } // namespace Core::HID | ||