Changing about IMU

Dependencies:   Servo mbed-rtos mbed

Fork of TurtleBot_V1 by TurtleBot

Committer:
worasuchad
Date:
Fri Mar 23 18:32:18 2018 +0000
Revision:
3:5bfa7291c639
edit and test IMU

Who changed what in which revision?

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