motion_input.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include "core/frontend/input.h"
  9. namespace InputCommon {
  10. class MotionInput {
  11. public:
  12. explicit MotionInput(f32 new_kp, f32 new_ki, f32 new_kd);
  13. MotionInput(const MotionInput&) = default;
  14. MotionInput& operator=(const MotionInput&) = default;
  15. MotionInput(MotionInput&&) = default;
  16. MotionInput& operator=(MotionInput&&) = default;
  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]] Input::MotionStatus GetMotion() const;
  32. [[nodiscard]] Input::MotionStatus GetRandomMotion(int accel_magnitude,
  33. int gyro_magnitude) const;
  34. [[nodiscard]] bool IsMoving(f32 sensitivity) const;
  35. [[nodiscard]] bool IsCalibrated(f32 sensitivity) const;
  36. private:
  37. void ResetOrientation();
  38. void SetOrientationFromAccelerometer();
  39. // PID constants
  40. f32 kp;
  41. f32 ki;
  42. f32 kd;
  43. // PID errors
  44. Common::Vec3f real_error;
  45. Common::Vec3f integral_error;
  46. Common::Vec3f derivative_error;
  47. Common::Quaternion<f32> quat{{0.0f, 0.0f, -1.0f}, 0.0f};
  48. Common::Vec3f rotations;
  49. Common::Vec3f accel;
  50. Common::Vec3f gyro;
  51. Common::Vec3f gyro_drift;
  52. f32 gyro_threshold = 0.0f;
  53. u32 reset_counter = 0;
  54. bool reset_enabled = true;
  55. bool only_accelerometer = true;
  56. };
  57. } // namespace InputCommon