motion_input.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included
  4. #pragma once
  5. #include "common/common_types.h"
  6. #include "common/quaternion.h"
  7. #include "common/vector_math.h"
  8. namespace Core::HID {
  9. class MotionInput {
  10. public:
  11. explicit MotionInput();
  12. MotionInput(const MotionInput&) = default;
  13. MotionInput& operator=(const MotionInput&) = default;
  14. MotionInput(MotionInput&&) = default;
  15. MotionInput& operator=(MotionInput&&) = default;
  16. void SetPID(f32 new_kp, f32 new_ki, f32 new_kd);
  17. void SetAcceleration(const Common::Vec3f& acceleration);
  18. void SetGyroscope(const Common::Vec3f& gyroscope);
  19. void SetQuaternion(const Common::Quaternion<f32>& quaternion);
  20. void SetGyroBias(const Common::Vec3f& bias);
  21. void SetGyroThreshold(f32 threshold);
  22. void EnableReset(bool reset);
  23. void ResetRotations();
  24. void UpdateRotation(u64 elapsed_time);
  25. void UpdateOrientation(u64 elapsed_time);
  26. [[nodiscard]] std::array<Common::Vec3f, 3> GetOrientation() const;
  27. [[nodiscard]] Common::Vec3f GetAcceleration() const;
  28. [[nodiscard]] Common::Vec3f GetGyroscope() const;
  29. [[nodiscard]] Common::Vec3f GetGyroBias() const;
  30. [[nodiscard]] Common::Vec3f GetRotations() const;
  31. [[nodiscard]] Common::Quaternion<f32> GetQuaternion() const;
  32. [[nodiscard]] bool IsMoving(f32 sensitivity) const;
  33. [[nodiscard]] bool IsCalibrated(f32 sensitivity) const;
  34. private:
  35. void ResetOrientation();
  36. void SetOrientationFromAccelerometer();
  37. // PID constants
  38. f32 kp;
  39. f32 ki;
  40. f32 kd;
  41. // PID errors
  42. Common::Vec3f real_error;
  43. Common::Vec3f integral_error;
  44. Common::Vec3f derivative_error;
  45. // Quaternion containing the device orientation
  46. Common::Quaternion<f32> quat{{0.0f, 0.0f, -1.0f}, 0.0f};
  47. // Number of full rotations in each axis
  48. Common::Vec3f rotations;
  49. // Acceleration vector measurement in G force
  50. Common::Vec3f accel;
  51. // Gyroscope vector measurement in radians/s.
  52. Common::Vec3f gyro;
  53. // Vector to be substracted from gyro measurements
  54. Common::Vec3f gyro_bias;
  55. // Minimum gyro amplitude to detect if the device is moving
  56. f32 gyro_threshold = 0.0f;
  57. // Number of invalid sequential data
  58. u32 reset_counter = 0;
  59. // If the provided data is invalid the device will be autocalibrated
  60. bool reset_enabled = true;
  61. // Use accelerometer values to calculate position
  62. bool only_accelerometer = true;
  63. };
  64. } // namespace Core::HID