motion_input.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 SetGyroDrift(const Common::Vec3f& drift);
  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 GetRotations() const;
  30. [[nodiscard]] Common::Quaternion<f32> GetQuaternion() const;
  31. [[nodiscard]] bool IsMoving(f32 sensitivity) const;
  32. [[nodiscard]] bool IsCalibrated(f32 sensitivity) const;
  33. private:
  34. void ResetOrientation();
  35. void SetOrientationFromAccelerometer();
  36. // PID constants
  37. f32 kp;
  38. f32 ki;
  39. f32 kd;
  40. // PID errors
  41. Common::Vec3f real_error;
  42. Common::Vec3f integral_error;
  43. Common::Vec3f derivative_error;
  44. Common::Quaternion<f32> quat{{0.0f, 0.0f, -1.0f}, 0.0f};
  45. Common::Vec3f rotations;
  46. Common::Vec3f accel;
  47. Common::Vec3f gyro;
  48. Common::Vec3f gyro_drift;
  49. f32 gyro_threshold = 0.0f;
  50. u32 reset_counter = 0;
  51. bool reset_enabled = true;
  52. bool only_accelerometer = true;
  53. };
  54. } // namespace Core::HID