motion_input.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include "common/common_types.h"
  5. #include "common/quaternion.h"
  6. #include "common/vector_math.h"
  7. namespace Core::HID {
  8. class MotionInput {
  9. public:
  10. static constexpr float ThresholdLoose = 0.01f;
  11. static constexpr float ThresholdStandard = 0.007f;
  12. static constexpr float ThresholdThight = 0.002f;
  13. static constexpr float IsAtRestRelaxed = 0.05f;
  14. static constexpr float IsAtRestLoose = 0.02f;
  15. static constexpr float IsAtRestStandard = 0.01f;
  16. static constexpr float IsAtRestThight = 0.005f;
  17. static constexpr float GyroMaxValue = 5.0f;
  18. static constexpr float AccelMaxValue = 7.0f;
  19. static constexpr std::size_t CalibrationSamples = 300;
  20. explicit MotionInput();
  21. MotionInput(const MotionInput&) = default;
  22. MotionInput& operator=(const MotionInput&) = default;
  23. MotionInput(MotionInput&&) = default;
  24. MotionInput& operator=(MotionInput&&) = default;
  25. void SetPID(f32 new_kp, f32 new_ki, f32 new_kd);
  26. void SetAcceleration(const Common::Vec3f& acceleration);
  27. void SetGyroscope(const Common::Vec3f& gyroscope);
  28. void SetQuaternion(const Common::Quaternion<f32>& quaternion);
  29. void SetEulerAngles(const Common::Vec3f& euler_angles);
  30. void SetGyroBias(const Common::Vec3f& bias);
  31. void SetGyroThreshold(f32 threshold);
  32. /// Applies a modifier on top of the normal gyro threshold
  33. void SetUserGyroThreshold(f32 threshold);
  34. void EnableReset(bool reset);
  35. void ResetRotations();
  36. void ResetQuaternion();
  37. void UpdateRotation(u64 elapsed_time);
  38. void UpdateOrientation(u64 elapsed_time);
  39. void Calibrate();
  40. [[nodiscard]] std::array<Common::Vec3f, 3> GetOrientation() const;
  41. [[nodiscard]] Common::Vec3f GetAcceleration() const;
  42. [[nodiscard]] Common::Vec3f GetGyroscope() const;
  43. [[nodiscard]] Common::Vec3f GetGyroBias() const;
  44. [[nodiscard]] Common::Vec3f GetRotations() const;
  45. [[nodiscard]] Common::Quaternion<f32> GetQuaternion() const;
  46. [[nodiscard]] Common::Vec3f GetEulerAngles() const;
  47. [[nodiscard]] bool IsMoving(f32 sensitivity) const;
  48. [[nodiscard]] bool IsCalibrated(f32 sensitivity) const;
  49. private:
  50. void StopCalibration();
  51. void ResetOrientation();
  52. void SetOrientationFromAccelerometer();
  53. // PID constants
  54. f32 kp;
  55. f32 ki;
  56. f32 kd;
  57. // PID errors
  58. Common::Vec3f real_error;
  59. Common::Vec3f integral_error;
  60. Common::Vec3f derivative_error;
  61. // Quaternion containing the device orientation
  62. Common::Quaternion<f32> quat;
  63. // Number of full rotations in each axis
  64. Common::Vec3f rotations;
  65. // Acceleration vector measurement in G force
  66. Common::Vec3f accel;
  67. // Gyroscope vector measurement in radians/s.
  68. Common::Vec3f gyro;
  69. // Vector to be subtracted from gyro measurements
  70. Common::Vec3f gyro_bias;
  71. // Minimum gyro amplitude to detect if the device is moving
  72. f32 gyro_threshold = 0.0f;
  73. // Multiplies gyro_threshold by this value
  74. f32 user_gyro_threshold = 0.0f;
  75. // Number of invalid sequential data
  76. u32 reset_counter = 0;
  77. // If the provided data is invalid the device will be autocalibrated
  78. bool reset_enabled = true;
  79. // Use accelerometer values to calculate position
  80. bool only_accelerometer = true;
  81. // When enabled it will aggressively adjust for gyro drift
  82. bool calibration_mode = false;
  83. // Used to auto disable calibration mode
  84. std::size_t calibration_counter = 0;
  85. };
  86. } // namespace Core::HID