GPS1_kevin projet c MPU9250

Dependencies:   mbed ST_401_84MHZ

Committer:
kekette
Date:
Thu Dec 12 15:34:50 2019 +0000
Revision:
0:507d1a0c6655
test

Who changed what in which revision?

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