Library implementing Madgwick's IMU and AHRS algorithms

Dependents:   Hexi_GPSIMU_Hotshoe

Committer:
Anaesthetix
Date:
Sun Dec 18 21:50:15 2016 +0000
Revision:
0:9b434b5e28d4
Child:
1:d7c70d593694
Library implementing Madgwick's IMU and AHRS algorithms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anaesthetix 0:9b434b5e28d4 1 //=============================================================================================
Anaesthetix 0:9b434b5e28d4 2 // MadgwickAHRS.c
Anaesthetix 0:9b434b5e28d4 3 //=============================================================================================
Anaesthetix 0:9b434b5e28d4 4 //
Anaesthetix 0:9b434b5e28d4 5 // Implementation of Madgwick's IMU and AHRS algorithms.
Anaesthetix 0:9b434b5e28d4 6 // See: http://www.x-io.co.uk/open-source-imu-and-ahrs-algorithms/
Anaesthetix 0:9b434b5e28d4 7 //
Anaesthetix 0:9b434b5e28d4 8 // From the x-io website "Open-source resources available on this website are
Anaesthetix 0:9b434b5e28d4 9 // provided under the GNU General Public Licence unless an alternative licence
Anaesthetix 0:9b434b5e28d4 10 // is provided in source."
Anaesthetix 0:9b434b5e28d4 11 //
Anaesthetix 0:9b434b5e28d4 12 // Date Author Notes
Anaesthetix 0:9b434b5e28d4 13 // 29/09/2011 SOH Madgwick Initial release
Anaesthetix 0:9b434b5e28d4 14 // 02/10/2011 SOH Madgwick Optimised for reduced CPU load
Anaesthetix 0:9b434b5e28d4 15 // 19/02/2012 SOH Madgwick Magnetometer measurement is normalised
Anaesthetix 0:9b434b5e28d4 16 // 18/12/2016 Added better fast inverse square root
Anaesthetix 0:9b434b5e28d4 17 //
Anaesthetix 0:9b434b5e28d4 18 //=============================================================================================
Anaesthetix 0:9b434b5e28d4 19
Anaesthetix 0:9b434b5e28d4 20 //-------------------------------------------------------------------------------------------
Anaesthetix 0:9b434b5e28d4 21 // Header files
Anaesthetix 0:9b434b5e28d4 22
Anaesthetix 0:9b434b5e28d4 23 #include "MadgwickAHRS.h"
Anaesthetix 0:9b434b5e28d4 24 #include <math.h>
Anaesthetix 0:9b434b5e28d4 25
Anaesthetix 0:9b434b5e28d4 26 //-------------------------------------------------------------------------------------------
Anaesthetix 0:9b434b5e28d4 27 // Definitions
Anaesthetix 0:9b434b5e28d4 28
Anaesthetix 0:9b434b5e28d4 29 #define sampleFreqDef 500.0f // sample frequency in Hz
Anaesthetix 0:9b434b5e28d4 30 #define betaDef 0.75f // 2 * proportional gain 0.1 - 0.5 - 5
Anaesthetix 0:9b434b5e28d4 31
Anaesthetix 0:9b434b5e28d4 32
Anaesthetix 0:9b434b5e28d4 33 //============================================================================================
Anaesthetix 0:9b434b5e28d4 34 // Functions
Anaesthetix 0:9b434b5e28d4 35
Anaesthetix 0:9b434b5e28d4 36 //-------------------------------------------------------------------------------------------
Anaesthetix 0:9b434b5e28d4 37 // AHRS algorithm update
Anaesthetix 0:9b434b5e28d4 38
Anaesthetix 0:9b434b5e28d4 39 Madgwick::Madgwick() {
Anaesthetix 0:9b434b5e28d4 40 beta = betaDef;
Anaesthetix 0:9b434b5e28d4 41 q0 = 1.0f;
Anaesthetix 0:9b434b5e28d4 42 q1 = 0.0f;
Anaesthetix 0:9b434b5e28d4 43 q2 = 0.0f;
Anaesthetix 0:9b434b5e28d4 44 q3 = 0.0f;
Anaesthetix 0:9b434b5e28d4 45 invSampleFreq = 1.0f / sampleFreqDef;
Anaesthetix 0:9b434b5e28d4 46 anglesComputed = 0;
Anaesthetix 0:9b434b5e28d4 47 }
Anaesthetix 0:9b434b5e28d4 48
Anaesthetix 0:9b434b5e28d4 49 void Madgwick::update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) {
Anaesthetix 0:9b434b5e28d4 50 float recipNorm;
Anaesthetix 0:9b434b5e28d4 51 float s0, s1, s2, s3;
Anaesthetix 0:9b434b5e28d4 52 float qDot1, qDot2, qDot3, qDot4;
Anaesthetix 0:9b434b5e28d4 53 float hx, hy;
Anaesthetix 0:9b434b5e28d4 54 float _2q0mx, _2q0my, _2q0mz, _2q1mx, _2bx, _2bz, _4bx, _4bz, _2q0, _2q1, _2q2, _2q3, _2q0q2, _2q2q3, q0q0, q0q1, q0q2, q0q3, q1q1, q1q2, q1q3, q2q2, q2q3, q3q3;
Anaesthetix 0:9b434b5e28d4 55
Anaesthetix 0:9b434b5e28d4 56 // Use IMU algorithm if magnetometer measurement invalid (avoids NaN in magnetometer normalisation)
Anaesthetix 0:9b434b5e28d4 57 if((mx == 0.0f) && (my == 0.0f) && (mz == 0.0f)) {
Anaesthetix 0:9b434b5e28d4 58 updateIMU(gx, gy, gz, ax, ay, az);
Anaesthetix 0:9b434b5e28d4 59 return;
Anaesthetix 0:9b434b5e28d4 60 }
Anaesthetix 0:9b434b5e28d4 61
Anaesthetix 0:9b434b5e28d4 62 // Convert gyroscope degrees/sec to radians/sec
Anaesthetix 0:9b434b5e28d4 63 gx *= 0.0174533f;
Anaesthetix 0:9b434b5e28d4 64 gy *= 0.0174533f;
Anaesthetix 0:9b434b5e28d4 65 gz *= 0.0174533f;
Anaesthetix 0:9b434b5e28d4 66
Anaesthetix 0:9b434b5e28d4 67 // Rate of change of quaternion from gyroscope
Anaesthetix 0:9b434b5e28d4 68 qDot1 = 0.5f * (-q1 * gx - q2 * gy - q3 * gz);
Anaesthetix 0:9b434b5e28d4 69 qDot2 = 0.5f * (q0 * gx + q2 * gz - q3 * gy);
Anaesthetix 0:9b434b5e28d4 70 qDot3 = 0.5f * (q0 * gy - q1 * gz + q3 * gx);
Anaesthetix 0:9b434b5e28d4 71 qDot4 = 0.5f * (q0 * gz + q1 * gy - q2 * gx);
Anaesthetix 0:9b434b5e28d4 72
Anaesthetix 0:9b434b5e28d4 73 // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)
Anaesthetix 0:9b434b5e28d4 74 if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) {
Anaesthetix 0:9b434b5e28d4 75
Anaesthetix 0:9b434b5e28d4 76 // Normalise accelerometer measurement
Anaesthetix 0:9b434b5e28d4 77 recipNorm = invSqrt(ax * ax + ay * ay + az * az);
Anaesthetix 0:9b434b5e28d4 78 ax *= recipNorm;
Anaesthetix 0:9b434b5e28d4 79 ay *= recipNorm;
Anaesthetix 0:9b434b5e28d4 80 az *= recipNorm;
Anaesthetix 0:9b434b5e28d4 81
Anaesthetix 0:9b434b5e28d4 82 // Normalise magnetometer measurement
Anaesthetix 0:9b434b5e28d4 83 recipNorm = invSqrt(mx * mx + my * my + mz * mz);
Anaesthetix 0:9b434b5e28d4 84 mx *= recipNorm;
Anaesthetix 0:9b434b5e28d4 85 my *= recipNorm;
Anaesthetix 0:9b434b5e28d4 86 mz *= recipNorm;
Anaesthetix 0:9b434b5e28d4 87
Anaesthetix 0:9b434b5e28d4 88 // Auxiliary variables to avoid repeated arithmetic
Anaesthetix 0:9b434b5e28d4 89 _2q0mx = 2.0f * q0 * mx;
Anaesthetix 0:9b434b5e28d4 90 _2q0my = 2.0f * q0 * my;
Anaesthetix 0:9b434b5e28d4 91 _2q0mz = 2.0f * q0 * mz;
Anaesthetix 0:9b434b5e28d4 92 _2q1mx = 2.0f * q1 * mx;
Anaesthetix 0:9b434b5e28d4 93 _2q0 = 2.0f * q0;
Anaesthetix 0:9b434b5e28d4 94 _2q1 = 2.0f * q1;
Anaesthetix 0:9b434b5e28d4 95 _2q2 = 2.0f * q2;
Anaesthetix 0:9b434b5e28d4 96 _2q3 = 2.0f * q3;
Anaesthetix 0:9b434b5e28d4 97 _2q0q2 = 2.0f * q0 * q2;
Anaesthetix 0:9b434b5e28d4 98 _2q2q3 = 2.0f * q2 * q3;
Anaesthetix 0:9b434b5e28d4 99 q0q0 = q0 * q0;
Anaesthetix 0:9b434b5e28d4 100 q0q1 = q0 * q1;
Anaesthetix 0:9b434b5e28d4 101 q0q2 = q0 * q2;
Anaesthetix 0:9b434b5e28d4 102 q0q3 = q0 * q3;
Anaesthetix 0:9b434b5e28d4 103 q1q1 = q1 * q1;
Anaesthetix 0:9b434b5e28d4 104 q1q2 = q1 * q2;
Anaesthetix 0:9b434b5e28d4 105 q1q3 = q1 * q3;
Anaesthetix 0:9b434b5e28d4 106 q2q2 = q2 * q2;
Anaesthetix 0:9b434b5e28d4 107 q2q3 = q2 * q3;
Anaesthetix 0:9b434b5e28d4 108 q3q3 = q3 * q3;
Anaesthetix 0:9b434b5e28d4 109
Anaesthetix 0:9b434b5e28d4 110 // Reference direction of Earth's magnetic field
Anaesthetix 0:9b434b5e28d4 111 hx = mx * q0q0 - _2q0my * q3 + _2q0mz * q2 + mx * q1q1 + _2q1 * my * q2 + _2q1 * mz * q3 - mx * q2q2 - mx * q3q3;
Anaesthetix 0:9b434b5e28d4 112 hy = _2q0mx * q3 + my * q0q0 - _2q0mz * q1 + _2q1mx * q2 - my * q1q1 + my * q2q2 + _2q2 * mz * q3 - my * q3q3;
Anaesthetix 0:9b434b5e28d4 113 _2bx = sqrtf(hx * hx + hy * hy);
Anaesthetix 0:9b434b5e28d4 114 _2bz = -_2q0mx * q2 + _2q0my * q1 + mz * q0q0 + _2q1mx * q3 - mz * q1q1 + _2q2 * my * q3 - mz * q2q2 + mz * q3q3;
Anaesthetix 0:9b434b5e28d4 115 _4bx = 2.0f * _2bx;
Anaesthetix 0:9b434b5e28d4 116 _4bz = 2.0f * _2bz;
Anaesthetix 0:9b434b5e28d4 117
Anaesthetix 0:9b434b5e28d4 118 // Gradient decent algorithm corrective step
Anaesthetix 0:9b434b5e28d4 119 s0 = -_2q2 * (2.0f * q1q3 - _2q0q2 - ax) + _2q1 * (2.0f * q0q1 + _2q2q3 - ay) - _2bz * q2 * (_2bx * (0.5f - q2q2 - q3q3) + _2bz * (q1q3 - q0q2) - mx) + (-_2bx * q3 + _2bz * q1) * (_2bx * (q1q2 - q0q3) + _2bz * (q0q1 + q2q3) - my) + _2bx * q2 * (_2bx * (q0q2 + q1q3) + _2bz * (0.5f - q1q1 - q2q2) - mz);
Anaesthetix 0:9b434b5e28d4 120 s1 = _2q3 * (2.0f * q1q3 - _2q0q2 - ax) + _2q0 * (2.0f * q0q1 + _2q2q3 - ay) - 4.0f * q1 * (1 - 2.0f * q1q1 - 2.0f * q2q2 - az) + _2bz * q3 * (_2bx * (0.5f - q2q2 - q3q3) + _2bz * (q1q3 - q0q2) - mx) + (_2bx * q2 + _2bz * q0) * (_2bx * (q1q2 - q0q3) + _2bz * (q0q1 + q2q3) - my) + (_2bx * q3 - _4bz * q1) * (_2bx * (q0q2 + q1q3) + _2bz * (0.5f - q1q1 - q2q2) - mz);
Anaesthetix 0:9b434b5e28d4 121 s2 = -_2q0 * (2.0f * q1q3 - _2q0q2 - ax) + _2q3 * (2.0f * q0q1 + _2q2q3 - ay) - 4.0f * q2 * (1 - 2.0f * q1q1 - 2.0f * q2q2 - az) + (-_4bx * q2 - _2bz * q0) * (_2bx * (0.5f - q2q2 - q3q3) + _2bz * (q1q3 - q0q2) - mx) + (_2bx * q1 + _2bz * q3) * (_2bx * (q1q2 - q0q3) + _2bz * (q0q1 + q2q3) - my) + (_2bx * q0 - _4bz * q2) * (_2bx * (q0q2 + q1q3) + _2bz * (0.5f - q1q1 - q2q2) - mz);
Anaesthetix 0:9b434b5e28d4 122 s3 = _2q1 * (2.0f * q1q3 - _2q0q2 - ax) + _2q2 * (2.0f * q0q1 + _2q2q3 - ay) + (-_4bx * q3 + _2bz * q1) * (_2bx * (0.5f - q2q2 - q3q3) + _2bz * (q1q3 - q0q2) - mx) + (-_2bx * q0 + _2bz * q2) * (_2bx * (q1q2 - q0q3) + _2bz * (q0q1 + q2q3) - my) + _2bx * q1 * (_2bx * (q0q2 + q1q3) + _2bz * (0.5f - q1q1 - q2q2) - mz);
Anaesthetix 0:9b434b5e28d4 123 recipNorm = invSqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude
Anaesthetix 0:9b434b5e28d4 124 s0 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 125 s1 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 126 s2 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 127 s3 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 128
Anaesthetix 0:9b434b5e28d4 129 // Apply feedback step
Anaesthetix 0:9b434b5e28d4 130 qDot1 -= beta * s0;
Anaesthetix 0:9b434b5e28d4 131 qDot2 -= beta * s1;
Anaesthetix 0:9b434b5e28d4 132 qDot3 -= beta * s2;
Anaesthetix 0:9b434b5e28d4 133 qDot4 -= beta * s3;
Anaesthetix 0:9b434b5e28d4 134 }
Anaesthetix 0:9b434b5e28d4 135
Anaesthetix 0:9b434b5e28d4 136 // Integrate rate of change of quaternion to yield quaternion
Anaesthetix 0:9b434b5e28d4 137 q0 += qDot1 * invSampleFreq;
Anaesthetix 0:9b434b5e28d4 138 q1 += qDot2 * invSampleFreq;
Anaesthetix 0:9b434b5e28d4 139 q2 += qDot3 * invSampleFreq;
Anaesthetix 0:9b434b5e28d4 140 q3 += qDot4 * invSampleFreq;
Anaesthetix 0:9b434b5e28d4 141
Anaesthetix 0:9b434b5e28d4 142 // Normalise quaternion
Anaesthetix 0:9b434b5e28d4 143 recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
Anaesthetix 0:9b434b5e28d4 144 q0 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 145 q1 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 146 q2 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 147 q3 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 148 anglesComputed = 0;
Anaesthetix 0:9b434b5e28d4 149 }
Anaesthetix 0:9b434b5e28d4 150
Anaesthetix 0:9b434b5e28d4 151 //-------------------------------------------------------------------------------------------
Anaesthetix 0:9b434b5e28d4 152 // IMU algorithm update
Anaesthetix 0:9b434b5e28d4 153
Anaesthetix 0:9b434b5e28d4 154 void Madgwick::updateIMU(float gx, float gy, float gz, float ax, float ay, float az) {
Anaesthetix 0:9b434b5e28d4 155 float recipNorm;
Anaesthetix 0:9b434b5e28d4 156 float s0, s1, s2, s3;
Anaesthetix 0:9b434b5e28d4 157 float qDot1, qDot2, qDot3, qDot4;
Anaesthetix 0:9b434b5e28d4 158 float _2q0, _2q1, _2q2, _2q3, _4q0, _4q1, _4q2 ,_8q1, _8q2, q0q0, q1q1, q2q2, q3q3;
Anaesthetix 0:9b434b5e28d4 159
Anaesthetix 0:9b434b5e28d4 160 // Convert gyroscope degrees/sec to radians/sec
Anaesthetix 0:9b434b5e28d4 161 gx *= 0.0174533f;
Anaesthetix 0:9b434b5e28d4 162 gy *= 0.0174533f;
Anaesthetix 0:9b434b5e28d4 163 gz *= 0.0174533f;
Anaesthetix 0:9b434b5e28d4 164
Anaesthetix 0:9b434b5e28d4 165 // Rate of change of quaternion from gyroscope
Anaesthetix 0:9b434b5e28d4 166 qDot1 = 0.5f * (-q1 * gx - q2 * gy - q3 * gz);
Anaesthetix 0:9b434b5e28d4 167 qDot2 = 0.5f * (q0 * gx + q2 * gz - q3 * gy);
Anaesthetix 0:9b434b5e28d4 168 qDot3 = 0.5f * (q0 * gy - q1 * gz + q3 * gx);
Anaesthetix 0:9b434b5e28d4 169 qDot4 = 0.5f * (q0 * gz + q1 * gy - q2 * gx);
Anaesthetix 0:9b434b5e28d4 170
Anaesthetix 0:9b434b5e28d4 171 // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)
Anaesthetix 0:9b434b5e28d4 172 if(!((ax == 0.0f) && (ay == 0.0f) && (az == 0.0f))) {
Anaesthetix 0:9b434b5e28d4 173
Anaesthetix 0:9b434b5e28d4 174 // Normalise accelerometer measurement
Anaesthetix 0:9b434b5e28d4 175 recipNorm = invSqrt(ax * ax + ay * ay + az * az);
Anaesthetix 0:9b434b5e28d4 176 ax *= recipNorm;
Anaesthetix 0:9b434b5e28d4 177 ay *= recipNorm;
Anaesthetix 0:9b434b5e28d4 178 az *= recipNorm;
Anaesthetix 0:9b434b5e28d4 179
Anaesthetix 0:9b434b5e28d4 180 // Auxiliary variables to avoid repeated arithmetic
Anaesthetix 0:9b434b5e28d4 181 _2q0 = 2.0f * q0;
Anaesthetix 0:9b434b5e28d4 182 _2q1 = 2.0f * q1;
Anaesthetix 0:9b434b5e28d4 183 _2q2 = 2.0f * q2;
Anaesthetix 0:9b434b5e28d4 184 _2q3 = 2.0f * q3;
Anaesthetix 0:9b434b5e28d4 185 _4q0 = 4.0f * q0;
Anaesthetix 0:9b434b5e28d4 186 _4q1 = 4.0f * q1;
Anaesthetix 0:9b434b5e28d4 187 _4q2 = 4.0f * q2;
Anaesthetix 0:9b434b5e28d4 188 _8q1 = 8.0f * q1;
Anaesthetix 0:9b434b5e28d4 189 _8q2 = 8.0f * q2;
Anaesthetix 0:9b434b5e28d4 190 q0q0 = q0 * q0;
Anaesthetix 0:9b434b5e28d4 191 q1q1 = q1 * q1;
Anaesthetix 0:9b434b5e28d4 192 q2q2 = q2 * q2;
Anaesthetix 0:9b434b5e28d4 193 q3q3 = q3 * q3;
Anaesthetix 0:9b434b5e28d4 194
Anaesthetix 0:9b434b5e28d4 195 // Gradient decent algorithm corrective step
Anaesthetix 0:9b434b5e28d4 196 s0 = _4q0 * q2q2 + _2q2 * ax + _4q0 * q1q1 - _2q1 * ay;
Anaesthetix 0:9b434b5e28d4 197 s1 = _4q1 * q3q3 - _2q3 * ax + 4.0f * q0q0 * q1 - _2q0 * ay - _4q1 + _8q1 * q1q1 + _8q1 * q2q2 + _4q1 * az;
Anaesthetix 0:9b434b5e28d4 198 s2 = 4.0f * q0q0 * q2 + _2q0 * ax + _4q2 * q3q3 - _2q3 * ay - _4q2 + _8q2 * q1q1 + _8q2 * q2q2 + _4q2 * az;
Anaesthetix 0:9b434b5e28d4 199 s3 = 4.0f * q1q1 * q3 - _2q1 * ax + 4.0f * q2q2 * q3 - _2q2 * ay;
Anaesthetix 0:9b434b5e28d4 200 recipNorm = invSqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude
Anaesthetix 0:9b434b5e28d4 201 s0 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 202 s1 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 203 s2 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 204 s3 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 205
Anaesthetix 0:9b434b5e28d4 206 // Apply feedback step
Anaesthetix 0:9b434b5e28d4 207 qDot1 -= beta * s0;
Anaesthetix 0:9b434b5e28d4 208 qDot2 -= beta * s1;
Anaesthetix 0:9b434b5e28d4 209 qDot3 -= beta * s2;
Anaesthetix 0:9b434b5e28d4 210 qDot4 -= beta * s3;
Anaesthetix 0:9b434b5e28d4 211 }
Anaesthetix 0:9b434b5e28d4 212
Anaesthetix 0:9b434b5e28d4 213 // Integrate rate of change of quaternion to yield quaternion
Anaesthetix 0:9b434b5e28d4 214 q0 += qDot1 * invSampleFreq;
Anaesthetix 0:9b434b5e28d4 215 q1 += qDot2 * invSampleFreq;
Anaesthetix 0:9b434b5e28d4 216 q2 += qDot3 * invSampleFreq;
Anaesthetix 0:9b434b5e28d4 217 q3 += qDot4 * invSampleFreq;
Anaesthetix 0:9b434b5e28d4 218
Anaesthetix 0:9b434b5e28d4 219 // Normalise quaternion
Anaesthetix 0:9b434b5e28d4 220 recipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
Anaesthetix 0:9b434b5e28d4 221 q0 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 222 q1 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 223 q2 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 224 q3 *= recipNorm;
Anaesthetix 0:9b434b5e28d4 225 anglesComputed = 0;
Anaesthetix 0:9b434b5e28d4 226 }
Anaesthetix 0:9b434b5e28d4 227
Anaesthetix 0:9b434b5e28d4 228 //-------------------------------------------------------------------------------------------
Anaesthetix 0:9b434b5e28d4 229 // Fast inverse square-root
Anaesthetix 0:9b434b5e28d4 230 // See: http://en.wikipedia.org/wiki/Fast_inverse_square_root
Anaesthetix 0:9b434b5e28d4 231
Anaesthetix 0:9b434b5e28d4 232 /*float Madgwick::invSqrt(float x) {
Anaesthetix 0:9b434b5e28d4 233 float halfx = 0.5f * x;
Anaesthetix 0:9b434b5e28d4 234 float y = x;
Anaesthetix 0:9b434b5e28d4 235 long i = *(long*)&y;
Anaesthetix 0:9b434b5e28d4 236 i = 0x5f3759df - (i>>1);
Anaesthetix 0:9b434b5e28d4 237 y = *(float*)&i;
Anaesthetix 0:9b434b5e28d4 238 y = y * (1.5f - (halfx * y * y));
Anaesthetix 0:9b434b5e28d4 239 y = y * (1.5f - (halfx * y * y));
Anaesthetix 0:9b434b5e28d4 240 return y;
Anaesthetix 0:9b434b5e28d4 241 } */
Anaesthetix 0:9b434b5e28d4 242
Anaesthetix 0:9b434b5e28d4 243 float Madgwick::invSqrt(float x){
Anaesthetix 0:9b434b5e28d4 244 unsigned int i = 0x5F1F1412 - (*(unsigned int*)&x >> 1);
Anaesthetix 0:9b434b5e28d4 245 float tmp = *(float*)&i;
Anaesthetix 0:9b434b5e28d4 246 return tmp * (1.69000231f - 0.714158168f * x * tmp * tmp);
Anaesthetix 0:9b434b5e28d4 247 }
Anaesthetix 0:9b434b5e28d4 248
Anaesthetix 0:9b434b5e28d4 249 //-------------------------------------------------------------------------------------------
Anaesthetix 0:9b434b5e28d4 250
Anaesthetix 0:9b434b5e28d4 251 void Madgwick::computeAngles()
Anaesthetix 0:9b434b5e28d4 252 {
Anaesthetix 0:9b434b5e28d4 253 roll = atan2f(q0*q1 + q2*q3, 0.5f - q1*q1 - q2*q2);
Anaesthetix 0:9b434b5e28d4 254 pitch = asinf(-2.0f * (q1*q3 - q0*q2));
Anaesthetix 0:9b434b5e28d4 255 yaw = atan2f(q1*q2 + q0*q3, 0.5f - q2*q2 - q3*q3);
Anaesthetix 0:9b434b5e28d4 256 anglesComputed = 1;
Anaesthetix 0:9b434b5e28d4 257 }