diff options
Diffstat (limited to 'src/input_common/motion_input.cpp')
| -rw-r--r-- | src/input_common/motion_input.cpp | 140 |
1 files changed, 132 insertions, 8 deletions
diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp index 22a849866..e89019723 100644 --- a/src/input_common/motion_input.cpp +++ b/src/input_common/motion_input.cpp | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included | 3 | // Refer to the license.txt file included |
| 4 | 4 | ||
| 5 | #include <random> | ||
| 5 | #include "common/math_util.h" | 6 | #include "common/math_util.h" |
| 6 | #include "input_common/motion_input.h" | 7 | #include "input_common/motion_input.h" |
| 7 | 8 | ||
| @@ -16,8 +17,16 @@ void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { | |||
| 16 | 17 | ||
| 17 | void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { | 18 | void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { |
| 18 | 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 | |||
| 19 | if (gyro.Length2() < gyro_threshold) { | 26 | if (gyro.Length2() < gyro_threshold) { |
| 20 | gyro = {}; | 27 | gyro = {}; |
| 28 | } else { | ||
| 29 | only_accelerometer = false; | ||
| 21 | } | 30 | } |
| 22 | } | 31 | } |
| 23 | 32 | ||
| @@ -68,7 +77,7 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 68 | f32 q4 = quat.xyz[2]; | 77 | f32 q4 = quat.xyz[2]; |
| 69 | const f32 sample_period = elapsed_time / 1000000.0f; | 78 | const f32 sample_period = elapsed_time / 1000000.0f; |
| 70 | 79 | ||
| 71 | // ignore invalid elapsed time | 80 | // Ignore invalid elapsed time |
| 72 | if (sample_period > 0.1f) { | 81 | if (sample_period > 0.1f) { |
| 73 | return; | 82 | return; |
| 74 | } | 83 | } |
| @@ -80,6 +89,13 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 80 | rad_gyro.y = -swap; | 89 | rad_gyro.y = -swap; |
| 81 | rad_gyro.z = -rad_gyro.z; | 90 | rad_gyro.z = -rad_gyro.z; |
| 82 | 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 | |||
| 83 | // Ignore drift correction if acceleration is not reliable | 99 | // Ignore drift correction if acceleration is not reliable |
| 84 | if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { | 100 | if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { |
| 85 | const f32 ax = -normal_accel.x; | 101 | const f32 ax = -normal_accel.x; |
| @@ -92,8 +108,11 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 92 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; | 108 | const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; |
| 93 | 109 | ||
| 94 | // 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 |
| 95 | const Common::Vec3f new_real_error = {az * vx - ax * vz, ay * vz - az * vy, | 111 | const Common::Vec3f new_real_error = { |
| 96 | ax * vy - ay * vx}; | 112 | az * vx - ax * vz, |
| 113 | ay * vz - az * vy, | ||
| 114 | ax * vy - ay * vx, | ||
| 115 | }; | ||
| 97 | 116 | ||
| 98 | derivative_error = new_real_error - real_error; | 117 | derivative_error = new_real_error - real_error; |
| 99 | real_error = new_real_error; | 118 | real_error = new_real_error; |
| @@ -106,9 +125,22 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { | |||
| 106 | } | 125 | } |
| 107 | 126 | ||
| 108 | // Apply feedback terms | 127 | // Apply feedback terms |
| 109 | rad_gyro += kp * real_error; | 128 | if (!only_accelerometer) { |
| 110 | rad_gyro += ki * integral_error; | 129 | rad_gyro += kp * real_error; |
| 111 | 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 | } | ||
| 112 | } | 144 | } |
| 113 | 145 | ||
| 114 | const f32 gx = rad_gyro.y; | 146 | const f32 gx = rad_gyro.y; |
| @@ -159,18 +191,49 @@ Common::Vec3f MotionInput::GetRotations() const { | |||
| 159 | return rotations; | 191 | return rotations; |
| 160 | } | 192 | } |
| 161 | 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 | |||
| 162 | void MotionInput::ResetOrientation() { | 225 | void MotionInput::ResetOrientation() { |
| 163 | if (!reset_enabled) { | 226 | if (!reset_enabled || only_accelerometer) { |
| 164 | return; | 227 | return; |
| 165 | } | 228 | } |
| 166 | if (!IsMoving(0.5f) && accel.z <= -0.9f) { | 229 | if (!IsMoving(0.5f) && accel.z <= -0.9f) { |
| 167 | ++reset_counter; | 230 | ++reset_counter; |
| 168 | if (reset_counter > 900) { | 231 | if (reset_counter > 900) { |
| 169 | // TODO: calculate quaternion from gravity vector | ||
| 170 | quat.w = 0; | 232 | quat.w = 0; |
| 171 | quat.xyz[0] = 0; | 233 | quat.xyz[0] = 0; |
| 172 | quat.xyz[1] = 0; | 234 | quat.xyz[1] = 0; |
| 173 | quat.xyz[2] = -1; | 235 | quat.xyz[2] = -1; |
| 236 | SetOrientationFromAccelerometer(); | ||
| 174 | integral_error = {}; | 237 | integral_error = {}; |
| 175 | reset_counter = 0; | 238 | reset_counter = 0; |
| 176 | } | 239 | } |
| @@ -178,4 +241,65 @@ void MotionInput::ResetOrientation() { | |||
| 178 | reset_counter = 0; | 241 | reset_counter = 0; |
| 179 | } | 242 | } |
| 180 | } | 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 | } | ||
| 181 | } // namespace InputCommon | 305 | } // namespace InputCommon |