Tisham Dhar / Hexi_GPSIMU_Hotshoe

Use hexiwear as a GPSIMU-AHRS for Nikon DSLR cameras

Dependencies:   FXOS8700Q FXAS21000 MBed_Adafruit-GPS-Library Hexi_OLED_SSD1351

Fork of Hexi_Blinky_Example by Hexiwear

/media/uploads/whatnick/hexiwear_docking_station_numbers.jpg

Committer:
whatnick
Date:
Tue Sep 20 04:18:34 2016 +0000
Revision:
24:cbdf0f7d33bd
Added sensor fusion code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
whatnick24:cbdf0f7d33bd 1#ifndef SENSOR_FUSION_H
whatnick24:cbdf0f7d33bd 2#define SENSOR_FUSION_H
whatnick24:cbdf0f7d33bd 3
whatnick24:cbdf0f7d33bd 4#include "mbed.h"
whatnick24:cbdf0f7d33bd 5
whatnick24:cbdf0f7d33bd 6// parameters for 6 DoF sensor fusion calculations
whatnick24:cbdf0f7d33bd 7float PI = 3.14159265358979323846f;
whatnick24:cbdf0f7d33bd 8float GyroMeasError = PI * (60.0f / 180.0f); // gyroscope measurement error in rads/s (start at 60 deg/s), then reduce after ~10 s to 3
whatnick24:cbdf0f7d33bd 9float beta = sqrt(3.0f / 4.0f) * GyroMeasError; // compute beta
whatnick24:cbdf0f7d33bd 10float GyroMeasDrift = PI * (1.0f / 180.0f); // gyroscope measurement drift in rad/s/s (start at 0.0 deg/s/s)
whatnick24:cbdf0f7d33bd 11float zeta = sqrt(3.0f / 4.0f) * GyroMeasDrift; // compute zeta, the other free parameter in the Madgwick scheme usually set to a small or zero value
whatnick24:cbdf0f7d33bd 12#define Kp 2.0f * 5.0f // these are the free parameters in the Mahony filter and fusion scheme, Kp for proportional feedback, Ki for integral
whatnick24:cbdf0f7d33bd 13#define Ki 0.0f
whatnick24:cbdf0f7d33bd 14
whatnick24:cbdf0f7d33bd 15
whatnick24:cbdf0f7d33bd 16float q[4] = {1.0f, 0.0f, 0.0f, 0.0f}; // vector to hold quaternion
whatnick24:cbdf0f7d33bd 17float eInt[3] = {0.0f, 0.0f, 0.0f}; // vector to hold integral error for Mahony method
whatnick24:cbdf0f7d33bd 18float pitch, yaw, roll;
whatnick24:cbdf0f7d33bd 19float deltat = 0.0f; // integration interval for both filter schemes
whatnick24:cbdf0f7d33bd 20int lastUpdate = 0, firstUpdate = 0, Now = 0; // used to calculate integration interval
whatnick24:cbdf0f7d33bd 21
whatnick24:cbdf0f7d33bd 22// Implementation of Sebastian Madgwick's "...efficient orientation filter for... inertial/magnetic sensor arrays"
whatnick24:cbdf0f7d33bd 23// (see http://www.x-io.co.uk/category/open-source/ for examples and more details)
whatnick24:cbdf0f7d33bd 24// which fuses acceleration, rotation rate, and magnetic moments to produce a quaternion-based estimate of absolute
whatnick24:cbdf0f7d33bd 25// device orientation -- which can be converted to yaw, pitch, and roll. Useful for stabilizing quadcopters, etc.
whatnick24:cbdf0f7d33bd 26// The performance of the orientation filter is at least as good as conventional Kalman-based filtering algorithms
whatnick24:cbdf0f7d33bd 27// but is much less computationally intensive---it can be performed on a 3.3 V Pro Mini operating at 8 MHz!
whatnick24:cbdf0f7d33bd 28void MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz)
whatnick24:cbdf0f7d33bd 29{
whatnick24:cbdf0f7d33bd 30 float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3]; // short name local variable for readability
whatnick24:cbdf0f7d33bd 31 float norm;
whatnick24:cbdf0f7d33bd 32 float hx, hy, _2bx, _2bz;
whatnick24:cbdf0f7d33bd 33 float s1, s2, s3, s4;
whatnick24:cbdf0f7d33bd 34 float qDot1, qDot2, qDot3, qDot4;
whatnick24:cbdf0f7d33bd 35
whatnick24:cbdf0f7d33bd 36 // Auxiliary variables to avoid repeated arithmetic
whatnick24:cbdf0f7d33bd 37 float _2q1mx;
whatnick24:cbdf0f7d33bd 38 float _2q1my;
whatnick24:cbdf0f7d33bd 39 float _2q1mz;
whatnick24:cbdf0f7d33bd 40 float _2q2mx;
whatnick24:cbdf0f7d33bd 41 float _4bx;
whatnick24:cbdf0f7d33bd 42 float _4bz;
whatnick24:cbdf0f7d33bd 43 float _2q1 = 2.0f * q1;
whatnick24:cbdf0f7d33bd 44 float _2q2 = 2.0f * q2;
whatnick24:cbdf0f7d33bd 45 float _2q3 = 2.0f * q3;
whatnick24:cbdf0f7d33bd 46 float _2q4 = 2.0f * q4;
whatnick24:cbdf0f7d33bd 47 float _2q1q3 = 2.0f * q1 * q3;
whatnick24:cbdf0f7d33bd 48 float _2q3q4 = 2.0f * q3 * q4;
whatnick24:cbdf0f7d33bd 49 float q1q1 = q1 * q1;
whatnick24:cbdf0f7d33bd 50 float q1q2 = q1 * q2;
whatnick24:cbdf0f7d33bd 51 float q1q3 = q1 * q3;
whatnick24:cbdf0f7d33bd 52 float q1q4 = q1 * q4;
whatnick24:cbdf0f7d33bd 53 float q2q2 = q2 * q2;
whatnick24:cbdf0f7d33bd 54 float q2q3 = q2 * q3;
whatnick24:cbdf0f7d33bd 55 float q2q4 = q2 * q4;
whatnick24:cbdf0f7d33bd 56 float q3q3 = q3 * q3;
whatnick24:cbdf0f7d33bd 57 float q3q4 = q3 * q4;
whatnick24:cbdf0f7d33bd 58 float q4q4 = q4 * q4;
whatnick24:cbdf0f7d33bd 59
whatnick24:cbdf0f7d33bd 60 // Normalise accelerometer measurement
whatnick24:cbdf0f7d33bd 61 norm = sqrt(ax * ax + ay * ay + az * az);
whatnick24:cbdf0f7d33bd 62 if (norm == 0.0f) return; // handle NaN
whatnick24:cbdf0f7d33bd 63 norm = 1.0f/norm;
whatnick24:cbdf0f7d33bd 64 ax *= norm;
whatnick24:cbdf0f7d33bd 65 ay *= norm;
whatnick24:cbdf0f7d33bd 66 az *= norm;
whatnick24:cbdf0f7d33bd 67
whatnick24:cbdf0f7d33bd 68 // Normalise magnetometer measurement
whatnick24:cbdf0f7d33bd 69 norm = sqrt(mx * mx + my * my + mz * mz);
whatnick24:cbdf0f7d33bd 70 if (norm == 0.0f) return; // handle NaN
whatnick24:cbdf0f7d33bd 71 norm = 1.0f/norm;
whatnick24:cbdf0f7d33bd 72 mx *= norm;
whatnick24:cbdf0f7d33bd 73 my *= norm;
whatnick24:cbdf0f7d33bd 74 mz *= norm;
whatnick24:cbdf0f7d33bd 75
whatnick24:cbdf0f7d33bd 76 // Reference direction of Earth's magnetic field
whatnick24:cbdf0f7d33bd 77 _2q1mx = 2.0f * q1 * mx;
whatnick24:cbdf0f7d33bd 78 _2q1my = 2.0f * q1 * my;
whatnick24:cbdf0f7d33bd 79 _2q1mz = 2.0f * q1 * mz;
whatnick24:cbdf0f7d33bd 80 _2q2mx = 2.0f * q2 * mx;
whatnick24:cbdf0f7d33bd 81 hx = mx * q1q1 - _2q1my * q4 + _2q1mz * q3 + mx * q2q2 + _2q2 * my * q3 + _2q2 * mz * q4 - mx * q3q3 - mx * q4q4;
whatnick24:cbdf0f7d33bd 82 hy = _2q1mx * q4 + my * q1q1 - _2q1mz * q2 + _2q2mx * q3 - my * q2q2 + my * q3q3 + _2q3 * mz * q4 - my * q4q4;
whatnick24:cbdf0f7d33bd 83 _2bx = sqrt(hx * hx + hy * hy);
whatnick24:cbdf0f7d33bd 84 _2bz = -_2q1mx * q3 + _2q1my * q2 + mz * q1q1 + _2q2mx * q4 - mz * q2q2 + _2q3 * my * q4 - mz * q3q3 + mz * q4q4;
whatnick24:cbdf0f7d33bd 85 _4bx = 2.0f * _2bx;
whatnick24:cbdf0f7d33bd 86 _4bz = 2.0f * _2bz;
whatnick24:cbdf0f7d33bd 87
whatnick24:cbdf0f7d33bd 88 // Gradient decent algorithm corrective step
whatnick24:cbdf0f7d33bd 89 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);
whatnick24:cbdf0f7d33bd 90 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);
whatnick24:cbdf0f7d33bd 91 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);
whatnick24:cbdf0f7d33bd 92 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);
whatnick24:cbdf0f7d33bd 93 norm = sqrt(s1 * s1 + s2 * s2 + s3 * s3 + s4 * s4); // normalise step magnitude
whatnick24:cbdf0f7d33bd 94 norm = 1.0f/norm;
whatnick24:cbdf0f7d33bd 95 s1 *= norm;
whatnick24:cbdf0f7d33bd 96 s2 *= norm;
whatnick24:cbdf0f7d33bd 97 s3 *= norm;
whatnick24:cbdf0f7d33bd 98 s4 *= norm;
whatnick24:cbdf0f7d33bd 99
whatnick24:cbdf0f7d33bd 100 // Compute rate of change of quaternion
whatnick24:cbdf0f7d33bd 101 qDot1 = 0.5f * (-q2 * gx - q3 * gy - q4 * gz) - beta * s1;
whatnick24:cbdf0f7d33bd 102 qDot2 = 0.5f * (q1 * gx + q3 * gz - q4 * gy) - beta * s2;
whatnick24:cbdf0f7d33bd 103 qDot3 = 0.5f * (q1 * gy - q2 * gz + q4 * gx) - beta * s3;
whatnick24:cbdf0f7d33bd 104 qDot4 = 0.5f * (q1 * gz + q2 * gy - q3 * gx) - beta * s4;
whatnick24:cbdf0f7d33bd 105
whatnick24:cbdf0f7d33bd 106 // Integrate to yield quaternion
whatnick24:cbdf0f7d33bd 107 q1 += qDot1 * deltat;
whatnick24:cbdf0f7d33bd 108 q2 += qDot2 * deltat;
whatnick24:cbdf0f7d33bd 109 q3 += qDot3 * deltat;
whatnick24:cbdf0f7d33bd 110 q4 += qDot4 * deltat;
whatnick24:cbdf0f7d33bd 111 norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4); // normalise quaternion
whatnick24:cbdf0f7d33bd 112 norm = 1.0f/norm;
whatnick24:cbdf0f7d33bd 113 q[0] = q1 * norm;
whatnick24:cbdf0f7d33bd 114 q[1] = q2 * norm;
whatnick24:cbdf0f7d33bd 115 q[2] = q3 * norm;
whatnick24:cbdf0f7d33bd 116 q[3] = q4 * norm;
whatnick24:cbdf0f7d33bd 117
whatnick24:cbdf0f7d33bd 118}
whatnick24:cbdf0f7d33bd 119
whatnick24:cbdf0f7d33bd 120// Similar to Madgwick scheme but uses proportional and integral filtering on the error between estimated reference vectors and
whatnick24:cbdf0f7d33bd 121// measured ones.
whatnick24:cbdf0f7d33bd 122void MahonyQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz)
whatnick24:cbdf0f7d33bd 123{
whatnick24:cbdf0f7d33bd 124 float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3]; // short name local variable for readability
whatnick24:cbdf0f7d33bd 125 float norm;
whatnick24:cbdf0f7d33bd 126 float hx, hy, bx, bz;
whatnick24:cbdf0f7d33bd 127 float vx, vy, vz, wx, wy, wz;
whatnick24:cbdf0f7d33bd 128 float ex, ey, ez;
whatnick24:cbdf0f7d33bd 129 float pa, pb, pc;
whatnick24:cbdf0f7d33bd 130
whatnick24:cbdf0f7d33bd 131 // Auxiliary variables to avoid repeated arithmetic
whatnick24:cbdf0f7d33bd 132 float q1q1 = q1 * q1;
whatnick24:cbdf0f7d33bd 133 float q1q2 = q1 * q2;
whatnick24:cbdf0f7d33bd 134 float q1q3 = q1 * q3;
whatnick24:cbdf0f7d33bd 135 float q1q4 = q1 * q4;
whatnick24:cbdf0f7d33bd 136 float q2q2 = q2 * q2;
whatnick24:cbdf0f7d33bd 137 float q2q3 = q2 * q3;
whatnick24:cbdf0f7d33bd 138 float q2q4 = q2 * q4;
whatnick24:cbdf0f7d33bd 139 float q3q3 = q3 * q3;
whatnick24:cbdf0f7d33bd 140 float q3q4 = q3 * q4;
whatnick24:cbdf0f7d33bd 141 float q4q4 = q4 * q4;
whatnick24:cbdf0f7d33bd 142
whatnick24:cbdf0f7d33bd 143 // Normalise accelerometer measurement
whatnick24:cbdf0f7d33bd 144 norm = sqrt(ax * ax + ay * ay + az * az);
whatnick24:cbdf0f7d33bd 145 if (norm == 0.0f) return; // handle NaN
whatnick24:cbdf0f7d33bd 146 norm = 1.0f / norm; // use reciprocal for division
whatnick24:cbdf0f7d33bd 147 ax *= norm;
whatnick24:cbdf0f7d33bd 148 ay *= norm;
whatnick24:cbdf0f7d33bd 149 az *= norm;
whatnick24:cbdf0f7d33bd 150
whatnick24:cbdf0f7d33bd 151 // Normalise magnetometer measurement
whatnick24:cbdf0f7d33bd 152 norm = sqrt(mx * mx + my * my + mz * mz);
whatnick24:cbdf0f7d33bd 153 if (norm == 0.0f) return; // handle NaN
whatnick24:cbdf0f7d33bd 154 norm = 1.0f / norm; // use reciprocal for division
whatnick24:cbdf0f7d33bd 155 mx *= norm;
whatnick24:cbdf0f7d33bd 156 my *= norm;
whatnick24:cbdf0f7d33bd 157 mz *= norm;
whatnick24:cbdf0f7d33bd 158
whatnick24:cbdf0f7d33bd 159 // Reference direction of Earth's magnetic field
whatnick24:cbdf0f7d33bd 160 hx = 2.0f * mx * (0.5f - q3q3 - q4q4) + 2.0f * my * (q2q3 - q1q4) + 2.0f * mz * (q2q4 + q1q3);
whatnick24:cbdf0f7d33bd 161 hy = 2.0f * mx * (q2q3 + q1q4) + 2.0f * my * (0.5f - q2q2 - q4q4) + 2.0f * mz * (q3q4 - q1q2);
whatnick24:cbdf0f7d33bd 162 bx = sqrt((hx * hx) + (hy * hy));
whatnick24:cbdf0f7d33bd 163 bz = 2.0f * mx * (q2q4 - q1q3) + 2.0f * my * (q3q4 + q1q2) + 2.0f * mz * (0.5f - q2q2 - q3q3);
whatnick24:cbdf0f7d33bd 164
whatnick24:cbdf0f7d33bd 165 // Estimated direction of gravity and magnetic field
whatnick24:cbdf0f7d33bd 166 vx = 2.0f * (q2q4 - q1q3);
whatnick24:cbdf0f7d33bd 167 vy = 2.0f * (q1q2 + q3q4);
whatnick24:cbdf0f7d33bd 168 vz = q1q1 - q2q2 - q3q3 + q4q4;
whatnick24:cbdf0f7d33bd 169 wx = 2.0f * bx * (0.5f - q3q3 - q4q4) + 2.0f * bz * (q2q4 - q1q3);
whatnick24:cbdf0f7d33bd 170 wy = 2.0f * bx * (q2q3 - q1q4) + 2.0f * bz * (q1q2 + q3q4);
whatnick24:cbdf0f7d33bd 171 wz = 2.0f * bx * (q1q3 + q2q4) + 2.0f * bz * (0.5f - q2q2 - q3q3);
whatnick24:cbdf0f7d33bd 172
whatnick24:cbdf0f7d33bd 173 // Error is cross product between estimated direction and measured direction of gravity
whatnick24:cbdf0f7d33bd 174 ex = (ay * vz - az * vy) + (my * wz - mz * wy);
whatnick24:cbdf0f7d33bd 175 ey = (az * vx - ax * vz) + (mz * wx - mx * wz);
whatnick24:cbdf0f7d33bd 176 ez = (ax * vy - ay * vx) + (mx * wy - my * wx);
whatnick24:cbdf0f7d33bd 177 if (Ki > 0.0f) {
whatnick24:cbdf0f7d33bd 178 eInt[0] += ex; // accumulate integral error
whatnick24:cbdf0f7d33bd 179 eInt[1] += ey;
whatnick24:cbdf0f7d33bd 180 eInt[2] += ez;
whatnick24:cbdf0f7d33bd 181 } else {
whatnick24:cbdf0f7d33bd 182 eInt[0] = 0.0f; // prevent integral wind up
whatnick24:cbdf0f7d33bd 183 eInt[1] = 0.0f;
whatnick24:cbdf0f7d33bd 184 eInt[2] = 0.0f;
whatnick24:cbdf0f7d33bd 185 }
whatnick24:cbdf0f7d33bd 186
whatnick24:cbdf0f7d33bd 187 // Apply feedback terms
whatnick24:cbdf0f7d33bd 188 gx = gx + Kp * ex + Ki * eInt[0];
whatnick24:cbdf0f7d33bd 189 gy = gy + Kp * ey + Ki * eInt[1];
whatnick24:cbdf0f7d33bd 190 gz = gz + Kp * ez + Ki * eInt[2];
whatnick24:cbdf0f7d33bd 191
whatnick24:cbdf0f7d33bd 192 // Integrate rate of change of quaternion
whatnick24:cbdf0f7d33bd 193 pa = q2;
whatnick24:cbdf0f7d33bd 194 pb = q3;
whatnick24:cbdf0f7d33bd 195 pc = q4;
whatnick24:cbdf0f7d33bd 196 q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * deltat);
whatnick24:cbdf0f7d33bd 197 q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * deltat);
whatnick24:cbdf0f7d33bd 198 q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * deltat);
whatnick24:cbdf0f7d33bd 199 q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * deltat);
whatnick24:cbdf0f7d33bd 200
whatnick24:cbdf0f7d33bd 201 // Normalise quaternion
whatnick24:cbdf0f7d33bd 202 norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4);
whatnick24:cbdf0f7d33bd 203 norm = 1.0f / norm;
whatnick24:cbdf0f7d33bd 204 q[0] = q1 * norm;
whatnick24:cbdf0f7d33bd 205 q[1] = q2 * norm;
whatnick24:cbdf0f7d33bd 206 q[2] = q3 * norm;
whatnick24:cbdf0f7d33bd 207 q[3] = q4 * norm;
whatnick24:cbdf0f7d33bd 208
whatnick24:cbdf0f7d33bd 209}
whatnick24:cbdf0f7d33bd 210
whatnick24:cbdf0f7d33bd 211#endif