2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-09-20 21:29:43 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/quaternion.h"
|
|
|
|
#include "common/vector_math.h"
|
|
|
|
|
|
|
|
namespace Core::HID {
|
|
|
|
|
|
|
|
class MotionInput {
|
|
|
|
public:
|
2023-02-10 01:05:20 +00:00
|
|
|
static constexpr float ThresholdLoose = 0.01f;
|
|
|
|
static constexpr float ThresholdStandard = 0.007f;
|
|
|
|
static constexpr float ThresholdThight = 0.002f;
|
|
|
|
|
|
|
|
static constexpr float IsAtRestRelaxed = 0.05f;
|
|
|
|
static constexpr float IsAtRestLoose = 0.02f;
|
|
|
|
static constexpr float IsAtRestStandard = 0.01f;
|
|
|
|
static constexpr float IsAtRestThight = 0.005f;
|
|
|
|
|
2023-02-22 03:47:47 +00:00
|
|
|
static constexpr float GyroMaxValue = 5.0f;
|
|
|
|
static constexpr float AccelMaxValue = 7.0f;
|
|
|
|
|
2023-05-08 17:17:27 +00:00
|
|
|
static constexpr std::size_t CalibrationSamples = 300;
|
|
|
|
|
2021-09-20 21:29:43 +00:00
|
|
|
explicit MotionInput();
|
|
|
|
|
|
|
|
MotionInput(const MotionInput&) = default;
|
|
|
|
MotionInput& operator=(const MotionInput&) = default;
|
|
|
|
|
|
|
|
MotionInput(MotionInput&&) = default;
|
|
|
|
MotionInput& operator=(MotionInput&&) = default;
|
|
|
|
|
|
|
|
void SetPID(f32 new_kp, f32 new_ki, f32 new_kd);
|
|
|
|
void SetAcceleration(const Common::Vec3f& acceleration);
|
|
|
|
void SetGyroscope(const Common::Vec3f& gyroscope);
|
|
|
|
void SetQuaternion(const Common::Quaternion<f32>& quaternion);
|
2023-05-05 18:29:26 +00:00
|
|
|
void SetEulerAngles(const Common::Vec3f& euler_angles);
|
2021-11-29 18:40:29 +00:00
|
|
|
void SetGyroBias(const Common::Vec3f& bias);
|
2021-09-20 21:29:43 +00:00
|
|
|
void SetGyroThreshold(f32 threshold);
|
|
|
|
|
2023-02-10 01:05:20 +00:00
|
|
|
/// Applies a modifier on top of the normal gyro threshold
|
|
|
|
void SetUserGyroThreshold(f32 threshold);
|
|
|
|
|
2021-09-20 21:29:43 +00:00
|
|
|
void EnableReset(bool reset);
|
|
|
|
void ResetRotations();
|
2023-02-22 03:47:47 +00:00
|
|
|
void ResetQuaternion();
|
2021-09-20 21:29:43 +00:00
|
|
|
|
|
|
|
void UpdateRotation(u64 elapsed_time);
|
|
|
|
void UpdateOrientation(u64 elapsed_time);
|
|
|
|
|
2023-05-08 17:17:27 +00:00
|
|
|
void Calibrate();
|
|
|
|
|
2021-09-20 21:29:43 +00:00
|
|
|
[[nodiscard]] std::array<Common::Vec3f, 3> GetOrientation() const;
|
|
|
|
[[nodiscard]] Common::Vec3f GetAcceleration() const;
|
|
|
|
[[nodiscard]] Common::Vec3f GetGyroscope() const;
|
2021-11-29 18:40:29 +00:00
|
|
|
[[nodiscard]] Common::Vec3f GetGyroBias() const;
|
2021-09-20 21:29:43 +00:00
|
|
|
[[nodiscard]] Common::Vec3f GetRotations() const;
|
|
|
|
[[nodiscard]] Common::Quaternion<f32> GetQuaternion() const;
|
2023-05-05 18:29:26 +00:00
|
|
|
[[nodiscard]] Common::Vec3f GetEulerAngles() const;
|
2021-09-20 21:29:43 +00:00
|
|
|
|
|
|
|
[[nodiscard]] bool IsMoving(f32 sensitivity) const;
|
|
|
|
[[nodiscard]] bool IsCalibrated(f32 sensitivity) const;
|
|
|
|
|
|
|
|
private:
|
2023-05-08 17:17:27 +00:00
|
|
|
void StopCalibration();
|
2021-09-20 21:29:43 +00:00
|
|
|
void ResetOrientation();
|
|
|
|
void SetOrientationFromAccelerometer();
|
|
|
|
|
|
|
|
// PID constants
|
|
|
|
f32 kp;
|
|
|
|
f32 ki;
|
|
|
|
f32 kd;
|
|
|
|
|
|
|
|
// PID errors
|
|
|
|
Common::Vec3f real_error;
|
|
|
|
Common::Vec3f integral_error;
|
|
|
|
Common::Vec3f derivative_error;
|
|
|
|
|
2021-11-02 01:49:14 +00:00
|
|
|
// Quaternion containing the device orientation
|
2023-02-22 03:47:47 +00:00
|
|
|
Common::Quaternion<f32> quat;
|
2021-11-02 01:49:14 +00:00
|
|
|
|
|
|
|
// Number of full rotations in each axis
|
2021-09-20 21:29:43 +00:00
|
|
|
Common::Vec3f rotations;
|
2021-11-02 01:49:14 +00:00
|
|
|
|
|
|
|
// Acceleration vector measurement in G force
|
2021-09-20 21:29:43 +00:00
|
|
|
Common::Vec3f accel;
|
2021-11-02 01:49:14 +00:00
|
|
|
|
|
|
|
// Gyroscope vector measurement in radians/s.
|
2021-09-20 21:29:43 +00:00
|
|
|
Common::Vec3f gyro;
|
2021-11-02 01:49:14 +00:00
|
|
|
|
2023-03-12 03:10:38 +00:00
|
|
|
// Vector to be subtracted from gyro measurements
|
2021-11-29 18:40:29 +00:00
|
|
|
Common::Vec3f gyro_bias;
|
2021-09-20 21:29:43 +00:00
|
|
|
|
2021-11-02 01:49:14 +00:00
|
|
|
// Minimum gyro amplitude to detect if the device is moving
|
2021-09-20 21:29:43 +00:00
|
|
|
f32 gyro_threshold = 0.0f;
|
2021-11-02 01:49:14 +00:00
|
|
|
|
2023-02-10 01:05:20 +00:00
|
|
|
// Multiplies gyro_threshold by this value
|
|
|
|
f32 user_gyro_threshold = 0.0f;
|
|
|
|
|
2021-11-02 01:49:14 +00:00
|
|
|
// Number of invalid sequential data
|
2021-09-20 21:29:43 +00:00
|
|
|
u32 reset_counter = 0;
|
2021-11-02 01:49:14 +00:00
|
|
|
|
|
|
|
// If the provided data is invalid the device will be autocalibrated
|
2021-09-20 21:29:43 +00:00
|
|
|
bool reset_enabled = true;
|
2021-11-02 01:49:14 +00:00
|
|
|
|
|
|
|
// Use accelerometer values to calculate position
|
2021-09-20 21:29:43 +00:00
|
|
|
bool only_accelerometer = true;
|
2023-05-08 17:17:27 +00:00
|
|
|
|
|
|
|
// When enabled it will aggressively adjust for gyro drift
|
|
|
|
bool calibration_mode = false;
|
|
|
|
|
|
|
|
// Used to auto disable calibration mode
|
|
|
|
std::size_t calibration_counter = 0;
|
2021-09-20 21:29:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Core::HID
|