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