NOT FINISHED YET!!! My first try to get a self built fully working Quadrocopter based on an mbed, a self built frame and some other more or less cheap parts.

Dependencies:   mbed MODI2C

Revision:
40:2ca410923691
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensors/Gyro_Acc/MPU6050.cpp	Fri Feb 14 14:17:32 2014 +0000
@@ -0,0 +1,82 @@
+#include "MPU6050.h"
+
+MPU6050::MPU6050(PinName sda, PinName scl) : I2C_Sensor(sda, scl, MPU6050_I2C_ADDRESS)
+{
+    // Turns on the MPU6050's gyro and initializes it
+    // register datasheet: http://www.invensense.com/mems/gyro/documents/RM-MPU-6000A.pdf
+    writeRegister(MPU6050_RA_PWR_MGMT_1, 0x01);         // wake up from sleep and chooses Gyro X-Axis as Clock source (stadard sleeping and with inacurate clock is 0x40)
+    writeRegister(MPU6050_RA_GYRO_CONFIG, 0x18);        // scales gyros range to +-2000dps
+    writeRegister(MPU6050_RA_ACCEL_CONFIG, 0x00);       // scales accelerometers range to +-2g
+}
+
+void MPU6050::read()
+{
+    readraw_gyro();                                          // read raw measurement data
+    readraw_acc();
+    
+    for (int i = 0; i < 3; i++)
+        data_gyro[i] = (raw_gyro[i] - offset_gyro[i])*0.07;             // subtract offset from calibration and multiply unit factor (datasheet s.10)
+    
+    for (int i = 0; i < 3; i++)
+        data_acc[i] = raw_acc[i] - offset_acc[i];             // TODO: didn't care about units because IMU-algorithm just uses vector direction
+}
+
+int MPU6050::readTemp()
+{
+    char buffer[2];                                     // 8-Bit pieces of temperature data
+    
+    readMultiRegister(MPU6050_RA_TEMP_OUT_H, buffer, 2);     // read the sensors register for the temperature
+    return (short) (buffer[0] << 8 | buffer[1]);
+}
+
+void MPU6050::readraw_gyro()
+{
+    char buffer[6];                                     // 8-Bit pieces of axis data
+    
+    readMultiRegister(MPU6050_RA_GYRO_XOUT_H | (1 << 7), buffer, 6); // read axis registers using I2C   // TODO: why?!   | (1 << 7)
+    
+    raw_gyro[0] = (short) (buffer[0] << 8 | buffer[1]);     // join 8-Bit pieces to 16-bit short integers
+    raw_gyro[1] = (short) (buffer[2] << 8 | buffer[3]);
+    raw_gyro[2] = (short) (buffer[4] << 8 | buffer[5]);
+}
+
+void MPU6050::readraw_acc()
+{
+    char buffer[6];                                     // 8-Bit pieces of axis data
+    
+    readMultiRegister(MPU6050_RA_ACCEL_XOUT_H | (1 << 7), buffer, 6); // read axis registers using I2C   // TODO: why?!   | (1 << 7)
+    
+    raw_acc[0] = (short) (buffer[0] << 8 | buffer[1]);     // join 8-Bit pieces to 16-bit short integers
+    raw_acc[1] = (short) (buffer[2] << 8 | buffer[3]);
+    raw_acc[2] = (short) (buffer[4] << 8 | buffer[5]);
+}
+
+void MPU6050::calibrate(int times, float separation_time)
+{
+    // calibrate sensor with an average of count samples (result of calibration stored in offset[])
+    // Calibrate Gyroscope ----------------------------------
+    float calib_gyro[3] = {0,0,0};                           // temporary array for the sum of calibration measurement
+    
+    for (int i = 0; i < times; i++) {                   // read 'times' times the data in a very short time
+        readraw_gyro();
+        for (int j = 0; j < 3; j++)
+            calib_gyro[j] += raw_gyro[j];
+        wait(separation_time);
+    }
+    
+    for (int i = 0; i < 3; i++)
+        offset_gyro[i] = calib_gyro[i]/times;                     // take the average of the calibration measurements
+    
+    // Calibrate Accelerometer ------------------------------- 
+    float calib_acc[3] = {0,0,0};                           // temporary array for the sum of calibration measurement
+    
+    for (int i = 0; i < times; i++) {                   // read 'times' times the data in a very short time
+        readraw_acc();
+        for (int j = 0; j < 3; j++)
+            calib_acc[j] += raw_acc[j];
+        wait(separation_time);
+    }
+    
+    for (int i = 0; i < 2; i++)
+        offset_acc[i] = calib_acc[i]/times;                     // take the average of the calibration measurements
+}
\ No newline at end of file