Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: FreeIMU FreeIMU_external_magnetometer
Fork of MPU6050_tmp by
Revision 11:9549be34fa7f, committed 2014-09-24
- 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();
    