get attitude(roll,pitch,yaw) by Nucleo-F303K8 and MPU9250.

Dependencies:   mbed

Fork of MPU9250 by Ilia Manenok

Committer:
gitakichi
Date:
Sat Aug 06 12:03:45 2016 +0000
Revision:
3:15a3bd361cb3
Parent:
2:855c83d4c7d6
??????????MPU9250??????NUCLEO-F303K8????????; get attitude by MPU9250 and Nucleo-F303K8.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gitakichi 2:855c83d4c7d6 1 #include "MPU9250.h"
gitakichi 2:855c83d4c7d6 2
gitakichi 2:855c83d4c7d6 3 MPU9250 mpu9250;
gitakichi 2:855c83d4c7d6 4
gitakichi 2:855c83d4c7d6 5 Timer t;
gitakichi 2:855c83d4c7d6 6
gitakichi 2:855c83d4c7d6 7 void attitude_setup(void)
gitakichi 2:855c83d4c7d6 8 {
gitakichi 2:855c83d4c7d6 9 i2c.frequency(400000); // use fast (400 kHz) I2C
gitakichi 3:15a3bd361cb3 10
gitakichi 2:855c83d4c7d6 11 t.start();
gitakichi 2:855c83d4c7d6 12
gitakichi 2:855c83d4c7d6 13 // Read the WHO_AM_I register, this is a good test of communication
gitakichi 2:855c83d4c7d6 14 uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250
gitakichi 2:855c83d4c7d6 15
gitakichi 2:855c83d4c7d6 16
gitakichi 2:855c83d4c7d6 17 if (whoami == 0x71) { // WHO_AM_I should always be 0x68
gitakichi 2:855c83d4c7d6 18 wait(1);
gitakichi 2:855c83d4c7d6 19 mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration
gitakichi 2:855c83d4c7d6 20 mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
gitakichi 2:855c83d4c7d6 21 wait(2);
gitakichi 2:855c83d4c7d6 22 mpu9250.initMPU9250();
gitakichi 2:855c83d4c7d6 23 mpu9250.initAK8963(magCalibration);
gitakichi 2:855c83d4c7d6 24 wait(2);
gitakichi 2:855c83d4c7d6 25 } else while(1) ; // Loop forever if communication doesn't happen
gitakichi 2:855c83d4c7d6 26
gitakichi 2:855c83d4c7d6 27 mpu9250.getAres(); // Get accelerometer sensitivity
gitakichi 2:855c83d4c7d6 28 mpu9250.getGres(); // Get gyro sensitivity
gitakichi 2:855c83d4c7d6 29 mpu9250.getMres(); // Get magnetometer sensitivity
gitakichi 2:855c83d4c7d6 30
gitakichi 2:855c83d4c7d6 31 magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated
gitakichi 2:855c83d4c7d6 32 magbias[1] = +120.; // User environmental x-axis correction in milliGauss
gitakichi 2:855c83d4c7d6 33 magbias[2] = +125.; // User environmental x-axis correction in milliGauss
gitakichi 2:855c83d4c7d6 34 }
gitakichi 2:855c83d4c7d6 35
gitakichi 2:855c83d4c7d6 36 int attitude_get(void)
gitakichi 2:855c83d4c7d6 37 {
gitakichi 2:855c83d4c7d6 38 // If intPin goes high, all data registers have new data
gitakichi 2:855c83d4c7d6 39 if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt
gitakichi 2:855c83d4c7d6 40
gitakichi 2:855c83d4c7d6 41 mpu9250.readAccelData(accelCount); // Read the x/y/z adc values
gitakichi 2:855c83d4c7d6 42 // Now we'll calculate the accleration value into actual g's
gitakichi 2:855c83d4c7d6 43 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
gitakichi 2:855c83d4c7d6 44 ay = (float)accelCount[1]*aRes - accelBias[1];
gitakichi 2:855c83d4c7d6 45 az = (float)accelCount[2]*aRes - accelBias[2];
gitakichi 2:855c83d4c7d6 46
gitakichi 2:855c83d4c7d6 47 mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values
gitakichi 2:855c83d4c7d6 48 // Calculate the gyro value into actual degrees per second
gitakichi 2:855c83d4c7d6 49 gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set
gitakichi 2:855c83d4c7d6 50 gy = (float)gyroCount[1]*gRes - gyroBias[1];
gitakichi 2:855c83d4c7d6 51 gz = (float)gyroCount[2]*gRes - gyroBias[2];
gitakichi 2:855c83d4c7d6 52
gitakichi 2:855c83d4c7d6 53 mpu9250.readMagData(magCount); // Read the x/y/z adc values
gitakichi 2:855c83d4c7d6 54 // Calculate the magnetometer values in milliGauss
gitakichi 2:855c83d4c7d6 55 // Include factory calibration per data sheet and user environmental corrections
gitakichi 2:855c83d4c7d6 56 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set
gitakichi 2:855c83d4c7d6 57 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1];
gitakichi 2:855c83d4c7d6 58 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
gitakichi 2:855c83d4c7d6 59
gitakichi 2:855c83d4c7d6 60
gitakichi 2:855c83d4c7d6 61 Now = t.read_us();
gitakichi 2:855c83d4c7d6 62 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
gitakichi 2:855c83d4c7d6 63 lastUpdate = Now;
gitakichi 2:855c83d4c7d6 64
gitakichi 2:855c83d4c7d6 65 // Pass gyro rate as rad/s
gitakichi 2:855c83d4c7d6 66 mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
gitakichi 2:855c83d4c7d6 67 mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
gitakichi 2:855c83d4c7d6 68
gitakichi 2:855c83d4c7d6 69 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]);
gitakichi 2:855c83d4c7d6 70 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
gitakichi 2:855c83d4c7d6 71 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]);
gitakichi 2:855c83d4c7d6 72 pitch *= 180.0f / PI;
gitakichi 2:855c83d4c7d6 73 yaw *= 180.0f / PI;
gitakichi 2:855c83d4c7d6 74 yaw -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
gitakichi 2:855c83d4c7d6 75 roll *= 180.0f / PI;
gitakichi 2:855c83d4c7d6 76
gitakichi 2:855c83d4c7d6 77 return 0;
gitakichi 2:855c83d4c7d6 78 }
gitakichi 2:855c83d4c7d6 79 return -1;
gitakichi 2:855c83d4c7d6 80 }
gitakichi 2:855c83d4c7d6 81