/
sensfusion6.c
384 lines (341 loc) · 10.9 KB
/
sensfusion6.c
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
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie Firmware
*
* Copyright (C) 2011-2012 Bitcraze AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#include <math.h>
#include "sensfusion6.h"
#include "log.h"
#include "param.h"
#include "physicalConstants.h"
#include "autoconf.h"
#ifdef CONFIG_IMU_MADGWICK_QUATERNION
#define BETA_DEF 0.01f // 2 * proportional gain
#else // MAHONY_QUATERNION_IMU
#define TWO_KP_DEF (2.0f * 0.4f) // 2 * proportional gain
#define TWO_KI_DEF (2.0f * 0.001f) // 2 * integral gain
#endif
#ifdef CONFIG_IMU_MADGWICK_QUATERNION
float beta = BETA_DEF; // 2 * proportional gain (Kp)
#else // MAHONY_QUATERNION_IMU
float twoKp = TWO_KP_DEF; // 2 * proportional gain (Kp)
float twoKi = TWO_KI_DEF; // 2 * integral gain (Ki)
float integralFBx = 0.0f;
float integralFBy = 0.0f;
float integralFBz = 0.0f; // integral error terms scaled by Ki
#endif
float qw = 1.0f;
float qx = 0.0f;
float qy = 0.0f;
float qz = 0.0f; // quaternion of sensor frame relative to auxiliary frame
static float gravX, gravY, gravZ; // Unit vector in the estimated gravity direction
// The acc in Z for static position (g)
// Set on first update, assuming we are in a static position since the sensors were just calibrates.
// This value will be better the more level the copter is at calibration time
static float baseZacc = 1.0;
static bool isInit;
static bool isCalibrated = false;
static void sensfusion6UpdateQImpl(float gx, float gy, float gz, float ax, float ay, float az, float dt);
static float sensfusion6GetAccZ(const float ax, const float ay, const float az);
static void estimatedGravityDirection(float* gx, float* gy, float* gz);
// TODO: Make math util file
static float invSqrt(float x);
void sensfusion6Init()
{
if(isInit)
return;
isInit = true;
}
bool sensfusion6Test(void)
{
return isInit;
}
void sensfusion6UpdateQ(float gx, float gy, float gz, float ax, float ay, float az, float dt)
{
sensfusion6UpdateQImpl(gx, gy, gz, ax, ay, az, dt);
estimatedGravityDirection(&gravX, &gravY, &gravZ);
if (!isCalibrated) {
baseZacc = sensfusion6GetAccZ(ax, ay, az);
isCalibrated = true;
}
}
#ifdef CONFIG_IMU_MADGWICK_QUATERNION
// Implementation of Madgwick's IMU and AHRS algorithms.
// See: http://www.x-io.co.uk/open-source-ahrs-with-x-imu
//
// Date Author Notes
// 29/09/2011 SOH Madgwick Initial release
// 02/10/2011 SOH Madgwick Optimised for reduced CPU load
static void sensfusion6UpdateQImpl(float gx, float gy, float gz, float ax, float ay, float az, float dt)
{
float recipNorm;
float s0, s1, s2, s3;
float qDot1, qDot2, qDot3, qDot4;
float _2qw, _2qx, _2qy, _2qz, _4qw, _4qx, _4qy ,_8qx, _8qy, qwqw, qxqx, qyqy, qzqz;
// Rate of change of quaternion from gyroscope
qDot1 = 0.5f * (-qx * gx - qy * gy - qz * gz);
qDot2 = 0.5f * (qw * gx + qy * gz - qz * gy);
qDot3 = 0.5f * (qw * gy - qx * gz + qz * gx);
qDot4 = 0.5f * (qw * gz + qx * gy - qy * gx);
// Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)
if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f)))
{
// Normalise accelerometer measurement
recipNorm = invSqrt(ax * ax + ay * ay + az * az);
ax *= recipNorm;
ay *= recipNorm;
az *= recipNorm;
// Auxiliary variables to avoid repeated arithmetic
_2qw = 2.0f * qw;
_2qx = 2.0f * qx;
_2qy = 2.0f * qy;
_2qz = 2.0f * qz;
_4qw = 4.0f * qw;
_4qx = 4.0f * qx;
_4qy = 4.0f * qy;
_8qx = 8.0f * qx;
_8qy = 8.0f * qy;
qwqw = qw * qw;
qxqx = qx * qx;
qyqy = qy * qy;
qzqz = qz * qz;
// Gradient decent algorithm corrective step
s0 = _4qw * qyqy + _2qy * ax + _4qw * qxqx - _2qx * ay;
s1 = _4qx * qzqz - _2qz * ax + 4.0f * qwqw * qx - _2qw * ay - _4qx + _8qx * qxqx + _8qx * qyqy + _4qx * az;
s2 = 4.0f * qwqw * qy + _2qw * ax + _4qy * qzqz - _2qz * ay - _4qy + _8qy * qxqx + _8qy * qyqy + _4qy * az;
s3 = 4.0f * qxqx * qz - _2qx * ax + 4.0f * qyqy * qz - _2qy * ay;
recipNorm = invSqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude
s0 *= recipNorm;
s1 *= recipNorm;
s2 *= recipNorm;
s3 *= recipNorm;
// Apply feedback step
qDot1 -= beta * s0;
qDot2 -= beta * s1;
qDot3 -= beta * s2;
qDot4 -= beta * s3;
}
// Integrate rate of change of quaternion to yield quaternion
qw += qDot1 * dt;
qx += qDot2 * dt;
qy += qDot3 * dt;
qz += qDot4 * dt;
// Normalise quaternion
recipNorm = invSqrt(qw*qw + qx*qx + qy*qy + qz*qz);
qw *= recipNorm;
qx *= recipNorm;
qy *= recipNorm;
qz *= recipNorm;
}
#else
// Madgwick's implementation of Mahony's AHRS algorithm.
// See: http://www.x-io.co.uk/open-source-ahrs-with-x-imu
//
// Date Author Notes
// 29/09/2011 SOH Madgwick Initial release
// 02/10/2011 SOH Madgwick Optimised for reduced CPU load
static void sensfusion6UpdateQImpl(float gx, float gy, float gz, float ax, float ay, float az, float dt)
{
float recipNorm;
float halfvx, halfvy, halfvz;
float halfex, halfey, halfez;
float qa, qb, qc;
gx = gx * M_PI_F / 180;
gy = gy * M_PI_F / 180;
gz = gz * M_PI_F / 180;
// Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)
if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f)))
{
// Normalise accelerometer measurement
recipNorm = invSqrt(ax * ax + ay * ay + az * az);
ax *= recipNorm;
ay *= recipNorm;
az *= recipNorm;
// Estimated direction of gravity and vector perpendicular to magnetic flux
halfvx = qx * qz - qw * qy;
halfvy = qw * qx + qy * qz;
halfvz = qw * qw - 0.5f + qz * qz;
// Error is sum of cross product between estimated and measured direction of gravity
halfex = (ay * halfvz - az * halfvy);
halfey = (az * halfvx - ax * halfvz);
halfez = (ax * halfvy - ay * halfvx);
// Compute and apply integral feedback if enabled
if(twoKi > 0.0f)
{
integralFBx += twoKi * halfex * dt; // integral error scaled by Ki
integralFBy += twoKi * halfey * dt;
integralFBz += twoKi * halfez * dt;
gx += integralFBx; // apply integral feedback
gy += integralFBy;
gz += integralFBz;
}
else
{
integralFBx = 0.0f; // prevent integral windup
integralFBy = 0.0f;
integralFBz = 0.0f;
}
// Apply proportional feedback
gx += twoKp * halfex;
gy += twoKp * halfey;
gz += twoKp * halfez;
}
// Integrate rate of change of quaternion
gx *= (0.5f * dt); // pre-multiply common factors
gy *= (0.5f * dt);
gz *= (0.5f * dt);
qa = qw;
qb = qx;
qc = qy;
qw += (-qb * gx - qc * gy - qz * gz);
qx += (qa * gx + qc * gz - qz * gy);
qy += (qa * gy - qb * gz + qz * gx);
qz += (qa * gz + qb * gy - qc * gx);
// Normalise quaternion
recipNorm = invSqrt(qw * qw + qx * qx + qy * qy + qz * qz);
qw *= recipNorm;
qx *= recipNorm;
qy *= recipNorm;
qz *= recipNorm;
}
#endif
void sensfusion6GetQuaternion(float* q_x, float* q_y, float* q_z, float* q_w)
{
*q_x = qx;
*q_y = qy;
*q_z = qz;
*q_w = qw;
}
void sensfusion6GetEulerRPY(float* roll, float* pitch, float* yaw)
{
float gx = gravX;
float gy = gravY;
float gz = gravZ;
if (gx>1) gx=1;
if (gx<-1) gx=-1;
*yaw = atan2f(2*(qw*qz + qx*qy), qw*qw + qx*qx - qy*qy - qz*qz) * 180 / M_PI_F;
*pitch = asinf(gx) * 180 / M_PI_F; //Pitch seems to be inverted
*roll = atan2f(gy, gz) * 180 / M_PI_F;
}
float sensfusion6GetAccZWithoutGravity(const float ax, const float ay, const float az)
{
return sensfusion6GetAccZ(ax, ay, az) - baseZacc;
}
//---------------------------------------------------------------------------------------------------
// Fast inverse square-root
// See: http://en.wikipedia.org/wiki/Fast_inverse_square_root
float invSqrt(float x)
{
float halfx = 0.5f * x;
float y = x;
long i = *(long*)&y;
i = 0x5f3759df - (i>>1);
y = *(float*)&i;
y = y * (1.5f - (halfx * y * y));
return y;
}
static float sensfusion6GetAccZ(const float ax, const float ay, const float az)
{
// return vertical acceleration
// (A dot G) / |G|, (|G| = 1) -> (A dot G)
return (ax * gravX + ay * gravY + az * gravZ);
}
static void estimatedGravityDirection(float* gx, float* gy, float* gz)
{
*gx = 2 * (qx * qz - qw * qy);
*gy = 2 * (qw * qx + qy * qz);
*gz = qw * qw - qx * qx - qy * qy + qz * qz;
}
/**
* Sensor fusion is the process of combining sensory data or data derived from
* disparate sources such that the resulting information has less uncertainty
* than would be possible when these sources were used individually.
*
* The sensfusion6 module uses an 3 axis accelerometer and a 3 axis gyro to get
* accurate attitude measurements.
*/
LOG_GROUP_START(sensfusion6)
/**
* @brief W quaternion
*/
LOG_ADD(LOG_FLOAT, qw, &qw)
/**
* @brief X quaternion
*/
LOG_ADD(LOG_FLOAT, qx, &qx)
/**
* @brief y quaternion
*/
LOG_ADD(LOG_FLOAT, qy, &qy)
/**
* @brief z quaternion
*/
LOG_ADD(LOG_FLOAT, qz, &qz)
/**
* @brief Gravity vector X
*/
LOG_ADD(LOG_FLOAT, gravityX, &gravX)
/**
* @brief Gravity vector Y
*/
LOG_ADD(LOG_FLOAT, gravityY, &gravY)
/**
* @brief Gravity vector Z
*/
LOG_ADD(LOG_FLOAT, gravityZ, &gravZ)
/**
* @brief Gravity scale factor after calibration
*/
LOG_ADD(LOG_FLOAT, accZbase, &baseZacc)
/**
* @brief Nonzero if complimentary filter been initialized
*/
LOG_ADD(LOG_UINT8, isInit, &isInit)
/**
* @brief Nonzero if gravity scale been calibrated
*/
LOG_ADD(LOG_UINT8, isCalibrated, &isCalibrated)
LOG_GROUP_STOP(sensfusion6)
/**
* Sensor fusion is the process of combining sensory data or data derived from
* disparate sources such that the resulting information has less uncertainty
* than would be possible when these sources were used individually.
*
* The sensfusion6 module uses an 3 axis accelerometer and a 3 axis gyro to get
* accurate attitude measurements.
*/
PARAM_GROUP_START(sensfusion6)
#ifdef CONFIG_IMU_MADGWICK_QUATERNION
PARAM_ADD(PARAM_FLOAT, beta, &beta)
#else // MAHONY_QUATERNION_IMU
/**
* @brief Integral gain (default: 0.002)
*/
PARAM_ADD_CORE(PARAM_FLOAT | PARAM_PERSISTENT, kp, &twoKp)
/**
* @brief Propotional gain (default: 0.8)
*/
PARAM_ADD_CORE(PARAM_FLOAT | PARAM_PERSISTENT, ki, &twoKi)
#endif
PARAM_ADD(PARAM_FLOAT, baseZacc, &baseZacc)
PARAM_GROUP_STOP(sensfusion6)