From 03b574ae2272fc8465e7d38f21b198fcb1885186 Mon Sep 17 00:00:00 2001 From: german Date: Thu, 17 Sep 2020 20:26:34 -0500 Subject: Add random motion input to SDL --- src/input_common/motion_input.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/input_common/motion_input.cpp') diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp index 22a849866..b99d3497f 100644 --- a/src/input_common/motion_input.cpp +++ b/src/input_common/motion_input.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included +#include #include "common/math_util.h" #include "input_common/motion_input.h" @@ -159,6 +160,37 @@ Common::Vec3f MotionInput::GetRotations() const { return rotations; } +Input::MotionStatus MotionInput::GetMotion() const { + const Common::Vec3f gyroscope = GetGyroscope(); + const Common::Vec3f accelerometer = GetAcceleration(); + const Common::Vec3f rotation = GetRotations(); + const std::array orientation = GetOrientation(); + return {accelerometer, gyroscope, rotation, orientation}; +} + +Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_magnitude) const { + std::random_device device; + std::mt19937 gen(device()); + std::uniform_int_distribution distribution(-1000, 1000); + const Common::Vec3f gyroscope = { + distribution(gen) * 0.001f, + distribution(gen) * 0.001f, + distribution(gen) * 0.001f, + }; + const Common::Vec3f accelerometer = { + distribution(gen) * 0.001f, + distribution(gen) * 0.001f, + distribution(gen) * 0.001f, + }; + const Common::Vec3f rotation = {}; + const std::array orientation = { + Common::Vec3f{1.0f, 0, 0}, + Common::Vec3f{0, 1.0f, 0}, + Common::Vec3f{0, 0, 1.0f}, + }; + return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation}; +} + void MotionInput::ResetOrientation() { if (!reset_enabled) { return; -- cgit v1.2.3 From a220d8799ed332c1d8f2231b18079b1210511bcd Mon Sep 17 00:00:00 2001 From: german Date: Sat, 3 Oct 2020 22:22:01 -0500 Subject: Add compatibility with only accelerometer and auto calibrate for drift --- src/input_common/motion_input.cpp | 112 ++++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 10 deletions(-) (limited to 'src/input_common/motion_input.cpp') diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp index 22a849866..d3e736044 100644 --- a/src/input_common/motion_input.cpp +++ b/src/input_common/motion_input.cpp @@ -16,8 +16,16 @@ void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { gyro = gyroscope - gyro_drift; + + // Auto adjust drift to minimize drift + if (!IsMoving(0.1f)) { + gyro_drift = (gyro_drift * 0.9999f) + (gyroscope * 0.0001f); + } + if (gyro.Length2() < gyro_threshold) { gyro = {}; + } else { + only_accelerometer = false; } } @@ -49,7 +57,7 @@ bool MotionInput::IsCalibrated(f32 sensitivity) const { return real_error.Length() < sensitivity; } -void MotionInput::UpdateRotation(u64 elapsed_time) { +void MotionInput::UpdateRotation(const u64 elapsed_time) { const f32 sample_period = elapsed_time / 1000000.0f; if (sample_period > 0.1f) { return; @@ -57,7 +65,7 @@ void MotionInput::UpdateRotation(u64 elapsed_time) { rotations += gyro * sample_period; } -void MotionInput::UpdateOrientation(u64 elapsed_time) { +void MotionInput::UpdateOrientation(const u64 elapsed_time) { if (!IsCalibrated(0.1f)) { ResetOrientation(); } @@ -68,7 +76,7 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { f32 q4 = quat.xyz[2]; const f32 sample_period = elapsed_time / 1000000.0f; - // ignore invalid elapsed time + // Ignore invalid elapsed time if (sample_period > 0.1f) { return; } @@ -80,6 +88,13 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { rad_gyro.y = -swap; rad_gyro.z = -rad_gyro.z; + // Clear gyro values if there is no gyro present + if (only_accelerometer) { + rad_gyro.x = 0; + rad_gyro.y = 0; + rad_gyro.z = 0; + } + // Ignore drift correction if acceleration is not reliable if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { const f32 ax = -normal_accel.x; @@ -92,8 +107,11 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; // Error is cross product between estimated direction and measured direction of gravity - const Common::Vec3f new_real_error = {az * vx - ax * vz, ay * vz - az * vy, - ax * vy - ay * vx}; + const Common::Vec3f new_real_error = { + az * vx - ax * vz, + ay * vz - az * vy, + ax * vy - ay * vx, + }; derivative_error = new_real_error - real_error; real_error = new_real_error; @@ -106,9 +124,22 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { } // Apply feedback terms - rad_gyro += kp * real_error; - rad_gyro += ki * integral_error; - rad_gyro += kd * derivative_error; + if (!only_accelerometer) { + rad_gyro += kp * real_error; + rad_gyro += ki * integral_error; + rad_gyro += kd * derivative_error; + } else { + // Give more weight to acelerometer values to compensate for the lack of gyro + rad_gyro += 35.0f * kp * real_error; + rad_gyro += 10.0f * ki * integral_error; + rad_gyro += 10.0f * kd * derivative_error; + + // Emulate gyro values for games that need them + gyro.x = -rad_gyro.y; + gyro.y = rad_gyro.x; + gyro.z = -rad_gyro.z; + UpdateRotation(elapsed_time); + } } const f32 gx = rad_gyro.y; @@ -143,6 +174,67 @@ std::array MotionInput::GetOrientation() const { Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])}; } +void MotionInput::SetOrientationFromAccelerometer() { + int iterations = 0; + const f32 sample_period = 0.015f; + + const auto normal_accel = accel.Normalized(); + const f32 ax = -normal_accel.x; + const f32 ay = normal_accel.y; + const f32 az = -normal_accel.z; + + while (!IsCalibrated(0.01f) && ++iterations < 100) { + // Short name local variable for readability + f32 q1 = quat.w; + f32 q2 = quat.xyz[0]; + f32 q3 = quat.xyz[1]; + f32 q4 = quat.xyz[2]; + + Common::Vec3f rad_gyro = {}; + const f32 ax = -normal_accel.x; + const f32 ay = normal_accel.y; + const f32 az = -normal_accel.z; + + // Estimated direction of gravity + const f32 vx = 2.0f * (q2 * q4 - q1 * q3); + const f32 vy = 2.0f * (q1 * q2 + q3 * q4); + const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; + + // Error is cross product between estimated direction and measured direction of gravity + const Common::Vec3f new_real_error = { + az * vx - ax * vz, + ay * vz - az * vy, + ax * vy - ay * vx, + }; + + derivative_error = new_real_error - real_error; + real_error = new_real_error; + + rad_gyro += 10.0f * kp * real_error; + rad_gyro += 5.0f * ki * integral_error; + rad_gyro += 10.0f * kd * derivative_error; + + const f32 gx = rad_gyro.y; + const f32 gy = rad_gyro.x; + const f32 gz = rad_gyro.z; + + // Integrate rate of change of quaternion + const f32 pa = q2; + const f32 pb = q3; + const f32 pc = q4; + q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period); + q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period); + q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period); + q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period); + + quat.w = q1; + quat.xyz[0] = q2; + quat.xyz[1] = q3; + quat.xyz[2] = q4; + quat = quat.Normalized(); + } +} + Common::Vec3f MotionInput::GetAcceleration() const { return accel; } @@ -160,17 +252,17 @@ Common::Vec3f MotionInput::GetRotations() const { } void MotionInput::ResetOrientation() { - if (!reset_enabled) { + if (!reset_enabled || only_accelerometer) { return; } if (!IsMoving(0.5f) && accel.z <= -0.9f) { ++reset_counter; if (reset_counter > 900) { - // TODO: calculate quaternion from gravity vector quat.w = 0; quat.xyz[0] = 0; quat.xyz[1] = 0; quat.xyz[2] = -1; + SetOrientationFromAccelerometer(); integral_error = {}; reset_counter = 0; } -- cgit v1.2.3 From a54aee290ff8f94d1fefc70121512dbc46f6c190 Mon Sep 17 00:00:00 2001 From: german Date: Sun, 4 Oct 2020 18:15:53 -0500 Subject: Address comments --- src/input_common/motion_input.cpp | 76 +++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 38 deletions(-) (limited to 'src/input_common/motion_input.cpp') diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp index d3e736044..182a2869a 100644 --- a/src/input_common/motion_input.cpp +++ b/src/input_common/motion_input.cpp @@ -57,7 +57,7 @@ bool MotionInput::IsCalibrated(f32 sensitivity) const { return real_error.Length() < sensitivity; } -void MotionInput::UpdateRotation(const u64 elapsed_time) { +void MotionInput::UpdateRotation(u64 elapsed_time) { const f32 sample_period = elapsed_time / 1000000.0f; if (sample_period > 0.1f) { return; @@ -65,7 +65,7 @@ void MotionInput::UpdateRotation(const u64 elapsed_time) { rotations += gyro * sample_period; } -void MotionInput::UpdateOrientation(const u64 elapsed_time) { +void MotionInput::UpdateOrientation(u64 elapsed_time) { if (!IsCalibrated(0.1f)) { ResetOrientation(); } @@ -174,6 +174,42 @@ std::array MotionInput::GetOrientation() const { Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])}; } +Common::Vec3f MotionInput::GetAcceleration() const { + return accel; +} + +Common::Vec3f MotionInput::GetGyroscope() const { + return gyro; +} + +Common::Quaternion MotionInput::GetQuaternion() const { + return quat; +} + +Common::Vec3f MotionInput::GetRotations() const { + return rotations; +} + +void MotionInput::ResetOrientation() { + if (!reset_enabled || only_accelerometer) { + return; + } + if (!IsMoving(0.5f) && accel.z <= -0.9f) { + ++reset_counter; + if (reset_counter > 900) { + quat.w = 0; + quat.xyz[0] = 0; + quat.xyz[1] = 0; + quat.xyz[2] = -1; + SetOrientationFromAccelerometer(); + integral_error = {}; + reset_counter = 0; + } + } else { + reset_counter = 0; + } +} + void MotionInput::SetOrientationFromAccelerometer() { int iterations = 0; const f32 sample_period = 0.015f; @@ -234,40 +270,4 @@ void MotionInput::SetOrientationFromAccelerometer() { quat = quat.Normalized(); } } - -Common::Vec3f MotionInput::GetAcceleration() const { - return accel; -} - -Common::Vec3f MotionInput::GetGyroscope() const { - return gyro; -} - -Common::Quaternion MotionInput::GetQuaternion() const { - return quat; -} - -Common::Vec3f MotionInput::GetRotations() const { - return rotations; -} - -void MotionInput::ResetOrientation() { - if (!reset_enabled || only_accelerometer) { - return; - } - if (!IsMoving(0.5f) && accel.z <= -0.9f) { - ++reset_counter; - if (reset_counter > 900) { - quat.w = 0; - quat.xyz[0] = 0; - quat.xyz[1] = 0; - quat.xyz[2] = -1; - SetOrientationFromAccelerometer(); - integral_error = {}; - reset_counter = 0; - } - } else { - reset_counter = 0; - } -} } // namespace InputCommon -- cgit v1.2.3 From 046c0c91a3ed665531f20955e7cfb86fe5b73213 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 14 Oct 2020 02:51:14 -0400 Subject: input_common/CMakeLists: Make some warnings errors Makes the input_common code warnings consistent with the rest of the codebase. --- src/input_common/motion_input.cpp | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) (limited to 'src/input_common/motion_input.cpp') diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp index e89019723..f77ba535d 100644 --- a/src/input_common/motion_input.cpp +++ b/src/input_common/motion_input.cpp @@ -8,8 +8,7 @@ namespace InputCommon { -MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) - : kp(new_kp), ki(new_ki), kd(new_kd), quat{{0, 0, -1}, 0} {} +MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) : kp(new_kp), ki(new_ki), kd(new_kd) {} void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { accel = acceleration; @@ -59,7 +58,7 @@ bool MotionInput::IsCalibrated(f32 sensitivity) const { } void MotionInput::UpdateRotation(u64 elapsed_time) { - const f32 sample_period = elapsed_time / 1000000.0f; + const auto sample_period = static_cast(elapsed_time) / 1000000.0f; if (sample_period > 0.1f) { return; } @@ -75,7 +74,7 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { f32 q2 = quat.xyz[0]; f32 q3 = quat.xyz[1]; f32 q4 = quat.xyz[2]; - const f32 sample_period = elapsed_time / 1000000.0f; + const auto sample_period = static_cast(elapsed_time) / 1000000.0f; // Ignore invalid elapsed time if (sample_period > 0.1f) { @@ -203,21 +202,21 @@ Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_m std::random_device device; std::mt19937 gen(device()); std::uniform_int_distribution distribution(-1000, 1000); - const Common::Vec3f gyroscope = { - distribution(gen) * 0.001f, - distribution(gen) * 0.001f, - distribution(gen) * 0.001f, + const Common::Vec3f gyroscope{ + static_cast(distribution(gen)) * 0.001f, + static_cast(distribution(gen)) * 0.001f, + static_cast(distribution(gen)) * 0.001f, }; - const Common::Vec3f accelerometer = { - distribution(gen) * 0.001f, - distribution(gen) * 0.001f, - distribution(gen) * 0.001f, + const Common::Vec3f accelerometer{ + static_cast(distribution(gen)) * 0.001f, + static_cast(distribution(gen)) * 0.001f, + static_cast(distribution(gen)) * 0.001f, }; - const Common::Vec3f rotation = {}; - const std::array orientation = { - Common::Vec3f{1.0f, 0, 0}, - Common::Vec3f{0, 1.0f, 0}, - Common::Vec3f{0, 0, 1.0f}, + constexpr Common::Vec3f rotation; + constexpr std::array orientation{ + Common::Vec3f{1.0f, 0.0f, 0.0f}, + Common::Vec3f{0.0f, 1.0f, 0.0f}, + Common::Vec3f{0.0f, 0.0f, 1.0f}, }; return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation}; } @@ -247,9 +246,6 @@ void MotionInput::SetOrientationFromAccelerometer() { const f32 sample_period = 0.015f; const auto normal_accel = accel.Normalized(); - const f32 ax = -normal_accel.x; - const f32 ay = normal_accel.y; - const f32 az = -normal_accel.z; while (!IsCalibrated(0.01f) && ++iterations < 100) { // Short name local variable for readability @@ -258,7 +254,7 @@ void MotionInput::SetOrientationFromAccelerometer() { f32 q3 = quat.xyz[1]; f32 q4 = quat.xyz[2]; - Common::Vec3f rad_gyro = {}; + Common::Vec3f rad_gyro; const f32 ax = -normal_accel.x; const f32 ay = normal_accel.y; const f32 az = -normal_accel.z; -- cgit v1.2.3 From a745d87971b2c9795e1b2c587bfe30b849b522fa Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Sat, 2 Jan 2021 09:00:05 -0500 Subject: general: Fix various spelling errors --- src/input_common/motion_input.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/input_common/motion_input.cpp') diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp index f77ba535d..6a65f175e 100644 --- a/src/input_common/motion_input.cpp +++ b/src/input_common/motion_input.cpp @@ -129,7 +129,7 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { rad_gyro += ki * integral_error; rad_gyro += kd * derivative_error; } else { - // Give more weight to acelerometer values to compensate for the lack of gyro + // Give more weight to accelerometer values to compensate for the lack of gyro rad_gyro += 35.0f * kp * real_error; rad_gyro += 10.0f * ki * integral_error; rad_gyro += 10.0f * kd * derivative_error; -- cgit v1.2.3