Not done - might need - for later

Dependencies:   FXOS8700CQ MPU6050IMU mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MPU6050.h"
00003 #include "FXOS8700CQ.h"
00004 
00005 float sum = 0;
00006 uint32_t sumCount = 0;
00007  
00008 MPU6050 mpu6050;
00009 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
00010 
00011 Timer t;
00012 
00013 InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt
00014 bool fxos_int2_triggered = false; // Interrupt status flags and data
00015  
00016 SRAWDATA accel_data; // Storage for the data from the sensor
00017 SRAWDATA magn_data;
00018 
00019 Serial pc(USBTX, USBRX); // tx, rx
00020 
00021 void trigger_fxos_int2(void){
00022     fxos_int2_triggered = true;
00023 }
00024  
00025 void print_reading(){
00026     pc.printf("A X:%5d,Y:%5d,Z:%5d   M X:%5d,Y:%5d,Z:%5d\r\n",
00027               accel_data.x, accel_data.y, accel_data.z,
00028               magn_data.x, magn_data.y, magn_data.z);
00029 }
00030 
00031 int main(){
00032   t.start();
00033   pc.baud(115200); // 200Hz x line of output data!  
00034   
00035   printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", fxos.get_whoami());
00036      
00037   fxos_int2.fall(&trigger_fxos_int2); // Iterrupt for active-low interrupt line from FXOS
00038   fxos.enable();
00039   
00040   pc.printf("Started data collection. Accelerometer at max %dg.\r\n",
00041   fxos.get_accel_scale());
00042  
00043   fxos.get_data(&accel_data, &magn_data); // clear interrupt from device
00044   fxos_int2_triggered = false; // un-trigger
00045   
00046   //Set up I2C
00047   i2c.frequency(400000);  // use fast (400 kHz) I2C   
00048     
00049   // Read the WHO_AM_I register, this is a good test of communication
00050   uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050);  // Read WHO_AM_I register for MPU-6050
00051   pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r");
00052   
00053   if (whoami == 0x68){ // WHO_AM_I should always be 0x68  
00054     pc.printf("MPU6050 is online...");
00055     wait(1);
00056     
00057     mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values
00058     pc.printf("x-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[0]); pc.printf("% of factory value \n\r");
00059     pc.printf("y-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[1]); pc.printf("% of factory value \n\r");
00060     pc.printf("z-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[2]); pc.printf("% of factory value \n\r");
00061     pc.printf("x-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[3]); pc.printf("% of factory value \n\r");
00062     pc.printf("y-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[4]); pc.printf("% of factory value \n\r");
00063     pc.printf("z-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[5]); pc.printf("% of factory value \n\r");
00064     wait(1);
00065  
00066     if(SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f){
00067         mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration
00068         mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers  
00069         mpu6050.initMPU6050(); pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
00070         wait(2);
00071     }else pc.printf("Device did not the pass self-test!\n\r");
00072     }else{
00073         pc.printf("Could not connect to MPU6050: \n\r");
00074         pc.printf("%#x \n",  whoami);
00075         while(1) ; // Loop forever if communication doesn't happen
00076     }
00077  
00078  while(1){
00079   // If data ready bit set, all data registers have new data
00080   if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) {  // check if data ready interrupt
00081     mpu6050.readAccelData(accelCount);  // Read the x/y/z adc values
00082     mpu6050.getAres();
00083     
00084     // Now we'll calculate the accleration value into actual g's
00085     ax = (float)accelCount[0]*aRes - accelBias[0];  // get actual g value, this depends on scale being set
00086     ay = (float)accelCount[1]*aRes - accelBias[1];   
00087     az = (float)accelCount[2]*aRes - accelBias[2];  
00088    
00089     mpu6050.readGyroData(gyroCount);  // Read the x/y/z adc values
00090     mpu6050.getGres();
00091  
00092     // Calculate the gyro value into actual degrees per second
00093     gx = (float)gyroCount[0]*gRes; // - gyroBias[0];  // get actual gyro value, this depends on scale being set
00094     gy = (float)gyroCount[1]*gRes; // - gyroBias[1];  
00095     gz = (float)gyroCount[2]*gRes; // - gyroBias[2];   
00096  
00097     tempCount = mpu6050.readTempData();  // Read the x/y/z adc values
00098     temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade
00099    }
00100    
00101    if(fxos_int2_triggered){
00102            fxos_int2_triggered = false; // un-trigger
00103            fxos.get_data(&accel_data, &magn_data);
00104    }  
00105    
00106     Now = t.read_us();
00107     deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
00108     lastUpdate = Now;
00109     
00110     sum += deltat;
00111     sumCount++;
00112     
00113     if(lastUpdate - firstUpdate > 10000000.0f) {
00114      beta = 0.04;  // decrease filter gain after stabilized
00115      zeta = 0.015; // increasey bias drift gain after stabilized
00116     }
00117     
00118    // Pass gyro rate as rad/s
00119     mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f);
00120  
00121     // Serial print and/or display at 0.5 s rate independent of data rates
00122     delt_t = t.read_ms() - count;
00123 
00124     pc.printf("ax = %f", 1000*ax); 
00125     pc.printf(" ay = %f", 1000*ay); 
00126     pc.printf(" az = %f  mg\n\r", 1000*az); 
00127  
00128     pc.printf("gx = %f", gx); 
00129     pc.printf(" gy = %f", gy); 
00130     pc.printf(" gz = %f  deg/s\n\r", gz); 
00131     
00132     pc.printf(" temperature = %f  C\n\r", temperature); 
00133     
00134     pc.printf("q0 = %f\n\r", q[0]);
00135     pc.printf("q1 = %f\n\r", q[1]);
00136     pc.printf("q2 = %f\n\r", q[2]);
00137     pc.printf("q3 = %f\n\r", q[3]);      
00138 
00139     
00140     
00141   // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
00142   // In this coordinate system, the positive z-axis is down toward Earth. 
00143   // 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.
00144   // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
00145   // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
00146   // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
00147   // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
00148   // applied in the correct order which for this configuration is yaw, pitch, and then roll.
00149   // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
00150     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]);   
00151     pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
00152     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]);
00153     pitch *= 180.0f / PI;
00154     yaw   *= 180.0f / PI; 
00155     roll  *= 180.0f / PI;
00156  
00157 //    pc.printf("Yaw, Pitch, Roll: \n\r");
00158 //    pc.printf("%f", yaw);
00159 //    pc.printf(", ");
00160 //    pc.printf("%f", pitch);
00161 //    pc.printf(", ");
00162 //    pc.printf("%f\n\r", roll);
00163 //    pc.printf("average rate = "); pc.printf("%f", (sumCount/sum)); pc.printf(" Hz\n\r");
00164  
00165      pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll);
00166      pc.printf("average rate = %f\n\r", (float) sumCount/sum);
00167  
00168     count = t.read_ms(); 
00169     sum = 0;
00170     sumCount = 0; 
00171 }
00172 }