MPU6050 FreeIMU library

Dependents:   FreeIMU FreeIMU_external_magnetometer

Fork of MPU6050_tmp by Aloïs Wolff

Async MPU6050 library

My port of the MPU6050 library samples the chip at 500Hz using Timer. Async I2C is achieved using a custom I2C library, which supports I2C calls from interrupts. Link given below:

Import libraryMODI2C

Improvements to Olieman's MODI2C library. Supports calls from IRQ.

Difference between this port and the Arduino MPU6050 library

The getMotion6 function only returns a copy of the last obtained readings, which is sampled at a frequency of 500Hz (adjustable). Hence it can be called at any frequency without taxing the I2C.

Revision:
9:d879deb55ae1
Parent:
7:95e74f827c08
Child:
11:9549be34fa7f
--- a/MPU6050.cpp	Tue Nov 05 11:28:54 2013 +0000
+++ b/MPU6050.cpp	Sat Nov 09 08:51:07 2013 +0000
@@ -40,7 +40,9 @@
 ===============================================
 */
 
+#include "MODI2C.h"
 #include "MPU6050.h"
+#include "rtos.h"
 
 //#define useDebugSerial
 
@@ -54,12 +56,8 @@
  */
 MPU6050::MPU6050() : debugSerial(USBTX, USBRX)
 {
-    devAddr = MPU6050_DEFAULT_ADDRESS;
-}
-
-MPU6050::MPU6050(I2C i2c) : debugSerial(USBTX, USBRX), i2Cdev(i2c)
-{
-    devAddr = MPU6050_DEFAULT_ADDRESS;
+    devAddr = MPU6050_DEFAULT_ADDRESS << 1;
+    debugSerial.baud(115200);
 }
 
 /** Specific address constructor.
@@ -70,7 +68,8 @@
  */
 MPU6050::MPU6050(uint8_t address) : debugSerial(USBTX, USBRX)
 {
-    devAddr = address;
+    devAddr = address << 1;
+    debugSerial.baud(115200);
 }
 
 /** Power on and prepare for general usage.
@@ -82,9 +81,7 @@
  */
 void MPU6050::initialize()
 {
-
 #ifdef useDebugSerial
-    debugSerial.baud(921600); //uses max serial speed
     debugSerial.printf("MPU6050::initialize start\n");
 #endif
     setClockSource(MPU6050_CLOCK_PLL_XGYRO);
@@ -1890,6 +1887,7 @@
     getMotion6(ax, ay, az, gx, gy, gz);
     // TODO: magnetometer integration
 }
+
 /** Get raw 6-axis motion sensor readings (accel/gyro).
  * Retrieves all currently available motion sensor values.
  * @param ax 16-bit signed integer container for accelerometer X-axis value
@@ -1904,14 +1902,34 @@
  */
 void MPU6050::getMotion6(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz)
 {
-    i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 14, buffer);
-    *ax = (((int16_t)buffer[0]) << 8) | buffer[1];
-    *ay = (((int16_t)buffer[2]) << 8) | buffer[3];
-    *az = (((int16_t)buffer[4]) << 8) | buffer[5];
-    *gx = (((int16_t)buffer[8]) << 8) | buffer[9];
-    *gy = (((int16_t)buffer[10]) << 8) | buffer[11];
-    *gz = (((int16_t)buffer[12]) << 8) | buffer[13];
+    *ax = ax_cache;
+    *ay = ay_cache;
+    *az = az_cache;
+    *gx = gx_cache;
+    *gy = gy_cache;
+    *gz = gz_cache;
 }
+
+uint32_t mpureadfin(uint32_t param){
+    MPU6050* ins = (MPU6050*)param;
+    ins->ax_cache = (((int16_t)ins->mpu_buffer[0]) << 8) | ins->mpu_buffer[1];
+    ins->ay_cache = (((int16_t)ins->mpu_buffer[2]) << 8) | ins->mpu_buffer[3];
+    ins->az_cache = (((int16_t)ins->mpu_buffer[4]) << 8) | ins->mpu_buffer[5];
+    ins->gx_cache = (((int16_t)ins->mpu_buffer[8]) << 8) | ins->mpu_buffer[9];
+    ins->gy_cache = (((int16_t)ins->mpu_buffer[10]) << 8) | ins->mpu_buffer[11];
+    ins->gz_cache = (((int16_t)ins->mpu_buffer[12]) << 8) | ins->mpu_buffer[13];
+    return 0;
+}
+
+void MPU6050::mpu_sample_func(){
+    i2Cdev.readBytes_nb(devAddr, &mpu_cmd, 14, (uint8_t*)mpu_buffer, &mpureadfin, this);
+}
+
+void MPU6050::start_sampling(){
+    mpu_cmd = MPU6050_RA_ACCEL_XOUT_H;
+    mpu_sampling.attach_us(this, &MPU6050::mpu_sample_func, 2000);
+}
+
 /** Get 3-axis accelerometer readings.
  * These registers store the most recent accelerometer measurements.
  * Accelerometer measurements are written to these registers at the Sample Rate