motion_input.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included
  4. #include <random>
  5. #include "common/math_util.h"
  6. #include "input_common/motion_input.h"
  7. namespace InputCommon {
  8. MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) : kp(new_kp), ki(new_ki), kd(new_kd) {}
  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. // Auto adjust drift to minimize drift
  15. if (!IsMoving(0.1f)) {
  16. gyro_drift = (gyro_drift * 0.9999f) + (gyroscope * 0.0001f);
  17. }
  18. if (gyro.Length2() < gyro_threshold) {
  19. gyro = {};
  20. } else {
  21. only_accelerometer = false;
  22. }
  23. }
  24. void MotionInput::SetQuaternion(const Common::Quaternion<f32>& quaternion) {
  25. quat = quaternion;
  26. }
  27. void MotionInput::SetGyroDrift(const Common::Vec3f& drift) {
  28. gyro_drift = drift;
  29. }
  30. void MotionInput::SetGyroThreshold(f32 threshold) {
  31. gyro_threshold = threshold;
  32. }
  33. void MotionInput::EnableReset(bool reset) {
  34. reset_enabled = reset;
  35. }
  36. void MotionInput::ResetRotations() {
  37. rotations = {};
  38. }
  39. bool MotionInput::IsMoving(f32 sensitivity) const {
  40. return gyro.Length() >= sensitivity || accel.Length() <= 0.9f || accel.Length() >= 1.1f;
  41. }
  42. bool MotionInput::IsCalibrated(f32 sensitivity) const {
  43. return real_error.Length() < sensitivity;
  44. }
  45. void MotionInput::UpdateRotation(u64 elapsed_time) {
  46. const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
  47. if (sample_period > 0.1f) {
  48. return;
  49. }
  50. rotations += gyro * sample_period;
  51. }
  52. void MotionInput::UpdateOrientation(u64 elapsed_time) {
  53. if (!IsCalibrated(0.1f)) {
  54. ResetOrientation();
  55. }
  56. // Short name local variable for readability
  57. f32 q1 = quat.w;
  58. f32 q2 = quat.xyz[0];
  59. f32 q3 = quat.xyz[1];
  60. f32 q4 = quat.xyz[2];
  61. const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
  62. // Ignore invalid elapsed time
  63. if (sample_period > 0.1f) {
  64. return;
  65. }
  66. const auto normal_accel = accel.Normalized();
  67. auto rad_gyro = gyro * Common::PI * 2;
  68. const f32 swap = rad_gyro.x;
  69. rad_gyro.x = rad_gyro.y;
  70. rad_gyro.y = -swap;
  71. rad_gyro.z = -rad_gyro.z;
  72. // Clear gyro values if there is no gyro present
  73. if (only_accelerometer) {
  74. rad_gyro.x = 0;
  75. rad_gyro.y = 0;
  76. rad_gyro.z = 0;
  77. }
  78. // Ignore drift correction if acceleration is not reliable
  79. if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) {
  80. const f32 ax = -normal_accel.x;
  81. const f32 ay = normal_accel.y;
  82. const f32 az = -normal_accel.z;
  83. // Estimated direction of gravity
  84. const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
  85. const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
  86. const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
  87. // Error is cross product between estimated direction and measured direction of gravity
  88. const Common::Vec3f new_real_error = {
  89. az * vx - ax * vz,
  90. ay * vz - az * vy,
  91. ax * vy - ay * vx,
  92. };
  93. derivative_error = new_real_error - real_error;
  94. real_error = new_real_error;
  95. // Prevent integral windup
  96. if (ki != 0.0f && !IsCalibrated(0.05f)) {
  97. integral_error += real_error;
  98. } else {
  99. integral_error = {};
  100. }
  101. // Apply feedback terms
  102. if (!only_accelerometer) {
  103. rad_gyro += kp * real_error;
  104. rad_gyro += ki * integral_error;
  105. rad_gyro += kd * derivative_error;
  106. } else {
  107. // Give more weight to accelerometer values to compensate for the lack of gyro
  108. rad_gyro += 35.0f * kp * real_error;
  109. rad_gyro += 10.0f * ki * integral_error;
  110. rad_gyro += 10.0f * kd * derivative_error;
  111. // Emulate gyro values for games that need them
  112. gyro.x = -rad_gyro.y;
  113. gyro.y = rad_gyro.x;
  114. gyro.z = -rad_gyro.z;
  115. UpdateRotation(elapsed_time);
  116. }
  117. }
  118. const f32 gx = rad_gyro.y;
  119. const f32 gy = rad_gyro.x;
  120. const f32 gz = rad_gyro.z;
  121. // Integrate rate of change of quaternion
  122. const f32 pa = q2;
  123. const f32 pb = q3;
  124. const f32 pc = q4;
  125. q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
  126. q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
  127. q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
  128. q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
  129. quat.w = q1;
  130. quat.xyz[0] = q2;
  131. quat.xyz[1] = q3;
  132. quat.xyz[2] = q4;
  133. quat = quat.Normalized();
  134. }
  135. std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const {
  136. const Common::Quaternion<float> quad{
  137. .xyz = {-quat.xyz[1], -quat.xyz[0], -quat.w},
  138. .w = -quat.xyz[2],
  139. };
  140. const std::array<float, 16> matrix4x4 = quad.ToMatrix();
  141. return {Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]),
  142. Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]),
  143. Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])};
  144. }
  145. Common::Vec3f MotionInput::GetAcceleration() const {
  146. return accel;
  147. }
  148. Common::Vec3f MotionInput::GetGyroscope() const {
  149. return gyro;
  150. }
  151. Common::Quaternion<f32> MotionInput::GetQuaternion() const {
  152. return quat;
  153. }
  154. Common::Vec3f MotionInput::GetRotations() const {
  155. return rotations;
  156. }
  157. Input::MotionStatus MotionInput::GetMotion() const {
  158. const Common::Vec3f gyroscope = GetGyroscope();
  159. const Common::Vec3f accelerometer = GetAcceleration();
  160. const Common::Vec3f rotation = GetRotations();
  161. const std::array<Common::Vec3f, 3> orientation = GetOrientation();
  162. const Common::Quaternion<f32> quaternion = GetQuaternion();
  163. return {accelerometer, gyroscope, rotation, orientation, quaternion};
  164. }
  165. Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_magnitude) const {
  166. std::random_device device;
  167. std::mt19937 gen(device());
  168. std::uniform_int_distribution<s16> distribution(-1000, 1000);
  169. const Common::Vec3f gyroscope{
  170. static_cast<f32>(distribution(gen)) * 0.001f,
  171. static_cast<f32>(distribution(gen)) * 0.001f,
  172. static_cast<f32>(distribution(gen)) * 0.001f,
  173. };
  174. const Common::Vec3f accelerometer{
  175. static_cast<f32>(distribution(gen)) * 0.001f,
  176. static_cast<f32>(distribution(gen)) * 0.001f,
  177. static_cast<f32>(distribution(gen)) * 0.001f,
  178. };
  179. constexpr Common::Vec3f rotation;
  180. constexpr std::array orientation{
  181. Common::Vec3f{1.0f, 0.0f, 0.0f},
  182. Common::Vec3f{0.0f, 1.0f, 0.0f},
  183. Common::Vec3f{0.0f, 0.0f, 1.0f},
  184. };
  185. constexpr Common::Quaternion<f32> quaternion{
  186. {0.0f, 0.0f, 0.0f},
  187. 1.0f,
  188. };
  189. return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation,
  190. quaternion};
  191. }
  192. void MotionInput::ResetOrientation() {
  193. if (!reset_enabled || only_accelerometer) {
  194. return;
  195. }
  196. if (!IsMoving(0.5f) && accel.z <= -0.9f) {
  197. ++reset_counter;
  198. if (reset_counter > 900) {
  199. quat.w = 0;
  200. quat.xyz[0] = 0;
  201. quat.xyz[1] = 0;
  202. quat.xyz[2] = -1;
  203. SetOrientationFromAccelerometer();
  204. integral_error = {};
  205. reset_counter = 0;
  206. }
  207. } else {
  208. reset_counter = 0;
  209. }
  210. }
  211. void MotionInput::SetOrientationFromAccelerometer() {
  212. int iterations = 0;
  213. const f32 sample_period = 0.015f;
  214. const auto normal_accel = accel.Normalized();
  215. while (!IsCalibrated(0.01f) && ++iterations < 100) {
  216. // Short name local variable for readability
  217. f32 q1 = quat.w;
  218. f32 q2 = quat.xyz[0];
  219. f32 q3 = quat.xyz[1];
  220. f32 q4 = quat.xyz[2];
  221. Common::Vec3f rad_gyro;
  222. const f32 ax = -normal_accel.x;
  223. const f32 ay = normal_accel.y;
  224. const f32 az = -normal_accel.z;
  225. // Estimated direction of gravity
  226. const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
  227. const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
  228. const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
  229. // Error is cross product between estimated direction and measured direction of gravity
  230. const Common::Vec3f new_real_error = {
  231. az * vx - ax * vz,
  232. ay * vz - az * vy,
  233. ax * vy - ay * vx,
  234. };
  235. derivative_error = new_real_error - real_error;
  236. real_error = new_real_error;
  237. rad_gyro += 10.0f * kp * real_error;
  238. rad_gyro += 5.0f * ki * integral_error;
  239. rad_gyro += 10.0f * kd * derivative_error;
  240. const f32 gx = rad_gyro.y;
  241. const f32 gy = rad_gyro.x;
  242. const f32 gz = rad_gyro.z;
  243. // Integrate rate of change of quaternion
  244. const f32 pa = q2;
  245. const f32 pb = q3;
  246. const f32 pc = q4;
  247. q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
  248. q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
  249. q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
  250. q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
  251. quat.w = q1;
  252. quat.xyz[0] = q2;
  253. quat.xyz[1] = q3;
  254. quat.xyz[2] = q4;
  255. quat = quat.Normalized();
  256. }
  257. }
  258. } // namespace InputCommon