robot

Dependencies:   MPU6050_Lab6_Part3 mbed

Fork of ESE519_Lab6_part3_skeleton by Carter Sharer

Committer:
csharer
Date:
Wed Oct 12 05:04:10 2016 +0000
Revision:
3:2f76ffbc5cef
Child:
4:2512939c10f0
first commit to BroBot;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csharer 3:2f76ffbc5cef 1 //BroBot_IMU.h
csharer 3:2f76ffbc5cef 2 // Contains everyting needed to interface with the IMU for BroBot
csharer 3:2f76ffbc5cef 3
csharer 3:2f76ffbc5cef 4 #define ANGLE_OFFSET 105
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 3:2f76ffbc5cef 21
csharer 3:2f76ffbc5cef 22 // Orientation/motion vars
csharer 3:2f76ffbc5cef 23 Quaternion q; // [w, x, y, z] quaternion container
csharer 3:2f76ffbc5cef 24
csharer 3:2f76ffbc5cef 25 InterruptIn checkpin(CHECKPIN);
csharer 3:2f76ffbc5cef 26
csharer 3:2f76ffbc5cef 27 // ================================================================
csharer 3:2f76ffbc5cef 28 // === IMU ===
csharer 3:2f76ffbc5cef 29 // ================================================================
csharer 3:2f76ffbc5cef 30 // DMP FUNCTIONS
csharer 3:2f76ffbc5cef 31 // This function defines the weight of the accel on the sensor fusion
csharer 3:2f76ffbc5cef 32 // default value is 0x80
csharer 3:2f76ffbc5cef 33 // The official invensense name is inv_key_0_96 (??)
csharer 3:2f76ffbc5cef 34 void dmpSetSensorFusionAccelGain(uint8_t gain)
csharer 3:2f76ffbc5cef 35 {
csharer 3:2f76ffbc5cef 36 // INV_KEY_0_96
csharer 3:2f76ffbc5cef 37 mpu.setMemoryBank(0);
csharer 3:2f76ffbc5cef 38 mpu.setMemoryStartAddress(0x60);
csharer 3:2f76ffbc5cef 39 mpu.writeMemoryByte(0);
csharer 3:2f76ffbc5cef 40 mpu.writeMemoryByte(gain);
csharer 3:2f76ffbc5cef 41 mpu.writeMemoryByte(0);
csharer 3:2f76ffbc5cef 42 mpu.writeMemoryByte(0);
csharer 3:2f76ffbc5cef 43 }
csharer 3:2f76ffbc5cef 44
csharer 3:2f76ffbc5cef 45 // Quick calculation to obtein Phi angle from quaternion solution (from DMP internal quaternion solution)
csharer 3:2f76ffbc5cef 46 float dmpGetPhi()
csharer 3:2f76ffbc5cef 47 {
csharer 3:2f76ffbc5cef 48 mpu.getFIFOBytes(fifoBuffer, 16); // We only read the quaternion
csharer 3:2f76ffbc5cef 49 mpu.dmpGetQuaternion(&q, fifoBuffer);
csharer 3:2f76ffbc5cef 50 mpu.resetFIFO(); // We always reset FIFO
csharer 3:2f76ffbc5cef 51
csharer 3:2f76ffbc5cef 52 //return( asin(-2*(q.x * q.z - q.w * q.y)) * 180/M_PI); //roll
csharer 3:2f76ffbc5cef 53 //return Phi angle (robot orientation) from quaternion DMP output
csharer 3:2f76ffbc5cef 54 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 55 }
csharer 3:2f76ffbc5cef 56
csharer 3:2f76ffbc5cef 57 // ================================================================
csharer 3:2f76ffbc5cef 58 // === INTERRUPT DETECTION ROUTINE ===
csharer 3:2f76ffbc5cef 59 // ================================================================
csharer 3:2f76ffbc5cef 60 volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
csharer 3:2f76ffbc5cef 61 void dmpDataReady()
csharer 3:2f76ffbc5cef 62 {
csharer 3:2f76ffbc5cef 63 mpuInterrupt = true;
csharer 3:2f76ffbc5cef 64 }