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.

Files at this revision

API Documentation at this revision

Comitter:
tyftyftyf
Date:
Wed Sep 24 01:10:42 2014 +0000
Parent:
10:11cc1b413f49
Commit message:
Allow starting and stopping MPU6050 sampling

Changed in this revision

MPU6050.cpp Show annotated file Show diff for this revision Revisions of this file
MPU6050.h Show annotated file Show diff for this revision Revisions of this file
--- a/MPU6050.cpp	Mon Dec 23 08:34:58 2013 +0000
+++ b/MPU6050.cpp	Wed Sep 24 01:10:42 2014 +0000
@@ -54,7 +54,7 @@
 /** Default constructor, uses default I2C address.
  * @see MPU6050_DEFAULT_ADDRESS
  */
-MPU6050::MPU6050() : debugSerial(USBTX, USBRX)
+MPU6050::MPU6050() : debugSerial(USBTX, USBRX), sampling(false)
 {
     devAddr = MPU6050_DEFAULT_ADDRESS << 1;
     debugSerial.baud(115200);
@@ -66,7 +66,7 @@
  * @see MPU6050_ADDRESS_AD0_LOW
  * @see MPU6050_ADDRESS_AD0_HIGH
  */
-MPU6050::MPU6050(uint8_t address) : debugSerial(USBTX, USBRX)
+MPU6050::MPU6050(uint8_t address) : debugSerial(USBTX, USBRX), sampling(false)
 {
     devAddr = address << 1;
     debugSerial.baud(115200);
@@ -1910,6 +1910,9 @@
     *gz = gz_cache;
 }
 
+/*
+ * Populate readings cache when finished reading from I2C
+ */
 uint32_t mpureadfin(uint32_t param){
     MPU6050* ins = (MPU6050*)param;
     ins->ax_cache = (((int16_t)ins->mpu_buffer[0]) << 8) | ins->mpu_buffer[1];
@@ -1921,11 +1924,20 @@
     return 0;
 }
 
+void MPU6050::sample(bool on){
+   if (sampling && !on)
+      mpu_sampling.detach();
+   if (!sampling && on)
+      start_sampling();
+   sampling = on;
+}
+
 void MPU6050::mpu_sample_func(){
     i2Cdev.readBytes_nb(devAddr, &mpu_cmd, 14, (uint8_t*)mpu_buffer, &mpureadfin, this);
 }
 
 void MPU6050::start_sampling(){
+    sampling = true;
     mpu_cmd = MPU6050_RA_ACCEL_XOUT_H;
     mpu_sampling.attach_us(this, &MPU6050::mpu_sample_func, 2000);
 }
--- a/MPU6050.h	Mon Dec 23 08:34:58 2013 +0000
+++ b/MPU6050.h	Wed Sep 24 01:10:42 2014 +0000
@@ -406,6 +406,8 @@
     private:
         I2Cdev i2Cdev;
         Serial debugSerial;
+        bool sampling;  // flag to indicate whether sampling ticker is running
+        
     public:
         MPU6050();
         MPU6050(MODI2C i2c);
@@ -430,7 +432,9 @@
         // SMPLRT_DIV register
         uint8_t getRate();
         void setRate(uint8_t rate);
-        
+
+        // turn sampling on or off
+        void sample(bool sampling);
 
         // CONFIG register
         uint8_t getExternalFrameSync();