diff options
Diffstat (limited to 'src/input_common/motion_input.cpp')
| -rw-r--r-- | src/input_common/motion_input.cpp | 305 |
1 files changed, 305 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..e89019723 --- /dev/null +++ b/src/input_common/motion_input.cpp | |||
| @@ -0,0 +1,305 @@ | |||
| 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 <random> | ||
| 6 | #include "common/math_util.h" | ||
| 7 | #include "input_common/motion_input.h" | ||
| 8 | |||
| 9 | namespace InputCommon { | ||
| 10 | |||
| 11 | MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) | ||
| 12 | : kp(new_kp), ki(new_ki), kd(new_kd), quat{{0, 0, -1}, 0} {} | ||
| 13 | |||
| 14 | void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { | ||
| 15 | accel = acceleration; | ||
| 16 | } | ||
| 17 | |||
| 18 | void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { | ||
| 19 | gyro = gyroscope - gyro_drift; | ||
| 20 | |||
| 21 | // Auto adjust drift to minimize drift | ||
| 22 | if (!IsMoving(0.1f)) { | ||
| 23 | gyro_drift = (gyro_drift * 0.9999f) + (gyroscope * 0.0001f); | ||
| 24 | } | ||
| 25 | |||
| 26 | if (gyro.Length2() < gyro_threshold) { | ||
| 27 | gyro = {}; | ||
| 28 | } else { | ||
| 29 | only_accelerometer = false; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | void MotionInput::SetQuaternion(const Common::Quaternion<f32>& quaternion) { | ||
| 34 | quat = quaternion; | ||
| 35 | } | ||
| 36 | |||
| 37 | void MotionInput::SetGyroDrift(const Common::Vec3f& drift) { | ||
| 38 | gyro_drift = drift; | ||
| 39 | } | ||
| 40 | |||
| 41 | void MotionInput::SetGyroThreshold(f32 threshold) { | ||
| 42 | gyro_threshold = threshold; | ||
| 43 | } | ||
| 44 | |||
| 45 | void MotionInput::EnableReset(bool reset) { | ||
| 46 | reset_enabled = reset; | ||
| 47 | } | ||
| 48 | |||
| 49 | void MotionInput::ResetRotations() { | ||
| 50 | rotations = {}; | ||
| 51 | } | ||
| 52 | |||
| 53 | bool MotionInput::IsMoving(f32 sensitivity) const { | ||
| 54 | return gyro.Length() >= sensitivity || accel.Length() <= 0.9f || accel.Length() >= 1.1f; | ||
| 55 | } | ||
| 56 | |||
| 57 | bool MotionInput::IsCalibrated(f32 sensitivity) const { | ||
| 58 | return real_error.Length() < sensitivity; | ||
| 59 | } | ||
| 60 | |||
| 61 | void MotionInput::UpdateRotation(u64 elapsed_time) { | ||
| 62 | const f32 sample_period = elapsed_time / 1000000.0f; | ||
| 63 | if (sample_period > 0.1f) { | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | rotations += gyro * sample_period; | ||
| 67 | } | ||
| 68 | |||
| 69 | void MotionInput::UpdateOrientation(u64 elapsed_time) { | ||
| 70 | if (!IsCalibrated(0.1f)) { | ||
| 71 | ResetOrientation(); | ||
| 72 | } | ||
| 73 | // Short name local variable for readability | ||
| 74 | f32 q1 = quat.w; | ||
| 75 | f32 q2 = quat.xyz[0]; | ||
| 76 | f32 q3 = quat.xyz[1]; | ||
| 77 | f32 q4 = quat.xyz[2]; | ||
| 78 | const f32 sample_period = elapsed_time / 1000000.0f; | ||
| 79 | |||
| 80 | // Ignore invalid elapsed time | ||
| 81 | if (sample_period > 0.1f) { | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | const auto normal_accel = accel.Normalized(); | ||
| 86 | auto rad_gyro = gyro * Common::PI * 2; | ||
| 87 | const f32 swap = rad_gyro.x; | ||
| 88 | rad_gyro.x = rad_gyro.y; | ||
| 89 | rad_gyro.y = -swap; | ||
| 90 | rad_gyro.z = -rad_gyro.z; | ||
| 91 | |||
| 92 | // Clear gyro values if there is no gyro present | ||
| 93 | if (only_accelerometer) { | ||
| 94 | rad_gyro.x = 0; | ||
| 95 | rad_gyro.y = 0; | ||
| 96 | rad_gyro.z = 0; | ||
| 97 | } | ||
| 98 | |||
| 99 | // Ignore drift correction if acceleration is not reliable | ||
| 100 | if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { | ||
| 101 | const f32 ax = -normal_accel.x; | ||
| 102 | const f32 ay = normal_accel.y; | ||
| 103 | const f32 az = -normal_accel.z; | ||
| 104 | |||
| 105 | // Estimated direction of gravity | ||
| 106 | const f32 vx = 2.0f * (q2 * q4 - q1 * q3); | ||
| 107 | const f32 vy = 2.0f * (q1 * q2 + q3 * q4); | ||
| 108 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; | ||
| 109 | |||
| 110 | // Error is cross product between estimated direction and measured direction of gravity | ||
| 111 | const Common::Vec3f new_real_error = { | ||
| 112 | az * vx - ax * vz, | ||
| 113 | ay * vz - az * vy, | ||
| 114 | ax * vy - ay * vx, | ||
| 115 | }; | ||
| 116 | |||
| 117 | derivative_error = new_real_error - real_error; | ||
| 118 | real_error = new_real_error; | ||
| 119 | |||
| 120 | // Prevent integral windup | ||
| 121 | if (ki != 0.0f && !IsCalibrated(0.05f)) { | ||
| 122 | integral_error += real_error; | ||
| 123 | } else { | ||
| 124 | integral_error = {}; | ||
| 125 | } | ||
| 126 | |||
| 127 | // Apply feedback terms | ||
| 128 | if (!only_accelerometer) { | ||
| 129 | rad_gyro += kp * real_error; | ||
| 130 | rad_gyro += ki * integral_error; | ||
| 131 | rad_gyro += kd * derivative_error; | ||
| 132 | } else { | ||
| 133 | // Give more weight to acelerometer values to compensate for the lack of gyro | ||
| 134 | rad_gyro += 35.0f * kp * real_error; | ||
| 135 | rad_gyro += 10.0f * ki * integral_error; | ||
| 136 | rad_gyro += 10.0f * kd * derivative_error; | ||
| 137 | |||
| 138 | // Emulate gyro values for games that need them | ||
| 139 | gyro.x = -rad_gyro.y; | ||
| 140 | gyro.y = rad_gyro.x; | ||
| 141 | gyro.z = -rad_gyro.z; | ||
| 142 | UpdateRotation(elapsed_time); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | const f32 gx = rad_gyro.y; | ||
| 147 | const f32 gy = rad_gyro.x; | ||
| 148 | const f32 gz = rad_gyro.z; | ||
| 149 | |||
| 150 | // Integrate rate of change of quaternion | ||
| 151 | const f32 pa = q2; | ||
| 152 | const f32 pb = q3; | ||
| 153 | const f32 pc = q4; | ||
| 154 | q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period); | ||
| 155 | q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period); | ||
| 156 | q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period); | ||
| 157 | q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period); | ||
| 158 | |||
| 159 | quat.w = q1; | ||
| 160 | quat.xyz[0] = q2; | ||
| 161 | quat.xyz[1] = q3; | ||
| 162 | quat.xyz[2] = q4; | ||
| 163 | quat = quat.Normalized(); | ||
| 164 | } | ||
| 165 | |||
| 166 | std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const { | ||
| 167 | const Common::Quaternion<float> quad{ | ||
| 168 | .xyz = {-quat.xyz[1], -quat.xyz[0], -quat.w}, | ||
| 169 | .w = -quat.xyz[2], | ||
| 170 | }; | ||
| 171 | const std::array<float, 16> matrix4x4 = quad.ToMatrix(); | ||
| 172 | |||
| 173 | return {Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]), | ||
| 174 | Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]), | ||
| 175 | Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])}; | ||
| 176 | } | ||
| 177 | |||
| 178 | Common::Vec3f MotionInput::GetAcceleration() const { | ||
| 179 | return accel; | ||
| 180 | } | ||
| 181 | |||
| 182 | Common::Vec3f MotionInput::GetGyroscope() const { | ||
| 183 | return gyro; | ||
| 184 | } | ||
| 185 | |||
| 186 | Common::Quaternion<f32> MotionInput::GetQuaternion() const { | ||
| 187 | return quat; | ||
| 188 | } | ||
| 189 | |||
| 190 | Common::Vec3f MotionInput::GetRotations() const { | ||
| 191 | return rotations; | ||
| 192 | } | ||
| 193 | |||
| 194 | Input::MotionStatus MotionInput::GetMotion() const { | ||
| 195 | const Common::Vec3f gyroscope = GetGyroscope(); | ||
| 196 | const Common::Vec3f accelerometer = GetAcceleration(); | ||
| 197 | const Common::Vec3f rotation = GetRotations(); | ||
| 198 | const std::array<Common::Vec3f, 3> orientation = GetOrientation(); | ||
| 199 | return {accelerometer, gyroscope, rotation, orientation}; | ||
| 200 | } | ||
| 201 | |||
| 202 | Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_magnitude) const { | ||
| 203 | std::random_device device; | ||
| 204 | std::mt19937 gen(device()); | ||
| 205 | std::uniform_int_distribution<s16> distribution(-1000, 1000); | ||
| 206 | const Common::Vec3f gyroscope = { | ||
| 207 | distribution(gen) * 0.001f, | ||
| 208 | distribution(gen) * 0.001f, | ||
| 209 | distribution(gen) * 0.001f, | ||
| 210 | }; | ||
| 211 | const Common::Vec3f accelerometer = { | ||
| 212 | distribution(gen) * 0.001f, | ||
| 213 | distribution(gen) * 0.001f, | ||
| 214 | distribution(gen) * 0.001f, | ||
| 215 | }; | ||
| 216 | const Common::Vec3f rotation = {}; | ||
| 217 | const std::array<Common::Vec3f, 3> orientation = { | ||
| 218 | Common::Vec3f{1.0f, 0, 0}, | ||
| 219 | Common::Vec3f{0, 1.0f, 0}, | ||
| 220 | Common::Vec3f{0, 0, 1.0f}, | ||
| 221 | }; | ||
| 222 | return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation}; | ||
| 223 | } | ||
| 224 | |||
| 225 | void MotionInput::ResetOrientation() { | ||
| 226 | if (!reset_enabled || only_accelerometer) { | ||
| 227 | return; | ||
| 228 | } | ||
| 229 | if (!IsMoving(0.5f) && accel.z <= -0.9f) { | ||
| 230 | ++reset_counter; | ||
| 231 | if (reset_counter > 900) { | ||
| 232 | quat.w = 0; | ||
| 233 | quat.xyz[0] = 0; | ||
| 234 | quat.xyz[1] = 0; | ||
| 235 | quat.xyz[2] = -1; | ||
| 236 | SetOrientationFromAccelerometer(); | ||
| 237 | integral_error = {}; | ||
| 238 | reset_counter = 0; | ||
| 239 | } | ||
| 240 | } else { | ||
| 241 | reset_counter = 0; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | void MotionInput::SetOrientationFromAccelerometer() { | ||
| 246 | int iterations = 0; | ||
| 247 | const f32 sample_period = 0.015f; | ||
| 248 | |||
| 249 | const auto normal_accel = accel.Normalized(); | ||
| 250 | const f32 ax = -normal_accel.x; | ||
| 251 | const f32 ay = normal_accel.y; | ||
| 252 | const f32 az = -normal_accel.z; | ||
| 253 | |||
| 254 | while (!IsCalibrated(0.01f) && ++iterations < 100) { | ||
| 255 | // Short name local variable for readability | ||
| 256 | f32 q1 = quat.w; | ||
| 257 | f32 q2 = quat.xyz[0]; | ||
| 258 | f32 q3 = quat.xyz[1]; | ||
| 259 | f32 q4 = quat.xyz[2]; | ||
| 260 | |||
| 261 | Common::Vec3f rad_gyro = {}; | ||
| 262 | const f32 ax = -normal_accel.x; | ||
| 263 | const f32 ay = normal_accel.y; | ||
| 264 | const f32 az = -normal_accel.z; | ||
| 265 | |||
| 266 | // Estimated direction of gravity | ||
| 267 | const f32 vx = 2.0f * (q2 * q4 - q1 * q3); | ||
| 268 | const f32 vy = 2.0f * (q1 * q2 + q3 * q4); | ||
| 269 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; | ||
| 270 | |||
| 271 | // Error is cross product between estimated direction and measured direction of gravity | ||
| 272 | const Common::Vec3f new_real_error = { | ||
| 273 | az * vx - ax * vz, | ||
| 274 | ay * vz - az * vy, | ||
| 275 | ax * vy - ay * vx, | ||
| 276 | }; | ||
| 277 | |||
| 278 | derivative_error = new_real_error - real_error; | ||
| 279 | real_error = new_real_error; | ||
| 280 | |||
| 281 | rad_gyro += 10.0f * kp * real_error; | ||
| 282 | rad_gyro += 5.0f * ki * integral_error; | ||
| 283 | rad_gyro += 10.0f * kd * derivative_error; | ||
| 284 | |||
| 285 | const f32 gx = rad_gyro.y; | ||
| 286 | const f32 gy = rad_gyro.x; | ||
| 287 | const f32 gz = rad_gyro.z; | ||
| 288 | |||
| 289 | // Integrate rate of change of quaternion | ||
| 290 | const f32 pa = q2; | ||
| 291 | const f32 pb = q3; | ||
| 292 | const f32 pc = q4; | ||
| 293 | q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period); | ||
| 294 | q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period); | ||
| 295 | q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period); | ||
| 296 | q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period); | ||
| 297 | |||
| 298 | quat.w = q1; | ||
| 299 | quat.xyz[0] = q2; | ||
| 300 | quat.xyz[1] = q3; | ||
| 301 | quat.xyz[2] = q4; | ||
| 302 | quat = quat.Normalized(); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | } // namespace InputCommon | ||