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