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