sfas
Dependents: Bracky-MPU6050-DMP
Fork of MPU6050 by
MyMPU6050.h@6:748bae310e1e, 2015-12-03 (annotated)
- Committer:
- amandaghassaei
- Date:
- Thu Dec 03 02:23:23 2015 +0000
- Revision:
- 6:748bae310e1e
- Child:
- 7:07f1f975429a
delegate stuff working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amandaghassaei | 6:748bae310e1e | 1 | // I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class using DMP (MotionApps v2.0) |
amandaghassaei | 6:748bae310e1e | 2 | // 6/21/2012 by Jeff Rowberg <jeff@rowberg.net> |
amandaghassaei | 6:748bae310e1e | 3 | /* ============================================ |
amandaghassaei | 6:748bae310e1e | 4 | I2Cdev device library code is placed under the MIT license |
amandaghassaei | 6:748bae310e1e | 5 | Copyright (c) 2012 Jeff Rowberg |
amandaghassaei | 6:748bae310e1e | 6 | */ |
amandaghassaei | 6:748bae310e1e | 7 | |
amandaghassaei | 6:748bae310e1e | 8 | #include "I2Cdev.h" |
amandaghassaei | 6:748bae310e1e | 9 | #include "MPU6050_6Axis_MotionApps20.h" |
amandaghassaei | 6:748bae310e1e | 10 | #include "CommDelegate.h" |
amandaghassaei | 6:748bae310e1e | 11 | |
amandaghassaei | 6:748bae310e1e | 12 | class MyMPU6050: public CommDelegate { |
amandaghassaei | 6:748bae310e1e | 13 | |
amandaghassaei | 6:748bae310e1e | 14 | public: |
amandaghassaei | 6:748bae310e1e | 15 | |
amandaghassaei | 6:748bae310e1e | 16 | MyMPU6050(PinName i2cSda, PinName i2cScl) : mpu(i2cSda, i2cScl), checkpin(p11){ |
amandaghassaei | 6:748bae310e1e | 17 | this->setup(); |
amandaghassaei | 6:748bae310e1e | 18 | } |
amandaghassaei | 6:748bae310e1e | 19 | |
amandaghassaei | 6:748bae310e1e | 20 | void loop() { |
amandaghassaei | 6:748bae310e1e | 21 | // if programming failed, don't try to do anything |
amandaghassaei | 6:748bae310e1e | 22 | if (!dmpReady) return; |
amandaghassaei | 6:748bae310e1e | 23 | |
amandaghassaei | 6:748bae310e1e | 24 | // wait for MPU interrupt or extra packet(s) available |
amandaghassaei | 6:748bae310e1e | 25 | if (!mpuInterrupt && fifoCount < packetSize) { |
amandaghassaei | 6:748bae310e1e | 26 | return; |
amandaghassaei | 6:748bae310e1e | 27 | // other program behavior stuff here |
amandaghassaei | 6:748bae310e1e | 28 | // . |
amandaghassaei | 6:748bae310e1e | 29 | // . |
amandaghassaei | 6:748bae310e1e | 30 | // . |
amandaghassaei | 6:748bae310e1e | 31 | // if you are really paranoid you can frequently test in between other |
amandaghassaei | 6:748bae310e1e | 32 | // stuff to see if mpuInterrupt is true, and if so, "break;" from the |
amandaghassaei | 6:748bae310e1e | 33 | // while() loop to immediately process the MPU data |
amandaghassaei | 6:748bae310e1e | 34 | // . |
amandaghassaei | 6:748bae310e1e | 35 | // . |
amandaghassaei | 6:748bae310e1e | 36 | // . |
amandaghassaei | 6:748bae310e1e | 37 | } |
amandaghassaei | 6:748bae310e1e | 38 | |
amandaghassaei | 6:748bae310e1e | 39 | // reset interrupt flag and get INT_STATUS byte |
amandaghassaei | 6:748bae310e1e | 40 | mpuInterrupt = false; |
amandaghassaei | 6:748bae310e1e | 41 | mpuIntStatus = mpu.getIntStatus(); |
amandaghassaei | 6:748bae310e1e | 42 | |
amandaghassaei | 6:748bae310e1e | 43 | // get current FIFO count |
amandaghassaei | 6:748bae310e1e | 44 | fifoCount = mpu.getFIFOCount(); |
amandaghassaei | 6:748bae310e1e | 45 | |
amandaghassaei | 6:748bae310e1e | 46 | // check for overflow (this should never happen unless our code is too inefficient) |
amandaghassaei | 6:748bae310e1e | 47 | if ((mpuIntStatus & 0x10) || fifoCount == 1024) { |
amandaghassaei | 6:748bae310e1e | 48 | // reset so we can continue cleanly |
amandaghassaei | 6:748bae310e1e | 49 | mpu.resetFIFO(); |
amandaghassaei | 6:748bae310e1e | 50 | //Serial.println(F("FIFO overflow!")); |
amandaghassaei | 6:748bae310e1e | 51 | |
amandaghassaei | 6:748bae310e1e | 52 | // otherwise, check for DMP data ready interrupt (this should happen frequently) |
amandaghassaei | 6:748bae310e1e | 53 | } else if (mpuIntStatus & 0x02) { |
amandaghassaei | 6:748bae310e1e | 54 | // wait for correct available data length, should be a VERY short wait |
amandaghassaei | 6:748bae310e1e | 55 | while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount(); |
amandaghassaei | 6:748bae310e1e | 56 | |
amandaghassaei | 6:748bae310e1e | 57 | // read a packet from FIFO |
amandaghassaei | 6:748bae310e1e | 58 | mpu.getFIFOBytes(fifoBuffer, packetSize); |
amandaghassaei | 6:748bae310e1e | 59 | |
amandaghassaei | 6:748bae310e1e | 60 | // track FIFO count here in case there is > 1 packet available |
amandaghassaei | 6:748bae310e1e | 61 | // (this lets us immediately read more without waiting for an interrupt) |
amandaghassaei | 6:748bae310e1e | 62 | fifoCount -= packetSize; |
amandaghassaei | 6:748bae310e1e | 63 | |
amandaghassaei | 6:748bae310e1e | 64 | // display Euler angles in degrees |
amandaghassaei | 6:748bae310e1e | 65 | mpu.dmpGetQuaternion(&q, fifoBuffer); |
amandaghassaei | 6:748bae310e1e | 66 | mpu.dmpGetGravity(&gravity, &q); |
amandaghassaei | 6:748bae310e1e | 67 | mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); |
amandaghassaei | 6:748bae310e1e | 68 | mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); |
amandaghassaei | 6:748bae310e1e | 69 | theta = ypr[2]; |
amandaghassaei | 6:748bae310e1e | 70 | mpu.dmpGetGyro(gyroData, fifoBuffer); |
amandaghassaei | 6:748bae310e1e | 71 | dtheta = gyroData[2]; |
amandaghassaei | 6:748bae310e1e | 72 | } |
amandaghassaei | 6:748bae310e1e | 73 | } |
amandaghassaei | 6:748bae310e1e | 74 | |
amandaghassaei | 6:748bae310e1e | 75 | void dmpDataReady() { |
amandaghassaei | 6:748bae310e1e | 76 | mpuInterrupt = true; |
amandaghassaei | 6:748bae310e1e | 77 | } |
amandaghassaei | 6:748bae310e1e | 78 | |
amandaghassaei | 6:748bae310e1e | 79 | float getTheta(){ |
amandaghassaei | 6:748bae310e1e | 80 | return theta; |
amandaghassaei | 6:748bae310e1e | 81 | } |
amandaghassaei | 6:748bae310e1e | 82 | |
amandaghassaei | 6:748bae310e1e | 83 | float getDtheta(){ |
amandaghassaei | 6:748bae310e1e | 84 | return dtheta; |
amandaghassaei | 6:748bae310e1e | 85 | } |
amandaghassaei | 6:748bae310e1e | 86 | |
amandaghassaei | 6:748bae310e1e | 87 | private: |
amandaghassaei | 6:748bae310e1e | 88 | |
amandaghassaei | 6:748bae310e1e | 89 | MPU6050 mpu; |
amandaghassaei | 6:748bae310e1e | 90 | |
amandaghassaei | 6:748bae310e1e | 91 | bool dmpReady; // set true if DMP init was successful |
amandaghassaei | 6:748bae310e1e | 92 | uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU |
amandaghassaei | 6:748bae310e1e | 93 | uint8_t devStatus; // return status after each device operation (0 = success, !0 = error) |
amandaghassaei | 6:748bae310e1e | 94 | uint16_t packetSize; // expected DMP packet size (default is 42 bytes) |
amandaghassaei | 6:748bae310e1e | 95 | uint16_t fifoCount; // count of all bytes currently in FIFO |
amandaghassaei | 6:748bae310e1e | 96 | uint8_t fifoBuffer[64]; // FIFO storage buffer |
amandaghassaei | 6:748bae310e1e | 97 | |
amandaghassaei | 6:748bae310e1e | 98 | // orientation/motion vars |
amandaghassaei | 6:748bae310e1e | 99 | Quaternion q; // [w, x, y, z] quaternion container |
amandaghassaei | 6:748bae310e1e | 100 | VectorFloat gravity; // [x, y, z] gravity vector |
amandaghassaei | 6:748bae310e1e | 101 | float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector |
amandaghassaei | 6:748bae310e1e | 102 | int16_t gyroData[3]; |
amandaghassaei | 6:748bae310e1e | 103 | float theta; |
amandaghassaei | 6:748bae310e1e | 104 | float dtheta; |
amandaghassaei | 6:748bae310e1e | 105 | |
amandaghassaei | 6:748bae310e1e | 106 | InterruptIn checkpin; |
amandaghassaei | 6:748bae310e1e | 107 | volatile bool mpuInterrupt; |
amandaghassaei | 6:748bae310e1e | 108 | |
amandaghassaei | 6:748bae310e1e | 109 | void setup() { |
amandaghassaei | 6:748bae310e1e | 110 | |
amandaghassaei | 6:748bae310e1e | 111 | theta = 0; |
amandaghassaei | 6:748bae310e1e | 112 | dtheta = 0; |
amandaghassaei | 6:748bae310e1e | 113 | |
amandaghassaei | 6:748bae310e1e | 114 | dmpReady = false; |
amandaghassaei | 6:748bae310e1e | 115 | mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high |
amandaghassaei | 6:748bae310e1e | 116 | |
amandaghassaei | 6:748bae310e1e | 117 | // initialize device |
amandaghassaei | 6:748bae310e1e | 118 | // pc.printf("Initializing I2C devices...\r\n"); |
amandaghassaei | 6:748bae310e1e | 119 | mpu.initialize(); |
amandaghassaei | 6:748bae310e1e | 120 | |
amandaghassaei | 6:748bae310e1e | 121 | // verify connection |
amandaghassaei | 6:748bae310e1e | 122 | // pc.printf("Testing device connections...\r\n"); |
amandaghassaei | 6:748bae310e1e | 123 | mpu.testConnection();//if() pc.printf("MPU6050 connection successful\r\n"); |
amandaghassaei | 6:748bae310e1e | 124 | // else pc.printf("MPU6050 connection failed\r\n"); |
amandaghassaei | 6:748bae310e1e | 125 | |
amandaghassaei | 6:748bae310e1e | 126 | // load and configure the DMP |
amandaghassaei | 6:748bae310e1e | 127 | // pc.printf("Initializing DMP...\r\n"); |
amandaghassaei | 6:748bae310e1e | 128 | devStatus = mpu.dmpInitialize(); |
amandaghassaei | 6:748bae310e1e | 129 | |
amandaghassaei | 6:748bae310e1e | 130 | // make sure it worked (returns 0 if so) |
amandaghassaei | 6:748bae310e1e | 131 | if (devStatus == 0) { |
amandaghassaei | 6:748bae310e1e | 132 | // turn on the DMP, now that it's ready |
amandaghassaei | 6:748bae310e1e | 133 | // pc.printf("Enabling DMP...\r\n"); |
amandaghassaei | 6:748bae310e1e | 134 | mpu.setDMPEnabled(true); |
amandaghassaei | 6:748bae310e1e | 135 | |
amandaghassaei | 6:748bae310e1e | 136 | // enable Arduino interrupt detection |
amandaghassaei | 6:748bae310e1e | 137 | // pc.printf("Enabling interrupt detection (Arduino external interrupt 0)...\r\n"); |
amandaghassaei | 6:748bae310e1e | 138 | checkpin.rise(this, &MyMPU6050::dmpDataReady); |
amandaghassaei | 6:748bae310e1e | 139 | |
amandaghassaei | 6:748bae310e1e | 140 | mpuIntStatus = mpu.getIntStatus(); |
amandaghassaei | 6:748bae310e1e | 141 | |
amandaghassaei | 6:748bae310e1e | 142 | // set our DMP Ready flag so the main loop() function knows it's okay to use it |
amandaghassaei | 6:748bae310e1e | 143 | // pc.printf("DMP ready! Waiting for first interrupt...\r\n"); |
amandaghassaei | 6:748bae310e1e | 144 | dmpReady = true; |
amandaghassaei | 6:748bae310e1e | 145 | |
amandaghassaei | 6:748bae310e1e | 146 | // get expected DMP packet size for later comparison |
amandaghassaei | 6:748bae310e1e | 147 | packetSize = mpu.dmpGetFIFOPacketSize(); |
amandaghassaei | 6:748bae310e1e | 148 | } else { |
amandaghassaei | 6:748bae310e1e | 149 | // ERROR! |
amandaghassaei | 6:748bae310e1e | 150 | // 1 = initial memory load failed |
amandaghassaei | 6:748bae310e1e | 151 | // 2 = DMP configuration updates failed |
amandaghassaei | 6:748bae310e1e | 152 | // (if it's going to break, usually the code will be 1) |
amandaghassaei | 6:748bae310e1e | 153 | |
amandaghassaei | 6:748bae310e1e | 154 | // pc.printf("DDMP Initialization failed (code "); |
amandaghassaei | 6:748bae310e1e | 155 | // pc.printf("%d", devStatus); |
amandaghassaei | 6:748bae310e1e | 156 | // pc.printf(")\r\n"); |
amandaghassaei | 6:748bae310e1e | 157 | } |
amandaghassaei | 6:748bae310e1e | 158 | |
amandaghassaei | 6:748bae310e1e | 159 | } |
amandaghassaei | 6:748bae310e1e | 160 | }; |