summaryrefslogtreecommitdiff
path: root/src/core/hid/motion_input.cpp
diff options
context:
space:
mode:
authorGravatar Feng Chen2021-12-18 13:57:14 +0800
committerGravatar GitHub2021-12-18 13:57:14 +0800
commite49184e6069a9d791d2df3c1958f5c4b1187e124 (patch)
treeb776caf722e0be0e680f67b0ad0842628162ef1c /src/core/hid/motion_input.cpp
parentImplement convert legacy to generic (diff)
parentMerge pull request #7570 from ameerj/favorites-expanded (diff)
downloadyuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.tar.gz
yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.tar.xz
yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.zip
Merge branch 'yuzu-emu:master' into convert_legacy
Diffstat (limited to 'src/core/hid/motion_input.cpp')
-rw-r--r--src/core/hid/motion_input.cpp280
1 files changed, 280 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..c25fea966
--- /dev/null
+++ b/src/core/hid/motion_input.cpp
@@ -0,0 +1,280 @@
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
76// Based on Madgwick's implementation of Mayhony's AHRS algorithm.
77// https://github.com/xioTechnologies/Open-Source-AHRS-With-x-IMU/blob/master/x-IMU%20IMU%20and%20AHRS%20Algorithms/x-IMU%20IMU%20and%20AHRS%20Algorithms/AHRS/MahonyAHRS.cs
78void MotionInput::UpdateOrientation(u64 elapsed_time) {
79 if (!IsCalibrated(0.1f)) {
80 ResetOrientation();
81 }
82 // Short name local variable for readability
83 f32 q1 = quat.w;
84 f32 q2 = quat.xyz[0];
85 f32 q3 = quat.xyz[1];
86 f32 q4 = quat.xyz[2];
87 const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
88
89 // Ignore invalid elapsed time
90 if (sample_period > 0.1f) {
91 return;
92 }
93
94 const auto normal_accel = accel.Normalized();
95 auto rad_gyro = gyro * Common::PI * 2;
96 const f32 swap = rad_gyro.x;
97 rad_gyro.x = rad_gyro.y;
98 rad_gyro.y = -swap;
99 rad_gyro.z = -rad_gyro.z;
100
101 // Clear gyro values if there is no gyro present
102 if (only_accelerometer) {
103 rad_gyro.x = 0;
104 rad_gyro.y = 0;
105 rad_gyro.z = 0;
106 }
107
108 // Ignore drift correction if acceleration is not reliable
109 if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) {
110 const f32 ax = -normal_accel.x;
111 const f32 ay = normal_accel.y;
112 const f32 az = -normal_accel.z;
113
114 // Estimated direction of gravity
115 const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
116 const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
117 const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
118
119 // Error is cross product between estimated direction and measured direction of gravity
120 const Common::Vec3f new_real_error = {
121 az * vx - ax * vz,
122 ay * vz - az * vy,
123 ax * vy - ay * vx,
124 };
125
126 derivative_error = new_real_error - real_error;
127 real_error = new_real_error;
128
129 // Prevent integral windup
130 if (ki != 0.0f && !IsCalibrated(0.05f)) {
131 integral_error += real_error;
132 } else {
133 integral_error = {};
134 }
135
136 // Apply feedback terms
137 if (!only_accelerometer) {
138 rad_gyro += kp * real_error;
139 rad_gyro += ki * integral_error;
140 rad_gyro += kd * derivative_error;
141 } else {
142 // Give more weight to accelerometer values to compensate for the lack of gyro
143 rad_gyro += 35.0f * kp * real_error;
144 rad_gyro += 10.0f * ki * integral_error;
145 rad_gyro += 10.0f * kd * derivative_error;
146
147 // Emulate gyro values for games that need them
148 gyro.x = -rad_gyro.y;
149 gyro.y = rad_gyro.x;
150 gyro.z = -rad_gyro.z;
151 UpdateRotation(elapsed_time);
152 }
153 }
154
155 const f32 gx = rad_gyro.y;
156 const f32 gy = rad_gyro.x;
157 const f32 gz = rad_gyro.z;
158
159 // Integrate rate of change of quaternion
160 const f32 pa = q2;
161 const f32 pb = q3;
162 const f32 pc = q4;
163 q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
164 q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
165 q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
166 q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
167
168 quat.w = q1;
169 quat.xyz[0] = q2;
170 quat.xyz[1] = q3;
171 quat.xyz[2] = q4;
172 quat = quat.Normalized();
173}
174
175std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const {
176 const Common::Quaternion<float> quad{
177 .xyz = {-quat.xyz[1], -quat.xyz[0], -quat.w},
178 .w = -quat.xyz[2],
179 };
180 const std::array<float, 16> matrix4x4 = quad.ToMatrix();
181
182 return {Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]),
183 Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]),
184 Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])};
185}
186
187Common::Vec3f MotionInput::GetAcceleration() const {
188 return accel;
189}
190
191Common::Vec3f MotionInput::GetGyroscope() const {
192 return gyro;
193}
194
195Common::Quaternion<f32> MotionInput::GetQuaternion() const {
196 return quat;
197}
198
199Common::Vec3f MotionInput::GetRotations() const {
200 return rotations;
201}
202
203void MotionInput::ResetOrientation() {
204 if (!reset_enabled || only_accelerometer) {
205 return;
206 }
207 if (!IsMoving(0.5f) && accel.z <= -0.9f) {
208 ++reset_counter;
209 if (reset_counter > 900) {
210 quat.w = 0;
211 quat.xyz[0] = 0;
212 quat.xyz[1] = 0;
213 quat.xyz[2] = -1;
214 SetOrientationFromAccelerometer();
215 integral_error = {};
216 reset_counter = 0;
217 }
218 } else {
219 reset_counter = 0;
220 }
221}
222
223void MotionInput::SetOrientationFromAccelerometer() {
224 int iterations = 0;
225 const f32 sample_period = 0.015f;
226
227 const auto normal_accel = accel.Normalized();
228
229 while (!IsCalibrated(0.01f) && ++iterations < 100) {
230 // Short name local variable for readability
231 f32 q1 = quat.w;
232 f32 q2 = quat.xyz[0];
233 f32 q3 = quat.xyz[1];
234 f32 q4 = quat.xyz[2];
235
236 Common::Vec3f rad_gyro;
237 const f32 ax = -normal_accel.x;
238 const f32 ay = normal_accel.y;
239 const f32 az = -normal_accel.z;
240
241 // Estimated direction of gravity
242 const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
243 const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
244 const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
245
246 // Error is cross product between estimated direction and measured direction of gravity
247 const Common::Vec3f new_real_error = {
248 az * vx - ax * vz,
249 ay * vz - az * vy,
250 ax * vy - ay * vx,
251 };
252
253 derivative_error = new_real_error - real_error;
254 real_error = new_real_error;
255
256 rad_gyro += 10.0f * kp * real_error;
257 rad_gyro += 5.0f * ki * integral_error;
258 rad_gyro += 10.0f * kd * derivative_error;
259
260 const f32 gx = rad_gyro.y;
261 const f32 gy = rad_gyro.x;
262 const f32 gz = rad_gyro.z;
263
264 // Integrate rate of change of quaternion
265 const f32 pa = q2;
266 const f32 pb = q3;
267 const f32 pc = q4;
268 q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
269 q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
270 q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
271 q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
272
273 quat.w = q1;
274 quat.xyz[0] = q2;
275 quat.xyz[1] = q3;
276 quat.xyz[2] = q4;
277 quat = quat.Normalized();
278 }
279}
280} // namespace Core::HID