Sopitchaya Lummaetee / Mbed 2 deprecated TurtleBot_V55

Dependencies:   Servo mbed-rtos mbed

Fork of TurtleBot_separateloop by Sopitchaya Lummaetee

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers attitude.h Source File

attitude.h

00001 #include "MPU9250.h"
00002 
00003 MPU9250 mpu9250;
00004 
00005 Timer t;
00006 
00007 void attitude_setup(void)
00008 {
00009     i2c.frequency(400000);  // use fast (400 kHz) I2C
00010     
00011     t.start();
00012 
00013     // Read the WHO_AM_I register, this is a good test of communication
00014     uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);  // Read WHO_AM_I register for MPU-9250
00015     
00016 
00017     if (whoami == 0x73) { // WHO_AM_I should always be 0x68
00018         wait(1);
00019         mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration
00020         mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
00021         wait(2);
00022         mpu9250.initMPU9250();
00023         mpu9250.initAK8963(magCalibration);
00024         wait(2);
00025     } else while(1) ; // Loop forever if communication doesn't happen
00026 
00027     mpu9250.getAres(); // Get accelerometer sensitivity
00028     mpu9250.getGres(); // Get gyro sensitivity
00029     mpu9250.getMres(); // Get magnetometer sensitivity
00030 
00031     magbias[0] = +470.;  // User environmental x-axis correction in milliGauss, should be automatically calculated
00032     magbias[1] = +120.;  // User environmental x-axis correction in milliGauss
00033     magbias[2] = +125.;  // User environmental x-axis correction in milliGauss
00034 }
00035 
00036 int attitude_get(void)
00037 {
00038     // If intPin goes high, all data registers have new data
00039     if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) {  // On interrupt, check if data ready interrupt
00040 
00041         mpu9250.readAccelData(accelCount);  // Read the x/y/z adc values
00042         // Now we'll calculate the accleration value into actual g's
00043         ax = (float)accelCount[0]*aRes - accelBias[0];  // get actual g value, this depends on scale being set
00044         ay = (float)accelCount[1]*aRes - accelBias[1];
00045         az = (float)accelCount[2]*aRes - accelBias[2];
00046 
00047         mpu9250.readGyroData(gyroCount);  // Read the x/y/z adc values
00048         // Calculate the gyro value into actual degrees per second
00049         gx = (float)gyroCount[0]*gRes - gyroBias[0];  // get actual gyro value, this depends on scale being set
00050         gy = (float)gyroCount[1]*gRes - gyroBias[1];
00051         gz = (float)gyroCount[2]*gRes - gyroBias[2];
00052 
00053         mpu9250.readMagData(magCount);  // Read the x/y/z adc values
00054         // Calculate the magnetometer values in milliGauss
00055         // Include factory calibration per data sheet and user environmental corrections
00056         mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0];  // get actual magnetometer value, this depends on scale being set
00057         my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1];
00058         mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
00059 
00060 
00061         Now = t.read_us();
00062         deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
00063         lastUpdate = Now;
00064 
00065         // Pass gyro rate as rad/s
00066         mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f,  my,  mx, mz);
00067         mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
00068 
00069         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]);
00070         pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
00071         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]);
00072         pitch *= 180.0f / PI;
00073         yaw   *= 180.0f / PI;
00074         yaw   -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
00075         roll  *= 180.0f / PI;
00076 
00077         return 0;
00078     }
00079     return -1;
00080 }
00081