2020-09-02 15:58:15 +00:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included
|
|
|
|
|
2020-09-18 01:26:34 +00:00
|
|
|
#include <random>
|
2020-09-02 15:58:15 +00:00
|
|
|
#include "common/math_util.h"
|
2020-08-24 00:26:47 +00:00
|
|
|
#include "input_common/motion_input.h"
|
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
|
2020-10-14 06:51:14 +00:00
|
|
|
MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) : kp(new_kp), ki(new_ki), kd(new_kd) {}
|
2020-08-24 00:26:47 +00:00
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) {
|
2020-08-24 00:26:47 +00:00
|
|
|
accel = acceleration;
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) {
|
2020-08-24 00:26:47 +00:00
|
|
|
gyro = gyroscope - gyro_drift;
|
2020-10-04 03:22:01 +00:00
|
|
|
|
|
|
|
// Auto adjust drift to minimize drift
|
|
|
|
if (!IsMoving(0.1f)) {
|
|
|
|
gyro_drift = (gyro_drift * 0.9999f) + (gyroscope * 0.0001f);
|
|
|
|
}
|
|
|
|
|
2020-08-24 00:26:47 +00:00
|
|
|
if (gyro.Length2() < gyro_threshold) {
|
|
|
|
gyro = {};
|
2020-10-04 03:22:01 +00:00
|
|
|
} else {
|
|
|
|
only_accelerometer = false;
|
2020-08-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
void MotionInput::SetQuaternion(const Common::Quaternion<f32>& quaternion) {
|
2020-08-24 00:26:47 +00:00
|
|
|
quat = quaternion;
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
void MotionInput::SetGyroDrift(const Common::Vec3f& drift) {
|
|
|
|
gyro_drift = drift;
|
2020-08-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MotionInput::SetGyroThreshold(f32 threshold) {
|
|
|
|
gyro_threshold = threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MotionInput::EnableReset(bool reset) {
|
|
|
|
reset_enabled = reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MotionInput::ResetRotations() {
|
|
|
|
rotations = {};
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
bool MotionInput::IsMoving(f32 sensitivity) const {
|
2020-08-26 21:49:24 +00:00
|
|
|
return gyro.Length() >= sensitivity || accel.Length() <= 0.9f || accel.Length() >= 1.1f;
|
2020-08-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
bool MotionInput::IsCalibrated(f32 sensitivity) const {
|
2020-08-26 21:49:24 +00:00
|
|
|
return real_error.Length() < sensitivity;
|
2020-08-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MotionInput::UpdateRotation(u64 elapsed_time) {
|
2020-10-14 06:51:14 +00:00
|
|
|
const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
|
2020-08-26 21:49:24 +00:00
|
|
|
if (sample_period > 0.1f) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rotations += gyro * sample_period;
|
2020-08-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MotionInput::UpdateOrientation(u64 elapsed_time) {
|
2020-08-26 21:49:24 +00:00
|
|
|
if (!IsCalibrated(0.1f)) {
|
|
|
|
ResetOrientation();
|
|
|
|
}
|
2020-08-24 00:26:47 +00:00
|
|
|
// Short name local variable for readability
|
2020-08-24 01:41:59 +00:00
|
|
|
f32 q1 = quat.w;
|
|
|
|
f32 q2 = quat.xyz[0];
|
|
|
|
f32 q3 = quat.xyz[1];
|
|
|
|
f32 q4 = quat.xyz[2];
|
2020-10-14 06:51:14 +00:00
|
|
|
const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
|
2020-08-24 00:26:47 +00:00
|
|
|
|
2020-10-04 03:22:01 +00:00
|
|
|
// Ignore invalid elapsed time
|
2020-08-26 21:49:24 +00:00
|
|
|
if (sample_period > 0.1f) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
const auto normal_accel = accel.Normalized();
|
2020-09-02 15:58:15 +00:00
|
|
|
auto rad_gyro = gyro * Common::PI * 2;
|
2020-08-26 21:49:24 +00:00
|
|
|
const f32 swap = rad_gyro.x;
|
|
|
|
rad_gyro.x = rad_gyro.y;
|
|
|
|
rad_gyro.y = -swap;
|
2020-08-24 00:26:47 +00:00
|
|
|
rad_gyro.z = -rad_gyro.z;
|
|
|
|
|
2020-10-04 03:22:01 +00:00
|
|
|
// Clear gyro values if there is no gyro present
|
|
|
|
if (only_accelerometer) {
|
|
|
|
rad_gyro.x = 0;
|
|
|
|
rad_gyro.y = 0;
|
|
|
|
rad_gyro.z = 0;
|
|
|
|
}
|
|
|
|
|
2020-08-26 21:49:24 +00:00
|
|
|
// Ignore drift correction if acceleration is not reliable
|
|
|
|
if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) {
|
2020-08-24 01:41:59 +00:00
|
|
|
const f32 ax = -normal_accel.x;
|
|
|
|
const f32 ay = normal_accel.y;
|
|
|
|
const f32 az = -normal_accel.z;
|
2020-08-24 00:26:47 +00:00
|
|
|
|
|
|
|
// Estimated direction of gravity
|
2020-08-24 01:41:59 +00:00
|
|
|
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;
|
2020-08-24 00:26:47 +00:00
|
|
|
|
|
|
|
// Error is cross product between estimated direction and measured direction of gravity
|
2020-10-04 03:22:01 +00:00
|
|
|
const Common::Vec3f new_real_error = {
|
|
|
|
az * vx - ax * vz,
|
|
|
|
ay * vz - az * vy,
|
|
|
|
ax * vy - ay * vx,
|
|
|
|
};
|
2020-08-24 00:26:47 +00:00
|
|
|
|
|
|
|
derivative_error = new_real_error - real_error;
|
|
|
|
real_error = new_real_error;
|
|
|
|
|
|
|
|
// Prevent integral windup
|
2020-08-26 21:49:24 +00:00
|
|
|
if (ki != 0.0f && !IsCalibrated(0.05f)) {
|
2020-08-24 00:26:47 +00:00
|
|
|
integral_error += real_error;
|
|
|
|
} else {
|
|
|
|
integral_error = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply feedback terms
|
2020-10-04 03:22:01 +00:00
|
|
|
if (!only_accelerometer) {
|
|
|
|
rad_gyro += kp * real_error;
|
|
|
|
rad_gyro += ki * integral_error;
|
|
|
|
rad_gyro += kd * derivative_error;
|
|
|
|
} else {
|
2021-01-02 14:00:05 +00:00
|
|
|
// Give more weight to accelerometer values to compensate for the lack of gyro
|
2020-10-04 03:22:01 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-08-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
const f32 gx = rad_gyro.y;
|
|
|
|
const f32 gy = rad_gyro.x;
|
|
|
|
const f32 gz = rad_gyro.z;
|
2020-08-24 00:26:47 +00:00
|
|
|
|
|
|
|
// Integrate rate of change of quaternion
|
2020-08-24 01:41:59 +00:00
|
|
|
const f32 pa = q2;
|
|
|
|
const f32 pb = q3;
|
|
|
|
const f32 pc = q4;
|
2020-08-24 00:26:47 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const {
|
2020-08-26 21:49:24 +00:00
|
|
|
const Common::Quaternion<float> quad{
|
|
|
|
.xyz = {-quat.xyz[1], -quat.xyz[0], -quat.w},
|
|
|
|
.w = -quat.xyz[2],
|
|
|
|
};
|
2020-08-24 01:41:59 +00:00
|
|
|
const std::array<float, 16> matrix4x4 = quad.ToMatrix();
|
2020-08-24 00:26:47 +00:00
|
|
|
|
2020-08-26 21:49:24 +00:00
|
|
|
return {Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]),
|
|
|
|
Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]),
|
|
|
|
Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])};
|
2020-08-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
Common::Vec3f MotionInput::GetAcceleration() const {
|
2020-08-24 00:26:47 +00:00
|
|
|
return accel;
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
Common::Vec3f MotionInput::GetGyroscope() const {
|
2020-08-24 00:26:47 +00:00
|
|
|
return gyro;
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
Common::Quaternion<f32> MotionInput::GetQuaternion() const {
|
2020-08-24 00:26:47 +00:00
|
|
|
return quat;
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
Common::Vec3f MotionInput::GetRotations() const {
|
2020-08-24 00:26:47 +00:00
|
|
|
return rotations;
|
|
|
|
}
|
|
|
|
|
2020-09-18 01:26:34 +00:00
|
|
|
Input::MotionStatus MotionInput::GetMotion() const {
|
|
|
|
const Common::Vec3f gyroscope = GetGyroscope();
|
|
|
|
const Common::Vec3f accelerometer = GetAcceleration();
|
|
|
|
const Common::Vec3f rotation = GetRotations();
|
|
|
|
const std::array<Common::Vec3f, 3> 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<s16> distribution(-1000, 1000);
|
2020-10-14 06:51:14 +00:00
|
|
|
const Common::Vec3f gyroscope{
|
|
|
|
static_cast<f32>(distribution(gen)) * 0.001f,
|
|
|
|
static_cast<f32>(distribution(gen)) * 0.001f,
|
|
|
|
static_cast<f32>(distribution(gen)) * 0.001f,
|
2020-09-18 01:26:34 +00:00
|
|
|
};
|
2020-10-14 06:51:14 +00:00
|
|
|
const Common::Vec3f accelerometer{
|
|
|
|
static_cast<f32>(distribution(gen)) * 0.001f,
|
|
|
|
static_cast<f32>(distribution(gen)) * 0.001f,
|
|
|
|
static_cast<f32>(distribution(gen)) * 0.001f,
|
2020-09-18 01:26:34 +00:00
|
|
|
};
|
2020-10-14 06:51:14 +00:00
|
|
|
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},
|
2020-09-18 01:26:34 +00:00
|
|
|
};
|
|
|
|
return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation};
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:41:59 +00:00
|
|
|
void MotionInput::ResetOrientation() {
|
2020-10-04 23:15:53 +00:00
|
|
|
if (!reset_enabled || only_accelerometer) {
|
2020-08-24 00:26:47 +00:00
|
|
|
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;
|
2020-10-04 23:15:53 +00:00
|
|
|
SetOrientationFromAccelerometer();
|
2020-08-24 00:26:47 +00:00
|
|
|
integral_error = {};
|
|
|
|
reset_counter = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reset_counter = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 23:15:53 +00:00
|
|
|
|
2020-10-04 03:22:01 +00:00
|
|
|
void MotionInput::SetOrientationFromAccelerometer() {
|
|
|
|
int iterations = 0;
|
|
|
|
const f32 sample_period = 0.015f;
|
|
|
|
|
|
|
|
const auto normal_accel = accel.Normalized();
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
2020-10-14 06:51:14 +00:00
|
|
|
Common::Vec3f rad_gyro;
|
2020-10-04 03:22:01 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 01:41:59 +00:00
|
|
|
} // namespace InputCommon
|