motion_input.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included
  4. #include "common/math_util.h"
  5. #include "input_common/motion_input.h"
  6. namespace InputCommon {
  7. MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd)
  8. : kp(new_kp), ki(new_ki), kd(new_kd), quat{{0, 0, -1}, 0} {}
  9. void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) {
  10. accel = acceleration;
  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. void MotionInput::SetQuaternion(const Common::Quaternion<f32>& quaternion) {
  19. quat = quaternion;
  20. }
  21. void MotionInput::SetGyroDrift(const Common::Vec3f& drift) {
  22. gyro_drift = drift;
  23. }
  24. void MotionInput::SetGyroThreshold(f32 threshold) {
  25. gyro_threshold = threshold;
  26. }
  27. void MotionInput::EnableReset(bool reset) {
  28. reset_enabled = reset;
  29. }
  30. void MotionInput::ResetRotations() {
  31. rotations = {};
  32. }
  33. bool MotionInput::IsMoving(f32 sensitivity) const {
  34. return gyro.Length() >= sensitivity || accel.Length() <= 0.9f || accel.Length() >= 1.1f;
  35. }
  36. bool MotionInput::IsCalibrated(f32 sensitivity) const {
  37. return real_error.Length() < sensitivity;
  38. }
  39. void MotionInput::UpdateRotation(u64 elapsed_time) {
  40. const f32 sample_period = elapsed_time / 1000000.0f;
  41. if (sample_period > 0.1f) {
  42. return;
  43. }
  44. rotations += gyro * sample_period;
  45. }
  46. void MotionInput::UpdateOrientation(u64 elapsed_time) {
  47. if (!IsCalibrated(0.1f)) {
  48. ResetOrientation();
  49. }
  50. // Short name local variable for readability
  51. f32 q1 = quat.w;
  52. f32 q2 = quat.xyz[0];
  53. f32 q3 = quat.xyz[1];
  54. f32 q4 = quat.xyz[2];
  55. const f32 sample_period = elapsed_time / 1000000.0f;
  56. // ignore invalid elapsed time
  57. if (sample_period > 0.1f) {
  58. return;
  59. }
  60. const auto normal_accel = accel.Normalized();
  61. auto rad_gyro = gyro * Common::PI * 2;
  62. const f32 swap = rad_gyro.x;
  63. rad_gyro.x = rad_gyro.y;
  64. rad_gyro.y = -swap;
  65. rad_gyro.z = -rad_gyro.z;
  66. // Ignore drift correction if acceleration is not reliable
  67. if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) {
  68. const f32 ax = -normal_accel.x;
  69. const f32 ay = normal_accel.y;
  70. const f32 az = -normal_accel.z;
  71. // Estimated direction of gravity
  72. const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
  73. const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
  74. const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
  75. // Error is cross product between estimated direction and measured direction of gravity
  76. const Common::Vec3f new_real_error = {az * vx - ax * vz, ay * vz - az * vy,
  77. ax * vy - ay * vx};
  78. derivative_error = new_real_error - real_error;
  79. real_error = new_real_error;
  80. // Prevent integral windup
  81. if (ki != 0.0f && !IsCalibrated(0.05f)) {
  82. integral_error += real_error;
  83. } else {
  84. integral_error = {};
  85. }
  86. // Apply feedback terms
  87. rad_gyro += kp * real_error;
  88. rad_gyro += ki * integral_error;
  89. rad_gyro += kd * derivative_error;
  90. }
  91. const f32 gx = rad_gyro.y;
  92. const f32 gy = rad_gyro.x;
  93. const f32 gz = rad_gyro.z;
  94. // Integrate rate of change of quaternion
  95. const f32 pa = q2;
  96. const f32 pb = q3;
  97. const f32 pc = q4;
  98. q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
  99. q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
  100. q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
  101. q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
  102. quat.w = q1;
  103. quat.xyz[0] = q2;
  104. quat.xyz[1] = q3;
  105. quat.xyz[2] = q4;
  106. quat = quat.Normalized();
  107. }
  108. std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const {
  109. const Common::Quaternion<float> quad{
  110. .xyz = {-quat.xyz[1], -quat.xyz[0], -quat.w},
  111. .w = -quat.xyz[2],
  112. };
  113. const std::array<float, 16> matrix4x4 = quad.ToMatrix();
  114. return {Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]),
  115. Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]),
  116. Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])};
  117. }
  118. Common::Vec3f MotionInput::GetAcceleration() const {
  119. return accel;
  120. }
  121. Common::Vec3f MotionInput::GetGyroscope() const {
  122. return gyro;
  123. }
  124. Common::Quaternion<f32> MotionInput::GetQuaternion() const {
  125. return quat;
  126. }
  127. Common::Vec3f MotionInput::GetRotations() const {
  128. return rotations;
  129. }
  130. void MotionInput::ResetOrientation() {
  131. if (!reset_enabled) {
  132. return;
  133. }
  134. if (!IsMoving(0.5f) && accel.z <= -0.9f) {
  135. ++reset_counter;
  136. if (reset_counter > 900) {
  137. // TODO: calculate quaternion from gravity vector
  138. quat.w = 0;
  139. quat.xyz[0] = 0;
  140. quat.xyz[1] = 0;
  141. quat.xyz[2] = -1;
  142. integral_error = {};
  143. reset_counter = 0;
  144. }
  145. } else {
  146. reset_counter = 0;
  147. }
  148. }
  149. } // namespace InputCommon