diff options
Diffstat (limited to 'src/input_common/motion_input.cpp')
| -rw-r--r-- | src/input_common/motion_input.cpp | 108 |
1 files changed, 100 insertions, 8 deletions
diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp index b99d3497f..e89019723 100644 --- a/src/input_common/motion_input.cpp +++ b/src/input_common/motion_input.cpp | |||
| @@ -17,8 +17,16 @@ void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { | |||
| 17 | 17 | ||
| 18 | void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { | 18 | void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { |
| 19 | gyro = gyroscope - gyro_drift; | 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 | |||
| 20 | if (gyro.Length2() < gyro_threshold) { | 26 | if (gyro.Length2() < gyro_threshold) { |
| 21 | gyro = {}; | 27 | gyro = {}; |
| 28 | } else { | ||
| 29 | only_accelerometer = false; | ||
| 22 | } | 30 | } |
| 23 | } | 31 | } |
| 24 | 32 | ||
| @@ -69,7 +77,7 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 69 | f32 q4 = quat.xyz[2]; | 77 | f32 q4 = quat.xyz[2]; |
| 70 | const f32 sample_period = elapsed_time / 1000000.0f; | 78 | const f32 sample_period = elapsed_time / 1000000.0f; |
| 71 | 79 | ||
| 72 | // ignore invalid elapsed time | 80 | // Ignore invalid elapsed time |
| 73 | if (sample_period > 0.1f) { | 81 | if (sample_period > 0.1f) { |
| 74 | return; | 82 | return; |
| 75 | } | 83 | } |
| @@ -81,6 +89,13 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 81 | rad_gyro.y = -swap; | 89 | rad_gyro.y = -swap; |
| 82 | rad_gyro.z = -rad_gyro.z; | 90 | rad_gyro.z = -rad_gyro.z; |
| 83 | 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 | |||
| 84 | // Ignore drift correction if acceleration is not reliable | 99 | // Ignore drift correction if acceleration is not reliable |
| 85 | if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { | 100 | if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { |
| 86 | const f32 ax = -normal_accel.x; | 101 | const f32 ax = -normal_accel.x; |
| @@ -93,8 +108,11 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 93 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; | 108 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; |
| 94 | 109 | ||
| 95 | // Error is cross product between estimated direction and measured direction of gravity | 110 | // Error is cross product between estimated direction and measured direction of gravity |
| 96 | const Common::Vec3f new_real_error = {az * vx - ax * vz, ay * vz - az * vy, | 111 | const Common::Vec3f new_real_error = { |
| 97 | ax * vy - ay * vx}; | 112 | az * vx - ax * vz, |
| 113 | ay * vz - az * vy, | ||
| 114 | ax * vy - ay * vx, | ||
| 115 | }; | ||
| 98 | 116 | ||
| 99 | derivative_error = new_real_error - real_error; | 117 | derivative_error = new_real_error - real_error; |
| 100 | real_error = new_real_error; | 118 | real_error = new_real_error; |
| @@ -107,9 +125,22 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 107 | } | 125 | } |
| 108 | 126 | ||
| 109 | // Apply feedback terms | 127 | // Apply feedback terms |
| 110 | rad_gyro += kp * real_error; | 128 | if (!only_accelerometer) { |
| 111 | rad_gyro += ki * integral_error; | 129 | rad_gyro += kp * real_error; |
| 112 | rad_gyro += kd * derivative_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 | } | ||
| 113 | } | 144 | } |
| 114 | 145 | ||
| 115 | const f32 gx = rad_gyro.y; | 146 | const f32 gx = rad_gyro.y; |
| @@ -192,17 +223,17 @@ Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_m | |||
| 192 | } | 223 | } |
| 193 | 224 | ||
| 194 | void MotionInput::ResetOrientation() { | 225 | void MotionInput::ResetOrientation() { |
| 195 | if (!reset_enabled) { | 226 | if (!reset_enabled || only_accelerometer) { |
| 196 | return; | 227 | return; |
| 197 | } | 228 | } |
| 198 | if (!IsMoving(0.5f) && accel.z <= -0.9f) { | 229 | if (!IsMoving(0.5f) && accel.z <= -0.9f) { |
| 199 | ++reset_counter; | 230 | ++reset_counter; |
| 200 | if (reset_counter > 900) { | 231 | if (reset_counter > 900) { |
| 201 | // TODO: calculate quaternion from gravity vector | ||
| 202 | quat.w = 0; | 232 | quat.w = 0; |
| 203 | quat.xyz[0] = 0; | 233 | quat.xyz[0] = 0; |
| 204 | quat.xyz[1] = 0; | 234 | quat.xyz[1] = 0; |
| 205 | quat.xyz[2] = -1; | 235 | quat.xyz[2] = -1; |
| 236 | SetOrientationFromAccelerometer(); | ||
| 206 | integral_error = {}; | 237 | integral_error = {}; |
| 207 | reset_counter = 0; | 238 | reset_counter = 0; |
| 208 | } | 239 | } |
| @@ -210,4 +241,65 @@ void MotionInput::ResetOrientation() { | |||
| 210 | reset_counter = 0; | 241 | reset_counter = 0; |
| 211 | } | 242 | } |
| 212 | } | 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 | } | ||
| 213 | } // namespace InputCommon | 305 | } // namespace InputCommon |