diff options
| author | 2020-09-02 11:09:18 -0400 | |
|---|---|---|
| committer | 2020-09-02 11:09:18 -0400 | |
| commit | f64917a85222bc98cd6363a6d97b9ccb5bd54a09 (patch) | |
| tree | 24fa5a10cca1d88f97ff49b7372dfe03956c5f70 /src/input_common/motion_input.cpp | |
| parent | Merge pull request #4584 from lioncash/libusb (diff) | |
| parent | Fix orientation errors and improve drift correction (diff) | |
| download | yuzu-f64917a85222bc98cd6363a6d97b9ccb5bd54a09.tar.gz yuzu-f64917a85222bc98cd6363a6d97b9ccb5bd54a09.tar.xz yuzu-f64917a85222bc98cd6363a6d97b9ccb5bd54a09.zip | |
Merge pull request #4570 from german77/motionInput
input_common: Add a basic class for motion devices
Diffstat (limited to 'src/input_common/motion_input.cpp')
| -rw-r--r-- | src/input_common/motion_input.cpp | 176 |
1 files changed, 176 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..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 | ||