motion_input.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "core/hid/motion_input.h"
  6. namespace Core::HID {
  7. MotionInput::MotionInput() {
  8. // Initialize PID constants with default values
  9. SetPID(0.3f, 0.005f, 0.0f);
  10. }
  11. void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) {
  12. kp = new_kp;
  13. ki = new_ki;
  14. kd = new_kd;
  15. }
  16. void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) {
  17. accel = acceleration;
  18. }
  19. void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) {
  20. gyro = gyroscope - gyro_drift;
  21. // Auto adjust drift to minimize drift
  22. if (!IsMoving(0.1f)) {
  23. gyro_drift = (gyro_drift * 0.9999f) + (gyroscope * 0.0001f);
  24. }
  25. if (gyro.Length2() < gyro_threshold) {
  26. gyro = {};
  27. } else {
  28. only_accelerometer = false;
  29. }
  30. }
  31. void MotionInput::SetQuaternion(const Common::Quaternion<f32>& quaternion) {
  32. quat = quaternion;
  33. }
  34. void MotionInput::SetGyroDrift(const Common::Vec3f& drift) {
  35. gyro_drift = drift;
  36. }
  37. void MotionInput::SetGyroThreshold(f32 threshold) {
  38. gyro_threshold = threshold;
  39. }
  40. void MotionInput::EnableReset(bool reset) {
  41. reset_enabled = reset;
  42. }
  43. void MotionInput::ResetRotations() {
  44. rotations = {};
  45. }
  46. bool MotionInput::IsMoving(f32 sensitivity) const {
  47. return gyro.Length() >= sensitivity || accel.Length() <= 0.9f || accel.Length() >= 1.1f;
  48. }
  49. bool MotionInput::IsCalibrated(f32 sensitivity) const {
  50. return real_error.Length() < sensitivity;
  51. }
  52. void MotionInput::UpdateRotation(u64 elapsed_time) {
  53. const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
  54. if (sample_period > 0.1f) {
  55. return;
  56. }
  57. rotations += gyro * sample_period;
  58. }
  59. // Based on Madgwick's implementation of Mayhony's AHRS algorithm.
  60. // https://github.com/xioTechnologies/Open-Source-AHRS-With-x-IMU/blob/master/x-IMU%20IMU%20and%20AHRS%20Algorithms/x-IMU%20IMU%20and%20AHRS%20Algorithms/AHRS/MahonyAHRS.cs
  61. void MotionInput::UpdateOrientation(u64 elapsed_time) {
  62. if (!IsCalibrated(0.1f)) {
  63. ResetOrientation();
  64. }
  65. // Short name local variable for readability
  66. f32 q1 = quat.w;
  67. f32 q2 = quat.xyz[0];
  68. f32 q3 = quat.xyz[1];
  69. f32 q4 = quat.xyz[2];
  70. const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
  71. // Ignore invalid elapsed time
  72. if (sample_period > 0.1f) {
  73. return;
  74. }
  75. const auto normal_accel = accel.Normalized();
  76. auto rad_gyro = gyro * Common::PI * 2;
  77. const f32 swap = rad_gyro.x;
  78. rad_gyro.x = rad_gyro.y;
  79. rad_gyro.y = -swap;
  80. rad_gyro.z = -rad_gyro.z;
  81. // Clear gyro values if there is no gyro present
  82. if (only_accelerometer) {
  83. rad_gyro.x = 0;
  84. rad_gyro.y = 0;
  85. rad_gyro.z = 0;
  86. }
  87. // Ignore drift correction if acceleration is not reliable
  88. if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) {
  89. const f32 ax = -normal_accel.x;
  90. const f32 ay = normal_accel.y;
  91. const f32 az = -normal_accel.z;
  92. // Estimated direction of gravity
  93. const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
  94. const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
  95. const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
  96. // Error is cross product between estimated direction and measured direction of gravity
  97. const Common::Vec3f new_real_error = {
  98. az * vx - ax * vz,
  99. ay * vz - az * vy,
  100. ax * vy - ay * vx,
  101. };
  102. derivative_error = new_real_error - real_error;
  103. real_error = new_real_error;
  104. // Prevent integral windup
  105. if (ki != 0.0f && !IsCalibrated(0.05f)) {
  106. integral_error += real_error;
  107. } else {
  108. integral_error = {};
  109. }
  110. // Apply feedback terms
  111. if (!only_accelerometer) {
  112. rad_gyro += kp * real_error;
  113. rad_gyro += ki * integral_error;
  114. rad_gyro += kd * derivative_error;
  115. } else {
  116. // Give more weight to accelerometer values to compensate for the lack of gyro
  117. rad_gyro += 35.0f * kp * real_error;
  118. rad_gyro += 10.0f * ki * integral_error;
  119. rad_gyro += 10.0f * kd * derivative_error;
  120. // Emulate gyro values for games that need them
  121. gyro.x = -rad_gyro.y;
  122. gyro.y = rad_gyro.x;
  123. gyro.z = -rad_gyro.z;
  124. UpdateRotation(elapsed_time);
  125. }
  126. }
  127. const f32 gx = rad_gyro.y;
  128. const f32 gy = rad_gyro.x;
  129. const f32 gz = rad_gyro.z;
  130. // Integrate rate of change of quaternion
  131. const f32 pa = q2;
  132. const f32 pb = q3;
  133. const f32 pc = q4;
  134. q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
  135. q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
  136. q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
  137. q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
  138. quat.w = q1;
  139. quat.xyz[0] = q2;
  140. quat.xyz[1] = q3;
  141. quat.xyz[2] = q4;
  142. quat = quat.Normalized();
  143. }
  144. std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const {
  145. const Common::Quaternion<float> quad{
  146. .xyz = {-quat.xyz[1], -quat.xyz[0], -quat.w},
  147. .w = -quat.xyz[2],
  148. };
  149. const std::array<float, 16> matrix4x4 = quad.ToMatrix();
  150. return {Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]),
  151. Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]),
  152. Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])};
  153. }
  154. Common::Vec3f MotionInput::GetAcceleration() const {
  155. return accel;
  156. }
  157. Common::Vec3f MotionInput::GetGyroscope() const {
  158. return gyro;
  159. }
  160. Common::Quaternion<f32> MotionInput::GetQuaternion() const {
  161. return quat;
  162. }
  163. Common::Vec3f MotionInput::GetRotations() const {
  164. return rotations;
  165. }
  166. void MotionInput::ResetOrientation() {
  167. if (!reset_enabled || only_accelerometer) {
  168. return;
  169. }
  170. if (!IsMoving(0.5f) && accel.z <= -0.9f) {
  171. ++reset_counter;
  172. if (reset_counter > 900) {
  173. quat.w = 0;
  174. quat.xyz[0] = 0;
  175. quat.xyz[1] = 0;
  176. quat.xyz[2] = -1;
  177. SetOrientationFromAccelerometer();
  178. integral_error = {};
  179. reset_counter = 0;
  180. }
  181. } else {
  182. reset_counter = 0;
  183. }
  184. }
  185. void MotionInput::SetOrientationFromAccelerometer() {
  186. int iterations = 0;
  187. const f32 sample_period = 0.015f;
  188. const auto normal_accel = accel.Normalized();
  189. while (!IsCalibrated(0.01f) && ++iterations < 100) {
  190. // Short name local variable for readability
  191. f32 q1 = quat.w;
  192. f32 q2 = quat.xyz[0];
  193. f32 q3 = quat.xyz[1];
  194. f32 q4 = quat.xyz[2];
  195. Common::Vec3f rad_gyro;
  196. const f32 ax = -normal_accel.x;
  197. const f32 ay = normal_accel.y;
  198. const f32 az = -normal_accel.z;
  199. // Estimated direction of gravity
  200. const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
  201. const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
  202. const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
  203. // Error is cross product between estimated direction and measured direction of gravity
  204. const Common::Vec3f new_real_error = {
  205. az * vx - ax * vz,
  206. ay * vz - az * vy,
  207. ax * vy - ay * vx,
  208. };
  209. derivative_error = new_real_error - real_error;
  210. real_error = new_real_error;
  211. rad_gyro += 10.0f * kp * real_error;
  212. rad_gyro += 5.0f * ki * integral_error;
  213. rad_gyro += 10.0f * kd * derivative_error;
  214. const f32 gx = rad_gyro.y;
  215. const f32 gy = rad_gyro.x;
  216. const f32 gz = rad_gyro.z;
  217. // Integrate rate of change of quaternion
  218. const f32 pa = q2;
  219. const f32 pb = q3;
  220. const f32 pc = q4;
  221. q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
  222. q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
  223. q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
  224. q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
  225. quat.w = q1;
  226. quat.xyz[0] = q2;
  227. quat.xyz[1] = q3;
  228. quat.xyz[2] = q4;
  229. quat = quat.Normalized();
  230. }
  231. }
  232. } // namespace Core::HID