CSE477 / swimate_v2

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Revision:
7:33a74adff0ff
Child:
8:8430a5c0914c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/receive_data.cpp	Sat May 17 01:20:31 2014 +0000
@@ -0,0 +1,97 @@
+#include "mbed.h"
+#include "MPU6050_6Axis_MotionApps20.h"
+#include "debug.h"
+
+// MPU
+MPU6050 mpu;
+InterruptIn dataReady(P0_15);
+
+// MPU control/status vars
+bool dmpReady = false;  // set true if DMP init was successful
+uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
+uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
+uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
+uint16_t fifoCount;     // count of all bytes currently in FIFO
+uint8_t fifoBuffer[64]; // FIFO storage buffer
+
+// orientation/motion vars
+Quaternion q;           // [w, x, y, z]         quaternion container
+VectorInt16 aa;         // [x, y, z]            accel sensor measurements
+VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
+VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
+VectorFloat gravity;    // [x, y, z]            gravity vector
+float euler[3];         // [psi, theta, phi]    Euler angle container
+float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
+
+volatile bool mpuInterrupt = false;
+void dataReadyISR() {
+    mpuInterrupt = true;
+}
+
+/* Returns a pointer to an array containing the most recent data, otherwise NULL.  Requires the mpu to be initialized. */
+void receive_data()
+{
+    static uint32_t n_overflows = 0;
+//    while (true) {
+    //if (!dmpReady) break;   // do nothing if dmp not ready
+    if (!dmpReady) return;
+
+    while (!mpuInterrupt && fifoCount < packetSize);
+
+    // Reset interrupt
+    mpuInterrupt = false;
+    mpuIntStatus = mpu.getIntStatus();
+
+    // get current FIFO count
+    fifoCount = mpu.getFIFOCount();
+    
+//    PC_PRINTF("%d, ", fifoCount); PC_PRINTF("%x, ", mpuIntStatus);
+    // check for overflow (this should never happen unless our code is too inefficient)
+    if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
+        PC_PRINTLN("**** FIFO OVERFLOW ****");
+        n_overflows++;
+        // reset so we can continue cleanly
+        mpu.resetFIFO();
+        // otherwise, check for DMP data ready interrupt (this should happen frequently)
+    } else if (mpuIntStatus & 0x02) {
+        // Wait for a full packet - should be very short wait
+        while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
+
+        // read a packet from FIFO
+        mpu.getFIFOBytes(fifoBuffer, packetSize);
+        fifoCount -= packetSize;
+
+        // Get acceleration data
+        mpu.dmpGetAccel(&aa, fifoBuffer);
+//            mpu.dmpGetQuaternion(&q, fifoBuffer);
+//            mpu.dmpGetGravity(&gravity, &q);
+//            mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
+//            mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
+
+        PC_PRINTF("%d, ", aa.x);
+        PC_PRINTF("%d, ", aa.y);
+        PC_PRINTLNF("%d", aa.z);
+//            OLED_SETCURS(0, 10); OLED_PRINTF("%d, ", aaWorld.x); OLED_PRINTF("%d, ", aaWorld.y); OLED_PRINTLNF("%d", aaWorld.z);
+
+//            fprintf(logFile, "%u,%d,%d,%d,%f,%f,%f,%f,%u\n", captureTime.read_ms(), aa.x, aa.y, aa.z, q.x, q.y, q.z, q.w, n_overflows);
+//        }
+    }
+}
+
+void receive_init() {
+    PC_PRINTLN("Initializing MPU");
+    mpu.initialize();
+    devStatus = mpu.dmpInitialize();
+    
+    if (devStatus == 0) {
+        mpu.setDMPEnabled(true);
+        packetSize = mpu.dmpGetFIFOPacketSize();
+        
+        PC_PRINTLN("DMP Initialized successfully!");
+        dmpReady = true;
+        dataReady.rise(dataReadyISR);
+    } else { // ERROR
+        PC_PRINTLNF("Error initializing MPU (code %d)", devStatus);
+        DIE(DMP_ERROR_RATE);
+    }
+}
\ No newline at end of file