diff options
| author | 2021-12-18 13:57:14 +0800 | |
|---|---|---|
| committer | 2021-12-18 13:57:14 +0800 | |
| commit | e49184e6069a9d791d2df3c1958f5c4b1187e124 (patch) | |
| tree | b776caf722e0be0e680f67b0ad0842628162ef1c /src/input_common/motion_input.cpp | |
| parent | Implement convert legacy to generic (diff) | |
| parent | Merge pull request #7570 from ameerj/favorites-expanded (diff) | |
| download | yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.tar.gz yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.tar.xz yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.zip | |
Merge branch 'yuzu-emu:master' into convert_legacy
Diffstat (limited to '')
| -rw-r--r-- | src/core/hid/motion_input.cpp (renamed from src/input_common/motion_input.cpp) | 57 |
1 files changed, 15 insertions, 42 deletions
diff --git a/src/input_common/motion_input.cpp b/src/core/hid/motion_input.cpp index 1c9d561c0..c25fea966 100644 --- a/src/input_common/motion_input.cpp +++ b/src/core/hid/motion_input.cpp | |||
| @@ -2,13 +2,21 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included | 3 | // Refer to the license.txt file included |
| 4 | 4 | ||
| 5 | #include <random> | ||
| 6 | #include "common/math_util.h" | 5 | #include "common/math_util.h" |
| 7 | #include "input_common/motion_input.h" | 6 | #include "core/hid/motion_input.h" |
| 8 | 7 | ||
| 9 | namespace InputCommon { | 8 | namespace Core::HID { |
| 10 | 9 | ||
| 11 | MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) : kp(new_kp), ki(new_ki), kd(new_kd) {} | 10 | MotionInput::MotionInput() { |
| 11 | // Initialize PID constants with default values | ||
| 12 | SetPID(0.3f, 0.005f, 0.0f); | ||
| 13 | } | ||
| 14 | |||
| 15 | void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) { | ||
| 16 | kp = new_kp; | ||
| 17 | ki = new_ki; | ||
| 18 | kd = new_kd; | ||
| 19 | } | ||
| 12 | 20 | ||
| 13 | void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { | 21 | void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { |
| 14 | accel = acceleration; | 22 | accel = acceleration; |
| @@ -65,6 +73,8 @@ void MotionInput::UpdateRotation(u64 elapsed_time) { | |||
| 65 | rotations += gyro * sample_period; | 73 | rotations += gyro * sample_period; |
| 66 | } | 74 | } |
| 67 | 75 | ||
| 76 | // Based on Madgwick's implementation of Mayhony's AHRS algorithm. | ||
| 77 | // 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 | ||
| 68 | void MotionInput::UpdateOrientation(u64 elapsed_time) { | 78 | void MotionInput::UpdateOrientation(u64 elapsed_time) { |
| 69 | if (!IsCalibrated(0.1f)) { | 79 | if (!IsCalibrated(0.1f)) { |
| 70 | ResetOrientation(); | 80 | ResetOrientation(); |
| @@ -190,43 +200,6 @@ Common::Vec3f MotionInput::GetRotations() const { | |||
| 190 | return rotations; | 200 | return rotations; |
| 191 | } | 201 | } |
| 192 | 202 | ||
| 193 | Input::MotionStatus MotionInput::GetMotion() const { | ||
| 194 | const Common::Vec3f gyroscope = GetGyroscope(); | ||
| 195 | const Common::Vec3f accelerometer = GetAcceleration(); | ||
| 196 | const Common::Vec3f rotation = GetRotations(); | ||
| 197 | const std::array<Common::Vec3f, 3> orientation = GetOrientation(); | ||
| 198 | const Common::Quaternion<f32> quaternion = GetQuaternion(); | ||
| 199 | return {accelerometer, gyroscope, rotation, orientation, quaternion}; | ||
| 200 | } | ||
| 201 | |||
| 202 | Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_magnitude) const { | ||
| 203 | std::random_device device; | ||
| 204 | std::mt19937 gen(device()); | ||
| 205 | std::uniform_int_distribution<s16> distribution(-1000, 1000); | ||
| 206 | const Common::Vec3f gyroscope{ | ||
| 207 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 208 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 209 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 210 | }; | ||
| 211 | const Common::Vec3f accelerometer{ | ||
| 212 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 213 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 214 | static_cast<f32>(distribution(gen)) * 0.001f, | ||
| 215 | }; | ||
| 216 | constexpr Common::Vec3f rotation; | ||
| 217 | constexpr std::array orientation{ | ||
| 218 | Common::Vec3f{1.0f, 0.0f, 0.0f}, | ||
| 219 | Common::Vec3f{0.0f, 1.0f, 0.0f}, | ||
| 220 | Common::Vec3f{0.0f, 0.0f, 1.0f}, | ||
| 221 | }; | ||
| 222 | constexpr Common::Quaternion<f32> quaternion{ | ||
| 223 | {0.0f, 0.0f, 0.0f}, | ||
| 224 | 1.0f, | ||
| 225 | }; | ||
| 226 | return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation, | ||
| 227 | quaternion}; | ||
| 228 | } | ||
| 229 | |||
| 230 | void MotionInput::ResetOrientation() { | 203 | void MotionInput::ResetOrientation() { |
| 231 | if (!reset_enabled || only_accelerometer) { | 204 | if (!reset_enabled || only_accelerometer) { |
| 232 | return; | 205 | return; |
| @@ -304,4 +277,4 @@ void MotionInput::SetOrientationFromAccelerometer() { | |||
| 304 | quat = quat.Normalized(); | 277 | quat = quat.Normalized(); |
| 305 | } | 278 | } |
| 306 | } | 279 | } |
| 307 | } // namespace InputCommon | 280 | } // namespace Core::HID |