bamlor nuttymaisuay

Dependencies:   mbed

Committer:
jaybehandsome
Date:
Sun Dec 10 07:16:50 2017 +0000
Revision:
0:1e46c1a32764
Child:
1:c102abc55118
bamloor nuttymaisuay;

Who changed what in which revision?

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