From 2d207ec609d1280bbfcdb822cceb8adc42afac3a Mon Sep 17 00:00:00 2001 From: german Date: Sun, 23 Aug 2020 19:26:47 -0500 Subject: Implement a basic class for motion devices --- src/input_common/motion_input.h | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/input_common/motion_input.h (limited to 'src/input_common/motion_input.h') diff --git a/src/input_common/motion_input.h b/src/input_common/motion_input.h new file mode 100644 index 000000000..4b8093d8c --- /dev/null +++ b/src/input_common/motion_input.h @@ -0,0 +1,62 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" +#include "common/quaternion.h" +#include "common/vector_math.h" + +namespace InputCommon { + +class MotionInput { +public: + MotionInput(f32 new_kp, f32 new_ki, f32 new_kd); + + void SetAcceleration(Common::Vec3f acceleration); + void SetGyroscope(Common::Vec3f acceleration); + void SetQuaternion(Common::Quaternion quaternion); + void SetGyroDrift(Common::Vec3f drift); + void SetGyroThreshold(f32 threshold); + + void EnableReset(bool reset); + void ResetRotations(); + + void UpdateRotation(u64 elapsed_time); + void UpdateOrientation(u64 elapsed_time); + + std::array GetOrientation(); + Common::Vec3f GetAcceleration(); + Common::Vec3f GetGyroscope(); + Common::Vec3f GetRotations(); + Common::Quaternion GetQuaternion(); + + bool IsMoving(f32 sensitivity); + bool IsCalibrated(f32 sensitivity); + + // PID constants + const f32 kp; + const f32 ki; + const f32 kd; + +private: + void resetOrientation(); + + // PID errors + Common::Vec3f real_error; + Common::Vec3f integral_error; + Common::Vec3f derivative_error; + + Common::Quaternion quat; + Common::Vec3f rotations; + Common::Vec3f accel; + Common::Vec3f gyro; + Common::Vec3f gyro_drift; + + f32 gyro_threshold; + f32 reset_counter; + bool reset_enabled; +}; + +} // namespace InputCommon -- cgit v1.2.3 From e6fc3b5662d441db7e1191a857bbb1b6ec1b06f4 Mon Sep 17 00:00:00 2001 From: german Date: Sun, 23 Aug 2020 20:41:59 -0500 Subject: Address comments --- src/input_common/motion_input.h | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'src/input_common/motion_input.h') diff --git a/src/input_common/motion_input.h b/src/input_common/motion_input.h index 4b8093d8c..d1a7a9e13 100644 --- a/src/input_common/motion_input.h +++ b/src/input_common/motion_input.h @@ -14,10 +14,16 @@ class MotionInput { public: MotionInput(f32 new_kp, f32 new_ki, f32 new_kd); - void SetAcceleration(Common::Vec3f acceleration); - void SetGyroscope(Common::Vec3f acceleration); - void SetQuaternion(Common::Quaternion quaternion); - void SetGyroDrift(Common::Vec3f drift); + MotionInput(const MotionInput&) = default; + MotionInput& operator=(const MotionInput&) = default; + + MotionInput(MotionInput&&) = default; + MotionInput& operator=(MotionInput&&) = default; + + void SetAcceleration(const Common::Vec3f& acceleration); + void SetGyroscope(const Common::Vec3f& acceleration); + void SetQuaternion(const Common::Quaternion& quaternion); + void SetGyroDrift(const Common::Vec3f& drift); void SetGyroThreshold(f32 threshold); void EnableReset(bool reset); @@ -26,23 +32,23 @@ public: void UpdateRotation(u64 elapsed_time); void UpdateOrientation(u64 elapsed_time); - std::array GetOrientation(); - Common::Vec3f GetAcceleration(); - Common::Vec3f GetGyroscope(); - Common::Vec3f GetRotations(); - Common::Quaternion GetQuaternion(); + std::array GetOrientation() const; + Common::Vec3f GetAcceleration() const; + Common::Vec3f GetGyroscope() const; + Common::Vec3f GetRotations() const; + Common::Quaternion GetQuaternion() const; + + bool IsMoving(f32 sensitivity) const; + bool IsCalibrated(f32 sensitivity) const; - bool IsMoving(f32 sensitivity); - bool IsCalibrated(f32 sensitivity); +private: + void ResetOrientation(); // PID constants const f32 kp; const f32 ki; const f32 kd; -private: - void resetOrientation(); - // PID errors Common::Vec3f real_error; Common::Vec3f integral_error; @@ -54,9 +60,9 @@ private: Common::Vec3f gyro; Common::Vec3f gyro_drift; - f32 gyro_threshold; - f32 reset_counter; - bool reset_enabled; + f32 gyro_threshold = 0.0f; + f32 reset_counter = 0.0f; + bool reset_enabled = true; }; } // namespace InputCommon -- cgit v1.2.3 From 1be18dc110e1c7193930c62542855c91179d179b Mon Sep 17 00:00:00 2001 From: german Date: Wed, 26 Aug 2020 16:49:24 -0500 Subject: Fix orientation errors and improve drift correction --- src/input_common/motion_input.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/input_common/motion_input.h') diff --git a/src/input_common/motion_input.h b/src/input_common/motion_input.h index d1a7a9e13..445798a54 100644 --- a/src/input_common/motion_input.h +++ b/src/input_common/motion_input.h @@ -61,7 +61,7 @@ private: Common::Vec3f gyro_drift; f32 gyro_threshold = 0.0f; - f32 reset_counter = 0.0f; + u32 reset_counter = 0; bool reset_enabled = true; }; -- cgit v1.2.3