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