MPU6050 module library
All credit to kriswiner @https://github.com/kriswiner. Just changed some code for my own purposes
MPU6050.cpp@1:ca4d8c044898, 2017-11-21 (annotated)
- Committer:
- kohlerba
- Date:
- Tue Nov 21 20:30:56 2017 +0000
- Revision:
- 1:ca4d8c044898
- Parent:
- 0:8a2cac9ba89e
MPU6050 module library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kohlerba | 0:8a2cac9ba89e | 1 | #include "mpu6050.h" |
kohlerba | 0:8a2cac9ba89e | 2 | |
kohlerba | 0:8a2cac9ba89e | 3 | int Gscale = GFS_250DPS; |
kohlerba | 0:8a2cac9ba89e | 4 | int Ascale = AFS_2G; |
kohlerba | 0:8a2cac9ba89e | 5 | |
kohlerba | 0:8a2cac9ba89e | 6 | I2C i2c(I2C_SDA, I2C_SCL); |
kohlerba | 0:8a2cac9ba89e | 7 | |
kohlerba | 0:8a2cac9ba89e | 8 | float aRes, gRes; |
kohlerba | 0:8a2cac9ba89e | 9 | |
kohlerba | 0:8a2cac9ba89e | 10 | int intPin = 12; |
kohlerba | 0:8a2cac9ba89e | 11 | |
kohlerba | 0:8a2cac9ba89e | 12 | int16_t accelCount[3]; // Stores the 16-bit signed accelerometer sensor output |
kohlerba | 0:8a2cac9ba89e | 13 | float ax, ay, az; // Stores the real accel value in g's |
kohlerba | 0:8a2cac9ba89e | 14 | int16_t gyroCount[3]; // Stores the 16-bit signed gyro sensor output |
kohlerba | 0:8a2cac9ba89e | 15 | float gx, gy, gz; // Stores the real gyro value in degrees per seconds |
kohlerba | 0:8a2cac9ba89e | 16 | float gyroBias[3] = {0, 0, 0}, accelBias[3] = {0, 0, 0}; // Bias corrections for gyro and accelerometer |
kohlerba | 0:8a2cac9ba89e | 17 | int16_t tempCount; // Stores the real internal chip temperature in degrees Celsius |
kohlerba | 0:8a2cac9ba89e | 18 | float temperature; |
kohlerba | 0:8a2cac9ba89e | 19 | float SelfTest[6]; |
kohlerba | 0:8a2cac9ba89e | 20 | |
kohlerba | 0:8a2cac9ba89e | 21 | int delt_t = 0; // used to control display output rate |
kohlerba | 0:8a2cac9ba89e | 22 | int count = 0; // used to control display output rate |
kohlerba | 0:8a2cac9ba89e | 23 | |
kohlerba | 0:8a2cac9ba89e | 24 | // parameters for 6 DoF sensor fusion calculations |
kohlerba | 0:8a2cac9ba89e | 25 | //float PI = 3.14159265358979323846f; |
kohlerba | 0:8a2cac9ba89e | 26 | 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 |
kohlerba | 0:8a2cac9ba89e | 27 | float beta = sqrt(3.0f / 4.0f) * GyroMeasError; // compute beta |
kohlerba | 0:8a2cac9ba89e | 28 | float GyroMeasDrift = PI * (1.0f / 180.0f); // gyroscope measurement drift in rad/s/s (start at 0.0 deg/s/s) |
kohlerba | 0:8a2cac9ba89e | 29 | 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 |
kohlerba | 0:8a2cac9ba89e | 30 | float pitch, yaw, roll; |
kohlerba | 0:8a2cac9ba89e | 31 | float deltat = 0.0f; // integration interval for both filter schemes |
kohlerba | 0:8a2cac9ba89e | 32 | int lastUpdate = 0, firstUpdate = 0, Now = 0; // used to calculate integration interval // used to calculate integration interval |
kohlerba | 0:8a2cac9ba89e | 33 | float q[4] = {1.0f, 0.0f, 0.0f, 0.0f}; // vector to hold quaternion |
kohlerba | 0:8a2cac9ba89e | 34 | |
kohlerba | 0:8a2cac9ba89e | 35 | void mpu6050::writeByte(uint8_t address, uint8_t subAddress, uint8_t data){ |
kohlerba | 0:8a2cac9ba89e | 36 | char data_write[2]; |
kohlerba | 0:8a2cac9ba89e | 37 | data_write[0] = subAddress; |
kohlerba | 0:8a2cac9ba89e | 38 | data_write[1] = data; |
kohlerba | 0:8a2cac9ba89e | 39 | i2c.write(address, data_write, 2, 0); |
kohlerba | 0:8a2cac9ba89e | 40 | } |
kohlerba | 0:8a2cac9ba89e | 41 | |
kohlerba | 0:8a2cac9ba89e | 42 | char mpu6050::readByte(uint8_t address, uint8_t subAddress){ |
kohlerba | 0:8a2cac9ba89e | 43 | char data[1]; // `data` will store the register data |
kohlerba | 0:8a2cac9ba89e | 44 | char data_write[1]; |
kohlerba | 0:8a2cac9ba89e | 45 | data_write[0] = subAddress; |
kohlerba | 0:8a2cac9ba89e | 46 | i2c.write(address, data_write, 1, 1); // no stop |
kohlerba | 0:8a2cac9ba89e | 47 | i2c.read(address, data, 1, 0); |
kohlerba | 0:8a2cac9ba89e | 48 | return data[0]; |
kohlerba | 0:8a2cac9ba89e | 49 | } |
kohlerba | 0:8a2cac9ba89e | 50 | |
kohlerba | 0:8a2cac9ba89e | 51 | void mpu6050::readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest){ |
kohlerba | 0:8a2cac9ba89e | 52 | char data[14]; |
kohlerba | 0:8a2cac9ba89e | 53 | char data_write[1]; |
kohlerba | 0:8a2cac9ba89e | 54 | data_write[0] = subAddress; |
kohlerba | 0:8a2cac9ba89e | 55 | i2c.write(address, data_write, 1, 1); // no stop |
kohlerba | 0:8a2cac9ba89e | 56 | i2c.read(address, data, count, 0); |
kohlerba | 0:8a2cac9ba89e | 57 | for(int ii = 0; ii < count; ii++) { |
kohlerba | 0:8a2cac9ba89e | 58 | dest[ii] = data[ii]; |
kohlerba | 0:8a2cac9ba89e | 59 | } |
kohlerba | 0:8a2cac9ba89e | 60 | } |
kohlerba | 0:8a2cac9ba89e | 61 | |
kohlerba | 0:8a2cac9ba89e | 62 | void mpu6050::getGres() { |
kohlerba | 0:8a2cac9ba89e | 63 | switch (Gscale) |
kohlerba | 0:8a2cac9ba89e | 64 | { |
kohlerba | 0:8a2cac9ba89e | 65 | // Possible gyro scales (and their register bit settings) are: |
kohlerba | 0:8a2cac9ba89e | 66 | // 250 DPS (00), 500 DPS (01), 1000 DPS (10), and 2000 DPS (11). |
kohlerba | 0:8a2cac9ba89e | 67 | // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value: |
kohlerba | 0:8a2cac9ba89e | 68 | case GFS_250DPS: |
kohlerba | 0:8a2cac9ba89e | 69 | gRes = 250.0/32768.0; |
kohlerba | 0:8a2cac9ba89e | 70 | break; |
kohlerba | 0:8a2cac9ba89e | 71 | case GFS_500DPS: |
kohlerba | 0:8a2cac9ba89e | 72 | gRes = 500.0/32768.0; |
kohlerba | 0:8a2cac9ba89e | 73 | break; |
kohlerba | 0:8a2cac9ba89e | 74 | case GFS_1000DPS: |
kohlerba | 0:8a2cac9ba89e | 75 | gRes = 1000.0/32768.0; |
kohlerba | 0:8a2cac9ba89e | 76 | break; |
kohlerba | 0:8a2cac9ba89e | 77 | case GFS_2000DPS: |
kohlerba | 0:8a2cac9ba89e | 78 | gRes = 2000.0/32768.0; |
kohlerba | 0:8a2cac9ba89e | 79 | break; |
kohlerba | 0:8a2cac9ba89e | 80 | } |
kohlerba | 0:8a2cac9ba89e | 81 | } |
kohlerba | 0:8a2cac9ba89e | 82 | |
kohlerba | 0:8a2cac9ba89e | 83 | void mpu6050::getAres() { |
kohlerba | 0:8a2cac9ba89e | 84 | switch (Ascale) |
kohlerba | 0:8a2cac9ba89e | 85 | { |
kohlerba | 0:8a2cac9ba89e | 86 | // Possible accelerometer scales (and their register bit settings) are: |
kohlerba | 0:8a2cac9ba89e | 87 | // 2 Gs (00), 4 Gs (01), 8 Gs (10), and 16 Gs (11). |
kohlerba | 0:8a2cac9ba89e | 88 | // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value: |
kohlerba | 0:8a2cac9ba89e | 89 | case AFS_2G: |
kohlerba | 0:8a2cac9ba89e | 90 | aRes = 2.0/32768.0; |
kohlerba | 0:8a2cac9ba89e | 91 | break; |
kohlerba | 0:8a2cac9ba89e | 92 | case AFS_4G: |
kohlerba | 0:8a2cac9ba89e | 93 | aRes = 4.0/32768.0; |
kohlerba | 0:8a2cac9ba89e | 94 | break; |
kohlerba | 0:8a2cac9ba89e | 95 | case AFS_8G: |
kohlerba | 0:8a2cac9ba89e | 96 | aRes = 8.0/32768.0; |
kohlerba | 0:8a2cac9ba89e | 97 | break; |
kohlerba | 0:8a2cac9ba89e | 98 | case AFS_16G: |
kohlerba | 0:8a2cac9ba89e | 99 | aRes = 16.0/32768.0; |
kohlerba | 0:8a2cac9ba89e | 100 | break; |
kohlerba | 0:8a2cac9ba89e | 101 | } |
kohlerba | 0:8a2cac9ba89e | 102 | } |
kohlerba | 0:8a2cac9ba89e | 103 | |
kohlerba | 0:8a2cac9ba89e | 104 | void mpu6050::readAccelData(int16_t * destination) |
kohlerba | 0:8a2cac9ba89e | 105 | { |
kohlerba | 0:8a2cac9ba89e | 106 | uint8_t rawData[6]; // x/y/z accel register data stored here |
kohlerba | 0:8a2cac9ba89e | 107 | readBytes(MPU6050_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]); // Read the six raw data registers into data array |
kohlerba | 0:8a2cac9ba89e | 108 | destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value |
kohlerba | 0:8a2cac9ba89e | 109 | destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ; |
kohlerba | 0:8a2cac9ba89e | 110 | destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ; |
kohlerba | 0:8a2cac9ba89e | 111 | } |
kohlerba | 0:8a2cac9ba89e | 112 | |
kohlerba | 0:8a2cac9ba89e | 113 | void mpu6050::readGyroData(int16_t * destination) |
kohlerba | 0:8a2cac9ba89e | 114 | { |
kohlerba | 0:8a2cac9ba89e | 115 | uint8_t rawData[6]; // x/y/z gyro register data stored here |
kohlerba | 0:8a2cac9ba89e | 116 | readBytes(MPU6050_ADDRESS, GYRO_XOUT_H, 6, &rawData[0]); // Read the six raw data registers sequentially into data array |
kohlerba | 0:8a2cac9ba89e | 117 | destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value |
kohlerba | 0:8a2cac9ba89e | 118 | destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ; |
kohlerba | 0:8a2cac9ba89e | 119 | destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ; |
kohlerba | 0:8a2cac9ba89e | 120 | } |
kohlerba | 0:8a2cac9ba89e | 121 | |
kohlerba | 0:8a2cac9ba89e | 122 | int16_t mpu6050::readTempData() |
kohlerba | 0:8a2cac9ba89e | 123 | { |
kohlerba | 0:8a2cac9ba89e | 124 | uint8_t rawData[2]; // x/y/z gyro register data stored here |
kohlerba | 0:8a2cac9ba89e | 125 | readBytes(MPU6050_ADDRESS, TEMP_OUT_H, 2, &rawData[0]); // Read the two raw data registers sequentially into data array |
kohlerba | 0:8a2cac9ba89e | 126 | return (int16_t)(((int16_t)rawData[0]) << 8 | rawData[1]) ; // Turn the MSB and LSB into a 16-bit value |
kohlerba | 0:8a2cac9ba89e | 127 | } |
kohlerba | 0:8a2cac9ba89e | 128 | |
kohlerba | 0:8a2cac9ba89e | 129 | // Configure the motion detection control for low power accelerometer mode |
kohlerba | 0:8a2cac9ba89e | 130 | void mpu6050::lowPowerAccelOnly() |
kohlerba | 0:8a2cac9ba89e | 131 | { |
kohlerba | 0:8a2cac9ba89e | 132 | |
kohlerba | 0:8a2cac9ba89e | 133 | // The sensor has a high-pass filter necessary to invoke to allow the sensor motion detection algorithms work properly |
kohlerba | 0:8a2cac9ba89e | 134 | // Motion detection occurs on free-fall (acceleration below a threshold for some time for all axes), motion (acceleration |
kohlerba | 0:8a2cac9ba89e | 135 | // above a threshold for some time on at least one axis), and zero-motion toggle (acceleration on each axis less than a |
kohlerba | 0:8a2cac9ba89e | 136 | // threshold for some time sets this flag, motion above the threshold turns it off). The high-pass filter takes gravity out |
kohlerba | 0:8a2cac9ba89e | 137 | // consideration for these threshold evaluations; otherwise, the flags would be set all the time! |
kohlerba | 0:8a2cac9ba89e | 138 | |
kohlerba | 0:8a2cac9ba89e | 139 | uint8_t c = readByte(MPU6050_ADDRESS, PWR_MGMT_1); |
kohlerba | 0:8a2cac9ba89e | 140 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c & ~0x30); // Clear sleep and cycle bits [5:6] |
kohlerba | 0:8a2cac9ba89e | 141 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c | 0x30); // Set sleep and cycle bits [5:6] to zero to make sure accelerometer is running |
kohlerba | 0:8a2cac9ba89e | 142 | |
kohlerba | 0:8a2cac9ba89e | 143 | c = readByte(MPU6050_ADDRESS, PWR_MGMT_2); |
kohlerba | 0:8a2cac9ba89e | 144 | writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c & ~0x38); // Clear standby XA, YA, and ZA bits [3:5] |
kohlerba | 0:8a2cac9ba89e | 145 | writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c | 0x00); // Set XA, YA, and ZA bits [3:5] to zero to make sure accelerometer is running |
kohlerba | 0:8a2cac9ba89e | 146 | |
kohlerba | 0:8a2cac9ba89e | 147 | c = readByte(MPU6050_ADDRESS, ACCEL_CONFIG); |
kohlerba | 0:8a2cac9ba89e | 148 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0x07); // Clear high-pass filter bits [2:0] |
kohlerba | 0:8a2cac9ba89e | 149 | // 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 |
kohlerba | 0:8a2cac9ba89e | 150 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c | 0x00); // Set ACCEL_HPF to 0; reset mode disbaling high-pass filter |
kohlerba | 0:8a2cac9ba89e | 151 | |
kohlerba | 0:8a2cac9ba89e | 152 | c = readByte(MPU6050_ADDRESS, CONFIG); |
kohlerba | 0:8a2cac9ba89e | 153 | writeByte(MPU6050_ADDRESS, CONFIG, c & ~0x07); // Clear low-pass filter bits [2:0] |
kohlerba | 0:8a2cac9ba89e | 154 | writeByte(MPU6050_ADDRESS, CONFIG, c | 0x00); // Set DLPD_CFG to 0; 260 Hz bandwidth, 1 kHz rate |
kohlerba | 0:8a2cac9ba89e | 155 | |
kohlerba | 0:8a2cac9ba89e | 156 | c = readByte(MPU6050_ADDRESS, INT_ENABLE); |
kohlerba | 0:8a2cac9ba89e | 157 | writeByte(MPU6050_ADDRESS, INT_ENABLE, c & ~0xFF); // Clear all interrupts |
kohlerba | 0:8a2cac9ba89e | 158 | writeByte(MPU6050_ADDRESS, INT_ENABLE, 0x40); // Enable motion threshold (bits 5) interrupt only |
kohlerba | 0:8a2cac9ba89e | 159 | |
kohlerba | 0:8a2cac9ba89e | 160 | // Motion detection interrupt requires the absolute value of any axis to lie above the detection threshold |
kohlerba | 0:8a2cac9ba89e | 161 | // for at least the counter duration |
kohlerba | 0:8a2cac9ba89e | 162 | writeByte(MPU6050_ADDRESS, MOT_THR, 0x80); // Set motion detection to 0.256 g; LSB = 2 mg |
kohlerba | 0:8a2cac9ba89e | 163 | writeByte(MPU6050_ADDRESS, MOT_DUR, 0x01); // Set motion detect duration to 1 ms; LSB is 1 ms @ 1 kHz rate |
kohlerba | 0:8a2cac9ba89e | 164 | |
kohlerba | 0:8a2cac9ba89e | 165 | wait(0.1); // Add delay for accumulation of samples |
kohlerba | 0:8a2cac9ba89e | 166 | |
kohlerba | 0:8a2cac9ba89e | 167 | c = readByte(MPU6050_ADDRESS, ACCEL_CONFIG); |
kohlerba | 0:8a2cac9ba89e | 168 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0x07); // Clear high-pass filter bits [2:0] |
kohlerba | 0:8a2cac9ba89e | 169 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c | 0x07); // Set ACCEL_HPF to 7; hold the initial accleration value as a referance |
kohlerba | 0:8a2cac9ba89e | 170 | |
kohlerba | 0:8a2cac9ba89e | 171 | c = readByte(MPU6050_ADDRESS, PWR_MGMT_2); |
kohlerba | 0:8a2cac9ba89e | 172 | writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c & ~0xC7); // Clear standby XA, YA, and ZA bits [3:5] and LP_WAKE_CTRL bits [6:7] |
kohlerba | 0:8a2cac9ba89e | 173 | writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c | 0x47); // Set wakeup frequency to 5 Hz, and disable XG, YG, and ZG gyros (bits [0:2]) |
kohlerba | 0:8a2cac9ba89e | 174 | |
kohlerba | 0:8a2cac9ba89e | 175 | c = readByte(MPU6050_ADDRESS, PWR_MGMT_1); |
kohlerba | 0:8a2cac9ba89e | 176 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c & ~0x20); // Clear sleep and cycle bit 5 |
kohlerba | 0:8a2cac9ba89e | 177 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c | 0x20); // Set cycle bit 5 to begin low power accelerometer motion interrupts |
kohlerba | 0:8a2cac9ba89e | 178 | |
kohlerba | 0:8a2cac9ba89e | 179 | } |
kohlerba | 0:8a2cac9ba89e | 180 | |
kohlerba | 0:8a2cac9ba89e | 181 | |
kohlerba | 0:8a2cac9ba89e | 182 | void mpu6050::reset() { |
kohlerba | 0:8a2cac9ba89e | 183 | // reset device |
kohlerba | 0:8a2cac9ba89e | 184 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device |
kohlerba | 0:8a2cac9ba89e | 185 | wait(0.1); |
kohlerba | 0:8a2cac9ba89e | 186 | } |
kohlerba | 0:8a2cac9ba89e | 187 | |
kohlerba | 0:8a2cac9ba89e | 188 | |
kohlerba | 0:8a2cac9ba89e | 189 | void mpu6050::init() |
kohlerba | 0:8a2cac9ba89e | 190 | { |
kohlerba | 0:8a2cac9ba89e | 191 | // Initialize MPU6050 device |
kohlerba | 0:8a2cac9ba89e | 192 | // wake up device |
kohlerba | 0:8a2cac9ba89e | 193 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x00); // Clear sleep mode bit (6), enable all sensors |
kohlerba | 0:8a2cac9ba89e | 194 | wait(0.1); // Delay 100 ms for PLL to get established on x-axis gyro; should check for PLL ready interrupt |
kohlerba | 0:8a2cac9ba89e | 195 | |
kohlerba | 0:8a2cac9ba89e | 196 | // get stable time source |
kohlerba | 0:8a2cac9ba89e | 197 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x01); // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001 |
kohlerba | 0:8a2cac9ba89e | 198 | |
kohlerba | 0:8a2cac9ba89e | 199 | // Configure Gyro and Accelerometer |
kohlerba | 0:8a2cac9ba89e | 200 | // Disable FSYNC and set accelerometer and gyro bandwidth to 44 and 42 Hz, respectively; |
kohlerba | 0:8a2cac9ba89e | 201 | // DLPF_CFG = bits 2:0 = 010; this sets the sample rate at 1 kHz for both |
kohlerba | 0:8a2cac9ba89e | 202 | // Maximum delay is 4.9 ms which is just over a 200 Hz maximum rate |
kohlerba | 0:8a2cac9ba89e | 203 | writeByte(MPU6050_ADDRESS, CONFIG, 0x03); |
kohlerba | 0:8a2cac9ba89e | 204 | |
kohlerba | 0:8a2cac9ba89e | 205 | // Set sample rate = gyroscope output rate/(1 + SMPLRT_DIV) |
kohlerba | 0:8a2cac9ba89e | 206 | writeByte(MPU6050_ADDRESS, SMPLRT_DIV, 0x04); // Use a 200 Hz rate; the same rate set in CONFIG above |
kohlerba | 0:8a2cac9ba89e | 207 | |
kohlerba | 0:8a2cac9ba89e | 208 | // Set gyroscope full scale range |
kohlerba | 0:8a2cac9ba89e | 209 | // Range selects FS_SEL and AFS_SEL are 0 - 3, so 2-bit values are left-shifted into positions 4:3 |
kohlerba | 0:8a2cac9ba89e | 210 | uint8_t c = readByte(MPU6050_ADDRESS, GYRO_CONFIG); |
kohlerba | 0:8a2cac9ba89e | 211 | writeByte(MPU6050_ADDRESS, GYRO_CONFIG, c & ~0xE0); // Clear self-test bits [7:5] |
kohlerba | 0:8a2cac9ba89e | 212 | writeByte(MPU6050_ADDRESS, GYRO_CONFIG, c & ~0x18); // Clear AFS bits [4:3] |
kohlerba | 0:8a2cac9ba89e | 213 | writeByte(MPU6050_ADDRESS, GYRO_CONFIG, c | Gscale << 3); // Set full scale range for the gyro |
kohlerba | 0:8a2cac9ba89e | 214 | |
kohlerba | 0:8a2cac9ba89e | 215 | // Set accelerometer configuration |
kohlerba | 0:8a2cac9ba89e | 216 | c = readByte(MPU6050_ADDRESS, ACCEL_CONFIG); |
kohlerba | 0:8a2cac9ba89e | 217 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0xE0); // Clear self-test bits [7:5] |
kohlerba | 0:8a2cac9ba89e | 218 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0x18); // Clear AFS bits [4:3] |
kohlerba | 0:8a2cac9ba89e | 219 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c | Ascale << 3); // Set full scale range for the accelerometer |
kohlerba | 0:8a2cac9ba89e | 220 | |
kohlerba | 0:8a2cac9ba89e | 221 | // Configure Interrupts and Bypass Enable |
kohlerba | 0:8a2cac9ba89e | 222 | // Set interrupt pin active high, push-pull, and clear on read of INT_STATUS, enable I2C_BYPASS_EN so additional chips |
kohlerba | 0:8a2cac9ba89e | 223 | // can join the I2C bus and all can be controlled by the Arduino as master |
kohlerba | 0:8a2cac9ba89e | 224 | writeByte(MPU6050_ADDRESS, INT_PIN_CFG, 0x22); |
kohlerba | 0:8a2cac9ba89e | 225 | writeByte(MPU6050_ADDRESS, INT_ENABLE, 0x01); // Enable data ready (bit 0) interrupt |
kohlerba | 0:8a2cac9ba89e | 226 | } |
kohlerba | 0:8a2cac9ba89e | 227 | |
kohlerba | 0:8a2cac9ba89e | 228 | // Function which accumulates gyro and accelerometer data after device initialization. It calculates the average |
kohlerba | 0:8a2cac9ba89e | 229 | // of the at-rest readings and then loads the resulting offsets into accelerometer and gyro bias registers. |
kohlerba | 0:8a2cac9ba89e | 230 | void mpu6050::calibrate(float * dest1, float * dest2) |
kohlerba | 0:8a2cac9ba89e | 231 | { |
kohlerba | 0:8a2cac9ba89e | 232 | uint8_t data[12]; // data array to hold accelerometer and gyro x, y, z, data |
kohlerba | 0:8a2cac9ba89e | 233 | uint16_t ii, packet_count, fifo_count; |
kohlerba | 0:8a2cac9ba89e | 234 | int32_t gyro_bias[3] = {0, 0, 0}, accel_bias[3] = {0, 0, 0}; |
kohlerba | 0:8a2cac9ba89e | 235 | |
kohlerba | 0:8a2cac9ba89e | 236 | // reset device, reset all registers, clear gyro and accelerometer bias registers |
kohlerba | 0:8a2cac9ba89e | 237 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device |
kohlerba | 0:8a2cac9ba89e | 238 | wait(0.1); |
kohlerba | 0:8a2cac9ba89e | 239 | |
kohlerba | 0:8a2cac9ba89e | 240 | // get stable time source |
kohlerba | 0:8a2cac9ba89e | 241 | // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001 |
kohlerba | 0:8a2cac9ba89e | 242 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x01); |
kohlerba | 0:8a2cac9ba89e | 243 | writeByte(MPU6050_ADDRESS, PWR_MGMT_2, 0x00); |
kohlerba | 0:8a2cac9ba89e | 244 | wait(0.2); |
kohlerba | 0:8a2cac9ba89e | 245 | |
kohlerba | 0:8a2cac9ba89e | 246 | // Configure device for bias calculation |
kohlerba | 0:8a2cac9ba89e | 247 | writeByte(MPU6050_ADDRESS, INT_ENABLE, 0x00); // Disable all interrupts |
kohlerba | 0:8a2cac9ba89e | 248 | writeByte(MPU6050_ADDRESS, FIFO_EN, 0x00); // Disable FIFO |
kohlerba | 0:8a2cac9ba89e | 249 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x00); // Turn on internal clock source |
kohlerba | 0:8a2cac9ba89e | 250 | writeByte(MPU6050_ADDRESS, I2C_MST_CTRL, 0x00); // Disable I2C master |
kohlerba | 0:8a2cac9ba89e | 251 | writeByte(MPU6050_ADDRESS, USER_CTRL, 0x00); // Disable FIFO and I2C master modes |
kohlerba | 0:8a2cac9ba89e | 252 | writeByte(MPU6050_ADDRESS, USER_CTRL, 0x0C); // Reset FIFO and DMP |
kohlerba | 0:8a2cac9ba89e | 253 | wait(0.015); |
kohlerba | 0:8a2cac9ba89e | 254 | |
kohlerba | 0:8a2cac9ba89e | 255 | // Configure MPU6050 gyro and accelerometer for bias calculation |
kohlerba | 0:8a2cac9ba89e | 256 | writeByte(MPU6050_ADDRESS, CONFIG, 0x01); // Set low-pass filter to 188 Hz |
kohlerba | 0:8a2cac9ba89e | 257 | writeByte(MPU6050_ADDRESS, SMPLRT_DIV, 0x00); // Set sample rate to 1 kHz |
kohlerba | 0:8a2cac9ba89e | 258 | writeByte(MPU6050_ADDRESS, GYRO_CONFIG, 0x00); // Set gyro full-scale to 250 degrees per second, maximum sensitivity |
kohlerba | 0:8a2cac9ba89e | 259 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, 0x00); // Set accelerometer full-scale to 2 g, maximum sensitivity |
kohlerba | 0:8a2cac9ba89e | 260 | |
kohlerba | 0:8a2cac9ba89e | 261 | uint16_t gyrosensitivity = 131; // = 131 LSB/degrees/sec |
kohlerba | 0:8a2cac9ba89e | 262 | uint16_t accelsensitivity = 16384; // = 16384 LSB/g |
kohlerba | 0:8a2cac9ba89e | 263 | |
kohlerba | 0:8a2cac9ba89e | 264 | // Configure FIFO to capture accelerometer and gyro data for bias calculation |
kohlerba | 0:8a2cac9ba89e | 265 | writeByte(MPU6050_ADDRESS, USER_CTRL, 0x40); // Enable FIFO |
kohlerba | 0:8a2cac9ba89e | 266 | writeByte(MPU6050_ADDRESS, FIFO_EN, 0x78); // Enable gyro and accelerometer sensors for FIFO (max size 1024 bytes in MPU-6050) |
kohlerba | 0:8a2cac9ba89e | 267 | wait(0.08); // accumulate 80 samples in 80 milliseconds = 960 bytes |
kohlerba | 0:8a2cac9ba89e | 268 | |
kohlerba | 0:8a2cac9ba89e | 269 | // At end of sample accumulation, turn off FIFO sensor read |
kohlerba | 0:8a2cac9ba89e | 270 | writeByte(MPU6050_ADDRESS, FIFO_EN, 0x00); // Disable gyro and accelerometer sensors for FIFO |
kohlerba | 0:8a2cac9ba89e | 271 | readBytes(MPU6050_ADDRESS, FIFO_COUNTH, 2, &data[0]); // read FIFO sample count |
kohlerba | 0:8a2cac9ba89e | 272 | fifo_count = ((uint16_t)data[0] << 8) | data[1]; |
kohlerba | 0:8a2cac9ba89e | 273 | packet_count = fifo_count/12;// How many sets of full gyro and accelerometer data for averaging |
kohlerba | 0:8a2cac9ba89e | 274 | |
kohlerba | 0:8a2cac9ba89e | 275 | for (ii = 0; ii < packet_count; ii++) { |
kohlerba | 0:8a2cac9ba89e | 276 | int16_t accel_temp[3] = {0, 0, 0}, gyro_temp[3] = {0, 0, 0}; |
kohlerba | 0:8a2cac9ba89e | 277 | readBytes(MPU6050_ADDRESS, FIFO_R_W, 12, &data[0]); // read data for averaging |
kohlerba | 0:8a2cac9ba89e | 278 | accel_temp[0] = (int16_t) (((int16_t)data[0] << 8) | data[1] ) ; // Form signed 16-bit integer for each sample in FIFO |
kohlerba | 0:8a2cac9ba89e | 279 | accel_temp[1] = (int16_t) (((int16_t)data[2] << 8) | data[3] ) ; |
kohlerba | 0:8a2cac9ba89e | 280 | accel_temp[2] = (int16_t) (((int16_t)data[4] << 8) | data[5] ) ; |
kohlerba | 0:8a2cac9ba89e | 281 | gyro_temp[0] = (int16_t) (((int16_t)data[6] << 8) | data[7] ) ; |
kohlerba | 0:8a2cac9ba89e | 282 | gyro_temp[1] = (int16_t) (((int16_t)data[8] << 8) | data[9] ) ; |
kohlerba | 0:8a2cac9ba89e | 283 | gyro_temp[2] = (int16_t) (((int16_t)data[10] << 8) | data[11]) ; |
kohlerba | 0:8a2cac9ba89e | 284 | |
kohlerba | 0:8a2cac9ba89e | 285 | accel_bias[0] += (int32_t) accel_temp[0]; // Sum individual signed 16-bit biases to get accumulated signed 32-bit biases |
kohlerba | 0:8a2cac9ba89e | 286 | accel_bias[1] += (int32_t) accel_temp[1]; |
kohlerba | 0:8a2cac9ba89e | 287 | accel_bias[2] += (int32_t) accel_temp[2]; |
kohlerba | 0:8a2cac9ba89e | 288 | gyro_bias[0] += (int32_t) gyro_temp[0]; |
kohlerba | 0:8a2cac9ba89e | 289 | gyro_bias[1] += (int32_t) gyro_temp[1]; |
kohlerba | 0:8a2cac9ba89e | 290 | gyro_bias[2] += (int32_t) gyro_temp[2]; |
kohlerba | 0:8a2cac9ba89e | 291 | |
kohlerba | 0:8a2cac9ba89e | 292 | } |
kohlerba | 0:8a2cac9ba89e | 293 | accel_bias[0] /= (int32_t) packet_count; // Normalize sums to get average count biases |
kohlerba | 0:8a2cac9ba89e | 294 | accel_bias[1] /= (int32_t) packet_count; |
kohlerba | 0:8a2cac9ba89e | 295 | accel_bias[2] /= (int32_t) packet_count; |
kohlerba | 0:8a2cac9ba89e | 296 | gyro_bias[0] /= (int32_t) packet_count; |
kohlerba | 0:8a2cac9ba89e | 297 | gyro_bias[1] /= (int32_t) packet_count; |
kohlerba | 0:8a2cac9ba89e | 298 | gyro_bias[2] /= (int32_t) packet_count; |
kohlerba | 0:8a2cac9ba89e | 299 | |
kohlerba | 0:8a2cac9ba89e | 300 | if(accel_bias[2] > 0L) {accel_bias[2] -= (int32_t) accelsensitivity;} // Remove gravity from the z-axis accelerometer bias calculation |
kohlerba | 0:8a2cac9ba89e | 301 | else {accel_bias[2] += (int32_t) accelsensitivity;} |
kohlerba | 0:8a2cac9ba89e | 302 | |
kohlerba | 0:8a2cac9ba89e | 303 | // Construct the gyro biases for push to the hardware gyro bias registers, which are reset to zero upon device startup |
kohlerba | 0:8a2cac9ba89e | 304 | 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 |
kohlerba | 0:8a2cac9ba89e | 305 | data[1] = (-gyro_bias[0]/4) & 0xFF; // Biases are additive, so change sign on calculated average gyro biases |
kohlerba | 0:8a2cac9ba89e | 306 | data[2] = (-gyro_bias[1]/4 >> 8) & 0xFF; |
kohlerba | 0:8a2cac9ba89e | 307 | data[3] = (-gyro_bias[1]/4) & 0xFF; |
kohlerba | 0:8a2cac9ba89e | 308 | data[4] = (-gyro_bias[2]/4 >> 8) & 0xFF; |
kohlerba | 0:8a2cac9ba89e | 309 | data[5] = (-gyro_bias[2]/4) & 0xFF; |
kohlerba | 0:8a2cac9ba89e | 310 | |
kohlerba | 0:8a2cac9ba89e | 311 | // Push gyro biases to hardware registers |
kohlerba | 0:8a2cac9ba89e | 312 | writeByte(MPU6050_ADDRESS, XG_OFFS_USRH, data[0]); |
kohlerba | 0:8a2cac9ba89e | 313 | writeByte(MPU6050_ADDRESS, XG_OFFS_USRL, data[1]); |
kohlerba | 0:8a2cac9ba89e | 314 | writeByte(MPU6050_ADDRESS, YG_OFFS_USRH, data[2]); |
kohlerba | 0:8a2cac9ba89e | 315 | writeByte(MPU6050_ADDRESS, YG_OFFS_USRL, data[3]); |
kohlerba | 0:8a2cac9ba89e | 316 | writeByte(MPU6050_ADDRESS, ZG_OFFS_USRH, data[4]); |
kohlerba | 0:8a2cac9ba89e | 317 | writeByte(MPU6050_ADDRESS, ZG_OFFS_USRL, data[5]); |
kohlerba | 0:8a2cac9ba89e | 318 | |
kohlerba | 0:8a2cac9ba89e | 319 | dest1[0] = (float) gyro_bias[0]/(float) gyrosensitivity; // construct gyro bias in deg/s for later manual subtraction |
kohlerba | 0:8a2cac9ba89e | 320 | dest1[1] = (float) gyro_bias[1]/(float) gyrosensitivity; |
kohlerba | 0:8a2cac9ba89e | 321 | dest1[2] = (float) gyro_bias[2]/(float) gyrosensitivity; |
kohlerba | 0:8a2cac9ba89e | 322 | |
kohlerba | 0:8a2cac9ba89e | 323 | // Construct the accelerometer biases for push to the hardware accelerometer bias registers. These registers contain |
kohlerba | 0:8a2cac9ba89e | 324 | // factory trim values which must be added to the calculated accelerometer biases; on boot up these registers will hold |
kohlerba | 0:8a2cac9ba89e | 325 | // non-zero values. In addition, bit 0 of the lower byte must be preserved since it is used for temperature |
kohlerba | 0:8a2cac9ba89e | 326 | // compensation calculations. Accelerometer bias registers expect bias input as 2048 LSB per g, so that |
kohlerba | 0:8a2cac9ba89e | 327 | // the accelerometer biases calculated above must be divided by 8. |
kohlerba | 0:8a2cac9ba89e | 328 | |
kohlerba | 0:8a2cac9ba89e | 329 | int32_t accel_bias_reg[3] = {0, 0, 0}; // A place to hold the factory accelerometer trim biases |
kohlerba | 0:8a2cac9ba89e | 330 | readBytes(MPU6050_ADDRESS, XA_OFFSET_H, 2, &data[0]); // Read factory accelerometer trim values |
kohlerba | 0:8a2cac9ba89e | 331 | accel_bias_reg[0] = (int16_t) ((int16_t)data[0] << 8) | data[1]; |
kohlerba | 0:8a2cac9ba89e | 332 | readBytes(MPU6050_ADDRESS, YA_OFFSET_H, 2, &data[0]); |
kohlerba | 0:8a2cac9ba89e | 333 | accel_bias_reg[1] = (int16_t) ((int16_t)data[0] << 8) | data[1]; |
kohlerba | 0:8a2cac9ba89e | 334 | readBytes(MPU6050_ADDRESS, ZA_OFFSET_H, 2, &data[0]); |
kohlerba | 0:8a2cac9ba89e | 335 | accel_bias_reg[2] = (int16_t) ((int16_t)data[0] << 8) | data[1]; |
kohlerba | 0:8a2cac9ba89e | 336 | |
kohlerba | 0:8a2cac9ba89e | 337 | uint32_t mask = 1uL; // Define mask for temperature compensation bit 0 of lower byte of accelerometer bias registers |
kohlerba | 0:8a2cac9ba89e | 338 | uint8_t mask_bit[3] = {0, 0, 0}; // Define array to hold mask bit for each accelerometer bias axis |
kohlerba | 0:8a2cac9ba89e | 339 | |
kohlerba | 0:8a2cac9ba89e | 340 | for(ii = 0; ii < 3; ii++) { |
kohlerba | 0:8a2cac9ba89e | 341 | if(accel_bias_reg[ii] & mask) mask_bit[ii] = 0x01; // If temperature compensation bit is set, record that fact in mask_bit |
kohlerba | 0:8a2cac9ba89e | 342 | } |
kohlerba | 0:8a2cac9ba89e | 343 | |
kohlerba | 0:8a2cac9ba89e | 344 | // Construct total accelerometer bias, including calculated average accelerometer bias from above |
kohlerba | 0:8a2cac9ba89e | 345 | accel_bias_reg[0] -= (accel_bias[0]/8); // Subtract calculated averaged accelerometer bias scaled to 2048 LSB/g (16 g full scale) |
kohlerba | 0:8a2cac9ba89e | 346 | accel_bias_reg[1] -= (accel_bias[1]/8); |
kohlerba | 0:8a2cac9ba89e | 347 | accel_bias_reg[2] -= (accel_bias[2]/8); |
kohlerba | 0:8a2cac9ba89e | 348 | |
kohlerba | 0:8a2cac9ba89e | 349 | data[0] = (accel_bias_reg[0] >> 8) & 0xFF; |
kohlerba | 0:8a2cac9ba89e | 350 | data[1] = (accel_bias_reg[0]) & 0xFF; |
kohlerba | 0:8a2cac9ba89e | 351 | data[1] = data[1] | mask_bit[0]; // preserve temperature compensation bit when writing back to accelerometer bias registers |
kohlerba | 0:8a2cac9ba89e | 352 | data[2] = (accel_bias_reg[1] >> 8) & 0xFF; |
kohlerba | 0:8a2cac9ba89e | 353 | data[3] = (accel_bias_reg[1]) & 0xFF; |
kohlerba | 0:8a2cac9ba89e | 354 | data[3] = data[3] | mask_bit[1]; // preserve temperature compensation bit when writing back to accelerometer bias registers |
kohlerba | 0:8a2cac9ba89e | 355 | data[4] = (accel_bias_reg[2] >> 8) & 0xFF; |
kohlerba | 0:8a2cac9ba89e | 356 | data[5] = (accel_bias_reg[2]) & 0xFF; |
kohlerba | 0:8a2cac9ba89e | 357 | data[5] = data[5] | mask_bit[2]; // preserve temperature compensation bit when writing back to accelerometer bias registers |
kohlerba | 0:8a2cac9ba89e | 358 | |
kohlerba | 0:8a2cac9ba89e | 359 | // Push accelerometer biases to hardware registers |
kohlerba | 0:8a2cac9ba89e | 360 | // writeByte(MPU6050_ADDRESS, XA_OFFSET_H, data[0]); |
kohlerba | 0:8a2cac9ba89e | 361 | // writeByte(MPU6050_ADDRESS, XA_OFFSET_L_TC, data[1]); |
kohlerba | 0:8a2cac9ba89e | 362 | // writeByte(MPU6050_ADDRESS, YA_OFFSET_H, data[2]); |
kohlerba | 0:8a2cac9ba89e | 363 | // writeByte(MPU6050_ADDRESS, YA_OFFSET_L_TC, data[3]); |
kohlerba | 0:8a2cac9ba89e | 364 | // writeByte(MPU6050_ADDRESS, ZA_OFFSET_H, data[4]); |
kohlerba | 0:8a2cac9ba89e | 365 | // writeByte(MPU6050_ADDRESS, ZA_OFFSET_L_TC, data[5]); |
kohlerba | 0:8a2cac9ba89e | 366 | |
kohlerba | 0:8a2cac9ba89e | 367 | // Output scaled accelerometer biases for manual subtraction in the main program |
kohlerba | 0:8a2cac9ba89e | 368 | dest2[0] = (float)accel_bias[0]/(float)accelsensitivity; |
kohlerba | 0:8a2cac9ba89e | 369 | dest2[1] = (float)accel_bias[1]/(float)accelsensitivity; |
kohlerba | 0:8a2cac9ba89e | 370 | dest2[2] = (float)accel_bias[2]/(float)accelsensitivity; |
kohlerba | 0:8a2cac9ba89e | 371 | } |
kohlerba | 0:8a2cac9ba89e | 372 | |
kohlerba | 0:8a2cac9ba89e | 373 | |
kohlerba | 0:8a2cac9ba89e | 374 | // Accelerometer and gyroscope self test; check calibration wrt factory settings |
kohlerba | 0:8a2cac9ba89e | 375 | void mpu6050::selfTest(float * destination) // Should return percent deviation from factory trim values, +/- 14 or less deviation is a pass |
kohlerba | 0:8a2cac9ba89e | 376 | { |
kohlerba | 0:8a2cac9ba89e | 377 | uint8_t rawData[4] = {0, 0, 0, 0}; |
kohlerba | 0:8a2cac9ba89e | 378 | uint8_t selfTest[6]; |
kohlerba | 0:8a2cac9ba89e | 379 | float factoryTrim[6]; |
kohlerba | 0:8a2cac9ba89e | 380 | |
kohlerba | 0:8a2cac9ba89e | 381 | // Configure the accelerometer for self-test |
kohlerba | 0:8a2cac9ba89e | 382 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, 0xF0); // Enable self test on all three axes and set accelerometer range to +/- 8 g |
kohlerba | 0:8a2cac9ba89e | 383 | writeByte(MPU6050_ADDRESS, GYRO_CONFIG, 0xE0); // Enable self test on all three axes and set gyro range to +/- 250 degrees/s |
kohlerba | 0:8a2cac9ba89e | 384 | wait(0.25); // Delay a while to let the device execute the self-test |
kohlerba | 0:8a2cac9ba89e | 385 | rawData[0] = readByte(MPU6050_ADDRESS, SELF_TEST_X); // X-axis self-test results |
kohlerba | 0:8a2cac9ba89e | 386 | rawData[1] = readByte(MPU6050_ADDRESS, SELF_TEST_Y); // Y-axis self-test results |
kohlerba | 0:8a2cac9ba89e | 387 | rawData[2] = readByte(MPU6050_ADDRESS, SELF_TEST_Z); // Z-axis self-test results |
kohlerba | 0:8a2cac9ba89e | 388 | rawData[3] = readByte(MPU6050_ADDRESS, SELF_TEST_A); // Mixed-axis self-test results |
kohlerba | 0:8a2cac9ba89e | 389 | // Extract the acceleration test results first |
kohlerba | 0:8a2cac9ba89e | 390 | selfTest[0] = (rawData[0] >> 3) | (rawData[3] & 0x30) >> 4 ; // XA_TEST result is a five-bit unsigned integer |
kohlerba | 0:8a2cac9ba89e | 391 | selfTest[1] = (rawData[1] >> 3) | (rawData[3] & 0x0C) >> 4 ; // YA_TEST result is a five-bit unsigned integer |
kohlerba | 0:8a2cac9ba89e | 392 | selfTest[2] = (rawData[2] >> 3) | (rawData[3] & 0x03) >> 4 ; // ZA_TEST result is a five-bit unsigned integer |
kohlerba | 0:8a2cac9ba89e | 393 | // Extract the gyration test results first |
kohlerba | 0:8a2cac9ba89e | 394 | selfTest[3] = rawData[0] & 0x1F ; // XG_TEST result is a five-bit unsigned integer |
kohlerba | 0:8a2cac9ba89e | 395 | selfTest[4] = rawData[1] & 0x1F ; // YG_TEST result is a five-bit unsigned integer |
kohlerba | 0:8a2cac9ba89e | 396 | selfTest[5] = rawData[2] & 0x1F ; // ZG_TEST result is a five-bit unsigned integer |
kohlerba | 0:8a2cac9ba89e | 397 | // Process results to allow final comparison with factory set values |
kohlerba | 0:8a2cac9ba89e | 398 | factoryTrim[0] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[0] - 1.0f)/30.0f))); // FT[Xa] factory trim calculation |
kohlerba | 0:8a2cac9ba89e | 399 | factoryTrim[1] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[1] - 1.0f)/30.0f))); // FT[Ya] factory trim calculation |
kohlerba | 0:8a2cac9ba89e | 400 | factoryTrim[2] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[2] - 1.0f)/30.0f))); // FT[Za] factory trim calculation |
kohlerba | 0:8a2cac9ba89e | 401 | factoryTrim[3] = ( 25.0f*131.0f)*(pow( 1.046f , (selfTest[3] - 1.0f) )); // FT[Xg] factory trim calculation |
kohlerba | 0:8a2cac9ba89e | 402 | factoryTrim[4] = (-25.0f*131.0f)*(pow( 1.046f , (selfTest[4] - 1.0f) )); // FT[Yg] factory trim calculation |
kohlerba | 0:8a2cac9ba89e | 403 | factoryTrim[5] = ( 25.0f*131.0f)*(pow( 1.046f , (selfTest[5] - 1.0f) )); // FT[Zg] factory trim calculation |
kohlerba | 0:8a2cac9ba89e | 404 | |
kohlerba | 0:8a2cac9ba89e | 405 | // Output self-test results and factory trim calculation if desired |
kohlerba | 0:8a2cac9ba89e | 406 | // Serial.println(selfTest[0]); Serial.println(selfTest[1]); Serial.println(selfTest[2]); |
kohlerba | 0:8a2cac9ba89e | 407 | // Serial.println(selfTest[3]); Serial.println(selfTest[4]); Serial.println(selfTest[5]); |
kohlerba | 0:8a2cac9ba89e | 408 | // Serial.println(factoryTrim[0]); Serial.println(factoryTrim[1]); Serial.println(factoryTrim[2]); |
kohlerba | 0:8a2cac9ba89e | 409 | // Serial.println(factoryTrim[3]); Serial.println(factoryTrim[4]); Serial.println(factoryTrim[5]); |
kohlerba | 0:8a2cac9ba89e | 410 | |
kohlerba | 0:8a2cac9ba89e | 411 | // Report results as a ratio of (STR - FT)/FT; the change from Factory Trim of the Self-Test Response |
kohlerba | 0:8a2cac9ba89e | 412 | // To get to percent, must multiply by 100 and subtract result from 100 |
kohlerba | 0:8a2cac9ba89e | 413 | for (int i = 0; i < 6; i++) { |
kohlerba | 0:8a2cac9ba89e | 414 | destination[i] = 100.0f + 100.0f*(selfTest[i] - factoryTrim[i])/factoryTrim[i]; // Report percent differences |
kohlerba | 0:8a2cac9ba89e | 415 | } |
kohlerba | 0:8a2cac9ba89e | 416 | |
kohlerba | 0:8a2cac9ba89e | 417 | } |
kohlerba | 0:8a2cac9ba89e | 418 | |
kohlerba | 0:8a2cac9ba89e | 419 | |
kohlerba | 0:8a2cac9ba89e | 420 | // Implementation of Sebastian Madgwick's "...efficient orientation filter for... inertial/magnetic sensor arrays" |
kohlerba | 0:8a2cac9ba89e | 421 | // (see http://www.x-io.co.uk/category/open-source/ for examples and more details) |
kohlerba | 0:8a2cac9ba89e | 422 | // which fuses acceleration and rotation rate to produce a quaternion-based estimate of relative |
kohlerba | 0:8a2cac9ba89e | 423 | // device orientation -- which can be converted to yaw, pitch, and roll. Useful for stabilizing quadcopters, etc. |
kohlerba | 0:8a2cac9ba89e | 424 | // The performance of the orientation filter is at least as good as conventional Kalman-based filtering algorithms |
kohlerba | 0:8a2cac9ba89e | 425 | // but is much less computationally intensive---it can be performed on a 3.3 V Pro Mini operating at 8 MHz! |
kohlerba | 0:8a2cac9ba89e | 426 | void mpu6050::MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz) |
kohlerba | 0:8a2cac9ba89e | 427 | { |
kohlerba | 0:8a2cac9ba89e | 428 | float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3]; // short name local variable for readability |
kohlerba | 0:8a2cac9ba89e | 429 | float norm; // vector norm |
kohlerba | 0:8a2cac9ba89e | 430 | float f1, f2, f3; // objective funcyion elements |
kohlerba | 0:8a2cac9ba89e | 431 | float J_11or24, J_12or23, J_13or22, J_14or21, J_32, J_33; // objective function Jacobian elements |
kohlerba | 0:8a2cac9ba89e | 432 | float qDot1, qDot2, qDot3, qDot4; |
kohlerba | 0:8a2cac9ba89e | 433 | float hatDot1, hatDot2, hatDot3, hatDot4; |
kohlerba | 0:8a2cac9ba89e | 434 | float gerrx, gerry, gerrz, gbiasx, gbiasy, gbiasz; // gyro bias error |
kohlerba | 0:8a2cac9ba89e | 435 | |
kohlerba | 0:8a2cac9ba89e | 436 | // Auxiliary variables to avoid repeated arithmetic |
kohlerba | 0:8a2cac9ba89e | 437 | float _halfq1 = 0.5f * q1; |
kohlerba | 0:8a2cac9ba89e | 438 | float _halfq2 = 0.5f * q2; |
kohlerba | 0:8a2cac9ba89e | 439 | float _halfq3 = 0.5f * q3; |
kohlerba | 0:8a2cac9ba89e | 440 | float _halfq4 = 0.5f * q4; |
kohlerba | 0:8a2cac9ba89e | 441 | float _2q1 = 2.0f * q1; |
kohlerba | 0:8a2cac9ba89e | 442 | float _2q2 = 2.0f * q2; |
kohlerba | 0:8a2cac9ba89e | 443 | float _2q3 = 2.0f * q3; |
kohlerba | 0:8a2cac9ba89e | 444 | float _2q4 = 2.0f * q4; |
kohlerba | 0:8a2cac9ba89e | 445 | // float _2q1q3 = 2.0f * q1 * q3; |
kohlerba | 0:8a2cac9ba89e | 446 | // float _2q3q4 = 2.0f * q3 * q4; |
kohlerba | 0:8a2cac9ba89e | 447 | |
kohlerba | 0:8a2cac9ba89e | 448 | // Normalise accelerometer measurement |
kohlerba | 0:8a2cac9ba89e | 449 | norm = sqrt(ax * ax + ay * ay + az * az); |
kohlerba | 0:8a2cac9ba89e | 450 | if (norm == 0.0f) return; // handle NaN |
kohlerba | 0:8a2cac9ba89e | 451 | norm = 1.0f/norm; |
kohlerba | 0:8a2cac9ba89e | 452 | ax *= norm; |
kohlerba | 0:8a2cac9ba89e | 453 | ay *= norm; |
kohlerba | 0:8a2cac9ba89e | 454 | az *= norm; |
kohlerba | 0:8a2cac9ba89e | 455 | |
kohlerba | 0:8a2cac9ba89e | 456 | // Compute the objective function and Jacobian |
kohlerba | 0:8a2cac9ba89e | 457 | f1 = _2q2 * q4 - _2q1 * q3 - ax; |
kohlerba | 0:8a2cac9ba89e | 458 | f2 = _2q1 * q2 + _2q3 * q4 - ay; |
kohlerba | 0:8a2cac9ba89e | 459 | f3 = 1.0f - _2q2 * q2 - _2q3 * q3 - az; |
kohlerba | 0:8a2cac9ba89e | 460 | J_11or24 = _2q3; |
kohlerba | 0:8a2cac9ba89e | 461 | J_12or23 = _2q4; |
kohlerba | 0:8a2cac9ba89e | 462 | J_13or22 = _2q1; |
kohlerba | 0:8a2cac9ba89e | 463 | J_14or21 = _2q2; |
kohlerba | 0:8a2cac9ba89e | 464 | J_32 = 2.0f * J_14or21; |
kohlerba | 0:8a2cac9ba89e | 465 | J_33 = 2.0f * J_11or24; |
kohlerba | 0:8a2cac9ba89e | 466 | |
kohlerba | 0:8a2cac9ba89e | 467 | // Compute the gradient (matrix multiplication) |
kohlerba | 0:8a2cac9ba89e | 468 | hatDot1 = J_14or21 * f2 - J_11or24 * f1; |
kohlerba | 0:8a2cac9ba89e | 469 | hatDot2 = J_12or23 * f1 + J_13or22 * f2 - J_32 * f3; |
kohlerba | 0:8a2cac9ba89e | 470 | hatDot3 = J_12or23 * f2 - J_33 *f3 - J_13or22 * f1; |
kohlerba | 0:8a2cac9ba89e | 471 | hatDot4 = J_14or21 * f1 + J_11or24 * f2; |
kohlerba | 0:8a2cac9ba89e | 472 | |
kohlerba | 0:8a2cac9ba89e | 473 | // Normalize the gradient |
kohlerba | 0:8a2cac9ba89e | 474 | norm = sqrt(hatDot1 * hatDot1 + hatDot2 * hatDot2 + hatDot3 * hatDot3 + hatDot4 * hatDot4); |
kohlerba | 0:8a2cac9ba89e | 475 | hatDot1 /= norm; |
kohlerba | 0:8a2cac9ba89e | 476 | hatDot2 /= norm; |
kohlerba | 0:8a2cac9ba89e | 477 | hatDot3 /= norm; |
kohlerba | 0:8a2cac9ba89e | 478 | hatDot4 /= norm; |
kohlerba | 0:8a2cac9ba89e | 479 | |
kohlerba | 0:8a2cac9ba89e | 480 | // Compute estimated gyroscope biases |
kohlerba | 0:8a2cac9ba89e | 481 | gerrx = _2q1 * hatDot2 - _2q2 * hatDot1 - _2q3 * hatDot4 + _2q4 * hatDot3; |
kohlerba | 0:8a2cac9ba89e | 482 | gerry = _2q1 * hatDot3 + _2q2 * hatDot4 - _2q3 * hatDot1 - _2q4 * hatDot2; |
kohlerba | 0:8a2cac9ba89e | 483 | gerrz = _2q1 * hatDot4 - _2q2 * hatDot3 + _2q3 * hatDot2 - _2q4 * hatDot1; |
kohlerba | 0:8a2cac9ba89e | 484 | |
kohlerba | 0:8a2cac9ba89e | 485 | // Compute and remove gyroscope biases |
kohlerba | 0:8a2cac9ba89e | 486 | gbiasx += gerrx * deltat * zeta; |
kohlerba | 0:8a2cac9ba89e | 487 | gbiasy += gerry * deltat * zeta; |
kohlerba | 0:8a2cac9ba89e | 488 | gbiasz += gerrz * deltat * zeta; |
kohlerba | 0:8a2cac9ba89e | 489 | // gx -= gbiasx; |
kohlerba | 0:8a2cac9ba89e | 490 | // gy -= gbiasy; |
kohlerba | 0:8a2cac9ba89e | 491 | // gz -= gbiasz; |
kohlerba | 0:8a2cac9ba89e | 492 | |
kohlerba | 0:8a2cac9ba89e | 493 | // Compute the quaternion derivative |
kohlerba | 0:8a2cac9ba89e | 494 | qDot1 = -_halfq2 * gx - _halfq3 * gy - _halfq4 * gz; |
kohlerba | 0:8a2cac9ba89e | 495 | qDot2 = _halfq1 * gx + _halfq3 * gz - _halfq4 * gy; |
kohlerba | 0:8a2cac9ba89e | 496 | qDot3 = _halfq1 * gy - _halfq2 * gz + _halfq4 * gx; |
kohlerba | 0:8a2cac9ba89e | 497 | qDot4 = _halfq1 * gz + _halfq2 * gy - _halfq3 * gx; |
kohlerba | 0:8a2cac9ba89e | 498 | |
kohlerba | 0:8a2cac9ba89e | 499 | // Compute then integrate estimated quaternion derivative |
kohlerba | 0:8a2cac9ba89e | 500 | q1 += (qDot1 -(beta * hatDot1)) * deltat; |
kohlerba | 0:8a2cac9ba89e | 501 | q2 += (qDot2 -(beta * hatDot2)) * deltat; |
kohlerba | 0:8a2cac9ba89e | 502 | q3 += (qDot3 -(beta * hatDot3)) * deltat; |
kohlerba | 0:8a2cac9ba89e | 503 | q4 += (qDot4 -(beta * hatDot4)) * deltat; |
kohlerba | 0:8a2cac9ba89e | 504 | |
kohlerba | 0:8a2cac9ba89e | 505 | // Normalize the quaternion |
kohlerba | 0:8a2cac9ba89e | 506 | norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4); // normalise quaternion |
kohlerba | 0:8a2cac9ba89e | 507 | norm = 1.0f/norm; |
kohlerba | 0:8a2cac9ba89e | 508 | q[0] = q1 * norm; |
kohlerba | 0:8a2cac9ba89e | 509 | q[1] = q2 * norm; |
kohlerba | 0:8a2cac9ba89e | 510 | q[2] = q3 * norm; |
kohlerba | 0:8a2cac9ba89e | 511 | q[3] = q4 * norm; |
kohlerba | 0:8a2cac9ba89e | 512 | |
kohlerba | 0:8a2cac9ba89e | 513 | } |