summaryrefslogtreecommitdiff
path: root/src/core/hid/motion_input.cpp
diff options
context:
space:
mode:
authorGravatar german772021-09-20 16:29:43 -0500
committerGravatar Narr the Reg2021-11-24 20:30:22 -0600
commit449576df93f6beb70cff0e009ccb2dd8bce1e085 (patch)
treefae04e0d292da6128a074cbe046de6169019774c /src/core/hid/motion_input.cpp
parentcore/hid: Move input_interpreter to hid (diff)
downloadyuzu-449576df93f6beb70cff0e009ccb2dd8bce1e085.tar.gz
yuzu-449576df93f6beb70cff0e009ccb2dd8bce1e085.tar.xz
yuzu-449576df93f6beb70cff0e009ccb2dd8bce1e085.zip
core/hid: Move motion_input, create input converter and hid_types
Diffstat (limited to 'src/core/hid/motion_input.cpp')
-rw-r--r--src/core/hid/motion_input.cpp278
1 files changed, 278 insertions, 0 deletions
diff --git a/src/core/hid/motion_input.cpp b/src/core/hid/motion_input.cpp
new file mode 100644
index 000000000..93f37b77b
--- /dev/null
+++ b/src/core/hid/motion_input.cpp
@@ -0,0 +1,278 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included
4
5#include "common/math_util.h"
6#include "core/hid/motion_input.h"
7
8namespace Core::HID {
9
10MotionInput::MotionInput() {
11 // Initialize PID constants with default values
12 SetPID(0.3f, 0.005f, 0.0f);
13}
14
15void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) {
16 kp = new_kp;
17 ki = new_ki;
18 kd = new_kd;
19}
20
21void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) {
22 accel = acceleration;
23}
24
25void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) {
26 gyro = gyroscope - gyro_drift;
27
28 // Auto adjust drift to minimize drift
29 if (!IsMoving(0.1f)) {
30 gyro_drift = (gyro_drift * 0.9999f) + (gyroscope * 0.0001f);
31 }
32
33 if (gyro.Length2() < gyro_threshold) {
34 gyro = {};
35 } else {
36 only_accelerometer = false;
37 }
38}
39
40void MotionInput::SetQuaternion(const Common::Quaternion<f32>& quaternion) {
41 quat = quaternion;
42}
43
44void MotionInput::SetGyroDrift(const Common::Vec3f& drift) {
45 gyro_drift = drift;
46}
47
48void MotionInput::SetGyroThreshold(f32 threshold) {
49 gyro_threshold = threshold;
50}
51
52void MotionInput::EnableReset(bool reset) {
53 reset_enabled = reset;
54}
55
56void MotionInput::ResetRotations() {
57 rotations = {};
58}
59
60bool MotionInput::IsMoving(f32 sensitivity) const {
61 return gyro.Length() >= sensitivity || accel.Length() <= 0.9f || accel.Length() >= 1.1f;
62}
63
64bool MotionInput::IsCalibrated(f32 sensitivity) const {
65 return real_error.Length() < sensitivity;
66}
67
68void MotionInput::UpdateRotation(u64 elapsed_time) {
69 const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
70 if (sample_period > 0.1f) {
71 return;
72 }
73 rotations += gyro * sample_period;
74}
75
76void MotionInput::UpdateOrientation(u64 elapsed_time) {
77 if (!IsCalibrated(0.1f)) {
78 ResetOrientation();
79 }
80 // Short name local variable for readability
81 f32 q1 = quat.w;
82 f32 q2 = quat.xyz[0];
83 f32 q3 = quat.xyz[1];
84 f32 q4 = quat.xyz[2];
85 const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
86
87 // Ignore invalid elapsed time
88 if (sample_period > 0.1f) {
89 return;
90 }
91
92 const auto normal_accel = accel.Normalized();
93 auto rad_gyro = gyro * Common::PI * 2;
94 const f32 swap = rad_gyro.x;
95 rad_gyro.x = rad_gyro.y;
96 rad_gyro.y = -swap;
97 rad_gyro.z = -rad_gyro.z;
98
99 // Clear gyro values if there is no gyro present
100 if (only_accelerometer) {
101 rad_gyro.x = 0;
102 rad_gyro.y = 0;
103 rad_gyro.z = 0;
104 }
105
106 // Ignore drift correction if acceleration is not reliable
107 if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) {
108 const f32 ax = -normal_accel.x;
109 const f32 ay = normal_accel.y;
110 const f32 az = -normal_accel.z;
111
112 // Estimated direction of gravity
113 const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
114 const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
115 const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
116
117 // Error is cross product between estimated direction and measured direction of gravity
118 const Common::Vec3f new_real_error = {
119 az * vx - ax * vz,
120 ay * vz - az * vy,
121 ax * vy - ay * vx,
122 };
123
124 derivative_error = new_real_error - real_error;
125 real_error = new_real_error;
126
127 // Prevent integral windup
128 if (ki != 0.0f && !IsCalibrated(0.05f)) {
129 integral_error += real_error;
130 } else {
131 integral_error = {};
132 }
133
134 // Apply feedback terms
135 if (!only_accelerometer) {
136 rad_gyro += kp * real_error;
137 rad_gyro += ki * integral_error;
138 rad_gyro += kd * derivative_error;
139 } else {
140 // Give more weight to accelerometer values to compensate for the lack of gyro
141 rad_gyro += 35.0f * kp * real_error;
142 rad_gyro += 10.0f * ki * integral_error;
143 rad_gyro += 10.0f * kd * derivative_error;
144
145 // Emulate gyro values for games that need them
146 gyro.x = -rad_gyro.y;
147 gyro.y = rad_gyro.x;
148 gyro.z = -rad_gyro.z;
149 UpdateRotation(elapsed_time);
150 }
151 }
152
153 const f32 gx = rad_gyro.y;
154 const f32 gy = rad_gyro.x;
155 const f32 gz = rad_gyro.z;
156
157 // Integrate rate of change of quaternion
158 const f32 pa = q2;
159 const f32 pb = q3;
160 const f32 pc = q4;
161 q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
162 q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
163 q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
164 q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
165
166 quat.w = q1;
167 quat.xyz[0] = q2;
168 quat.xyz[1] = q3;
169 quat.xyz[2] = q4;
170 quat = quat.Normalized();
171}
172
173std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const {
174 const Common::Quaternion<float> quad{
175 .xyz = {-quat.xyz[1], -quat.xyz[0], -quat.w},
176 .w = -quat.xyz[2],
177 };
178 const std::array<float, 16> matrix4x4 = quad.ToMatrix();
179
180 return {Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]),
181 Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]),
182 Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])};
183}
184
185Common::Vec3f MotionInput::GetAcceleration() const {
186 return accel;
187}
188
189Common::Vec3f MotionInput::GetGyroscope() const {
190 return gyro;
191}
192
193Common::Quaternion<f32> MotionInput::GetQuaternion() const {
194 return quat;
195}
196
197Common::Vec3f MotionInput::GetRotations() const {
198 return rotations;
199}
200
201void MotionInput::ResetOrientation() {
202 if (!reset_enabled || only_accelerometer) {
203 return;
204 }
205 if (!IsMoving(0.5f) && accel.z <= -0.9f) {
206 ++reset_counter;
207 if (reset_counter > 900) {
208 quat.w = 0;
209 quat.xyz[0] = 0;
210 quat.xyz[1] = 0;
211 quat.xyz[2] = -1;
212 SetOrientationFromAccelerometer();
213 integral_error = {};
214 reset_counter = 0;
215 }
216 } else {
217 reset_counter = 0;
218 }
219}
220
221void MotionInput::SetOrientationFromAccelerometer() {
222 int iterations = 0;
223 const f32 sample_period = 0.015f;
224
225 const auto normal_accel = accel.Normalized();
226
227 while (!IsCalibrated(0.01f) && ++iterations < 100) {
228 // Short name local variable for readability
229 f32 q1 = quat.w;
230 f32 q2 = quat.xyz[0];
231 f32 q3 = quat.xyz[1];
232 f32 q4 = quat.xyz[2];
233
234 Common::Vec3f rad_gyro;
235 const f32 ax = -normal_accel.x;
236 const f32 ay = normal_accel.y;
237 const f32 az = -normal_accel.z;
238
239 // Estimated direction of gravity
240 const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
241 const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
242 const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
243
244 // Error is cross product between estimated direction and measured direction of gravity
245 const Common::Vec3f new_real_error = {
246 az * vx - ax * vz,
247 ay * vz - az * vy,
248 ax * vy - ay * vx,
249 };
250
251 derivative_error = new_real_error - real_error;
252 real_error = new_real_error;
253
254 rad_gyro += 10.0f * kp * real_error;
255 rad_gyro += 5.0f * ki * integral_error;
256 rad_gyro += 10.0f * kd * derivative_error;
257
258 const f32 gx = rad_gyro.y;
259 const f32 gy = rad_gyro.x;
260 const f32 gz = rad_gyro.z;
261
262 // Integrate rate of change of quaternion
263 const f32 pa = q2;
264 const f32 pb = q3;
265 const f32 pc = q4;
266 q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
267 q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
268 q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
269 q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
270
271 quat.w = q1;
272 quat.xyz[0] = q2;
273 quat.xyz[1] = q3;
274 quat.xyz[2] = q4;
275 quat = quat.Normalized();
276 }
277}
278} // namespace Core::HID