summaryrefslogtreecommitdiff
path: root/src/core/hid/motion_input.cpp
diff options
context:
space:
mode:
authorGravatar liamwhite2023-05-09 09:47:29 -0400
committerGravatar GitHub2023-05-09 09:47:29 -0400
commit5890b96ce5d17e3702805ebfc1601a45295c94d0 (patch)
treedb8ea76449236b9899e05b34f68000e4c603e64a /src/core/hid/motion_input.cpp
parentMerge pull request #10206 from FernandoS27/astc-3d (diff)
parentyuzu: Make 3d cube with joycon shape (diff)
downloadyuzu-5890b96ce5d17e3702805ebfc1601a45295c94d0.tar.gz
yuzu-5890b96ce5d17e3702805ebfc1601a45295c94d0.tar.xz
yuzu-5890b96ce5d17e3702805ebfc1601a45295c94d0.zip
Merge pull request #10203 from german77/calibration
core: hid: Allow to calibrate gyro sensor
Diffstat (limited to 'src/core/hid/motion_input.cpp')
-rw-r--r--src/core/hid/motion_input.cpp21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/core/hid/motion_input.cpp b/src/core/hid/motion_input.cpp
index b60478dbb..f56f2ae1d 100644
--- a/src/core/hid/motion_input.cpp
+++ b/src/core/hid/motion_input.cpp
@@ -37,11 +37,17 @@ void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) {
37 gyro.y = std::clamp(gyro.y, -GyroMaxValue, GyroMaxValue); 37 gyro.y = std::clamp(gyro.y, -GyroMaxValue, GyroMaxValue);
38 gyro.z = std::clamp(gyro.z, -GyroMaxValue, GyroMaxValue); 38 gyro.z = std::clamp(gyro.z, -GyroMaxValue, GyroMaxValue);
39 39
40 // Auto adjust drift to minimize drift 40 // Auto adjust gyro_bias to minimize drift
41 if (!IsMoving(IsAtRestRelaxed)) { 41 if (!IsMoving(IsAtRestRelaxed)) {
42 gyro_bias = (gyro_bias * 0.9999f) + (gyroscope * 0.0001f); 42 gyro_bias = (gyro_bias * 0.9999f) + (gyroscope * 0.0001f);
43 } 43 }
44 44
45 // Adjust drift when calibration mode is enabled
46 if (calibration_mode) {
47 gyro_bias = (gyro_bias * 0.99f) + (gyroscope * 0.01f);
48 StopCalibration();
49 }
50
45 if (gyro.Length() < gyro_threshold * user_gyro_threshold) { 51 if (gyro.Length() < gyro_threshold * user_gyro_threshold) {
46 gyro = {}; 52 gyro = {};
47 } else { 53 } else {
@@ -107,6 +113,19 @@ void MotionInput::UpdateRotation(u64 elapsed_time) {
107 rotations += gyro * sample_period; 113 rotations += gyro * sample_period;
108} 114}
109 115
116void MotionInput::Calibrate() {
117 calibration_mode = true;
118 calibration_counter = 0;
119}
120
121void MotionInput::StopCalibration() {
122 if (calibration_counter++ > CalibrationSamples) {
123 calibration_mode = false;
124 ResetQuaternion();
125 ResetRotations();
126 }
127}
128
110// Based on Madgwick's implementation of Mayhony's AHRS algorithm. 129// Based on Madgwick's implementation of Mayhony's AHRS algorithm.
111// https://github.com/xioTechnologies/Open-Source-AHRS-With-x-IMU/blob/master/x-IMU%20IMU%20and%20AHRS%20Algorithms/x-IMU%20IMU%20and%20AHRS%20Algorithms/AHRS/MahonyAHRS.cs 130// https://github.com/xioTechnologies/Open-Source-AHRS-With-x-IMU/blob/master/x-IMU%20IMU%20and%20AHRS%20Algorithms/x-IMU%20IMU%20and%20AHRS%20Algorithms/AHRS/MahonyAHRS.cs
112void MotionInput::UpdateOrientation(u64 elapsed_time) { 131void MotionInput::UpdateOrientation(u64 elapsed_time) {