MPU9250 test with polling or ISR

Dependencies:   mbed

Committer:
manitou
Date:
Sat Sep 10 14:15:19 2016 +0000
Revision:
0:31cc139b7d1e
Child:
1:0158e4d78423
MPU9250 test polling or ISR

Who changed what in which revision?

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