-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.cpp
246 lines (205 loc) · 7.57 KB
/
helpers.cpp
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
#include "helpers.h"
#include <QtMath>
void MadgwickAHRS::Update(float gx, float gy, float gz, float ax, float ay, float az, float beta, float samplePeriod)
{
float q1 = orientation.scalar();
float q2 = orientation.x();
float q3 = orientation.y();
float q4 = orientation.z();
float norm;
float s1, s2, s3, s4;
float qDot1, qDot2, qDot3, qDot4;
// Auxiliary variables to avoid repeated arithmetic
float _2q1 = 2.0 * q1;
float _2q2 = 2.0 * q2;
float _2q3 = 2.0 * q3;
float _2q4 = 2.0 * q4;
float _4q1 = 4.0 * q1;
float _4q2 = 4.0 * q2;
float _4q3 = 4.0 * q3;
float _8q2 = 8.0 * q2;
float _8q3 = 8.0 * q3;
float q1q1 = q1 * q1;
float q2q2 = q2 * q2;
float q3q3 = q3 * q3;
float q4q4 = q4 * q4;
// Normalise accelerometer measurement
norm = (float) sqrt(ax * ax + ay * ay + az * az);
if (norm == 0.0)
return; // handle NaN
norm = 1 / norm; // use reciprocal for division
ax *= norm;
ay *= norm;
az *= norm;
// Gradient decent algorithm corrective step
s1 = _4q1 * q3q3 + _2q3 * ax + _4q1 * q2q2 - _2q2 * ay;
s2 = _4q2 * q4q4 - _2q4 * ax + 4.0 * q1q1 * q2 - _2q1 * ay - _4q2 + _8q2 * q2q2 + _8q2 * q3q3 + _4q2 * az;
s3 = 4.0 * q1q1 * q3 + _2q1 * ax + _4q3 * q4q4 - _2q4 * ay - _4q3 + _8q3 * q2q2 + _8q3 * q3q3 + _4q3 * az;
s4 = 4.0 * q2q2 * q4 - _2q2 * ax + 4.0 * q3q3 * q4 - _2q3 * ay;
norm = 1.0 / sqrt(s1 * s1 + s2 * s2 + s3 * s3 + s4 * s4); // normalise step magnitude
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 * samplePeriod;
q2 += qDot2 * samplePeriod;
q3 += qDot3 * samplePeriod;
q4 += qDot4 * samplePeriod;
norm = 1.0 / sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4); // normalise quaternion
orientation.setScalar(q1 * norm);
orientation.setX(q2 * norm);
orientation.setY(q3 * norm);
orientation.setZ(q4 * norm);
}
void BMI055Integrator::init(AScale AccelerometerScale, GScale GyroscopeScale)
{
accRes = getAccRes(AccelerometerScale);
gyroRes = getGyroRes(GyroscopeScale);
}
void BMI055Integrator::Recenter()
{
mutex.lock();
zero = QQuaternion(1.0, 0.0, 0.0, 0.0) * fusion.orientation.inverted();
mutex.unlock();
}
void BMI055Integrator::Recalibrate()
{
mutex.lock();
isCalibrating = true;
accOffset = QVector3D(0.0,0.0,0.0);
gyroOffset = QVector3D(0.0,0.0,0.0);
mutex.unlock();
}
float BMI055Integrator::getAccRes(AScale Scale)
{
switch (Scale)
{
case AFS_2G:
return 2.0f / 2048.0f;
case AFS_4G:
return 4.0f / 2048.0f;
case AFS_8G:
return 8.0f / 2048.0f;
case AFS_16G:
return 16.0f / 2048.0f;
default:
return 0;
}
}
float BMI055Integrator::getGyroRes(GScale Scale)
{
switch (Scale)
{
// Possible gyro scales (and their register bit settings) are:
// 125 DPS (100), 250 DPS (011), 500 DPS (010), 1000 DPS (001), and 2000 DPS (000).
case GFS_125DPS:
return (float)(0.00381f * (M_PI / 180)); //return 124.87 / (32768.0 * 4); // per data sheet, not exactly 125!?
case GFS_250DPS:
return (float)(0.007622f * (M_PI / 180)); //1.0 / 262.4; //return 249.75 / 32768.0;
case GFS_500DPS:
return (float)(0.01524f * (M_PI / 180));//1.0 / 262.4; //return 499.5 / 32768.0;
case GFS_1000DPS:
return (float)(0.03048f * (M_PI / 180)); //1.0 / 262.4; //return 999.0 / 32768.0;
case GFS_2000DPS:
return (float)(0.06097f * (M_PI / 180));//1.0 / 262.4; //return 1998.0 / 32768.0;
default:
return 0;
}
}
QQuaternion BMI055Integrator::Integrate(QVector3D linearAcceleration, QVector3D angularAcceleration, uint timestamp)
{
mutex.unlock();
// calculate offset
if (samplesLeft > 0)
{
samplesLeft--;
accOffset += linearAcceleration;
gyroOffset += angularAcceleration;
return QQuaternion(1.0, 0.0, 0.0, 0.0);
}
// offset calculated
else if(samplesLeft == 0)
{
samplesLeft--;
accOffset /= 2000;
gyroOffset /= 2000;
gravityVector = accOffset;
gravityVector.normalize();
accOffset -= gravityVector;
prevTimestamp = timestamp;
return QQuaternion(1.0, 0.0, 0.0, 0.0);
}
else if (samplesLeft > -1500)
{
samplesLeft--;
linearAcceleration = linearAcceleration - accOffset;
linearAcceleration.normalize();
angularAcceleration -= gyroOffset;
float interval = 0;
if (prevTimestamp > timestamp)
interval = (timestamp + (0xFFFFFF - prevTimestamp)) / 1000000.0f;
else
interval = (timestamp - prevTimestamp) / 1000000.0f;
fusion.Update(angularAcceleration.x(), angularAcceleration.y(), angularAcceleration.z(), linearAcceleration.x(), linearAcceleration.y(), linearAcceleration.z(), 1.5f, interval);
prevTimestamp = timestamp;
return QQuaternion(1.0, 0.0, 0.0, 0.0);
}
else if (samplesLeft > -2000)
{
samplesLeft--;
linearAcceleration = linearAcceleration - accOffset;
linearAcceleration.normalize();
angularAcceleration -= gyroOffset;
float interval = 0;
if (prevTimestamp > timestamp)
interval = (timestamp + (0xFFFFFF - prevTimestamp)) / 1000000.0f;
else
interval = (timestamp - prevTimestamp) / 1000000.0f;
fusion.Update(angularAcceleration.x(), angularAcceleration.y(), angularAcceleration.z(), linearAcceleration.x(), linearAcceleration.y(), linearAcceleration.z(), 0.05f, interval);
prevTimestamp = timestamp;
return QQuaternion(1.0, 0.0, 0.0, 0.0);
}
else if (samplesLeft == -2000)
{
samplesLeft--;
linearAcceleration = linearAcceleration - accOffset;
linearAcceleration.normalize();
angularAcceleration -= gyroOffset;
float interval = 0;
if (prevTimestamp > timestamp)
interval = (timestamp + (0xFFFFFF - prevTimestamp)) / 1000000.0f;
else
interval = (timestamp - prevTimestamp) / 1000000.0f;
fusion.Update(angularAcceleration.x(), angularAcceleration.y(), angularAcceleration.z(), linearAcceleration.x(), linearAcceleration.y(), linearAcceleration.z(), 0.035f, interval);
prevTimestamp = timestamp;
zero = QQuaternion(1.0, 0.0, 0.0, 0.0) * fusion.orientation.inverted();
isCalibrating = false;
return QQuaternion(1.0, 0.0, 0.0, 0.0);
}
else
{
linearAcceleration = linearAcceleration - accOffset;
linearAcceleration.normalize();
angularAcceleration -= gyroOffset;
float interval = 0;
if (prevTimestamp > timestamp)
interval = (timestamp + (0xFFFFFF - prevTimestamp)) / 1000000.0f;
else
interval = (timestamp - prevTimestamp) / 1000000.0f;
fusion.Update(angularAcceleration.x(), angularAcceleration.y(), angularAcceleration.y(), linearAcceleration.x(), linearAcceleration.y(), linearAcceleration.z(), 0.035f, interval);
prevTimestamp = timestamp;
// if (recenter)
// {
// zero = Quaternion.Identity * Quaternion.Inverse(fusion.Quaternion);
// recenter = false;
// }
return zero * fusion.orientation.inverted();
}
mutex.unlock();
}