NRF+MPU6050

Dependencies:   mbed nRF24L01P

Committer:
AlexQian
Date:
Wed Mar 20 10:44:31 2019 +0000
Revision:
4:1b985e622d26
Parent:
3:46535ec6d8b1
Child:
5:6a93542b8922
Fly_test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AlexQian 3:46535ec6d8b1 1 #ifndef MPU6050_H
AlexQian 3:46535ec6d8b1 2 #define MPU6050_H
AlexQian 3:46535ec6d8b1 3
AlexQian 3:46535ec6d8b1 4 #include "mbed.h"
AlexQian 3:46535ec6d8b1 5 #include "math.h"
AlexQian 3:46535ec6d8b1 6
AlexQian 3:46535ec6d8b1 7 // Define registers per MPU6050, Register Map and Descriptions, Rev 4.2, 08/19/2013 6 DOF Motion sensor fusion device
AlexQian 3:46535ec6d8b1 8 // Invensense Inc., www.invensense.com
AlexQian 3:46535ec6d8b1 9 // See also MPU-6050 Register Map and Descriptions, Revision 4.0, RM-MPU-6050A-00, 9/12/2012 for registers not listed in
AlexQian 3:46535ec6d8b1 10 // above document; the MPU6050 and MPU 9150 are virtually identical but the latter has an on-board magnetic sensor
AlexQian 3:46535ec6d8b1 11 //
AlexQian 3:46535ec6d8b1 12 #define XGOFFS_TC 0x00 // Bit 7 PWR_MODE, bits 6:1 XG_OFFS_TC, bit 0 OTP_BNK_VLD
AlexQian 3:46535ec6d8b1 13 #define YGOFFS_TC 0x01
AlexQian 3:46535ec6d8b1 14 #define ZGOFFS_TC 0x02
AlexQian 3:46535ec6d8b1 15 #define X_FINE_GAIN 0x03 // [7:0] fine gain
AlexQian 3:46535ec6d8b1 16 #define Y_FINE_GAIN 0x04
AlexQian 3:46535ec6d8b1 17 #define Z_FINE_GAIN 0x05
AlexQian 3:46535ec6d8b1 18 #define XA_OFFSET_H 0x06 // User-defined trim values for accelerometer
AlexQian 3:46535ec6d8b1 19 #define XA_OFFSET_L_TC 0x07
AlexQian 3:46535ec6d8b1 20 #define YA_OFFSET_H 0x08
AlexQian 3:46535ec6d8b1 21 #define YA_OFFSET_L_TC 0x09
AlexQian 3:46535ec6d8b1 22 #define ZA_OFFSET_H 0x0A
AlexQian 3:46535ec6d8b1 23 #define ZA_OFFSET_L_TC 0x0B
AlexQian 3:46535ec6d8b1 24 #define SELF_TEST_X 0x0D
AlexQian 3:46535ec6d8b1 25 #define SELF_TEST_Y 0x0E
AlexQian 3:46535ec6d8b1 26 #define SELF_TEST_Z 0x0F
AlexQian 3:46535ec6d8b1 27 #define SELF_TEST_A 0x10
AlexQian 3:46535ec6d8b1 28 #define XG_OFFS_USRH 0x13 // User-defined trim values for gyroscope; supported in MPU-6050?
AlexQian 3:46535ec6d8b1 29 #define XG_OFFS_USRL 0x14
AlexQian 3:46535ec6d8b1 30 #define YG_OFFS_USRH 0x15
AlexQian 3:46535ec6d8b1 31 #define YG_OFFS_USRL 0x16
AlexQian 3:46535ec6d8b1 32 #define ZG_OFFS_USRH 0x17
AlexQian 3:46535ec6d8b1 33 #define ZG_OFFS_USRL 0x18
AlexQian 3:46535ec6d8b1 34 #define SMPLRT_DIV 0x19
AlexQian 3:46535ec6d8b1 35 #define CONFIG 0x1A
AlexQian 3:46535ec6d8b1 36 #define GYRO_CONFIG 0x1B
AlexQian 3:46535ec6d8b1 37 #define ACCEL_CONFIG 0x1C
AlexQian 3:46535ec6d8b1 38 #define FF_THR 0x1D // Free-fall
AlexQian 3:46535ec6d8b1 39 #define FF_DUR 0x1E // Free-fall
AlexQian 3:46535ec6d8b1 40 #define MOT_THR 0x1F // Motion detection threshold bits [7:0]
AlexQian 3:46535ec6d8b1 41 #define MOT_DUR 0x20 // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms
AlexQian 3:46535ec6d8b1 42 #define ZMOT_THR 0x21 // Zero-motion detection threshold bits [7:0]
AlexQian 3:46535ec6d8b1 43 #define ZRMOT_DUR 0x22 // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms
AlexQian 3:46535ec6d8b1 44 #define FIFO_EN 0x23
AlexQian 3:46535ec6d8b1 45 #define I2C_MST_CTRL 0x24
AlexQian 3:46535ec6d8b1 46 #define I2C_SLV0_ADDR 0x25
AlexQian 3:46535ec6d8b1 47 #define I2C_SLV0_REG 0x26
AlexQian 3:46535ec6d8b1 48 #define I2C_SLV0_CTRL 0x27
AlexQian 3:46535ec6d8b1 49 #define I2C_SLV1_ADDR 0x28
AlexQian 3:46535ec6d8b1 50 #define I2C_SLV1_REG 0x29
AlexQian 3:46535ec6d8b1 51 #define I2C_SLV1_CTRL 0x2A
AlexQian 3:46535ec6d8b1 52 #define I2C_SLV2_ADDR 0x2B
AlexQian 3:46535ec6d8b1 53 #define I2C_SLV2_REG 0x2C
AlexQian 3:46535ec6d8b1 54 #define I2C_SLV2_CTRL 0x2D
AlexQian 3:46535ec6d8b1 55 #define I2C_SLV3_ADDR 0x2E
AlexQian 3:46535ec6d8b1 56 #define I2C_SLV3_REG 0x2F
AlexQian 3:46535ec6d8b1 57 #define I2C_SLV3_CTRL 0x30
AlexQian 3:46535ec6d8b1 58 #define I2C_SLV4_ADDR 0x31
AlexQian 3:46535ec6d8b1 59 #define I2C_SLV4_REG 0x32
AlexQian 3:46535ec6d8b1 60 #define I2C_SLV4_DO 0x33
AlexQian 3:46535ec6d8b1 61 #define I2C_SLV4_CTRL 0x34
AlexQian 3:46535ec6d8b1 62 #define I2C_SLV4_DI 0x35
AlexQian 3:46535ec6d8b1 63 #define I2C_MST_STATUS 0x36
AlexQian 3:46535ec6d8b1 64 #define INT_PIN_CFG 0x37
AlexQian 3:46535ec6d8b1 65 #define INT_ENABLE 0x38
AlexQian 3:46535ec6d8b1 66 #define DMP_INT_STATUS 0x39 // Check DMP interrupt
AlexQian 3:46535ec6d8b1 67 #define INT_STATUS 0x3A
AlexQian 3:46535ec6d8b1 68 #define ACCEL_XOUT_H 0x3B
AlexQian 3:46535ec6d8b1 69 #define ACCEL_XOUT_L 0x3C
AlexQian 3:46535ec6d8b1 70 #define ACCEL_YOUT_H 0x3D
AlexQian 3:46535ec6d8b1 71 #define ACCEL_YOUT_L 0x3E
AlexQian 3:46535ec6d8b1 72 #define ACCEL_ZOUT_H 0x3F
AlexQian 3:46535ec6d8b1 73 #define ACCEL_ZOUT_L 0x40
AlexQian 3:46535ec6d8b1 74 #define TEMP_OUT_H 0x41
AlexQian 3:46535ec6d8b1 75 #define TEMP_OUT_L 0x42
AlexQian 3:46535ec6d8b1 76 #define GYRO_XOUT_H 0x43
AlexQian 3:46535ec6d8b1 77 #define GYRO_XOUT_L 0x44
AlexQian 3:46535ec6d8b1 78 #define GYRO_YOUT_H 0x45
AlexQian 3:46535ec6d8b1 79 #define GYRO_YOUT_L 0x46
AlexQian 3:46535ec6d8b1 80 #define GYRO_ZOUT_H 0x47
AlexQian 3:46535ec6d8b1 81 #define GYRO_ZOUT_L 0x48
AlexQian 3:46535ec6d8b1 82 #define EXT_SENS_DATA_00 0x49
AlexQian 3:46535ec6d8b1 83 #define EXT_SENS_DATA_01 0x4A
AlexQian 3:46535ec6d8b1 84 #define EXT_SENS_DATA_02 0x4B
AlexQian 3:46535ec6d8b1 85 #define EXT_SENS_DATA_03 0x4C
AlexQian 3:46535ec6d8b1 86 #define EXT_SENS_DATA_04 0x4D
AlexQian 3:46535ec6d8b1 87 #define EXT_SENS_DATA_05 0x4E
AlexQian 3:46535ec6d8b1 88 #define EXT_SENS_DATA_06 0x4F
AlexQian 3:46535ec6d8b1 89 #define EXT_SENS_DATA_07 0x50
AlexQian 3:46535ec6d8b1 90 #define EXT_SENS_DATA_08 0x51
AlexQian 3:46535ec6d8b1 91 #define EXT_SENS_DATA_09 0x52
AlexQian 3:46535ec6d8b1 92 #define EXT_SENS_DATA_10 0x53
AlexQian 3:46535ec6d8b1 93 #define EXT_SENS_DATA_11 0x54
AlexQian 3:46535ec6d8b1 94 #define EXT_SENS_DATA_12 0x55
AlexQian 3:46535ec6d8b1 95 #define EXT_SENS_DATA_13 0x56
AlexQian 3:46535ec6d8b1 96 #define EXT_SENS_DATA_14 0x57
AlexQian 3:46535ec6d8b1 97 #define EXT_SENS_DATA_15 0x58
AlexQian 3:46535ec6d8b1 98 #define EXT_SENS_DATA_16 0x59
AlexQian 3:46535ec6d8b1 99 #define EXT_SENS_DATA_17 0x5A
AlexQian 3:46535ec6d8b1 100 #define EXT_SENS_DATA_18 0x5B
AlexQian 3:46535ec6d8b1 101 #define EXT_SENS_DATA_19 0x5C
AlexQian 3:46535ec6d8b1 102 #define EXT_SENS_DATA_20 0x5D
AlexQian 3:46535ec6d8b1 103 #define EXT_SENS_DATA_21 0x5E
AlexQian 3:46535ec6d8b1 104 #define EXT_SENS_DATA_22 0x5F
AlexQian 3:46535ec6d8b1 105 #define EXT_SENS_DATA_23 0x60
AlexQian 3:46535ec6d8b1 106 #define MOT_DETECT_STATUS 0x61
AlexQian 3:46535ec6d8b1 107 #define I2C_SLV0_DO 0x63
AlexQian 3:46535ec6d8b1 108 #define I2C_SLV1_DO 0x64
AlexQian 3:46535ec6d8b1 109 #define I2C_SLV2_DO 0x65
AlexQian 3:46535ec6d8b1 110 #define I2C_SLV3_DO 0x66
AlexQian 3:46535ec6d8b1 111 #define I2C_MST_DELAY_CTRL 0x67
AlexQian 3:46535ec6d8b1 112 #define SIGNAL_PATH_RESET 0x68
AlexQian 3:46535ec6d8b1 113 #define MOT_DETECT_CTRL 0x69
AlexQian 3:46535ec6d8b1 114 #define USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP
AlexQian 3:46535ec6d8b1 115 #define PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode
AlexQian 3:46535ec6d8b1 116 #define PWR_MGMT_2 0x6C
AlexQian 3:46535ec6d8b1 117 #define DMP_BANK 0x6D // Activates a specific bank in the DMP
AlexQian 3:46535ec6d8b1 118 #define DMP_RW_PNT 0x6E // Set read/write pointer to a specific start address in specified DMP bank
AlexQian 3:46535ec6d8b1 119 #define DMP_REG 0x6F // Register in DMP from which to read or to which to write
AlexQian 3:46535ec6d8b1 120 #define DMP_REG_1 0x70
AlexQian 3:46535ec6d8b1 121 #define DMP_REG_2 0x71
AlexQian 3:46535ec6d8b1 122 #define FIFO_COUNTH 0x72
AlexQian 3:46535ec6d8b1 123 #define FIFO_COUNTL 0x73
AlexQian 3:46535ec6d8b1 124 #define FIFO_R_W 0x74
AlexQian 3:46535ec6d8b1 125 #define WHO_AM_I_MPU6050 0x75 // Should return 0x68
AlexQian 3:46535ec6d8b1 126
AlexQian 3:46535ec6d8b1 127 // Using the GY-521 breakout board, I set ADO to 0 by grounding through a 4k7 resistor
AlexQian 3:46535ec6d8b1 128 // Seven-bit device address is 110100 for ADO = 0 and 110101 for ADO = 1
AlexQian 3:46535ec6d8b1 129 #define ADO 0
AlexQian 3:46535ec6d8b1 130 #if ADO
AlexQian 3:46535ec6d8b1 131 #define MPU6050_ADDRESS 0x69<<1 // Device address when ADO = 1
AlexQian 3:46535ec6d8b1 132 #else
AlexQian 3:46535ec6d8b1 133 #define MPU6050_ADDRESS 0x68<<1 // Device address when ADO = 0
AlexQian 3:46535ec6d8b1 134 #endif
AlexQian 3:46535ec6d8b1 135 Timer t;
AlexQian 3:46535ec6d8b1 136 // Set initial input parameters
AlexQian 3:46535ec6d8b1 137 enum Ascale {
AlexQian 3:46535ec6d8b1 138 AFS_2G = 0,
AlexQian 3:46535ec6d8b1 139 AFS_4G,
AlexQian 3:46535ec6d8b1 140 AFS_8G,
AlexQian 3:46535ec6d8b1 141 AFS_16G
AlexQian 3:46535ec6d8b1 142 };
AlexQian 3:46535ec6d8b1 143
AlexQian 3:46535ec6d8b1 144 enum Gscale {
AlexQian 3:46535ec6d8b1 145 GFS_250DPS = 0,
AlexQian 3:46535ec6d8b1 146 GFS_500DPS,
AlexQian 3:46535ec6d8b1 147 GFS_1000DPS,
AlexQian 3:46535ec6d8b1 148 GFS_2000DPS
AlexQian 3:46535ec6d8b1 149 };
AlexQian 3:46535ec6d8b1 150
AlexQian 3:46535ec6d8b1 151 // Specify sensor full scale
AlexQian 3:46535ec6d8b1 152 int Gscale = GFS_250DPS;
AlexQian 3:46535ec6d8b1 153 int Ascale = AFS_2G;
AlexQian 3:46535ec6d8b1 154
AlexQian 3:46535ec6d8b1 155 //Set up I2C, (SDA,SCL)
AlexQian 4:1b985e622d26 156
AlexQian 3:46535ec6d8b1 157
AlexQian 3:46535ec6d8b1 158 //DigitalOut myled(LED1);
AlexQian 3:46535ec6d8b1 159
AlexQian 3:46535ec6d8b1 160 float aRes, gRes; // scale resolutions per LSB for the sensors
AlexQian 3:46535ec6d8b1 161
AlexQian 3:46535ec6d8b1 162 // Pin definitions
AlexQian 3:46535ec6d8b1 163 int intPin = 12; // These can be changed, 2 and 3 are the Arduinos ext int pins
AlexQian 3:46535ec6d8b1 164
AlexQian 3:46535ec6d8b1 165 int16_t accelCount[3]; // Stores the 16-bit signed accelerometer sensor output
AlexQian 3:46535ec6d8b1 166 float ax, ay, az; // Stores the real accel value in g's
AlexQian 3:46535ec6d8b1 167 int16_t gyroCount[3]; // Stores the 16-bit signed gyro sensor output
AlexQian 3:46535ec6d8b1 168 float gx, gy, gz; // Stores the real gyro value in degrees per seconds
AlexQian 3:46535ec6d8b1 169 float gyroBias[3] = {0, 0, 0}, accelBias[3] = {0, 0, 0}; // Bias corrections for gyro and accelerometer
AlexQian 3:46535ec6d8b1 170 int16_t tempCount; // Stores the real internal chip temperature in degrees Celsius
AlexQian 3:46535ec6d8b1 171 float temperature;
AlexQian 3:46535ec6d8b1 172 float SelfTest[6];
AlexQian 3:46535ec6d8b1 173
AlexQian 3:46535ec6d8b1 174 int delt_t = 0; // used to control display output rate
AlexQian 3:46535ec6d8b1 175 int count1 = 0; // used to control display output rate
AlexQian 3:46535ec6d8b1 176
AlexQian 3:46535ec6d8b1 177 // parameters for 6 DoF sensor fusion calculations
AlexQian 3:46535ec6d8b1 178 float PI = 3.14159265358979323846f;
AlexQian 3:46535ec6d8b1 179 float GyroMeasError = PI * (60.0f / 180.0f); // gyroscope measurement error in rads/s (start at 60 deg/s), then reduce after ~10 s to 3
AlexQian 3:46535ec6d8b1 180 float beta = sqrt(3.0f / 4.0f) * GyroMeasError; // compute beta
AlexQian 3:46535ec6d8b1 181 float GyroMeasDrift = PI * (1.0f / 180.0f); // gyroscope measurement drift in rad/s/s (start at 0.0 deg/s/s)
AlexQian 3:46535ec6d8b1 182 float zeta = sqrt(3.0f / 4.0f) * GyroMeasDrift; // compute zeta, the other free parameter in the Madgwick scheme usually set to a small or zero value
AlexQian 3:46535ec6d8b1 183 //float pitch, yaw, roll;
AlexQian 3:46535ec6d8b1 184 float deltat = 0.0f; // integration interval for both filter schemes
AlexQian 3:46535ec6d8b1 185 int lastUpdate = 0, firstUpdate = 0, Now = 0; // used to calculate integration interval // used to calculate integration interval
AlexQian 3:46535ec6d8b1 186 float q[4] = {1.0f, 0.0f, 0.0f, 0.0f}; // vector to hold quaternion
AlexQian 3:46535ec6d8b1 187
AlexQian 4:1b985e622d26 188 class mpu6050 {
AlexQian 3:46535ec6d8b1 189
AlexQian 3:46535ec6d8b1 190 protected:
AlexQian 4:1b985e622d26 191 I2C i2c;
AlexQian 4:1b985e622d26 192 Timer t;
AlexQian 3:46535ec6d8b1 193
AlexQian 3:46535ec6d8b1 194 public:
AlexQian 3:46535ec6d8b1 195 //===================================================================================================================
AlexQian 3:46535ec6d8b1 196 //====== Set of useful function to access acceleratio, gyroscope, and temperature data
AlexQian 3:46535ec6d8b1 197 //===================================================================================================================
AlexQian 3:46535ec6d8b1 198
AlexQian 4:1b985e622d26 199 mpu6050(PinName SDA,PinName SCL):i2c(SDA,SCL)
AlexQian 3:46535ec6d8b1 200 {
AlexQian 4:1b985e622d26 201 t.start();
AlexQian 4:1b985e622d26 202 i2c.frequency(400000);
AlexQian 3:46535ec6d8b1 203 }
AlexQian 3:46535ec6d8b1 204 void writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
AlexQian 3:46535ec6d8b1 205 {
AlexQian 3:46535ec6d8b1 206 char data_write[2];
AlexQian 3:46535ec6d8b1 207 data_write[0] = subAddress;
AlexQian 3:46535ec6d8b1 208 data_write[1] = data;
AlexQian 3:46535ec6d8b1 209 i2c.write(address, data_write, 2, 0);
AlexQian 3:46535ec6d8b1 210 }
AlexQian 3:46535ec6d8b1 211
AlexQian 3:46535ec6d8b1 212 char readByte(uint8_t address, uint8_t subAddress)
AlexQian 3:46535ec6d8b1 213 {
AlexQian 3:46535ec6d8b1 214 char data[1]; // `data` will store the register data
AlexQian 3:46535ec6d8b1 215 char data_write[1];
AlexQian 3:46535ec6d8b1 216 data_write[0] = subAddress;
AlexQian 3:46535ec6d8b1 217 i2c.write(address, data_write, 1, 1); // no stop
AlexQian 3:46535ec6d8b1 218 i2c.read(address, data, 1, 0);
AlexQian 3:46535ec6d8b1 219 return data[0];
AlexQian 3:46535ec6d8b1 220 }
AlexQian 3:46535ec6d8b1 221
AlexQian 3:46535ec6d8b1 222 void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest)
AlexQian 3:46535ec6d8b1 223 {
AlexQian 3:46535ec6d8b1 224 char data[14];
AlexQian 3:46535ec6d8b1 225 char data_write[1];
AlexQian 3:46535ec6d8b1 226 data_write[0] = subAddress;
AlexQian 3:46535ec6d8b1 227 i2c.write(address, data_write, 1, 1); // no stop
AlexQian 3:46535ec6d8b1 228 i2c.read(address, data, count, 0);
AlexQian 3:46535ec6d8b1 229 for(int ii = 0; ii < count; ii++) {
AlexQian 3:46535ec6d8b1 230 dest[ii] = data[ii];
AlexQian 3:46535ec6d8b1 231 }
AlexQian 3:46535ec6d8b1 232 }
AlexQian 3:46535ec6d8b1 233
AlexQian 3:46535ec6d8b1 234
AlexQian 3:46535ec6d8b1 235 void getGres() {
AlexQian 3:46535ec6d8b1 236 switch (Gscale)
AlexQian 3:46535ec6d8b1 237 {
AlexQian 3:46535ec6d8b1 238 // Possible gyro scales (and their register bit settings) are:
AlexQian 3:46535ec6d8b1 239 // 250 DPS (00), 500 DPS (01), 1000 DPS (10), and 2000 DPS (11).
AlexQian 3:46535ec6d8b1 240 // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value:
AlexQian 3:46535ec6d8b1 241 case GFS_250DPS:
AlexQian 3:46535ec6d8b1 242 gRes = 250.0/32768.0;
AlexQian 3:46535ec6d8b1 243 break;
AlexQian 3:46535ec6d8b1 244 case GFS_500DPS:
AlexQian 3:46535ec6d8b1 245 gRes = 500.0/32768.0;
AlexQian 3:46535ec6d8b1 246 break;
AlexQian 3:46535ec6d8b1 247 case GFS_1000DPS:
AlexQian 3:46535ec6d8b1 248 gRes = 1000.0/32768.0;
AlexQian 3:46535ec6d8b1 249 break;
AlexQian 3:46535ec6d8b1 250 case GFS_2000DPS:
AlexQian 3:46535ec6d8b1 251 gRes = 2000.0/32768.0;
AlexQian 3:46535ec6d8b1 252 break;
AlexQian 3:46535ec6d8b1 253 }
AlexQian 3:46535ec6d8b1 254 }
AlexQian 3:46535ec6d8b1 255
AlexQian 3:46535ec6d8b1 256 void getAres() {
AlexQian 3:46535ec6d8b1 257 switch (Ascale)
AlexQian 3:46535ec6d8b1 258 {
AlexQian 3:46535ec6d8b1 259 // Possible accelerometer scales (and their register bit settings) are:
AlexQian 3:46535ec6d8b1 260 // 2 Gs (00), 4 Gs (01), 8 Gs (10), and 16 Gs (11).
AlexQian 3:46535ec6d8b1 261 // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value:
AlexQian 3:46535ec6d8b1 262 case AFS_2G:
AlexQian 3:46535ec6d8b1 263 aRes = 2.0/32768.0;
AlexQian 3:46535ec6d8b1 264 break;
AlexQian 3:46535ec6d8b1 265 case AFS_4G:
AlexQian 3:46535ec6d8b1 266 aRes = 4.0/32768.0;
AlexQian 3:46535ec6d8b1 267 break;
AlexQian 3:46535ec6d8b1 268 case AFS_8G:
AlexQian 3:46535ec6d8b1 269 aRes = 8.0/32768.0;
AlexQian 3:46535ec6d8b1 270 break;
AlexQian 3:46535ec6d8b1 271 case AFS_16G:
AlexQian 3:46535ec6d8b1 272 aRes = 16.0/32768.0;
AlexQian 3:46535ec6d8b1 273 break;
AlexQian 3:46535ec6d8b1 274 }
AlexQian 3:46535ec6d8b1 275 }
AlexQian 3:46535ec6d8b1 276
AlexQian 3:46535ec6d8b1 277
AlexQian 3:46535ec6d8b1 278 void readAccelData(int16_t * destination)
AlexQian 3:46535ec6d8b1 279 {
AlexQian 3:46535ec6d8b1 280 uint8_t rawData[6]; // x/y/z accel register data stored here
AlexQian 3:46535ec6d8b1 281 readBytes(MPU6050_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]); // Read the six raw data registers into data array
AlexQian 3:46535ec6d8b1 282 destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
AlexQian 3:46535ec6d8b1 283 destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ;
AlexQian 3:46535ec6d8b1 284 destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ;
AlexQian 3:46535ec6d8b1 285 }
AlexQian 3:46535ec6d8b1 286
AlexQian 3:46535ec6d8b1 287 void readGyroData(int16_t * destination)
AlexQian 3:46535ec6d8b1 288 {
AlexQian 3:46535ec6d8b1 289 uint8_t rawData[6]; // x/y/z gyro register data stored here
AlexQian 3:46535ec6d8b1 290 readBytes(MPU6050_ADDRESS, GYRO_XOUT_H, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
AlexQian 3:46535ec6d8b1 291 destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
AlexQian 3:46535ec6d8b1 292 destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ;
AlexQian 3:46535ec6d8b1 293 destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ;
AlexQian 3:46535ec6d8b1 294 }
AlexQian 3:46535ec6d8b1 295
AlexQian 3:46535ec6d8b1 296 int16_t readTempData()
AlexQian 3:46535ec6d8b1 297 {
AlexQian 3:46535ec6d8b1 298 uint8_t rawData[2]; // x/y/z gyro register data stored here
AlexQian 3:46535ec6d8b1 299 readBytes(MPU6050_ADDRESS, TEMP_OUT_H, 2, &rawData[0]); // Read the two raw data registers sequentially into data array
AlexQian 3:46535ec6d8b1 300 return (int16_t)(((int16_t)rawData[0]) << 8 | rawData[1]) ; // Turn the MSB and LSB into a 16-bit value
AlexQian 3:46535ec6d8b1 301 }
AlexQian 3:46535ec6d8b1 302
AlexQian 3:46535ec6d8b1 303
AlexQian 3:46535ec6d8b1 304
AlexQian 3:46535ec6d8b1 305 // Configure the motion detection control for low power accelerometer mode
AlexQian 3:46535ec6d8b1 306 void LowPowerAccelOnly()
AlexQian 3:46535ec6d8b1 307 {
AlexQian 3:46535ec6d8b1 308
AlexQian 3:46535ec6d8b1 309 // The sensor has a high-pass filter necessary to invoke to allow the sensor motion detection algorithms work properly
AlexQian 3:46535ec6d8b1 310 // Motion detection occurs on free-fall (acceleration below a threshold for some time for all axes), motion (acceleration
AlexQian 3:46535ec6d8b1 311 // above a threshold for some time on at least one axis), and zero-motion toggle (acceleration on each axis less than a
AlexQian 3:46535ec6d8b1 312 // threshold for some time sets this flag, motion above the threshold turns it off). The high-pass filter takes gravity out
AlexQian 3:46535ec6d8b1 313 // consideration for these threshold evaluations; otherwise, the flags would be set all the time!
AlexQian 3:46535ec6d8b1 314
AlexQian 3:46535ec6d8b1 315 uint8_t c = readByte(MPU6050_ADDRESS, PWR_MGMT_1);
AlexQian 3:46535ec6d8b1 316 writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c & ~0x30); // Clear sleep and cycle bits [5:6]
AlexQian 3:46535ec6d8b1 317 writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c | 0x30); // Set sleep and cycle bits [5:6] to zero to make sure accelerometer is running
AlexQian 3:46535ec6d8b1 318
AlexQian 3:46535ec6d8b1 319 c = readByte(MPU6050_ADDRESS, PWR_MGMT_2);
AlexQian 3:46535ec6d8b1 320 writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c & ~0x38); // Clear standby XA, YA, and ZA bits [3:5]
AlexQian 3:46535ec6d8b1 321 writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c | 0x00); // Set XA, YA, and ZA bits [3:5] to zero to make sure accelerometer is running
AlexQian 3:46535ec6d8b1 322
AlexQian 3:46535ec6d8b1 323 c = readByte(MPU6050_ADDRESS, ACCEL_CONFIG);
AlexQian 3:46535ec6d8b1 324 writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0x07); // Clear high-pass filter bits [2:0]
AlexQian 3:46535ec6d8b1 325 // Set high-pass filter to 0) reset (disable), 1) 5 Hz, 2) 2.5 Hz, 3) 1.25 Hz, 4) 0.63 Hz, or 7) Hold
AlexQian 3:46535ec6d8b1 326 writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c | 0x00); // Set ACCEL_HPF to 0; reset mode disbaling high-pass filter
AlexQian 3:46535ec6d8b1 327
AlexQian 3:46535ec6d8b1 328 c = readByte(MPU6050_ADDRESS, CONFIG);
AlexQian 3:46535ec6d8b1 329 writeByte(MPU6050_ADDRESS, CONFIG, c & ~0x07); // Clear low-pass filter bits [2:0]
AlexQian 3:46535ec6d8b1 330 writeByte(MPU6050_ADDRESS, CONFIG, c | 0x00); // Set DLPD_CFG to 0; 260 Hz bandwidth, 1 kHz rate
AlexQian 3:46535ec6d8b1 331
AlexQian 3:46535ec6d8b1 332 c = readByte(MPU6050_ADDRESS, INT_ENABLE);
AlexQian 3:46535ec6d8b1 333 writeByte(MPU6050_ADDRESS, INT_ENABLE, c & ~0xFF); // Clear all interrupts
AlexQian 3:46535ec6d8b1 334 writeByte(MPU6050_ADDRESS, INT_ENABLE, 0x40); // Enable motion threshold (bits 5) interrupt only
AlexQian 3:46535ec6d8b1 335
AlexQian 3:46535ec6d8b1 336 // Motion detection interrupt requires the absolute value of any axis to lie above the detection threshold
AlexQian 3:46535ec6d8b1 337 // for at least the counter duration
AlexQian 3:46535ec6d8b1 338 writeByte(MPU6050_ADDRESS, MOT_THR, 0x80); // Set motion detection to 0.256 g; LSB = 2 mg
AlexQian 3:46535ec6d8b1 339 writeByte(MPU6050_ADDRESS, MOT_DUR, 0x01); // Set motion detect duration to 1 ms; LSB is 1 ms @ 1 kHz rate
AlexQian 3:46535ec6d8b1 340
AlexQian 3:46535ec6d8b1 341 wait(0.1); // Add delay for accumulation of samples
AlexQian 3:46535ec6d8b1 342
AlexQian 3:46535ec6d8b1 343 c = readByte(MPU6050_ADDRESS, ACCEL_CONFIG);
AlexQian 3:46535ec6d8b1 344 writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0x07); // Clear high-pass filter bits [2:0]
AlexQian 3:46535ec6d8b1 345 writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c | 0x07); // Set ACCEL_HPF to 7; hold the initial accleration value as a referance
AlexQian 3:46535ec6d8b1 346
AlexQian 3:46535ec6d8b1 347 c = readByte(MPU6050_ADDRESS, PWR_MGMT_2);
AlexQian 3:46535ec6d8b1 348 writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c & ~0xC7); // Clear standby XA, YA, and ZA bits [3:5] and LP_WAKE_CTRL bits [6:7]
AlexQian 3:46535ec6d8b1 349 writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c | 0x47); // Set wakeup frequency to 5 Hz, and disable XG, YG, and ZG gyros (bits [0:2])
AlexQian 3:46535ec6d8b1 350
AlexQian 3:46535ec6d8b1 351 c = readByte(MPU6050_ADDRESS, PWR_MGMT_1);
AlexQian 3:46535ec6d8b1 352 writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c & ~0x20); // Clear sleep and cycle bit 5
AlexQian 3:46535ec6d8b1 353 writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c | 0x20); // Set cycle bit 5 to begin low power accelerometer motion interrupts
AlexQian 3:46535ec6d8b1 354
AlexQian 3:46535ec6d8b1 355 }
AlexQian 3:46535ec6d8b1 356
AlexQian 3:46535ec6d8b1 357
AlexQian 3:46535ec6d8b1 358 void resetMPU6050() {
AlexQian 3:46535ec6d8b1 359 // reset device
AlexQian 3:46535ec6d8b1 360 writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device
AlexQian 3:46535ec6d8b1 361 wait(0.1);
AlexQian 3:46535ec6d8b1 362 }
AlexQian 3:46535ec6d8b1 363
AlexQian 3:46535ec6d8b1 364
AlexQian 3:46535ec6d8b1 365 void initMPU6050()
AlexQian 3:46535ec6d8b1 366 {
AlexQian 3:46535ec6d8b1 367 // Initialize MPU6050 device
AlexQian 3:46535ec6d8b1 368 // wake up device
AlexQian 3:46535ec6d8b1 369 writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x00); // Clear sleep mode bit (6), enable all sensors
AlexQian 3:46535ec6d8b1 370 wait(0.1); // Delay 100 ms for PLL to get established on x-axis gyro; should check for PLL ready interrupt
AlexQian 3:46535ec6d8b1 371
AlexQian 3:46535ec6d8b1 372 // get stable time source
AlexQian 3:46535ec6d8b1 373 writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x01); // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001
AlexQian 3:46535ec6d8b1 374
AlexQian 3:46535ec6d8b1 375 // Configure Gyro and Accelerometer
AlexQian 3:46535ec6d8b1 376 // Disable FSYNC and set accelerometer and gyro bandwidth to 44 and 42 Hz, respectively;
AlexQian 3:46535ec6d8b1 377 // DLPF_CFG = bits 2:0 = 010; this sets the sample rate at 1 kHz for both
AlexQian 3:46535ec6d8b1 378 // Maximum delay is 4.9 ms which is just over a 200 Hz maximum rate
AlexQian 3:46535ec6d8b1 379 writeByte(MPU6050_ADDRESS, CONFIG, 0x03);
AlexQian 3:46535ec6d8b1 380
AlexQian 3:46535ec6d8b1 381 // Set sample rate = gyroscope output rate/(1 + SMPLRT_DIV)
AlexQian 3:46535ec6d8b1 382 writeByte(MPU6050_ADDRESS, SMPLRT_DIV, 0x04); // Use a 200 Hz rate; the same rate set in CONFIG above
AlexQian 3:46535ec6d8b1 383
AlexQian 3:46535ec6d8b1 384 // Set gyroscope full scale range
AlexQian 3:46535ec6d8b1 385 // Range selects FS_SEL and AFS_SEL are 0 - 3, so 2-bit values are left-shifted into positions 4:3
AlexQian 3:46535ec6d8b1 386 uint8_t c = readByte(MPU6050_ADDRESS, GYRO_CONFIG);
AlexQian 3:46535ec6d8b1 387 writeByte(MPU6050_ADDRESS, GYRO_CONFIG, c & ~0xE0); // Clear self-test bits [7:5]
AlexQian 3:46535ec6d8b1 388 writeByte(MPU6050_ADDRESS, GYRO_CONFIG, c & ~0x18); // Clear AFS bits [4:3]
AlexQian 3:46535ec6d8b1 389 writeByte(MPU6050_ADDRESS, GYRO_CONFIG, c | Gscale << 3); // Set full scale range for the gyro
AlexQian 3:46535ec6d8b1 390
AlexQian 3:46535ec6d8b1 391 // Set accelerometer configuration
AlexQian 3:46535ec6d8b1 392 c = readByte(MPU6050_ADDRESS, ACCEL_CONFIG);
AlexQian 3:46535ec6d8b1 393 writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0xE0); // Clear self-test bits [7:5]
AlexQian 3:46535ec6d8b1 394 writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0x18); // Clear AFS bits [4:3]
AlexQian 3:46535ec6d8b1 395 writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c | Ascale << 3); // Set full scale range for the accelerometer
AlexQian 3:46535ec6d8b1 396
AlexQian 3:46535ec6d8b1 397 // Configure Interrupts and Bypass Enable
AlexQian 3:46535ec6d8b1 398 // Set interrupt pin active high, push-pull, and clear on read of INT_STATUS, enable I2C_BYPASS_EN so additional chips
AlexQian 3:46535ec6d8b1 399 // can join the I2C bus and all can be controlled by the Arduino as master
AlexQian 3:46535ec6d8b1 400 writeByte(MPU6050_ADDRESS, INT_PIN_CFG, 0x22);
AlexQian 3:46535ec6d8b1 401 writeByte(MPU6050_ADDRESS, INT_ENABLE, 0x01); // Enable data ready (bit 0) interrupt
AlexQian 3:46535ec6d8b1 402 }
AlexQian 3:46535ec6d8b1 403
AlexQian 3:46535ec6d8b1 404 // Function which accumulates gyro and accelerometer data after device initialization. It calculates the average
AlexQian 3:46535ec6d8b1 405 // of the at-rest readings and then loads the resulting offsets into accelerometer and gyro bias registers.
AlexQian 3:46535ec6d8b1 406 void calibrateMPU6050(float * dest1, float * dest2)
AlexQian 3:46535ec6d8b1 407 {
AlexQian 3:46535ec6d8b1 408 uint8_t data[12]; // data array to hold accelerometer and gyro x, y, z, data
AlexQian 3:46535ec6d8b1 409 uint16_t ii, packet_count, fifo_count;
AlexQian 3:46535ec6d8b1 410 int32_t gyro_bias[3] = {0, 0, 0}, accel_bias[3] = {0, 0, 0};
AlexQian 3:46535ec6d8b1 411
AlexQian 3:46535ec6d8b1 412 // reset device, reset all registers, clear gyro and accelerometer bias registers
AlexQian 3:46535ec6d8b1 413 writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device
AlexQian 3:46535ec6d8b1 414 wait(0.1);
AlexQian 3:46535ec6d8b1 415
AlexQian 3:46535ec6d8b1 416 // get stable time source
AlexQian 3:46535ec6d8b1 417 // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001
AlexQian 3:46535ec6d8b1 418 writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x01);
AlexQian 3:46535ec6d8b1 419 writeByte(MPU6050_ADDRESS, PWR_MGMT_2, 0x00);
AlexQian 3:46535ec6d8b1 420 wait(0.2);
AlexQian 3:46535ec6d8b1 421
AlexQian 3:46535ec6d8b1 422 // Configure device for bias calculation
AlexQian 3:46535ec6d8b1 423 writeByte(MPU6050_ADDRESS, INT_ENABLE, 0x00); // Disable all interrupts
AlexQian 3:46535ec6d8b1 424 writeByte(MPU6050_ADDRESS, FIFO_EN, 0x00); // Disable FIFO
AlexQian 3:46535ec6d8b1 425 writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x00); // Turn on internal clock source
AlexQian 3:46535ec6d8b1 426 writeByte(MPU6050_ADDRESS, I2C_MST_CTRL, 0x00); // Disable I2C master
AlexQian 3:46535ec6d8b1 427 writeByte(MPU6050_ADDRESS, USER_CTRL, 0x00); // Disable FIFO and I2C master modes
AlexQian 3:46535ec6d8b1 428 writeByte(MPU6050_ADDRESS, USER_CTRL, 0x0C); // Reset FIFO and DMP
AlexQian 3:46535ec6d8b1 429 wait(0.015);
AlexQian 3:46535ec6d8b1 430
AlexQian 3:46535ec6d8b1 431 // Configure MPU6050 gyro and accelerometer for bias calculation
AlexQian 3:46535ec6d8b1 432 writeByte(MPU6050_ADDRESS, CONFIG, 0x01); // Set low-pass filter to 188 Hz
AlexQian 3:46535ec6d8b1 433 writeByte(MPU6050_ADDRESS, SMPLRT_DIV, 0x00); // Set sample rate to 1 kHz
AlexQian 3:46535ec6d8b1 434 writeByte(MPU6050_ADDRESS, GYRO_CONFIG, 0x00); // Set gyro full-scale to 250 degrees per second, maximum sensitivity
AlexQian 3:46535ec6d8b1 435 writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, 0x00); // Set accelerometer full-scale to 2 g, maximum sensitivity
AlexQian 3:46535ec6d8b1 436
AlexQian 3:46535ec6d8b1 437 uint16_t gyrosensitivity = 131; // = 131 LSB/degrees/sec
AlexQian 3:46535ec6d8b1 438 uint16_t accelsensitivity = 16384; // = 16384 LSB/g
AlexQian 3:46535ec6d8b1 439
AlexQian 3:46535ec6d8b1 440 // Configure FIFO to capture accelerometer and gyro data for bias calculation
AlexQian 3:46535ec6d8b1 441 writeByte(MPU6050_ADDRESS, USER_CTRL, 0x40); // Enable FIFO
AlexQian 3:46535ec6d8b1 442 writeByte(MPU6050_ADDRESS, FIFO_EN, 0x78); // Enable gyro and accelerometer sensors for FIFO (max size 1024 bytes in MPU-6050)
AlexQian 3:46535ec6d8b1 443 wait(0.08); // accumulate 80 samples in 80 milliseconds = 960 bytes
AlexQian 3:46535ec6d8b1 444
AlexQian 3:46535ec6d8b1 445 // At end of sample accumulation, turn off FIFO sensor read
AlexQian 3:46535ec6d8b1 446 writeByte(MPU6050_ADDRESS, FIFO_EN, 0x00); // Disable gyro and accelerometer sensors for FIFO
AlexQian 3:46535ec6d8b1 447 readBytes(MPU6050_ADDRESS, FIFO_COUNTH, 2, &data[0]); // read FIFO sample count
AlexQian 3:46535ec6d8b1 448 fifo_count = ((uint16_t)data[0] << 8) | data[1];
AlexQian 3:46535ec6d8b1 449 packet_count = fifo_count/12;// How many sets of full gyro and accelerometer data for averaging
AlexQian 3:46535ec6d8b1 450
AlexQian 3:46535ec6d8b1 451 for (ii = 0; ii < packet_count; ii++) {
AlexQian 3:46535ec6d8b1 452 int16_t accel_temp[3] = {0, 0, 0}, gyro_temp[3] = {0, 0, 0};
AlexQian 3:46535ec6d8b1 453 readBytes(MPU6050_ADDRESS, FIFO_R_W, 12, &data[0]); // read data for averaging
AlexQian 3:46535ec6d8b1 454 accel_temp[0] = (int16_t) (((int16_t)data[0] << 8) | data[1] ) ; // Form signed 16-bit integer for each sample in FIFO
AlexQian 3:46535ec6d8b1 455 accel_temp[1] = (int16_t) (((int16_t)data[2] << 8) | data[3] ) ;
AlexQian 3:46535ec6d8b1 456 accel_temp[2] = (int16_t) (((int16_t)data[4] << 8) | data[5] ) ;
AlexQian 3:46535ec6d8b1 457 gyro_temp[0] = (int16_t) (((int16_t)data[6] << 8) | data[7] ) ;
AlexQian 3:46535ec6d8b1 458 gyro_temp[1] = (int16_t) (((int16_t)data[8] << 8) | data[9] ) ;
AlexQian 3:46535ec6d8b1 459 gyro_temp[2] = (int16_t) (((int16_t)data[10] << 8) | data[11]) ;
AlexQian 3:46535ec6d8b1 460
AlexQian 3:46535ec6d8b1 461 accel_bias[0] += (int32_t) accel_temp[0]; // Sum individual signed 16-bit biases to get accumulated signed 32-bit biases
AlexQian 3:46535ec6d8b1 462 accel_bias[1] += (int32_t) accel_temp[1];
AlexQian 3:46535ec6d8b1 463 accel_bias[2] += (int32_t) accel_temp[2];
AlexQian 3:46535ec6d8b1 464 gyro_bias[0] += (int32_t) gyro_temp[0];
AlexQian 3:46535ec6d8b1 465 gyro_bias[1] += (int32_t) gyro_temp[1];
AlexQian 3:46535ec6d8b1 466 gyro_bias[2] += (int32_t) gyro_temp[2];
AlexQian 3:46535ec6d8b1 467
AlexQian 3:46535ec6d8b1 468 }
AlexQian 3:46535ec6d8b1 469 accel_bias[0] /= (int32_t) packet_count; // Normalize sums to get average count biases
AlexQian 3:46535ec6d8b1 470 accel_bias[1] /= (int32_t) packet_count;
AlexQian 3:46535ec6d8b1 471 accel_bias[2] /= (int32_t) packet_count;
AlexQian 3:46535ec6d8b1 472 gyro_bias[0] /= (int32_t) packet_count;
AlexQian 3:46535ec6d8b1 473 gyro_bias[1] /= (int32_t) packet_count;
AlexQian 3:46535ec6d8b1 474 gyro_bias[2] /= (int32_t) packet_count;
AlexQian 3:46535ec6d8b1 475
AlexQian 3:46535ec6d8b1 476 if(accel_bias[2] > 0L) {accel_bias[2] -= (int32_t) accelsensitivity;} // Remove gravity from the z-axis accelerometer bias calculation
AlexQian 3:46535ec6d8b1 477 else {accel_bias[2] += (int32_t) accelsensitivity;}
AlexQian 3:46535ec6d8b1 478
AlexQian 3:46535ec6d8b1 479 // Construct the gyro biases for push to the hardware gyro bias registers, which are reset to zero upon device startup
AlexQian 3:46535ec6d8b1 480 data[0] = (-gyro_bias[0]/4 >> 8) & 0xFF; // Divide by 4 to get 32.9 LSB per deg/s to conform to expected bias input format
AlexQian 3:46535ec6d8b1 481 data[1] = (-gyro_bias[0]/4) & 0xFF; // Biases are additive, so change sign on calculated average gyro biases
AlexQian 3:46535ec6d8b1 482 data[2] = (-gyro_bias[1]/4 >> 8) & 0xFF;
AlexQian 3:46535ec6d8b1 483 data[3] = (-gyro_bias[1]/4) & 0xFF;
AlexQian 3:46535ec6d8b1 484 data[4] = (-gyro_bias[2]/4 >> 8) & 0xFF;
AlexQian 3:46535ec6d8b1 485 data[5] = (-gyro_bias[2]/4) & 0xFF;
AlexQian 3:46535ec6d8b1 486
AlexQian 3:46535ec6d8b1 487 // Push gyro biases to hardware registers
AlexQian 3:46535ec6d8b1 488 writeByte(MPU6050_ADDRESS, XG_OFFS_USRH, data[0]);
AlexQian 3:46535ec6d8b1 489 writeByte(MPU6050_ADDRESS, XG_OFFS_USRL, data[1]);
AlexQian 3:46535ec6d8b1 490 writeByte(MPU6050_ADDRESS, YG_OFFS_USRH, data[2]);
AlexQian 3:46535ec6d8b1 491 writeByte(MPU6050_ADDRESS, YG_OFFS_USRL, data[3]);
AlexQian 3:46535ec6d8b1 492 writeByte(MPU6050_ADDRESS, ZG_OFFS_USRH, data[4]);
AlexQian 3:46535ec6d8b1 493 writeByte(MPU6050_ADDRESS, ZG_OFFS_USRL, data[5]);
AlexQian 3:46535ec6d8b1 494
AlexQian 3:46535ec6d8b1 495 dest1[0] = (float) gyro_bias[0]/(float) gyrosensitivity; // construct gyro bias in deg/s for later manual subtraction
AlexQian 3:46535ec6d8b1 496 dest1[1] = (float) gyro_bias[1]/(float) gyrosensitivity;
AlexQian 3:46535ec6d8b1 497 dest1[2] = (float) gyro_bias[2]/(float) gyrosensitivity;
AlexQian 3:46535ec6d8b1 498
AlexQian 3:46535ec6d8b1 499 // Construct the accelerometer biases for push to the hardware accelerometer bias registers. These registers contain
AlexQian 3:46535ec6d8b1 500 // factory trim values which must be added to the calculated accelerometer biases; on boot up these registers will hold
AlexQian 3:46535ec6d8b1 501 // non-zero values. In addition, bit 0 of the lower byte must be preserved since it is used for temperature
AlexQian 3:46535ec6d8b1 502 // compensation calculations. Accelerometer bias registers expect bias input as 2048 LSB per g, so that
AlexQian 3:46535ec6d8b1 503 // the accelerometer biases calculated above must be divided by 8.
AlexQian 3:46535ec6d8b1 504
AlexQian 3:46535ec6d8b1 505 int32_t accel_bias_reg[3] = {0, 0, 0}; // A place to hold the factory accelerometer trim biases
AlexQian 3:46535ec6d8b1 506 readBytes(MPU6050_ADDRESS, XA_OFFSET_H, 2, &data[0]); // Read factory accelerometer trim values
AlexQian 3:46535ec6d8b1 507 accel_bias_reg[0] = (int16_t) ((int16_t)data[0] << 8) | data[1];
AlexQian 3:46535ec6d8b1 508 readBytes(MPU6050_ADDRESS, YA_OFFSET_H, 2, &data[0]);
AlexQian 3:46535ec6d8b1 509 accel_bias_reg[1] = (int16_t) ((int16_t)data[0] << 8) | data[1];
AlexQian 3:46535ec6d8b1 510 readBytes(MPU6050_ADDRESS, ZA_OFFSET_H, 2, &data[0]);
AlexQian 3:46535ec6d8b1 511 accel_bias_reg[2] = (int16_t) ((int16_t)data[0] << 8) | data[1];
AlexQian 3:46535ec6d8b1 512
AlexQian 3:46535ec6d8b1 513 uint32_t mask = 1uL; // Define mask for temperature compensation bit 0 of lower byte of accelerometer bias registers
AlexQian 3:46535ec6d8b1 514 uint8_t mask_bit[3] = {0, 0, 0}; // Define array to hold mask bit for each accelerometer bias axis
AlexQian 3:46535ec6d8b1 515
AlexQian 3:46535ec6d8b1 516 for(ii = 0; ii < 3; ii++) {
AlexQian 3:46535ec6d8b1 517 if(accel_bias_reg[ii] & mask) mask_bit[ii] = 0x01; // If temperature compensation bit is set, record that fact in mask_bit
AlexQian 3:46535ec6d8b1 518 }
AlexQian 3:46535ec6d8b1 519
AlexQian 3:46535ec6d8b1 520 // Construct total accelerometer bias, including calculated average accelerometer bias from above
AlexQian 3:46535ec6d8b1 521 accel_bias_reg[0] -= (accel_bias[0]/8); // Subtract calculated averaged accelerometer bias scaled to 2048 LSB/g (16 g full scale)
AlexQian 3:46535ec6d8b1 522 accel_bias_reg[1] -= (accel_bias[1]/8);
AlexQian 3:46535ec6d8b1 523 accel_bias_reg[2] -= (accel_bias[2]/8);
AlexQian 3:46535ec6d8b1 524
AlexQian 3:46535ec6d8b1 525 data[0] = (accel_bias_reg[0] >> 8) & 0xFF;
AlexQian 3:46535ec6d8b1 526 data[1] = (accel_bias_reg[0]) & 0xFF;
AlexQian 3:46535ec6d8b1 527 data[1] = data[1] | mask_bit[0]; // preserve temperature compensation bit when writing back to accelerometer bias registers
AlexQian 3:46535ec6d8b1 528 data[2] = (accel_bias_reg[1] >> 8) & 0xFF;
AlexQian 3:46535ec6d8b1 529 data[3] = (accel_bias_reg[1]) & 0xFF;
AlexQian 3:46535ec6d8b1 530 data[3] = data[3] | mask_bit[1]; // preserve temperature compensation bit when writing back to accelerometer bias registers
AlexQian 3:46535ec6d8b1 531 data[4] = (accel_bias_reg[2] >> 8) & 0xFF;
AlexQian 3:46535ec6d8b1 532 data[5] = (accel_bias_reg[2]) & 0xFF;
AlexQian 3:46535ec6d8b1 533 data[5] = data[5] | mask_bit[2]; // preserve temperature compensation bit when writing back to accelerometer bias registers
AlexQian 3:46535ec6d8b1 534
AlexQian 3:46535ec6d8b1 535 // Push accelerometer biases to hardware registers
AlexQian 3:46535ec6d8b1 536 // writeByte(MPU6050_ADDRESS, XA_OFFSET_H, data[0]);
AlexQian 3:46535ec6d8b1 537 // writeByte(MPU6050_ADDRESS, XA_OFFSET_L_TC, data[1]);
AlexQian 3:46535ec6d8b1 538 // writeByte(MPU6050_ADDRESS, YA_OFFSET_H, data[2]);
AlexQian 3:46535ec6d8b1 539 // writeByte(MPU6050_ADDRESS, YA_OFFSET_L_TC, data[3]);
AlexQian 3:46535ec6d8b1 540 // writeByte(MPU6050_ADDRESS, ZA_OFFSET_H, data[4]);
AlexQian 3:46535ec6d8b1 541 // writeByte(MPU6050_ADDRESS, ZA_OFFSET_L_TC, data[5]);
AlexQian 3:46535ec6d8b1 542
AlexQian 3:46535ec6d8b1 543 // Output scaled accelerometer biases for manual subtraction in the main program
AlexQian 3:46535ec6d8b1 544 dest2[0] = (float)accel_bias[0]/(float)accelsensitivity;
AlexQian 3:46535ec6d8b1 545 dest2[1] = (float)accel_bias[1]/(float)accelsensitivity;
AlexQian 3:46535ec6d8b1 546 dest2[2] = (float)accel_bias[2]/(float)accelsensitivity;
AlexQian 3:46535ec6d8b1 547 }
AlexQian 3:46535ec6d8b1 548
AlexQian 3:46535ec6d8b1 549
AlexQian 3:46535ec6d8b1 550 // Accelerometer and gyroscope self test; check calibration wrt factory settings
AlexQian 3:46535ec6d8b1 551 void MPU6050SelfTest(float * destination) // Should return percent deviation from factory trim values, +/- 14 or less deviation is a pass
AlexQian 3:46535ec6d8b1 552 {
AlexQian 3:46535ec6d8b1 553 uint8_t rawData[4] = {0, 0, 0, 0};
AlexQian 3:46535ec6d8b1 554 uint8_t selfTest[6];
AlexQian 3:46535ec6d8b1 555 float factoryTrim[6];
AlexQian 3:46535ec6d8b1 556
AlexQian 3:46535ec6d8b1 557 // Configure the accelerometer for self-test
AlexQian 3:46535ec6d8b1 558 writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, 0xF0); // Enable self test on all three axes and set accelerometer range to +/- 8 g
AlexQian 3:46535ec6d8b1 559 writeByte(MPU6050_ADDRESS, GYRO_CONFIG, 0xE0); // Enable self test on all three axes and set gyro range to +/- 250 degrees/s
AlexQian 3:46535ec6d8b1 560 wait(0.25); // Delay a while to let the device execute the self-test
AlexQian 3:46535ec6d8b1 561 rawData[0] = readByte(MPU6050_ADDRESS, SELF_TEST_X); // X-axis self-test results
AlexQian 3:46535ec6d8b1 562 rawData[1] = readByte(MPU6050_ADDRESS, SELF_TEST_Y); // Y-axis self-test results
AlexQian 3:46535ec6d8b1 563 rawData[2] = readByte(MPU6050_ADDRESS, SELF_TEST_Z); // Z-axis self-test results
AlexQian 3:46535ec6d8b1 564 rawData[3] = readByte(MPU6050_ADDRESS, SELF_TEST_A); // Mixed-axis self-test results
AlexQian 3:46535ec6d8b1 565 // Extract the acceleration test results first
AlexQian 3:46535ec6d8b1 566 selfTest[0] = (rawData[0] >> 3) | (rawData[3] & 0x30) >> 4 ; // XA_TEST result is a five-bit unsigned integer
AlexQian 3:46535ec6d8b1 567 selfTest[1] = (rawData[1] >> 3) | (rawData[3] & 0x0C) >> 4 ; // YA_TEST result is a five-bit unsigned integer
AlexQian 3:46535ec6d8b1 568 selfTest[2] = (rawData[2] >> 3) | (rawData[3] & 0x03) >> 4 ; // ZA_TEST result is a five-bit unsigned integer
AlexQian 3:46535ec6d8b1 569 // Extract the gyration test results first
AlexQian 3:46535ec6d8b1 570 selfTest[3] = rawData[0] & 0x1F ; // XG_TEST result is a five-bit unsigned integer
AlexQian 3:46535ec6d8b1 571 selfTest[4] = rawData[1] & 0x1F ; // YG_TEST result is a five-bit unsigned integer
AlexQian 3:46535ec6d8b1 572 selfTest[5] = rawData[2] & 0x1F ; // ZG_TEST result is a five-bit unsigned integer
AlexQian 3:46535ec6d8b1 573 // Process results to allow final comparison with factory set values
AlexQian 3:46535ec6d8b1 574 factoryTrim[0] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[0] - 1.0f)/30.0f))); // FT[Xa] factory trim calculation
AlexQian 3:46535ec6d8b1 575 factoryTrim[1] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[1] - 1.0f)/30.0f))); // FT[Ya] factory trim calculation
AlexQian 3:46535ec6d8b1 576 factoryTrim[2] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[2] - 1.0f)/30.0f))); // FT[Za] factory trim calculation
AlexQian 3:46535ec6d8b1 577 factoryTrim[3] = ( 25.0f*131.0f)*(pow( 1.046f , (selfTest[3] - 1.0f) )); // FT[Xg] factory trim calculation
AlexQian 3:46535ec6d8b1 578 factoryTrim[4] = (-25.0f*131.0f)*(pow( 1.046f , (selfTest[4] - 1.0f) )); // FT[Yg] factory trim calculation
AlexQian 3:46535ec6d8b1 579 factoryTrim[5] = ( 25.0f*131.0f)*(pow( 1.046f , (selfTest[5] - 1.0f) )); // FT[Zg] factory trim calculation
AlexQian 3:46535ec6d8b1 580
AlexQian 3:46535ec6d8b1 581 // Output self-test results and factory trim calculation if desired
AlexQian 3:46535ec6d8b1 582 // Serial.println(selfTest[0]); Serial.println(selfTest[1]); Serial.println(selfTest[2]);
AlexQian 3:46535ec6d8b1 583 // Serial.println(selfTest[3]); Serial.println(selfTest[4]); Serial.println(selfTest[5]);
AlexQian 3:46535ec6d8b1 584 // Serial.println(factoryTrim[0]); Serial.println(factoryTrim[1]); Serial.println(factoryTrim[2]);
AlexQian 3:46535ec6d8b1 585 // Serial.println(factoryTrim[3]); Serial.println(factoryTrim[4]); Serial.println(factoryTrim[5]);
AlexQian 3:46535ec6d8b1 586
AlexQian 3:46535ec6d8b1 587 // Report results as a ratio of (STR - FT)/FT; the change from Factory Trim of the Self-Test Response
AlexQian 3:46535ec6d8b1 588 // To get to percent, must multiply by 100 and subtract result from 100
AlexQian 3:46535ec6d8b1 589 for (int i = 0; i < 6; i++) {
AlexQian 3:46535ec6d8b1 590 destination[i] = 100.0f + 100.0f*(selfTest[i] - factoryTrim[i])/factoryTrim[i]; // Report percent differences
AlexQian 3:46535ec6d8b1 591 }
AlexQian 3:46535ec6d8b1 592
AlexQian 3:46535ec6d8b1 593 }
AlexQian 3:46535ec6d8b1 594
AlexQian 3:46535ec6d8b1 595
AlexQian 3:46535ec6d8b1 596 // Implementation of Sebastian Madgwick's "...efficient orientation filter for... inertial/magnetic sensor arrays"
AlexQian 3:46535ec6d8b1 597 // (see http://www.x-io.co.uk/category/open-source/ for examples and more details)
AlexQian 3:46535ec6d8b1 598 // which fuses acceleration and rotation rate to produce a quaternion-based estimate of relative
AlexQian 3:46535ec6d8b1 599 // device orientation -- which can be converted to yaw, pitch, and roll. Useful for stabilizing quadcopters, etc.
AlexQian 3:46535ec6d8b1 600 // The performance of the orientation filter is at least as good as conventional Kalman-based filtering algorithms
AlexQian 3:46535ec6d8b1 601 // but is much less computationally intensive---it can be performed on a 3.3 V Pro Mini operating at 8 MHz!
AlexQian 3:46535ec6d8b1 602 void MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz)
AlexQian 3:46535ec6d8b1 603 {
AlexQian 3:46535ec6d8b1 604 float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3]; // short name local variable for readability
AlexQian 3:46535ec6d8b1 605 float norm; // vector norm
AlexQian 3:46535ec6d8b1 606 float f1, f2, f3; // objective funcyion elements
AlexQian 3:46535ec6d8b1 607 float J_11or24, J_12or23, J_13or22, J_14or21, J_32, J_33; // objective function Jacobian elements
AlexQian 3:46535ec6d8b1 608 float qDot1, qDot2, qDot3, qDot4;
AlexQian 3:46535ec6d8b1 609 float hatDot1, hatDot2, hatDot3, hatDot4;
AlexQian 3:46535ec6d8b1 610 float gerrx, gerry, gerrz, gbiasx, gbiasy, gbiasz; // gyro bias error
AlexQian 3:46535ec6d8b1 611
AlexQian 3:46535ec6d8b1 612 // Auxiliary variables to avoid repeated arithmetic
AlexQian 3:46535ec6d8b1 613 float _halfq1 = 0.5f * q1;
AlexQian 3:46535ec6d8b1 614 float _halfq2 = 0.5f * q2;
AlexQian 3:46535ec6d8b1 615 float _halfq3 = 0.5f * q3;
AlexQian 3:46535ec6d8b1 616 float _halfq4 = 0.5f * q4;
AlexQian 3:46535ec6d8b1 617 float _2q1 = 2.0f * q1;
AlexQian 3:46535ec6d8b1 618 float _2q2 = 2.0f * q2;
AlexQian 3:46535ec6d8b1 619 float _2q3 = 2.0f * q3;
AlexQian 3:46535ec6d8b1 620 float _2q4 = 2.0f * q4;
AlexQian 3:46535ec6d8b1 621 // float _2q1q3 = 2.0f * q1 * q3;
AlexQian 3:46535ec6d8b1 622 // float _2q3q4 = 2.0f * q3 * q4;
AlexQian 3:46535ec6d8b1 623
AlexQian 3:46535ec6d8b1 624 // Normalise accelerometer measurement
AlexQian 3:46535ec6d8b1 625 norm = sqrt(ax * ax + ay * ay + az * az);
AlexQian 3:46535ec6d8b1 626 if (norm == 0.0f) return; // handle NaN
AlexQian 3:46535ec6d8b1 627 norm = 1.0f/norm;
AlexQian 3:46535ec6d8b1 628 ax *= norm;
AlexQian 3:46535ec6d8b1 629 ay *= norm;
AlexQian 3:46535ec6d8b1 630 az *= norm;
AlexQian 3:46535ec6d8b1 631
AlexQian 3:46535ec6d8b1 632 // Compute the objective function and Jacobian
AlexQian 3:46535ec6d8b1 633 f1 = _2q2 * q4 - _2q1 * q3 - ax;
AlexQian 3:46535ec6d8b1 634 f2 = _2q1 * q2 + _2q3 * q4 - ay;
AlexQian 3:46535ec6d8b1 635 f3 = 1.0f - _2q2 * q2 - _2q3 * q3 - az;
AlexQian 3:46535ec6d8b1 636 J_11or24 = _2q3;
AlexQian 3:46535ec6d8b1 637 J_12or23 = _2q4;
AlexQian 3:46535ec6d8b1 638 J_13or22 = _2q1;
AlexQian 3:46535ec6d8b1 639 J_14or21 = _2q2;
AlexQian 3:46535ec6d8b1 640 J_32 = 2.0f * J_14or21;
AlexQian 3:46535ec6d8b1 641 J_33 = 2.0f * J_11or24;
AlexQian 3:46535ec6d8b1 642
AlexQian 3:46535ec6d8b1 643 // Compute the gradient (matrix multiplication)
AlexQian 3:46535ec6d8b1 644 hatDot1 = J_14or21 * f2 - J_11or24 * f1;
AlexQian 3:46535ec6d8b1 645 hatDot2 = J_12or23 * f1 + J_13or22 * f2 - J_32 * f3;
AlexQian 3:46535ec6d8b1 646 hatDot3 = J_12or23 * f2 - J_33 *f3 - J_13or22 * f1;
AlexQian 3:46535ec6d8b1 647 hatDot4 = J_14or21 * f1 + J_11or24 * f2;
AlexQian 3:46535ec6d8b1 648
AlexQian 3:46535ec6d8b1 649 // Normalize the gradient
AlexQian 3:46535ec6d8b1 650 norm = sqrt(hatDot1 * hatDot1 + hatDot2 * hatDot2 + hatDot3 * hatDot3 + hatDot4 * hatDot4);
AlexQian 3:46535ec6d8b1 651 hatDot1 /= norm;
AlexQian 3:46535ec6d8b1 652 hatDot2 /= norm;
AlexQian 3:46535ec6d8b1 653 hatDot3 /= norm;
AlexQian 3:46535ec6d8b1 654 hatDot4 /= norm;
AlexQian 3:46535ec6d8b1 655
AlexQian 3:46535ec6d8b1 656 // Compute estimated gyroscope biases
AlexQian 3:46535ec6d8b1 657 gerrx = _2q1 * hatDot2 - _2q2 * hatDot1 - _2q3 * hatDot4 + _2q4 * hatDot3;
AlexQian 3:46535ec6d8b1 658 gerry = _2q1 * hatDot3 + _2q2 * hatDot4 - _2q3 * hatDot1 - _2q4 * hatDot2;
AlexQian 3:46535ec6d8b1 659 gerrz = _2q1 * hatDot4 - _2q2 * hatDot3 + _2q3 * hatDot2 - _2q4 * hatDot1;
AlexQian 3:46535ec6d8b1 660
AlexQian 3:46535ec6d8b1 661 // Compute and remove gyroscope biases
AlexQian 3:46535ec6d8b1 662 gbiasx += gerrx * deltat * zeta;
AlexQian 3:46535ec6d8b1 663 gbiasy += gerry * deltat * zeta;
AlexQian 3:46535ec6d8b1 664 gbiasz += gerrz * deltat * zeta;
AlexQian 3:46535ec6d8b1 665 // gx -= gbiasx;
AlexQian 3:46535ec6d8b1 666 // gy -= gbiasy;
AlexQian 3:46535ec6d8b1 667 // gz -= gbiasz;
AlexQian 3:46535ec6d8b1 668
AlexQian 3:46535ec6d8b1 669 // Compute the quaternion derivative
AlexQian 3:46535ec6d8b1 670 qDot1 = -_halfq2 * gx - _halfq3 * gy - _halfq4 * gz;
AlexQian 3:46535ec6d8b1 671 qDot2 = _halfq1 * gx + _halfq3 * gz - _halfq4 * gy;
AlexQian 3:46535ec6d8b1 672 qDot3 = _halfq1 * gy - _halfq2 * gz + _halfq4 * gx;
AlexQian 3:46535ec6d8b1 673 qDot4 = _halfq1 * gz + _halfq2 * gy - _halfq3 * gx;
AlexQian 3:46535ec6d8b1 674
AlexQian 3:46535ec6d8b1 675 // Compute then integrate estimated quaternion derivative
AlexQian 3:46535ec6d8b1 676 q1 += (qDot1 -(beta * hatDot1)) * deltat;
AlexQian 3:46535ec6d8b1 677 q2 += (qDot2 -(beta * hatDot2)) * deltat;
AlexQian 3:46535ec6d8b1 678 q3 += (qDot3 -(beta * hatDot3)) * deltat;
AlexQian 3:46535ec6d8b1 679 q4 += (qDot4 -(beta * hatDot4)) * deltat;
AlexQian 3:46535ec6d8b1 680
AlexQian 3:46535ec6d8b1 681 // Normalize the quaternion
AlexQian 3:46535ec6d8b1 682 norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4); // normalise quaternion
AlexQian 3:46535ec6d8b1 683 norm = 1.0f/norm;
AlexQian 3:46535ec6d8b1 684 q[0] = q1 * norm;
AlexQian 3:46535ec6d8b1 685 q[1] = q2 * norm;
AlexQian 3:46535ec6d8b1 686 q[2] = q3 * norm;
AlexQian 3:46535ec6d8b1 687 q[3] = q4 * norm;
AlexQian 3:46535ec6d8b1 688
AlexQian 3:46535ec6d8b1 689 }
AlexQian 3:46535ec6d8b1 690 int Init()
AlexQian 3:46535ec6d8b1 691 {
AlexQian 3:46535ec6d8b1 692 i2c.frequency(400000); // use fast (400 kHz) I2C
AlexQian 3:46535ec6d8b1 693
AlexQian 3:46535ec6d8b1 694 t.start();
AlexQian 3:46535ec6d8b1 695
AlexQian 3:46535ec6d8b1 696
AlexQian 3:46535ec6d8b1 697 // Read the WHO_AM_I register, this is a good test of communication
AlexQian 3:46535ec6d8b1 698 uint8_t whoami = readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050
AlexQian 3:46535ec6d8b1 699 //pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r");
AlexQian 3:46535ec6d8b1 700
AlexQian 3:46535ec6d8b1 701 if (whoami == 0x68) // WHO_AM_I should always be 0x68
AlexQian 3:46535ec6d8b1 702 {
AlexQian 3:46535ec6d8b1 703 //pc.printf("MPU6050 is online...");
AlexQian 3:46535ec6d8b1 704 wait(1);
AlexQian 3:46535ec6d8b1 705
AlexQian 3:46535ec6d8b1 706
AlexQian 3:46535ec6d8b1 707 MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values
AlexQian 3:46535ec6d8b1 708 //pc.printf("x-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[0]); pc.printf("% of factory value \n\r");
AlexQian 3:46535ec6d8b1 709 // pc.printf("y-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[1]); pc.printf("% of factory value \n\r");
AlexQian 3:46535ec6d8b1 710 // pc.printf("z-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[2]); pc.printf("% of factory value \n\r");
AlexQian 3:46535ec6d8b1 711 // pc.printf("x-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[3]); pc.printf("% of factory value \n\r");
AlexQian 3:46535ec6d8b1 712 // pc.printf("y-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[4]); pc.printf("% of factory value \n\r");
AlexQian 3:46535ec6d8b1 713 // pc.printf("z-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[5]); pc.printf("% of factory value \n\r");
AlexQian 3:46535ec6d8b1 714 wait(1);
AlexQian 3:46535ec6d8b1 715
AlexQian 3:46535ec6d8b1 716 if(SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f)
AlexQian 3:46535ec6d8b1 717 {
AlexQian 3:46535ec6d8b1 718 resetMPU6050(); // Reset registers to default in preparation for device calibration
AlexQian 3:46535ec6d8b1 719 calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
AlexQian 3:46535ec6d8b1 720 initMPU6050(); //pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
AlexQian 3:46535ec6d8b1 721 wait(2);
AlexQian 3:46535ec6d8b1 722 }
AlexQian 3:46535ec6d8b1 723 else
AlexQian 3:46535ec6d8b1 724 {
AlexQian 3:46535ec6d8b1 725 //pc.printf("Device did not the pass self-test!\n\r");
AlexQian 3:46535ec6d8b1 726 return 1;
AlexQian 3:46535ec6d8b1 727 }
AlexQian 3:46535ec6d8b1 728 }
AlexQian 3:46535ec6d8b1 729 else
AlexQian 3:46535ec6d8b1 730 {
AlexQian 3:46535ec6d8b1 731 // pc.printf("Could not connect to MPU6050: \n\r");
AlexQian 3:46535ec6d8b1 732 // pc.printf("%#x \n", whoami);
AlexQian 3:46535ec6d8b1 733 return 1;
AlexQian 3:46535ec6d8b1 734 // while(1) ; // Loop forever if communication doesn't happen
AlexQian 3:46535ec6d8b1 735 }
AlexQian 3:46535ec6d8b1 736 return 0;
AlexQian 3:46535ec6d8b1 737 }
AlexQian 3:46535ec6d8b1 738 void receiveData(float *yaw, float *pitch , float *roll )
AlexQian 3:46535ec6d8b1 739 {
AlexQian 3:46535ec6d8b1 740 if(readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt
AlexQian 3:46535ec6d8b1 741 readAccelData(accelCount); // Read the x/y/z adc values
AlexQian 3:46535ec6d8b1 742 getAres();
AlexQian 3:46535ec6d8b1 743
AlexQian 3:46535ec6d8b1 744 // Now we'll calculate the accleration value into actual g's
AlexQian 3:46535ec6d8b1 745 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
AlexQian 3:46535ec6d8b1 746 ay = (float)accelCount[1]*aRes - accelBias[1];
AlexQian 3:46535ec6d8b1 747 az = (float)accelCount[2]*aRes - accelBias[2];
AlexQian 3:46535ec6d8b1 748
AlexQian 3:46535ec6d8b1 749 readGyroData(gyroCount); // Read the x/y/z adc values
AlexQian 3:46535ec6d8b1 750 getGres();
AlexQian 3:46535ec6d8b1 751
AlexQian 3:46535ec6d8b1 752 // Calculate the gyro value into actual degrees per second
AlexQian 3:46535ec6d8b1 753 gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set
AlexQian 3:46535ec6d8b1 754 gy = (float)gyroCount[1]*gRes; // - gyroBias[1];
AlexQian 3:46535ec6d8b1 755 gz = (float)gyroCount[2]*gRes; // - gyroBias[2];
AlexQian 3:46535ec6d8b1 756
AlexQian 3:46535ec6d8b1 757 tempCount = readTempData(); // Read the x/y/z adc values
AlexQian 3:46535ec6d8b1 758 temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade
AlexQian 3:46535ec6d8b1 759 }
AlexQian 3:46535ec6d8b1 760
AlexQian 3:46535ec6d8b1 761 Now = t.read_us();
AlexQian 3:46535ec6d8b1 762 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
AlexQian 3:46535ec6d8b1 763 lastUpdate = Now;
AlexQian 3:46535ec6d8b1 764
AlexQian 3:46535ec6d8b1 765 // sum += deltat;
AlexQian 3:46535ec6d8b1 766 // sumCount++;
AlexQian 3:46535ec6d8b1 767
AlexQian 3:46535ec6d8b1 768 if(lastUpdate - firstUpdate > 10000000.0f) {
AlexQian 3:46535ec6d8b1 769 beta = 0.04; // decrease filter gain after stabilized
AlexQian 3:46535ec6d8b1 770 zeta = 0.015; // increasey bias drift gain after stabilized
AlexQian 3:46535ec6d8b1 771 }
AlexQian 3:46535ec6d8b1 772
AlexQian 3:46535ec6d8b1 773 // Pass gyro rate as rad/s
AlexQian 3:46535ec6d8b1 774 MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f);
AlexQian 3:46535ec6d8b1 775
AlexQian 3:46535ec6d8b1 776 // Serial print and/or display at 0.5 s rate independent of data rates
AlexQian 3:46535ec6d8b1 777 delt_t = t.read_ms() - count1;
AlexQian 4:1b985e622d26 778 if (delt_t > 50) { // update LCD once per half-second independent of read rate
AlexQian 3:46535ec6d8b1 779
AlexQian 3:46535ec6d8b1 780 // pc.printf("ax = %f", 1000*ax);
AlexQian 3:46535ec6d8b1 781 // pc.printf(" ay = %f", 1000*ay);
AlexQian 3:46535ec6d8b1 782 // pc.printf(" az = %f mg\n\r", 1000*az);
AlexQian 3:46535ec6d8b1 783 //
AlexQian 3:46535ec6d8b1 784 // pc.printf("gx = %f", gx);
AlexQian 3:46535ec6d8b1 785 // pc.printf(" gy = %f", gy);
AlexQian 3:46535ec6d8b1 786 // pc.printf(" gz = %f deg/s\n\r", gz);
AlexQian 3:46535ec6d8b1 787 //
AlexQian 3:46535ec6d8b1 788 // pc.printf(" temperature = %f C\n\r", temperature);
AlexQian 3:46535ec6d8b1 789 //
AlexQian 3:46535ec6d8b1 790 // pc.printf("q0 = %f\n\r", q[0]);
AlexQian 3:46535ec6d8b1 791 // pc.printf("q1 = %f\n\r", q[1]);
AlexQian 3:46535ec6d8b1 792 // pc.printf("q2 = %f\n\r", q[2]);
AlexQian 3:46535ec6d8b1 793 // pc.printf("q3 = %f\n\r", q[3]);
AlexQian 3:46535ec6d8b1 794
AlexQian 3:46535ec6d8b1 795 *yaw = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]);
AlexQian 3:46535ec6d8b1 796 *pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
AlexQian 3:46535ec6d8b1 797 *roll = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
AlexQian 3:46535ec6d8b1 798 *pitch *= 180.0f / PI;
AlexQian 3:46535ec6d8b1 799 *yaw *= 180.0f / PI;
AlexQian 3:46535ec6d8b1 800 *roll *= 180.0f / PI;
AlexQian 3:46535ec6d8b1 801
AlexQian 3:46535ec6d8b1 802 //pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll);
AlexQian 3:46535ec6d8b1 803
AlexQian 3:46535ec6d8b1 804 count1 = t.read_ms();
AlexQian 3:46535ec6d8b1 805 }
AlexQian 3:46535ec6d8b1 806 }
AlexQian 3:46535ec6d8b1 807
AlexQian 3:46535ec6d8b1 808
AlexQian 3:46535ec6d8b1 809 };
AlexQian 3:46535ec6d8b1 810
AlexQian 3:46535ec6d8b1 811 #endif