nRF24L01, encoder, pca9685, pid

Dependencies:   mbed QEI-1 LibPN532 nRF24L01P xiugai

Committer:
brainliang
Date:
Thu Nov 07 06:31:09 2019 +0000
Revision:
7:6f8157c58241
Parent:
3:ee5e434e047e
V5

Who changed what in which revision?

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