ESE519 Lab6 Part3

Dependencies:   MPU6050_Lab6_Part3 mbed

Fork of BroBot_v2 by Carter Sharer

Committer:
csharer
Date:
Thu Nov 10 19:20:55 2016 +0000
Revision:
6:ae3e6aefe908
Parent:
BroBot_IMU.h@4:2512939c10f0
ese519 lab6 part 3;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csharer 6:ae3e6aefe908 1 //balance_bot_IMU.h
csharer 6:ae3e6aefe908 2 // Contains everyting needed to interface with the IMU for balancebot
csharer 4:2512939c10f0 3 //Source links: https: developer.mbed.org/users/Sissors/code/MPU6050/docs/5c63e20c50f3/classMPU6050.html
csharer 4:2512939c10f0 4 // https://developer.mbed.org/users/paulbartell/code/MPU6050-DMP/file/95449a48c5c0/MPU6050_6Axis_MotionApps20.h
csharer 3:2f76ffbc5cef 5
csharer 3:2f76ffbc5cef 6 // class default I2C address is 0x68
csharer 3:2f76ffbc5cef 7 // specific I2C addresses may be passed as a parameter here
csharer 3:2f76ffbc5cef 8 // AD0 low = 0x68 (default for SparkFun breakout and InvenSense evaluation board)
csharer 3:2f76ffbc5cef 9 // AD0 high = 0x69
csharer 3:2f76ffbc5cef 10 MPU6050 mpu;
csharer 3:2f76ffbc5cef 11 //MPU6050 mpu(0x69); // <-- use for AD0 `high
csharer 3:2f76ffbc5cef 12 // MPU control/status vars
csharer 3:2f76ffbc5cef 13
csharer 3:2f76ffbc5cef 14 float angle, angle_old;
csharer 3:2f76ffbc5cef 15 bool dmpReady = false; // set true if DMP init was successful
csharer 3:2f76ffbc5cef 16 uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
csharer 3:2f76ffbc5cef 17 uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
csharer 3:2f76ffbc5cef 18 uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
csharer 3:2f76ffbc5cef 19 uint16_t fifoCount; // count of all bytes currently in FIFO
csharer 3:2f76ffbc5cef 20 uint8_t fifoBuffer[64]; // FIFO storage buffer
csharer 4:2512939c10f0 21 float dAngle;
csharer 4:2512939c10f0 22 float new_angle;
csharer 3:2f76ffbc5cef 23 // Orientation/motion vars
csharer 3:2f76ffbc5cef 24 Quaternion q; // [w, x, y, z] quaternion container
csharer 3:2f76ffbc5cef 25
csharer 3:2f76ffbc5cef 26 InterruptIn checkpin(CHECKPIN);
csharer 3:2f76ffbc5cef 27
csharer 3:2f76ffbc5cef 28 // ================================================================
csharer 3:2f76ffbc5cef 29 // === IMU ===
csharer 3:2f76ffbc5cef 30 // ================================================================
csharer 3:2f76ffbc5cef 31 // DMP FUNCTIONS
csharer 3:2f76ffbc5cef 32 // This function defines the weight of the accel on the sensor fusion
csharer 3:2f76ffbc5cef 33 // default value is 0x80
csharer 3:2f76ffbc5cef 34 // The official invensense name is inv_key_0_96 (??)
csharer 3:2f76ffbc5cef 35 void dmpSetSensorFusionAccelGain(uint8_t gain)
csharer 3:2f76ffbc5cef 36 {
csharer 3:2f76ffbc5cef 37 // INV_KEY_0_96
csharer 3:2f76ffbc5cef 38 mpu.setMemoryBank(0);
csharer 3:2f76ffbc5cef 39 mpu.setMemoryStartAddress(0x60);
csharer 3:2f76ffbc5cef 40 mpu.writeMemoryByte(0);
csharer 3:2f76ffbc5cef 41 mpu.writeMemoryByte(gain);
csharer 3:2f76ffbc5cef 42 mpu.writeMemoryByte(0);
csharer 3:2f76ffbc5cef 43 mpu.writeMemoryByte(0);
csharer 3:2f76ffbc5cef 44 }
csharer 3:2f76ffbc5cef 45
csharer 3:2f76ffbc5cef 46 // Quick calculation to obtein Phi angle from quaternion solution (from DMP internal quaternion solution)
csharer 3:2f76ffbc5cef 47 float dmpGetPhi()
csharer 3:2f76ffbc5cef 48 {
csharer 3:2f76ffbc5cef 49 mpu.getFIFOBytes(fifoBuffer, 16); // We only read the quaternion
csharer 3:2f76ffbc5cef 50 mpu.dmpGetQuaternion(&q, fifoBuffer);
csharer 3:2f76ffbc5cef 51 mpu.resetFIFO(); // We always reset FIFO
csharer 3:2f76ffbc5cef 52
csharer 3:2f76ffbc5cef 53 //return( asin(-2*(q.x * q.z - q.w * q.y)) * 180/M_PI); //roll
csharer 3:2f76ffbc5cef 54 //return Phi angle (robot orientation) from quaternion DMP output
csharer 3:2f76ffbc5cef 55 return (atan2(2 * (q.y * q.z + q.w * q.x), q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z) * RAD2GRAD);
csharer 3:2f76ffbc5cef 56 }
csharer 3:2f76ffbc5cef 57
csharer 3:2f76ffbc5cef 58 // ================================================================
csharer 3:2f76ffbc5cef 59 // === INTERRUPT DETECTION ROUTINE ===
csharer 3:2f76ffbc5cef 60 // ================================================================
csharer 3:2f76ffbc5cef 61 volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
csharer 4:2512939c10f0 62 Serial pc1(USBTX, USBRX);
csharer 3:2f76ffbc5cef 63 void dmpDataReady()
csharer 3:2f76ffbc5cef 64 {
csharer 3:2f76ffbc5cef 65 mpuInterrupt = true;
csharer 3:2f76ffbc5cef 66 }