summaryrefslogtreecommitdiff
path: root/src/input_common/motion_input.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/motion_input.cpp')
-rw-r--r--src/input_common/motion_input.cpp301
1 files changed, 301 insertions, 0 deletions
diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp
new file mode 100644
index 000000000..f77ba535d
--- /dev/null
+++ b/src/input_common/motion_input.cpp
@@ -0,0 +1,301 @@
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 acelerometer 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 return {accelerometer, gyroscope, rotation, orientation};
199}
200
201Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_magnitude) const {
202 std::random_device device;
203 std::mt19937 gen(device());
204 std::uniform_int_distribution<s16> distribution(-1000, 1000);
205 const Common::Vec3f gyroscope{
206 static_cast<f32>(distribution(gen)) * 0.001f,
207 static_cast<f32>(distribution(gen)) * 0.001f,
208 static_cast<f32>(distribution(gen)) * 0.001f,
209 };
210 const Common::Vec3f accelerometer{
211 static_cast<f32>(distribution(gen)) * 0.001f,
212 static_cast<f32>(distribution(gen)) * 0.001f,
213 static_cast<f32>(distribution(gen)) * 0.001f,
214 };
215 constexpr Common::Vec3f rotation;
216 constexpr std::array orientation{
217 Common::Vec3f{1.0f, 0.0f, 0.0f},
218 Common::Vec3f{0.0f, 1.0f, 0.0f},
219 Common::Vec3f{0.0f, 0.0f, 1.0f},
220 };
221 return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation};
222}
223
224void MotionInput::ResetOrientation() {
225 if (!reset_enabled || only_accelerometer) {
226 return;
227 }
228 if (!IsMoving(0.5f) && accel.z <= -0.9f) {
229 ++reset_counter;
230 if (reset_counter > 900) {
231 quat.w = 0;
232 quat.xyz[0] = 0;
233 quat.xyz[1] = 0;
234 quat.xyz[2] = -1;
235 SetOrientationFromAccelerometer();
236 integral_error = {};
237 reset_counter = 0;
238 }
239 } else {
240 reset_counter = 0;
241 }
242}
243
244void MotionInput::SetOrientationFromAccelerometer() {
245 int iterations = 0;
246 const f32 sample_period = 0.015f;
247
248 const auto normal_accel = accel.Normalized();
249
250 while (!IsCalibrated(0.01f) && ++iterations < 100) {
251 // Short name local variable for readability
252 f32 q1 = quat.w;
253 f32 q2 = quat.xyz[0];
254 f32 q3 = quat.xyz[1];
255 f32 q4 = quat.xyz[2];
256
257 Common::Vec3f rad_gyro;
258 const f32 ax = -normal_accel.x;
259 const f32 ay = normal_accel.y;
260 const f32 az = -normal_accel.z;
261
262 // Estimated direction of gravity
263 const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
264 const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
265 const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
266
267 // Error is cross product between estimated direction and measured direction of gravity
268 const Common::Vec3f new_real_error = {
269 az * vx - ax * vz,
270 ay * vz - az * vy,
271 ax * vy - ay * vx,
272 };
273
274 derivative_error = new_real_error - real_error;
275 real_error = new_real_error;
276
277 rad_gyro += 10.0f * kp * real_error;
278 rad_gyro += 5.0f * ki * integral_error;
279 rad_gyro += 10.0f * kd * derivative_error;
280
281 const f32 gx = rad_gyro.y;
282 const f32 gy = rad_gyro.x;
283 const f32 gz = rad_gyro.z;
284
285 // Integrate rate of change of quaternion
286 const f32 pa = q2;
287 const f32 pb = q3;
288 const f32 pc = q4;
289 q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
290 q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
291 q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
292 q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
293
294 quat.w = q1;
295 quat.xyz[0] = q2;
296 quat.xyz[1] = q3;
297 quat.xyz[2] = q4;
298 quat = quat.Normalized();
299 }
300}
301} // namespace InputCommon