aboutsummaryrefslogtreecommitdiff
path: root/src/input_common/motion_input.h
blob: abb957f04bd0de60184b1ba84112d4996e9941e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included

#pragma once

#include "common/common_types.h"
#include "common/quaternion.h"
#include "common/vector_math.h"
#include "core/frontend/input.h"

namespace InputCommon {

class MotionInput {
public:
    MotionInput(f32 new_kp, f32 new_ki, f32 new_kd);

    MotionInput(const MotionInput&) = default;
    MotionInput& operator=(const MotionInput&) = default;

    MotionInput(MotionInput&&) = default;
    MotionInput& operator=(MotionInput&&) = default;

    void SetAcceleration(const Common::Vec3f& acceleration);
    void SetGyroscope(const Common::Vec3f& gyroscope);
    void SetQuaternion(const Common::Quaternion<f32>& quaternion);
    void SetGyroDrift(const Common::Vec3f& drift);
    void SetGyroThreshold(f32 threshold);

    void EnableReset(bool reset);
    void ResetRotations();

    void UpdateRotation(u64 elapsed_time);
    void UpdateOrientation(u64 elapsed_time);

    std::array<Common::Vec3f, 3> GetOrientation() const;
    Common::Vec3f GetAcceleration() const;
    Common::Vec3f GetGyroscope() const;
    Common::Vec3f GetRotations() const;
    Common::Quaternion<f32> GetQuaternion() const;
    Input::MotionStatus GetMotion() const;
    Input::MotionStatus GetRandomMotion(int accel_magnitude, int gyro_magnitude) const;

    bool IsMoving(f32 sensitivity) const;
    bool IsCalibrated(f32 sensitivity) const;

private:
    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;

    Common::Quaternion<f32> quat{{0.0f, 0.0f, -1.0f}, 0.0f};
    Common::Vec3f rotations;
    Common::Vec3f accel;
    Common::Vec3f gyro;
    Common::Vec3f gyro_drift;

    f32 gyro_threshold = 0.0f;
    u32 reset_counter = 0;
    bool reset_enabled = true;
    bool only_accelerometer = true;
};

} // namespace InputCommon