MPU6050 library using i2c interface on LPC1768 - Complementary filter is added. Now program can calculate pitch and roll angles.

Dependents:   Q2_Stabi

Fork of MPU6050 by Baser Kandehir

Committer:
Decimus
Date:
Mon May 30 08:11:07 2016 +0000
Revision:
7:4e799f8ec792
Parent:
5:5bff0edcdff8
[+]

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BaserK 3:a173ad187e67 1 /*
BaserK 3:a173ad187e67 2 * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr
BaserK 3:a173ad187e67 3 *
BaserK 3:a173ad187e67 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
BaserK 3:a173ad187e67 5 * of this software and associated documentation files (the "Software"), to deal
BaserK 3:a173ad187e67 6 * in the Software without restriction, including without limitation the rights
BaserK 3:a173ad187e67 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
BaserK 3:a173ad187e67 8 * copies of the Software, and to permit persons to whom the Software is
BaserK 3:a173ad187e67 9 * furnished to do so, subject to the following conditions:
BaserK 3:a173ad187e67 10 *
BaserK 3:a173ad187e67 11 * The above copyright notice and this permission notice shall be included in
BaserK 3:a173ad187e67 12 * all copies or substantial portions of the Software.
BaserK 3:a173ad187e67 13 *
BaserK 3:a173ad187e67 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
BaserK 3:a173ad187e67 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
BaserK 3:a173ad187e67 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
BaserK 3:a173ad187e67 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
BaserK 3:a173ad187e67 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
BaserK 3:a173ad187e67 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
BaserK 3:a173ad187e67 20 * THE SOFTWARE.
BaserK 3:a173ad187e67 21 *
BaserK 3:a173ad187e67 22 */
BaserK 3:a173ad187e67 23
BaserK 0:954f15bd95f1 24 // Most of the code is adapted from Kris Winer's MPU6050 library
BaserK 0:954f15bd95f1 25
BaserK 0:954f15bd95f1 26 #ifndef MPU6050_H
BaserK 0:954f15bd95f1 27 #define MPU6050_H
BaserK 0:954f15bd95f1 28
BaserK 0:954f15bd95f1 29 #include "mbed.h"
BaserK 0:954f15bd95f1 30 #include "math.h"
BaserK 0:954f15bd95f1 31 #include "MPU6050RegDef.h"
BaserK 0:954f15bd95f1 32
Decimus 7:4e799f8ec792 33 //#define PI 3.14159265359 // This value will be used when calculating angles
Decimus 7:4e799f8ec792 34 //#define dt 0.005 // 200 Hz sampling period
BaserK 1:a248e65a25cc 35
BaserK 0:954f15bd95f1 36 extern float aRes, gRes;
BaserK 0:954f15bd95f1 37
BaserK 0:954f15bd95f1 38 /* whoAmI func uses this func, variables etc */
Decimus 7:4e799f8ec792 39 //extern Ticker toggler1;
Decimus 7:4e799f8ec792 40 //extern Serial pc;
Decimus 7:4e799f8ec792 41 //extern DigitalOut led2;
Decimus 7:4e799f8ec792 42 //extern void toggle_led1();
BaserK 0:954f15bd95f1 43
BaserK 2:3e0dfce73a58 44 /* Sensor datas to be used in program */
BaserK 0:954f15bd95f1 45 extern float ax,ay,az;
BaserK 0:954f15bd95f1 46 extern float gx,gy,gz;
BaserK 0:954f15bd95f1 47 extern int16_t accelData[3],gyroData[3],tempData;
BaserK 0:954f15bd95f1 48 extern float accelBias[3], gyroBias[3];
BaserK 0:954f15bd95f1 49
BaserK 0:954f15bd95f1 50 /* Function Prototypes */
Decimus 7:4e799f8ec792 51 class MPU6050 {
BaserK 0:954f15bd95f1 52 public:
Decimus 7:4e799f8ec792 53 MPU6050(PinName p_sda, PinName p_scl);
Decimus 7:4e799f8ec792 54
BaserK 0:954f15bd95f1 55 void getAres();
BaserK 0:954f15bd95f1 56 void getGres();
BaserK 0:954f15bd95f1 57 void writeByte(uint8_t address, uint8_t subAddress, uint8_t data);
BaserK 0:954f15bd95f1 58 char readByte(uint8_t address, uint8_t subAddress);
BaserK 0:954f15bd95f1 59 void readBytes(uint8_t address, uint8_t subAddress, uint8_t byteNum, uint8_t* dest);
BaserK 0:954f15bd95f1 60 void whoAmI();
BaserK 0:954f15bd95f1 61 void init();
BaserK 0:954f15bd95f1 62 void reset();
Decimus 7:4e799f8ec792 63 void getAccelRaw(int16_t* dest);
Decimus 7:4e799f8ec792 64 void getGyroRaw(int16_t* dest);
Decimus 7:4e799f8ec792 65 void getGyro(float* dest);
BaserK 0:954f15bd95f1 66 int16_t readTempData();
BaserK 0:954f15bd95f1 67 void calibrate(float* dest1, float* dest2);
Decimus 7:4e799f8ec792 68 //void complementaryFilter(float* pitch, float* roll);
Decimus 7:4e799f8ec792 69
Decimus 7:4e799f8ec792 70 protected:
Decimus 7:4e799f8ec792 71 I2C i2c;
Decimus 7:4e799f8ec792 72 char currentAcceleroRange;
Decimus 7:4e799f8ec792 73 char currentGyroRange;
BaserK 0:954f15bd95f1 74 };
BaserK 0:954f15bd95f1 75
BaserK 0:954f15bd95f1 76 #endif