motion_input.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_bias;
  21. // Auto adjust drift to minimize drift
  22. if (!IsMoving(0.1f)) {
  23. gyro_bias = (gyro_bias * 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::SetGyroBias(const Common::Vec3f& bias) {
  35. gyro_bias = bias;
  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::Vec3f MotionInput::GetGyroBias() const {
  161. return gyro_bias;
  162. }
  163. Common::Quaternion<f32> MotionInput::GetQuaternion() const {
  164. return quat;
  165. }
  166. Common::Vec3f MotionInput::GetRotations() const {
  167. return rotations;
  168. }
  169. void MotionInput::ResetOrientation() {
  170. if (!reset_enabled || only_accelerometer) {
  171. return;
  172. }
  173. if (!IsMoving(0.5f) && accel.z <= -0.9f) {
  174. ++reset_counter;
  175. if (reset_counter > 900) {
  176. quat.w = 0;
  177. quat.xyz[0] = 0;
  178. quat.xyz[1] = 0;
  179. quat.xyz[2] = -1;
  180. SetOrientationFromAccelerometer();
  181. integral_error = {};
  182. reset_counter = 0;
  183. }
  184. } else {
  185. reset_counter = 0;
  186. }
  187. }
  188. void MotionInput::SetOrientationFromAccelerometer() {
  189. int iterations = 0;
  190. const f32 sample_period = 0.015f;
  191. const auto normal_accel = accel.Normalized();
  192. while (!IsCalibrated(0.01f) && ++iterations < 100) {
  193. // Short name local variable for readability
  194. f32 q1 = quat.w;
  195. f32 q2 = quat.xyz[0];
  196. f32 q3 = quat.xyz[1];
  197. f32 q4 = quat.xyz[2];
  198. Common::Vec3f rad_gyro;
  199. const f32 ax = -normal_accel.x;
  200. const f32 ay = normal_accel.y;
  201. const f32 az = -normal_accel.z;
  202. // Estimated direction of gravity
  203. const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
  204. const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
  205. const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
  206. // Error is cross product between estimated direction and measured direction of gravity
  207. const Common::Vec3f new_real_error = {
  208. az * vx - ax * vz,
  209. ay * vz - az * vy,
  210. ax * vy - ay * vx,
  211. };
  212. derivative_error = new_real_error - real_error;
  213. real_error = new_real_error;
  214. rad_gyro += 10.0f * kp * real_error;
  215. rad_gyro += 5.0f * ki * integral_error;
  216. rad_gyro += 10.0f * kd * derivative_error;
  217. const f32 gx = rad_gyro.y;
  218. const f32 gy = rad_gyro.x;
  219. const f32 gz = rad_gyro.z;
  220. // Integrate rate of change of quaternion
  221. const f32 pa = q2;
  222. const f32 pb = q3;
  223. const f32 pc = q4;
  224. q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
  225. q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
  226. q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
  227. q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
  228. quat.w = q1;
  229. quat.xyz[0] = q2;
  230. quat.xyz[1] = q3;
  231. quat.xyz[2] = q4;
  232. quat = quat.Normalized();
  233. }
  234. }
  235. } // namespace Core::HID