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.cpp112
1 files changed, 102 insertions, 10 deletions
diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp
index 22a849866..d3e736044 100644
--- a/src/input_common/motion_input.cpp
+++ b/src/input_common/motion_input.cpp
@@ -16,8 +16,16 @@ void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) {
16 16
17void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) { 17void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) {
18 gyro = gyroscope - gyro_drift; 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
19 if (gyro.Length2() < gyro_threshold) { 25 if (gyro.Length2() < gyro_threshold) {
20 gyro = {}; 26 gyro = {};
27 } else {
28 only_accelerometer = false;
21 } 29 }
22} 30}
23 31
@@ -49,7 +57,7 @@ bool MotionInput::IsCalibrated(f32 sensitivity) const {
49 return real_error.Length() < sensitivity; 57 return real_error.Length() < sensitivity;
50} 58}
51 59
52void MotionInput::UpdateRotation(u64 elapsed_time) { 60void MotionInput::UpdateRotation(const u64 elapsed_time) {
53 const f32 sample_period = elapsed_time / 1000000.0f; 61 const f32 sample_period = elapsed_time / 1000000.0f;
54 if (sample_period > 0.1f) { 62 if (sample_period > 0.1f) {
55 return; 63 return;
@@ -57,7 +65,7 @@ void MotionInput::UpdateRotation(u64 elapsed_time) {
57 rotations += gyro * sample_period; 65 rotations += gyro * sample_period;
58} 66}
59 67
60void MotionInput::UpdateOrientation(u64 elapsed_time) { 68void MotionInput::UpdateOrientation(const u64 elapsed_time) {
61 if (!IsCalibrated(0.1f)) { 69 if (!IsCalibrated(0.1f)) {
62 ResetOrientation(); 70 ResetOrientation();
63 } 71 }
@@ -68,7 +76,7 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) {
68 f32 q4 = quat.xyz[2]; 76 f32 q4 = quat.xyz[2];
69 const f32 sample_period = elapsed_time / 1000000.0f; 77 const f32 sample_period = elapsed_time / 1000000.0f;
70 78
71 // ignore invalid elapsed time 79 // Ignore invalid elapsed time
72 if (sample_period > 0.1f) { 80 if (sample_period > 0.1f) {
73 return; 81 return;
74 } 82 }
@@ -80,6 +88,13 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) {
80 rad_gyro.y = -swap; 88 rad_gyro.y = -swap;
81 rad_gyro.z = -rad_gyro.z; 89 rad_gyro.z = -rad_gyro.z;
82 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
83 // Ignore drift correction if acceleration is not reliable 98 // Ignore drift correction if acceleration is not reliable
84 if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) { 99 if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) {
85 const f32 ax = -normal_accel.x; 100 const f32 ax = -normal_accel.x;
@@ -92,8 +107,11 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) {
92 const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4; 107 const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
93 108
94 // Error is cross product between estimated direction and measured direction of gravity 109 // Error is cross product between estimated direction and measured direction of gravity
95 const Common::Vec3f new_real_error = {az * vx - ax * vz, ay * vz - az * vy, 110 const Common::Vec3f new_real_error = {
96 ax * vy - ay * vx}; 111 az * vx - ax * vz,
112 ay * vz - az * vy,
113 ax * vy - ay * vx,
114 };
97 115
98 derivative_error = new_real_error - real_error; 116 derivative_error = new_real_error - real_error;
99 real_error = new_real_error; 117 real_error = new_real_error;
@@ -106,9 +124,22 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) {
106 } 124 }
107 125
108 // Apply feedback terms 126 // Apply feedback terms
109 rad_gyro += kp * real_error; 127 if (!only_accelerometer) {
110 rad_gyro += ki * integral_error; 128 rad_gyro += kp * real_error;
111 rad_gyro += kd * derivative_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 }
112 } 143 }
113 144
114 const f32 gx = rad_gyro.y; 145 const f32 gx = rad_gyro.y;
@@ -143,6 +174,67 @@ std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const {
143 Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])}; 174 Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])};
144} 175}
145 176
177void MotionInput::SetOrientationFromAccelerometer() {
178 int iterations = 0;
179 const f32 sample_period = 0.015f;
180
181 const auto normal_accel = accel.Normalized();
182 const f32 ax = -normal_accel.x;
183 const f32 ay = normal_accel.y;
184 const f32 az = -normal_accel.z;
185
186 while (!IsCalibrated(0.01f) && ++iterations < 100) {
187 // Short name local variable for readability
188 f32 q1 = quat.w;
189 f32 q2 = quat.xyz[0];
190 f32 q3 = quat.xyz[1];
191 f32 q4 = quat.xyz[2];
192
193 Common::Vec3f rad_gyro = {};
194 const f32 ax = -normal_accel.x;
195 const f32 ay = normal_accel.y;
196 const f32 az = -normal_accel.z;
197
198 // Estimated direction of gravity
199 const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
200 const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
201 const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
202
203 // Error is cross product between estimated direction and measured direction of gravity
204 const Common::Vec3f new_real_error = {
205 az * vx - ax * vz,
206 ay * vz - az * vy,
207 ax * vy - ay * vx,
208 };
209
210 derivative_error = new_real_error - real_error;
211 real_error = new_real_error;
212
213 rad_gyro += 10.0f * kp * real_error;
214 rad_gyro += 5.0f * ki * integral_error;
215 rad_gyro += 10.0f * kd * derivative_error;
216
217 const f32 gx = rad_gyro.y;
218 const f32 gy = rad_gyro.x;
219 const f32 gz = rad_gyro.z;
220
221 // Integrate rate of change of quaternion
222 const f32 pa = q2;
223 const f32 pb = q3;
224 const f32 pc = q4;
225 q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
226 q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
227 q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
228 q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
229
230 quat.w = q1;
231 quat.xyz[0] = q2;
232 quat.xyz[1] = q3;
233 quat.xyz[2] = q4;
234 quat = quat.Normalized();
235 }
236}
237
146Common::Vec3f MotionInput::GetAcceleration() const { 238Common::Vec3f MotionInput::GetAcceleration() const {
147 return accel; 239 return accel;
148} 240}
@@ -160,17 +252,17 @@ Common::Vec3f MotionInput::GetRotations() const {
160} 252}
161 253
162void MotionInput::ResetOrientation() { 254void MotionInput::ResetOrientation() {
163 if (!reset_enabled) { 255 if (!reset_enabled || only_accelerometer) {
164 return; 256 return;
165 } 257 }
166 if (!IsMoving(0.5f) && accel.z <= -0.9f) { 258 if (!IsMoving(0.5f) && accel.z <= -0.9f) {
167 ++reset_counter; 259 ++reset_counter;
168 if (reset_counter > 900) { 260 if (reset_counter > 900) {
169 // TODO: calculate quaternion from gravity vector
170 quat.w = 0; 261 quat.w = 0;
171 quat.xyz[0] = 0; 262 quat.xyz[0] = 0;
172 quat.xyz[1] = 0; 263 quat.xyz[1] = 0;
173 quat.xyz[2] = -1; 264 quat.xyz[2] = -1;
265 SetOrientationFromAccelerometer();
174 integral_error = {}; 266 integral_error = {};
175 reset_counter = 0; 267 reset_counter = 0;
176 } 268 }