Maxwell Poster / Mbed 2 deprecated CS335_Final_Project_i2c_MPU6050_Gyro_Mouse

Dependencies:   mbed MPU6050_template ledControl2 USBDevice

Files at this revision

API Documentation at this revision

Comitter:
BaserK
Date:
Mon Jul 13 13:19:08 2015 +0000
Parent:
2:497faa1563ea
Child:
4:33fef1998fc8
Commit message:
Complementary filter and pitch, roll angles are added

Changed in this revision

MPU6050.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/MPU6050.lib	Thu Jul 09 12:28:26 2015 +0000
+++ b/MPU6050.lib	Mon Jul 13 13:19:08 2015 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/BaserK/code/MPU6050/#954f15bd95f1
+https://developer.mbed.org/users/BaserK/code/MPU6050/#a248e65a25cc
--- a/main.cpp	Thu Jul 09 12:28:26 2015 +0000
+++ b/main.cpp	Mon Jul 13 13:19:08 2015 +0000
@@ -40,9 +40,15 @@
 Serial ftdi(p13,p14);    // default baud rate: 9600
 MPU6050 mpu6050;         // class: MPU6050, object: mpu6050 
 Ticker toggler1;
+Ticker filter;           
 
 void toggle_led1();
 void toggle_led2();
+void complementaryFilter(float* pitch, float* roll);
+void compFilter();
+
+float pitchAngle = 0;
+float rollAngle = 0;
 
 int main() 
 {
@@ -60,28 +66,72 @@
     
     while(1) 
     {
-        /* Get actual acc value */
-        mpu6050.readAccelData(accelData);
-        mpu6050.getAres();
-        ax = accelData[0]*aRes - accelBias[0];
-        ay = accelData[1]*aRes - accelBias[1];
-        az = accelData[2]*aRes - accelBias[2]; 
+//        /* Get actual acc value */
+//        mpu6050.readAccelData(accelData);
+//        mpu6050.getAres();
+//        ax = accelData[0]*aRes - accelBias[0];
+//        ay = accelData[1]*aRes - accelBias[1];
+//        az = accelData[2]*aRes - accelBias[2]; 
+//        
+//        /* Get actual gyro value */
+//        mpu6050.readGyroData(gyroData);
+//        mpu6050.getGres();     
+//        gx = gyroData[0]*gRes;  // - gyroBias[0];      // Results are better without extracting gyroBias[i]
+//        gy = gyroData[1]*gRes;  // - gyroBias[1]; 
+//        gz = gyroData[2]*gRes;  // - gyroBias[2]; 
+//        
+//        ftdi.printf(" _____________________________________________________________  \r\n");
+//        ftdi.printf("| Accelerometer(g) | ax=%.3f | ay=%.3f | az=%.3f                \r\n",ax,ay,az);
+//        ftdi.printf("| Gyroscope(deg/s) | gx=%.3f | gy=%.3f | gz=%.3f                \r\n",gx,gy,gz);
+//        ftdi.printf("|_____________________________________________________________  \r\n\r\n");
+//        
+//        wait(2.5);
+                
+        filter.attach(&compFilter, 0.005);    // Call the complementaryFilter func. every 5 ms (200 Hz sampling period)
         
-        /* Get actual gyro value */
-        mpu6050.readGyroData(gyroData);
-        mpu6050.getGres();     
-        gx = gyroData[0]*gRes;  // - gyroBias[0];      // Results are better without extracting gyroBias[i]
-        gy = gyroData[1]*gRes;  // - gyroBias[1]; 
-        gz = gyroData[2]*gRes;  // - gyroBias[2]; 
+        ftdi.printf(" _______________\r\n");
+        ftdi.printf("| Pitch: %.3f   \r\n",pitchAngle);
+        ftdi.printf("| Roll:  %.3f   \r\n",rollAngle);
+        ftdi.printf("|_______________\r\n\r\n");
         
-        ftdi.printf(" _____________________________________________________________  \r\n");
-        ftdi.printf("| Accelerometer(g) | ax=%.3f | ay=%.3f | az=%.3f                \r\n",ax,ay,az);
-        ftdi.printf("| Gyroscope(deg/s) | gx=%.3f | gy=%.3f | gz=%.3f                \r\n",gx,gy,gz);
-        ftdi.printf("|_____________________________________________________________  \r\n\r\n");
-        
-        wait(2.5);
+        wait(1);
     }
 }
 
 void toggle_led1() {ledToggle(1);}
 void toggle_led2() {ledToggle(2);}
+
+/* This function is created to avoid address error that caused from Ticker.attach func */ 
+void compFilter() {complementaryFilter(&pitchAngle, &rollAngle);}
+
+void complementaryFilter(float* pitch, float* roll)
+{
+    /* Get actual acc value */
+    mpu6050.readAccelData(accelData);
+    mpu6050.getAres();
+    ax = accelData[0]*aRes - accelBias[0];
+    ay = accelData[1]*aRes - accelBias[1];
+    az = accelData[2]*aRes - accelBias[2]; 
+
+    /* Get actual gyro value */
+    mpu6050.readGyroData(gyroData);
+    mpu6050.getGres();     
+    gx = gyroData[0]*gRes;  // - gyroBias[0];      // Results are better without extracting gyroBias[i]
+    gy = gyroData[1]*gRes;  // - gyroBias[1]; 
+    gz = gyroData[2]*gRes;  // - gyroBias[2]; 
+
+    float pitchAcc, rollAcc;
+
+    /* Integrate the gyro data(deg/s) over time to get angle */
+    *pitch += gx * dt;  // Angle around the X-axis
+    *roll -=  gy * dt;  // Angle around the Y-axis
+    
+    /* Turning around the X-axis results in a vector on the Y-axis
+    whereas turning around the Y-axis results in a vector on the X-axis. */
+    pitchAcc = atan2f((float)accelData[1], (float)accelData[2])*180/PI;
+    rollAcc  = atan2f((float)accelData[0], (float)accelData[2])*180/PI;
+    
+    /* Apply Complementary Filter */
+    *pitch = *pitch * 0.98 + pitchAcc * 0.02;
+    *roll  = *roll  * 0.98 + rollAcc  * 0.02;   
+}
\ No newline at end of file
--- a/mbed.bld	Thu Jul 09 12:28:26 2015 +0000
+++ b/mbed.bld	Mon Jul 13 13:19:08 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/da0ca467f8b5
\ No newline at end of file