-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRFT_filters.hpp
531 lines (439 loc) · 19.8 KB
/
RFT_filters.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
Filter classes and static methods
Copyright (c) 2021 Simon D. Levy
MIT License
*/
#pragma once
#include <math.h>
#include <stdint.h>
#ifndef M_PI
static const float M_PI = 3.141593;
#endif
namespace rft {
class Filter {
private:
// y = Ax + b helper for frame-of-reference conversion methods
static void dot(float A[3][3], float x[3], float y[3])
{
for (uint8_t j = 0; j < 3; ++j) {
y[j] = 0;
for (uint8_t k = 0; k < 3; ++k) {
y[j] += A[j][k] * x[k];
}
}
}
public:
static float complementary(float a, float b, float c)
{
return a * c + b * (1 - c);
}
static float constrainMinMax(float val, float min, float max)
{
return (val<min) ? min : ((val>max) ? max : val);
}
static float constrainAbs(float val, float max)
{
return constrainMinMax(val, -max, +max);
}
static void quat2euler(float qw, float qx, float qy, float qz,
float & ex, float & ey, float & ez)
{
ex = atan2(2.0f*(qw*qx+qy*qz), qw*qw-qx*qx-qy*qy+qz*qz);
ey = asin(2.0f*(qx*qz-qw*qy));
ez = atan2(2.0f*(qx*qy+qw*qz), qw*qw+qx*qx-qy*qy-qz*qz);
}
static void euler2quat(const float eulerAngles[3], float quaternion[4])
{
// Convenient renaming
float phi = eulerAngles[0] / 2;
float the = eulerAngles[1] / 2;
float psi = eulerAngles[2] / 2;
// Pre-computation
float cph = cos(phi);
float cth = cos(the);
float cps = cos(psi);
float sph = sin(phi);
float sth = sin(the);
float sps = sin(psi);
// Conversion
quaternion[0] = cph * cth * cps + sph * sth * sps;
quaternion[1] = cph * sth * sps - sph * cth * cps;
quaternion[2] = -cph * sth * cps - sph * cth * sps;
quaternion[3] = cph * cth * sps - sph * sth * cps;
}
static float deg2rad(float degrees)
{
return degrees * M_PI / 180;
}
static float rad2deg(float radians)
{
return radians * 180 / M_PI;
}
static void inertial2body(float inertial[3], const float rotation[3], float body[3])
{
float phi = rotation[0];
float theta = rotation[1];
float psi = rotation[2];
float cph = cos(phi);
float sph = sin(phi);
float cth = cos(theta);
float sth = sin(theta);
float cps = cos(psi);
float sps = sin(psi);
float R[3][3] = { {cps * cth, cth * sps, -sth},
{cps * sph * sth - cph * sps, cph * cps + sph * sps * sth, cth * sph},
{sph * sps + cph * cps * sth, cph * sps * sth - cps * sph, cph * cth} };
dot(R, inertial, body);
}
static void body2inertial(float body[3], const float rotation[3], float inertial[3])
{
float phi = rotation[0];
float theta = rotation[1];
float psi = rotation[2];
float cph = cos(phi);
float sph = sin(phi);
float cth = cos(theta);
float sth = sin(theta);
float cps = cos(psi);
float sps = sin(psi);
float R[3][3] = { {cps * cth, cps * sph * sth - cph * sps, sph * sps + cph * cps * sth},
{cth * sps, cph * cps + sph * sps * sth, cph * sps * sth - cps * sph},
{-sth, cth * sph, cph * cth} };
dot(R, body, inertial);
}
}; // class Filter
class LowPassFilter {
private:
float _history[256] = {0};
uint8_t _historySize = {0};
uint8_t _historyIdx = {0};
float _sum = {0};
public:
LowPassFilter(uint16_t historySize=50)
{
_historySize = historySize;
}
void begin(void)
{
for (uint8_t k=0; k<_historySize; ++k) {
_history[k] = 0;
}
_historyIdx = 0;
_sum = 0;
}
float update(float value)
{
uint8_t indexplus1 = (_historyIdx + 1) % _historySize;
_history[_historyIdx] = value;
_sum += _history[_historyIdx];
_sum -= _history[indexplus1];
_historyIdx = indexplus1;
return _sum / _historySize;
}
}; // class LowPassFilter
class QuaternionFilter {
public:
float q1;
float q2;
float q3;
float q4;
protected:
QuaternionFilter(void)
{
q1 = 1;
q2 = 0;
q3 = 0;
q4 = 0;
}
};
class MadgwickQuaternionFilter : public QuaternionFilter {
protected:
float _beta = 0;
MadgwickQuaternionFilter(float beta)
: QuaternionFilter()
{
_beta = beta;
}
};
class MadgwickQuaternionFilter9DOF : public MadgwickQuaternionFilter {
public:
MadgwickQuaternionFilter9DOF(float beta)
: MadgwickQuaternionFilter(beta) { }
// Adapted from https://github.com/kriswiner/MPU9250/blob/master/quaternionFilters.ino
void update(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz, float deltat)
{
float norm;
float hx, hy, _2bx, _2bz;
float s1, s2, s3, s4;
float qDot1, qDot2, qDot3, qDot4;
// Auxiliary variables to avoid repeated arithmetic
float _2q1mx;
float _2q1my;
float _2q1mz;
float _2q2mx;
float _4bx;
float _4bz;
float _2q1 = 2.0f * q1;
float _2q2 = 2.0f * q2;
float _2q3 = 2.0f * q3;
float _2q4 = 2.0f * q4;
float _2q1q3 = 2.0f * q1 * q3;
float _2q3q4 = 2.0f * q3 * q4;
float q1q1 = q1 * q1;
float q1q2 = q1 * q2;
float q1q3 = q1 * q3;
float q1q4 = q1 * q4;
float q2q2 = q2 * q2;
float q2q3 = q2 * q3;
float q2q4 = q2 * q4;
float q3q3 = q3 * q3;
float q3q4 = q3 * q4;
float q4q4 = q4 * q4;
// Normalise accelerometer measurement
norm = sqrtf(ax * ax + ay * ay + az * az);
if (norm == 0.0f) return; // handle NaN
norm = 1.0f/norm;
ax *= norm;
ay *= norm;
az *= norm;
// Normalise magnetometer measurement
norm = sqrtf(mx * mx + my * my + mz * mz);
if (norm == 0.0f) return; // handle NaN
norm = 1.0f/norm;
mx *= norm;
my *= norm;
mz *= norm;
// Reference direction of Earth's magnetic field
_2q1mx = 2.0f * q1 * mx;
_2q1my = 2.0f * q1 * my;
_2q1mz = 2.0f * q1 * mz;
_2q2mx = 2.0f * q2 * mx;
hx = mx * q1q1 - _2q1my * q4 + _2q1mz * q3 + mx * q2q2 + _2q2 * my * q3 + _2q2 * mz * q4 - mx * q3q3 - mx * q4q4;
hy = _2q1mx * q4 + my * q1q1 - _2q1mz * q2 + _2q2mx * q3 - my * q2q2 + my * q3q3 + _2q3 * mz * q4 - my * q4q4;
_2bx = sqrtf(hx * hx + hy * hy);
_2bz = -_2q1mx * q3 + _2q1my * q2 + mz * q1q1 + _2q2mx * q4 - mz * q2q2 + _2q3 * my * q4 - mz * q3q3 + mz * q4q4;
_4bx = 2.0f * _2bx;
_4bz = 2.0f * _2bz;
// Gradient decent algorithm corrective step
s1 = -_2q3 * (2.0f * q2q4 - _2q1q3 - ax) +
_2q2 * (2.0f * q1q2 + _2q3q4 - ay) -
_2bz * q3 * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) +
(-_2bx * q4 + _2bz * q2) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) +
_2bx * q3 * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
s2 = _2q4 * (2.0f * q2q4 - _2q1q3 - ax) +
_2q1 * (2.0f * q1q2 + _2q3q4 - ay) -
4.0f * q2 * (1.0f - 2.0f * q2q2 - 2.0f * q3q3 - az) +
_2bz * q4 * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) +
(_2bx * q3 + _2bz * q1) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) +
(_2bx * q4 - _4bz * q2) * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
s3 = -_2q1 * (2.0f * q2q4 - _2q1q3 - ax) +
_2q4 * (2.0f * q1q2 + _2q3q4 - ay) -
4.0f * q3 * (1.0f - 2.0f * q2q2 - 2.0f * q3q3 - az) +
(-_4bx * q3 - _2bz * q1) * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) +
(_2bx * q2 + _2bz * q4) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) +
(_2bx * q1 - _4bz * q3) * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
s4 = _2q2 * (2.0f * q2q4 - _2q1q3 - ax) +
_2q3 * (2.0f * q1q2 + _2q3q4 - ay) +
(-_4bx * q4 + _2bz * q2) * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) +
(-_2bx * q1 + _2bz * q3) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) +
_2bx * q2 * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
// Normalize step magnitude
norm = sqrtf(s1 * s1 + s2 * s2 + s3 * s3 + s4 * s4);
norm = 1.0f/norm;
s1 *= norm;
s2 *= norm;
s3 *= norm;
s4 *= norm;
// Compute rate of change of quaternion
qDot1 = 0.5f * (-q2 * gx - q3 * gy - q4 * gz) - _beta * s1;
qDot2 = 0.5f * (q1 * gx + q3 * gz - q4 * gy) - _beta * s2;
qDot3 = 0.5f * (q1 * gy - q2 * gz + q4 * gx) - _beta * s3;
qDot4 = 0.5f * (q1 * gz + q2 * gy - q3 * gx) - _beta * s4;
// Integrate to yield quaternion
q1 += qDot1 * deltat;
q2 += qDot2 * deltat;
q3 += qDot3 * deltat;
q4 += qDot4 * deltat;
norm = sqrtf(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4); // normalise quaternion
norm = 1.0f/norm;
}
}; // class MadgwickQuaternionFilter9DOF
class MadgwickQuaternionFilter6DOF : public MadgwickQuaternionFilter {
private:
float _zeta = 0;
public:
MadgwickQuaternionFilter6DOF(float beta, float zeta)
: MadgwickQuaternionFilter(beta)
{
_zeta = zeta;
}
// Adapted from https://github.com/kriswiner/MPU6050/blob/master/quaternionFilter.ino
void update(float ax, float ay, float az, float gx, float gy, float gz, float deltat)
{
static float gbiasx, gbiasy, gbiasz; // gyro bias error
// Auxiliary variables to avoid repeated arithmetic
float _halfq1 = 0.5f * q1;
float _halfq2 = 0.5f * q2;
float _halfq3 = 0.5f * q3;
float _halfq4 = 0.5f * q4;
float _2q1 = 2.0f * q1;
float _2q2 = 2.0f * q2;
float _2q3 = 2.0f * q3;
float _2q4 = 2.0f * q4;
//float _2q1q3 = 2.0f * q1 * q3;
//float _2q3q4 = 2.0f * q3 * q4;
// Normalise accelerometer measurement
float norm = sqrt(ax * ax + ay * ay + az * az);
if (norm == 0.0f) return; // handle NaN
norm = 1.0f/norm;
ax *= norm;
ay *= norm;
az *= norm;
// Compute the objective function and Jacobian
float f1 = _2q2 * q4 - _2q1 * q3 - ax;
float f2 = _2q1 * q2 + _2q3 * q4 - ay;
float f3 = 1.0f - _2q2 * q2 - _2q3 * q3 - az;
float J_11or24 = _2q3;
float J_12or23 = _2q4;
float J_13or22 = _2q1;
float J_14or21 = _2q2;
float J_32 = 2.0f * J_14or21;
float J_33 = 2.0f * J_11or24;
// Compute the gradient (matrix multiplication)
float hatDot1 = J_14or21 * f2 - J_11or24 * f1;
float hatDot2 = J_12or23 * f1 + J_13or22 * f2 - J_32 * f3;
float hatDot3 = J_12or23 * f2 - J_33 *f3 - J_13or22 * f1;
float hatDot4 = J_14or21 * f1 + J_11or24 * f2;
// Normalize the gradient
norm = sqrt(hatDot1 * hatDot1 + hatDot2 * hatDot2 + hatDot3 * hatDot3 + hatDot4 * hatDot4);
hatDot1 /= norm;
hatDot2 /= norm;
hatDot3 /= norm;
hatDot4 /= norm;
// Compute estimated gyroscope biases
float gerrx = _2q1 * hatDot2 - _2q2 * hatDot1 - _2q3 * hatDot4 + _2q4 * hatDot3;
float gerry = _2q1 * hatDot3 + _2q2 * hatDot4 - _2q3 * hatDot1 - _2q4 * hatDot2;
float gerrz = _2q1 * hatDot4 - _2q2 * hatDot3 + _2q3 * hatDot2 - _2q4 * hatDot1;
// Compute and remove gyroscope biases
gbiasx += gerrx * deltat * _zeta;
gbiasy += gerry * deltat * _zeta;
gbiasz += gerrz * deltat * _zeta;
gx -= gbiasx;
gy -= gbiasy;
gz -= gbiasz;
// Compute the quaternion derivative
float qDot1 = -_halfq2 * gx - _halfq3 * gy - _halfq4 * gz;
float qDot2 = _halfq1 * gx + _halfq3 * gz - _halfq4 * gy;
float qDot3 = _halfq1 * gy - _halfq2 * gz + _halfq4 * gx;
float qDot4 = _halfq1 * gz + _halfq2 * gy - _halfq3 * gx;
// Compute then integrate estimated quaternion derivative
q1 += (qDot1 -(_beta * hatDot1)) * deltat;
q2 += (qDot2 -(_beta * hatDot2)) * deltat;
q3 += (qDot3 -(_beta * hatDot3)) * deltat;
q4 += (qDot4 -(_beta * hatDot4)) * deltat;
// Normalize the quaternion
norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4); // normalise quaternion
norm = 1.0f/norm;
q1 *= norm;
q2 *= norm;
q3 *= norm;
q4 *= norm;
}
}; // class MadgwickQuaternionFilter6DOF
class MahonyQuaternionFilter9DOF : public QuaternionFilter {
private:
// Free parameters in the Mahony filter and fusion scheme, Kp for proportional feedback, Ki for integral
const float Kp = 2.0f * 5.0f;
const float Ki = 0.0f;
float _eInt[3] = {0};
public:
MahonyQuaternionFilter9DOF(void)
: QuaternionFilter()
{
_eInt[0] = 0;
_eInt[1] = 0;
_eInt[2] = 0;
}
// Adapted from https://github.com/kriswiner/MPU9250/blob/master/quaternionFilters.ino
void update(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz, float deltat)
{
float norm;
float hx, hy, bx, bz;
float vx, vy, vz, wx, wy, wz;
float ex, ey, ez;
float pa, pb, pc;
// Auxiliary variables to avoid repeated arithmetic
float q1q1 = q1 * q1;
float q1q2 = q1 * q2;
float q1q3 = q1 * q3;
float q1q4 = q1 * q4;
float q2q2 = q2 * q2;
float q2q3 = q2 * q3;
float q2q4 = q2 * q4;
float q3q3 = q3 * q3;
float q3q4 = q3 * q4;
float q4q4 = q4 * q4;
// Normalise accelerometer measurement
norm = sqrtf(ax * ax + ay * ay + az * az);
if (norm == 0.0f) return; // handle NaN
norm = 1.0f / norm; // use reciprocal for division
ax *= norm;
ay *= norm;
az *= norm;
// Normalise magnetometer measurement
norm = sqrtf(mx * mx + my * my + mz * mz);
if (norm == 0.0f) return; // handle NaN
norm = 1.0f / norm; // use reciprocal for division
mx *= norm;
my *= norm;
mz *= norm;
// Reference direction of Earth's magnetic field
hx = 2.0f * mx * (0.5f - q3q3 - q4q4) + 2.0f * my * (q2q3 - q1q4) + 2.0f * mz * (q2q4 + q1q3);
hy = 2.0f * mx * (q2q3 + q1q4) + 2.0f * my * (0.5f - q2q2 - q4q4) + 2.0f * mz * (q3q4 - q1q2);
bx = sqrtf((hx * hx) + (hy * hy));
bz = 2.0f * mx * (q2q4 - q1q3) + 2.0f * my * (q3q4 + q1q2) + 2.0f * mz * (0.5f - q2q2 - q3q3);
// Estimated direction of gravity and magnetic field
vx = 2.0f * (q2q4 - q1q3);
vy = 2.0f * (q1q2 + q3q4);
vz = q1q1 - q2q2 - q3q3 + q4q4;
wx = 2.0f * bx * (0.5f - q3q3 - q4q4) + 2.0f * bz * (q2q4 - q1q3);
wy = 2.0f * bx * (q2q3 - q1q4) + 2.0f * bz * (q1q2 + q3q4);
wz = 2.0f * bx * (q1q3 + q2q4) + 2.0f * bz * (0.5f - q2q2 - q3q3);
// Error is cross product between estimated direction and measured direction of gravity
ex = (ay * vz - az * vy) + (my * wz - mz * wy);
ey = (az * vx - ax * vz) + (mz * wx - mx * wz);
ez = (ax * vy - ay * vx) + (mx * wy - my * wx);
if (Ki > 0.0f)
{
_eInt[0] += ex; // accumulate integral error
_eInt[1] += ey;
_eInt[2] += ez;
}
else
{
_eInt[0] = 0.0f; // prevent integral wind up
_eInt[1] = 0.0f;
_eInt[2] = 0.0f;
}
// Apply feedback terms
gx = gx + Kp * ex + Ki * _eInt[0];
gy = gy + Kp * ey + Ki * _eInt[1];
gz = gz + Kp * ez + Ki * _eInt[2];
// Integrate rate of change of quaternion
pa = q2;
pb = q3;
pc = q4;
q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * deltat);
q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * deltat);
q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * deltat);
q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * deltat);
// Normalise quaternion
norm = sqrtf(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4);
norm = 1.0f / norm;
q1 *= norm;
q2 *= norm;
q3 *= norm;
q4 *= norm;
}
}; // class MahonyQuaternionFilter
} // namespace rft