Filter for 9250

Dependencies:   mbed

Fork of MPU9250 by Ilia Manenok

Committer:
imanyonok
Date:
Fri May 06 12:40:21 2016 +0000
Revision:
0:ccea261dce7a
Child:
1:3cf2930938a8
for 9250 filter Magvick

Who changed what in which revision?

UserRevisionLine numberNew contents of line
imanyonok 0:ccea261dce7a 1 /* MPU9250 Basic Example Code
imanyonok 0:ccea261dce7a 2 by: Kris Winer
imanyonok 0:ccea261dce7a 3 date: April 1, 2014
imanyonok 0:ccea261dce7a 4 license: Beerware - Use this code however you'd like. If you
imanyonok 0:ccea261dce7a 5 find it useful you can buy me a beer some time.
imanyonok 0:ccea261dce7a 6
imanyonok 0:ccea261dce7a 7 Demonstrate basic MPU-9250 functionality including parameterizing the register addresses, initializing the sensor,
imanyonok 0:ccea261dce7a 8 getting properly scaled accelerometer, gyroscope, and magnetometer data out. Added display functions to
imanyonok 0:ccea261dce7a 9 allow display to on breadboard monitor. Addition of 9 DoF sensor fusion using open source Madgwick and
imanyonok 0:ccea261dce7a 10 Mahony filter algorithms. Sketch runs on the 3.3 V 8 MHz Pro Mini and the Teensy 3.1.
imanyonok 0:ccea261dce7a 11
imanyonok 0:ccea261dce7a 12 SDA and SCL should have external pull-up resistors (to 3.3V).
imanyonok 0:ccea261dce7a 13 10k resistors are on the EMSENSR-9250 breakout board.
imanyonok 0:ccea261dce7a 14
imanyonok 0:ccea261dce7a 15 Hardware setup:
imanyonok 0:ccea261dce7a 16 MPU9250 Breakout --------- Arduino
imanyonok 0:ccea261dce7a 17 VDD ---------------------- 3.3V
imanyonok 0:ccea261dce7a 18 VDDI --------------------- 3.3V
imanyonok 0:ccea261dce7a 19 SDA ----------------------- A4
imanyonok 0:ccea261dce7a 20 SCL ----------------------- A5
imanyonok 0:ccea261dce7a 21 GND ---------------------- GND
imanyonok 0:ccea261dce7a 22
imanyonok 0:ccea261dce7a 23 Note: The MPU9250 is an I2C sensor and uses the Arduino Wire library.
imanyonok 0:ccea261dce7a 24 Because the sensor is not 5V tolerant, we are using a 3.3 V 8 MHz Pro Mini or a 3.3 V Teensy 3.1.
imanyonok 0:ccea261dce7a 25 We have disabled the internal pull-ups used by the Wire library in the Wire.h/twi.c utility file.
imanyonok 0:ccea261dce7a 26 We are also using the 400 kHz fast I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file.
imanyonok 0:ccea261dce7a 27 */
imanyonok 0:ccea261dce7a 28
imanyonok 0:ccea261dce7a 29 #include "mbed.h"
imanyonok 0:ccea261dce7a 30 #include "MPU9250.h"
imanyonok 0:ccea261dce7a 31
imanyonok 0:ccea261dce7a 32
imanyonok 0:ccea261dce7a 33 float sum = 0;
imanyonok 0:ccea261dce7a 34 uint32_t sumCount = 0;
imanyonok 0:ccea261dce7a 35
imanyonok 0:ccea261dce7a 36 MPU9250 mpu9250;
imanyonok 0:ccea261dce7a 37
imanyonok 0:ccea261dce7a 38 Timer t;
imanyonok 0:ccea261dce7a 39
imanyonok 0:ccea261dce7a 40 Serial pc(USBTX, USBRX); // tx, rx
imanyonok 0:ccea261dce7a 41
imanyonok 0:ccea261dce7a 42
imanyonok 0:ccea261dce7a 43 int main()
imanyonok 0:ccea261dce7a 44 {
imanyonok 0:ccea261dce7a 45 pc.baud(9600);
imanyonok 0:ccea261dce7a 46
imanyonok 0:ccea261dce7a 47 //Set up I2C
imanyonok 0:ccea261dce7a 48 i2c.frequency(400000); // use fast (400 kHz) I2C
imanyonok 0:ccea261dce7a 49
imanyonok 0:ccea261dce7a 50 pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
imanyonok 0:ccea261dce7a 51
imanyonok 0:ccea261dce7a 52 t.start();
imanyonok 0:ccea261dce7a 53
imanyonok 0:ccea261dce7a 54
imanyonok 0:ccea261dce7a 55
imanyonok 0:ccea261dce7a 56 // Read the WHO_AM_I register, this is a good test of communication
imanyonok 0:ccea261dce7a 57 uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250
imanyonok 0:ccea261dce7a 58 pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r");
imanyonok 0:ccea261dce7a 59
imanyonok 0:ccea261dce7a 60 if (whoami == 0x71) // WHO_AM_I should always be 0x68
imanyonok 0:ccea261dce7a 61 {
imanyonok 0:ccea261dce7a 62 pc.printf("MPU9250 is online...\n\r");
imanyonok 0:ccea261dce7a 63 wait(1);
imanyonok 0:ccea261dce7a 64
imanyonok 0:ccea261dce7a 65
imanyonok 0:ccea261dce7a 66 mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration
imanyonok 0:ccea261dce7a 67 mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
imanyonok 0:ccea261dce7a 68 //pc.printf("x gyro bias = %f\n\r", gyroBias[0]);
imanyonok 0:ccea261dce7a 69 //pc.printf("y gyro bias = %f\n\r", gyroBias[1]);
imanyonok 0:ccea261dce7a 70 //pc.printf("z gyro bias = %f\n\r", gyroBias[2]);
imanyonok 0:ccea261dce7a 71 //pc.printf("x accel bias = %f\n\r", accelBias[0]);
imanyonok 0:ccea261dce7a 72 //pc.printf("y accel bias = %f\n\r", accelBias[1]);
imanyonok 0:ccea261dce7a 73 //pc.printf("z accel bias = %f\n\r", accelBias[2]);
imanyonok 0:ccea261dce7a 74 wait(2);
imanyonok 0:ccea261dce7a 75 mpu9250.initMPU9250();
imanyonok 0:ccea261dce7a 76 pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
imanyonok 0:ccea261dce7a 77 mpu9250.initAK8963(magCalibration);
imanyonok 0:ccea261dce7a 78 pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer
imanyonok 0:ccea261dce7a 79 pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale));
imanyonok 0:ccea261dce7a 80 pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale));
imanyonok 0:ccea261dce7a 81 if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r");
imanyonok 0:ccea261dce7a 82 if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r");
imanyonok 0:ccea261dce7a 83 if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r");
imanyonok 0:ccea261dce7a 84 if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r");
imanyonok 0:ccea261dce7a 85 wait(2);
imanyonok 0:ccea261dce7a 86 }
imanyonok 0:ccea261dce7a 87 else
imanyonok 0:ccea261dce7a 88 {
imanyonok 0:ccea261dce7a 89 pc.printf("Could not connect to MPU9250: \n\r");
imanyonok 0:ccea261dce7a 90 pc.printf("%#x \n", whoami);
imanyonok 0:ccea261dce7a 91
imanyonok 0:ccea261dce7a 92
imanyonok 0:ccea261dce7a 93
imanyonok 0:ccea261dce7a 94 while(1) ; // Loop forever if communication doesn't happen
imanyonok 0:ccea261dce7a 95 }
imanyonok 0:ccea261dce7a 96
imanyonok 0:ccea261dce7a 97 mpu9250.getAres(); // Get accelerometer sensitivity
imanyonok 0:ccea261dce7a 98 mpu9250.getGres(); // Get gyro sensitivity
imanyonok 0:ccea261dce7a 99 mpu9250.getMres(); // Get magnetometer sensitivity
imanyonok 0:ccea261dce7a 100 //pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes);
imanyonok 0:ccea261dce7a 101 //pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes);
imanyonok 0:ccea261dce7a 102 //pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes);
imanyonok 0:ccea261dce7a 103 magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated
imanyonok 0:ccea261dce7a 104 magbias[1] = +120.; // User environmental x-axis correction in milliGauss
imanyonok 0:ccea261dce7a 105 magbias[2] = +125.; // User environmental x-axis correction in milliGauss
imanyonok 0:ccea261dce7a 106
imanyonok 0:ccea261dce7a 107 while(1) {
imanyonok 0:ccea261dce7a 108
imanyonok 0:ccea261dce7a 109 // If intPin goes high, all data registers have new data
imanyonok 0:ccea261dce7a 110 if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt
imanyonok 0:ccea261dce7a 111
imanyonok 0:ccea261dce7a 112 mpu9250.readAccelData(accelCount); // Read the x/y/z adc values
imanyonok 0:ccea261dce7a 113 // Now we'll calculate the accleration value into actual g's
imanyonok 0:ccea261dce7a 114 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
imanyonok 0:ccea261dce7a 115 ay = (float)accelCount[1]*aRes - accelBias[1];
imanyonok 0:ccea261dce7a 116 az = (float)accelCount[2]*aRes - accelBias[2];
imanyonok 0:ccea261dce7a 117
imanyonok 0:ccea261dce7a 118 mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values
imanyonok 0:ccea261dce7a 119 // Calculate the gyro value into actual degrees per second
imanyonok 0:ccea261dce7a 120 gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set
imanyonok 0:ccea261dce7a 121 gy = (float)gyroCount[1]*gRes - gyroBias[1];
imanyonok 0:ccea261dce7a 122 gz = (float)gyroCount[2]*gRes - gyroBias[2];
imanyonok 0:ccea261dce7a 123
imanyonok 0:ccea261dce7a 124 mpu9250.readMagData(magCount); // Read the x/y/z adc values
imanyonok 0:ccea261dce7a 125 // Calculate the magnetometer values in milliGauss
imanyonok 0:ccea261dce7a 126 // Include factory calibration per data sheet and user environmental corrections
imanyonok 0:ccea261dce7a 127 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set
imanyonok 0:ccea261dce7a 128 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1];
imanyonok 0:ccea261dce7a 129 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
imanyonok 0:ccea261dce7a 130 }
imanyonok 0:ccea261dce7a 131
imanyonok 0:ccea261dce7a 132 Now = t.read_us();
imanyonok 0:ccea261dce7a 133 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
imanyonok 0:ccea261dce7a 134 lastUpdate = Now;
imanyonok 0:ccea261dce7a 135
imanyonok 0:ccea261dce7a 136 sum += deltat;
imanyonok 0:ccea261dce7a 137 sumCount++;
imanyonok 0:ccea261dce7a 138
imanyonok 0:ccea261dce7a 139 // if(lastUpdate - firstUpdate > 10000000.0f) {
imanyonok 0:ccea261dce7a 140 // beta = 0.04; // decrease filter gain after stabilized
imanyonok 0:ccea261dce7a 141 // zeta = 0.015; // increasey bias drift gain after stabilized
imanyonok 0:ccea261dce7a 142 // }
imanyonok 0:ccea261dce7a 143
imanyonok 0:ccea261dce7a 144 // Pass gyro rate as rad/s
imanyonok 0:ccea261dce7a 145 mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
imanyonok 0:ccea261dce7a 146 mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
imanyonok 0:ccea261dce7a 147
imanyonok 0:ccea261dce7a 148 // Serial print and/or display at 0.5 s rate independent of data rates
imanyonok 0:ccea261dce7a 149 delt_t = t.read_ms() - count;
imanyonok 0:ccea261dce7a 150 if (delt_t > 500) { // update LCD once per half-second independent of read rate
imanyonok 0:ccea261dce7a 151
imanyonok 0:ccea261dce7a 152 pc.printf("ax = %f", 1000*ax);
imanyonok 0:ccea261dce7a 153 pc.printf(" ay = %f", 1000*ay);
imanyonok 0:ccea261dce7a 154 pc.printf(" az = %f mg\n\r", 1000*az);
imanyonok 0:ccea261dce7a 155
imanyonok 0:ccea261dce7a 156 //pc.printf("gx = %f", gx);
imanyonok 0:ccea261dce7a 157 //pc.printf(" gy = %f", gy);
imanyonok 0:ccea261dce7a 158 //pc.printf(" gz = %f deg/s\n\r", gz);
imanyonok 0:ccea261dce7a 159
imanyonok 0:ccea261dce7a 160 //pc.printf("gx = %f", mx);
imanyonok 0:ccea261dce7a 161 //pc.printf(" gy = %f", my);
imanyonok 0:ccea261dce7a 162 //pc.printf(" gz = %f mG\n\r", mz);
imanyonok 0:ccea261dce7a 163
imanyonok 0:ccea261dce7a 164 tempCount = mpu9250.readTempData(); // Read the adc values
imanyonok 0:ccea261dce7a 165 temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
imanyonok 0:ccea261dce7a 166 //pc.printf(" temperature = %f C\n\r", temperature);
imanyonok 0:ccea261dce7a 167
imanyonok 0:ccea261dce7a 168 //pc.printf("q0 = %f\n\r", q[0]);
imanyonok 0:ccea261dce7a 169 //pc.printf("q1 = %f\n\r", q[1]);
imanyonok 0:ccea261dce7a 170 //pc.printf("q2 = %f\n\r", q[2]);
imanyonok 0:ccea261dce7a 171 //pc.printf("q3 = %f\n\r", q[3]);
imanyonok 0:ccea261dce7a 172
imanyonok 0:ccea261dce7a 173
imanyonok 0:ccea261dce7a 174 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
imanyonok 0:ccea261dce7a 175 // In this coordinate system, the positive z-axis is down toward Earth.
imanyonok 0:ccea261dce7a 176 // Yaw is the angle between Sensor x-axis and Earth magnetic North (or true North if corrected for local declination, looking down on the sensor positive yaw is counterclockwise.
imanyonok 0:ccea261dce7a 177 // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
imanyonok 0:ccea261dce7a 178 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
imanyonok 0:ccea261dce7a 179 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
imanyonok 0:ccea261dce7a 180 // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
imanyonok 0:ccea261dce7a 181 // applied in the correct order which for this configuration is yaw, pitch, and then roll.
imanyonok 0:ccea261dce7a 182 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
imanyonok 0:ccea261dce7a 183 yaw = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]);
imanyonok 0:ccea261dce7a 184 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
imanyonok 0:ccea261dce7a 185 roll = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
imanyonok 0:ccea261dce7a 186 pitch *= 180.0f / PI;
imanyonok 0:ccea261dce7a 187 yaw *= 180.0f / PI;
imanyonok 0:ccea261dce7a 188 yaw -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
imanyonok 0:ccea261dce7a 189 roll *= 180.0f / PI;
imanyonok 0:ccea261dce7a 190
imanyonok 0:ccea261dce7a 191 pc.printf("Yaw, Pitch, Roll: %f %f %f \n\r", yaw, pitch, roll);
imanyonok 0:ccea261dce7a 192 //pc.printf("average rate = %f\n\r", (float) sumCount/sum);
imanyonok 0:ccea261dce7a 193
imanyonok 0:ccea261dce7a 194 myled= !myled;
imanyonok 0:ccea261dce7a 195 count = t.read_ms();
imanyonok 0:ccea261dce7a 196 sum = 0;
imanyonok 0:ccea261dce7a 197 sumCount = 0;
imanyonok 0:ccea261dce7a 198 }
imanyonok 0:ccea261dce7a 199 }
imanyonok 0:ccea261dce7a 200
imanyonok 0:ccea261dce7a 201 }