MPU9250 test with polling or ISR

Dependencies:   mbed

Committer:
manitou
Date:
Mon Nov 19 11:58:46 2018 +0000
Revision:
1:0158e4d78423
Parent:
0:31cc139b7d1e
format

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