diff options
Diffstat (limited to 'src/input_common/motion_input.cpp')
| -rw-r--r-- | src/input_common/motion_input.cpp | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp new file mode 100644 index 000000000..22a849866 --- /dev/null +++ b/src/input_common/motion_input.cpp | |||
| @@ -0,0 +1,181 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included | ||
| 4 | |||
| 5 | #include "common/math_util.h" | ||
| 6 | #include "input_common/motion_input.h" | ||
| 7 | |||
| 8 | namespace InputCommon { | ||
| 9 | |||
| 10 | MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) | ||
| 11 | : kp(new_kp), ki(new_ki), kd(new_kd), quat{{0, 0, -1}, 0} {} | ||
| 12 | |||
| 13 | void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { | ||
| 14 | accel = acceleration; | ||
| 15 | } | ||
| 16 | |||
| 17 | void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { | ||
| 18 | gyro = gyroscope - gyro_drift; | ||
| 19 | if (gyro.Length2() < gyro_threshold) { | ||
| 20 | gyro = {}; | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | void MotionInput::SetQuaternion(const Common::Quaternion<f32>& quaternion) { | ||
| 25 | quat = quaternion; | ||
| 26 | } | ||
| 27 | |||
| 28 | void MotionInput::SetGyroDrift(const Common::Vec3f& drift) { | ||
| 29 | gyro_drift = drift; | ||
| 30 | } | ||
| 31 | |||
| 32 | void MotionInput::SetGyroThreshold(f32 threshold) { | ||
| 33 | gyro_threshold = threshold; | ||
| 34 | } | ||
| 35 | |||
| 36 | void MotionInput::EnableReset(bool reset) { | ||
| 37 | reset_enabled = reset; | ||
| 38 | } | ||
| 39 | |||
| 40 | void MotionInput::ResetRotations() { | ||
| 41 | rotations = {}; | ||
| 42 | } | ||
| 43 | |||
| 44 | bool MotionInput::IsMoving(f32 sensitivity) const { | ||
| 45 | return gyro.Length() >= sensitivity || accel.Length() <= 0.9f || accel.Length() >= 1.1f; | ||
| 46 | } | ||
| 47 | |||
| 48 | bool MotionInput::IsCalibrated(f32 sensitivity) const { | ||
| 49 | return real_error.Length() < sensitivity; | ||
| 50 | } | ||
| 51 | |||
| 52 | void MotionInput::UpdateRotation(u64 elapsed_time) { | ||
| 53 | const f32 sample_period = elapsed_time / 1000000.0f; | ||
| 54 | if (sample_period > 0.1f) { | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | rotations += gyro * sample_period; | ||
| 58 | } | ||
| 59 | |||
| 60 | void MotionInput::UpdateOrientation(u64 elapsed_time) { | ||
| 61 | if (!IsCalibrated(0.1f)) { | ||
| 62 | ResetOrientation(); | ||
| 63 | } | ||
| 64 | // Short name local variable for readability | ||
| 65 | f32 q1 = quat.w; | ||
| 66 | f32 q2 = quat.xyz[0]; | ||
| 67 | f32 q3 = quat.xyz[1]; | ||
| 68 | f32 q4 = quat.xyz[2]; | ||
| 69 | const f32 sample_period = elapsed_time / 1000000.0f; | ||
| 70 | |||
| 71 | // ignore invalid elapsed time | ||
| 72 | if (sample_period > 0.1f) { | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | |||
| 76 | const auto normal_accel = accel.Normalized(); | ||
| 77 | auto rad_gyro = gyro * Common::PI * 2; | ||
| 78 | const f32 swap = rad_gyro.x; | ||
| 79 | rad_gyro.x = rad_gyro.y; | ||
| 80 | rad_gyro.y = -swap; | ||
| 81 | rad_gyro.z = -rad_gyro.z; | ||
| 82 | |||
| 83 | // Ignore drift correction if acceleration is not reliable | ||
| 84 | if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { | ||
| 85 | const f32 ax = -normal_accel.x; | ||
| 86 | const f32 ay = normal_accel.y; | ||
| 87 | const f32 az = -normal_accel.z; | ||
| 88 | |||
| 89 | // Estimated direction of gravity | ||
| 90 | const f32 vx = 2.0f * (q2 * q4 - q1 * q3); | ||
| 91 | const f32 vy = 2.0f * (q1 * q2 + q3 * q4); | ||
| 92 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; | ||
| 93 | |||
| 94 | // 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, | ||
| 96 | ax * vy - ay * vx}; | ||
| 97 | |||
| 98 | derivative_error = new_real_error - real_error; | ||
| 99 | real_error = new_real_error; | ||
| 100 | |||
| 101 | // Prevent integral windup | ||
| 102 | if (ki != 0.0f && !IsCalibrated(0.05f)) { | ||
| 103 | integral_error += real_error; | ||
| 104 | } else { | ||
| 105 | integral_error = {}; | ||
| 106 | } | ||
| 107 | |||
| 108 | // Apply feedback terms | ||
| 109 | rad_gyro += kp * real_error; | ||
| 110 | rad_gyro += ki * integral_error; | ||
| 111 | rad_gyro += kd * derivative_error; | ||
| 112 | } | ||
| 113 | |||
| 114 | const f32 gx = rad_gyro.y; | ||
| 115 | const f32 gy = rad_gyro.x; | ||
| 116 | const f32 gz = rad_gyro.z; | ||
| 117 | |||
| 118 | // Integrate rate of change of quaternion | ||
| 119 | const f32 pa = q2; | ||
| 120 | const f32 pb = q3; | ||
| 121 | const f32 pc = q4; | ||
| 122 | q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period); | ||
| 123 | q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period); | ||
| 124 | q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period); | ||
| 125 | q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period); | ||
| 126 | |||
| 127 | quat.w = q1; | ||
| 128 | quat.xyz[0] = q2; | ||
| 129 | quat.xyz[1] = q3; | ||
| 130 | quat.xyz[2] = q4; | ||
| 131 | quat = quat.Normalized(); | ||
| 132 | } | ||
| 133 | |||
| 134 | std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const { | ||
| 135 | const Common::Quaternion<float> quad{ | ||
| 136 | .xyz = {-quat.xyz[1], -quat.xyz[0], -quat.w}, | ||
| 137 | .w = -quat.xyz[2], | ||
| 138 | }; | ||
| 139 | const std::array<float, 16> matrix4x4 = quad.ToMatrix(); | ||
| 140 | |||
| 141 | return {Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]), | ||
| 142 | Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]), | ||
| 143 | Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])}; | ||
| 144 | } | ||
| 145 | |||
| 146 | Common::Vec3f MotionInput::GetAcceleration() const { | ||
| 147 | return accel; | ||
| 148 | } | ||
| 149 | |||
| 150 | Common::Vec3f MotionInput::GetGyroscope() const { | ||
| 151 | return gyro; | ||
| 152 | } | ||
| 153 | |||
| 154 | Common::Quaternion<f32> MotionInput::GetQuaternion() const { | ||
| 155 | return quat; | ||
| 156 | } | ||
| 157 | |||
| 158 | Common::Vec3f MotionInput::GetRotations() const { | ||
| 159 | return rotations; | ||
| 160 | } | ||
| 161 | |||
| 162 | void MotionInput::ResetOrientation() { | ||
| 163 | if (!reset_enabled) { | ||
| 164 | return; | ||
| 165 | } | ||
| 166 | if (!IsMoving(0.5f) && accel.z <= -0.9f) { | ||
| 167 | ++reset_counter; | ||
| 168 | if (reset_counter > 900) { | ||
| 169 | // TODO: calculate quaternion from gravity vector | ||
| 170 | quat.w = 0; | ||
| 171 | quat.xyz[0] = 0; | ||
| 172 | quat.xyz[1] = 0; | ||
| 173 | quat.xyz[2] = -1; | ||
| 174 | integral_error = {}; | ||
| 175 | reset_counter = 0; | ||
| 176 | } | ||
| 177 | } else { | ||
| 178 | reset_counter = 0; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } // namespace InputCommon | ||