A Jedi light saber controller program with the following "features": - Using RGB LEDs - User can change light colors with a button - Motion dependent (PWM) sounds with a MPU6050 motion sensor - Low voltage detection
Dependencies: L152RE_USBDevice STM32_USB48MHz Watchdog mbed
MPU6050IMU/MPU6050.cpp@2:59a7d4677474, 2016-03-24 (annotated)
- Committer:
- nightmechanic
- Date:
- Thu Mar 24 22:42:59 2016 +0000
- Revision:
- 2:59a7d4677474
- Child:
- 4:7e4bb0c29d3b
re-arranging the MPU6050 driver: ; separation into h and cpp files; etc.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nightmechanic | 2:59a7d4677474 | 1 | #include "MPU6050.h" |
nightmechanic | 2:59a7d4677474 | 2 | #include "mbed.h" |
nightmechanic | 2:59a7d4677474 | 3 | #include "math.h" |
nightmechanic | 2:59a7d4677474 | 4 | |
nightmechanic | 2:59a7d4677474 | 5 | |
nightmechanic | 2:59a7d4677474 | 6 | |
nightmechanic | 2:59a7d4677474 | 7 | // Specify sensor full scale |
nightmechanic | 2:59a7d4677474 | 8 | int Gscale = GFS_250DPS; |
nightmechanic | 2:59a7d4677474 | 9 | int Ascale = AFS_8G; |
nightmechanic | 2:59a7d4677474 | 10 | |
nightmechanic | 2:59a7d4677474 | 11 | //Set up I2C, (SDA,SCL) |
nightmechanic | 2:59a7d4677474 | 12 | I2C MPU_i2c(PB_9, PB_8); |
nightmechanic | 2:59a7d4677474 | 13 | |
nightmechanic | 2:59a7d4677474 | 14 | //DigitalOut myled(LED1); |
nightmechanic | 2:59a7d4677474 | 15 | |
nightmechanic | 2:59a7d4677474 | 16 | float aRes, gRes; // scale resolutions per LSB for the sensors |
nightmechanic | 2:59a7d4677474 | 17 | |
nightmechanic | 2:59a7d4677474 | 18 | // Pin definitions |
nightmechanic | 2:59a7d4677474 | 19 | int intPin = 12; // These can be changed, 2 and 3 are the Arduinos ext int pins |
nightmechanic | 2:59a7d4677474 | 20 | |
nightmechanic | 2:59a7d4677474 | 21 | int16_t accelCount[3]; // Stores the 16-bit signed accelerometer sensor output |
nightmechanic | 2:59a7d4677474 | 22 | float ax, ay, az; // Stores the real accel value in g's |
nightmechanic | 2:59a7d4677474 | 23 | int16_t gyroCount[3]; // Stores the 16-bit signed gyro sensor output |
nightmechanic | 2:59a7d4677474 | 24 | float gx, gy, gz; // Stores the real gyro value in degrees per seconds |
nightmechanic | 2:59a7d4677474 | 25 | float gyroBias[3] = {0, 0, 0}, accelBias[3] = {0, 0, 0}; // Bias corrections for gyro and accelerometer |
nightmechanic | 2:59a7d4677474 | 26 | int16_t tempCount; // Stores the real internal chip temperature in degrees Celsius |
nightmechanic | 2:59a7d4677474 | 27 | float temperature; |
nightmechanic | 2:59a7d4677474 | 28 | float SelfTest[6]; |
nightmechanic | 2:59a7d4677474 | 29 | |
nightmechanic | 2:59a7d4677474 | 30 | //int delt_t = 0; // used to control display output rate |
nightmechanic | 2:59a7d4677474 | 31 | //int count = 0; // used to control display output rate |
nightmechanic | 2:59a7d4677474 | 32 | float sum = 0; |
nightmechanic | 2:59a7d4677474 | 33 | uint32_t sumCount = 0; |
nightmechanic | 2:59a7d4677474 | 34 | |
nightmechanic | 2:59a7d4677474 | 35 | // parameters for 6 DoF sensor fusion calculations |
nightmechanic | 2:59a7d4677474 | 36 | float PI = 3.14159265358979323846f; |
nightmechanic | 2:59a7d4677474 | 37 | 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 |
nightmechanic | 2:59a7d4677474 | 38 | float beta = sqrt(3.0f / 4.0f) * GyroMeasError; // compute beta |
nightmechanic | 2:59a7d4677474 | 39 | float GyroMeasDrift = PI * (1.0f / 180.0f); // gyroscope measurement drift in rad/s/s (start at 0.0 deg/s/s) |
nightmechanic | 2:59a7d4677474 | 40 | 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 |
nightmechanic | 2:59a7d4677474 | 41 | float pitch, yaw, roll; |
nightmechanic | 2:59a7d4677474 | 42 | float deltat = 0.0f; // integration interval for both filter schemes |
nightmechanic | 2:59a7d4677474 | 43 | int lastUpdate = 0, firstUpdate = 0, Now = 0; // used to calculate integration interval // used to calculate integration interval |
nightmechanic | 2:59a7d4677474 | 44 | float q[4] = {1.0f, 0.0f, 0.0f, 0.0f}; // vector to hold quaternion |
nightmechanic | 2:59a7d4677474 | 45 | |
nightmechanic | 2:59a7d4677474 | 46 | |
nightmechanic | 2:59a7d4677474 | 47 | void MPU6050::writeByte(uint8_t address, uint8_t subAddress, uint8_t data) |
nightmechanic | 2:59a7d4677474 | 48 | { |
nightmechanic | 2:59a7d4677474 | 49 | char data_write[2]; |
nightmechanic | 2:59a7d4677474 | 50 | data_write[0] = subAddress; |
nightmechanic | 2:59a7d4677474 | 51 | data_write[1] = data; |
nightmechanic | 2:59a7d4677474 | 52 | __disable_irq(); |
nightmechanic | 2:59a7d4677474 | 53 | MPU_i2c.write(address, data_write, 2, 0); |
nightmechanic | 2:59a7d4677474 | 54 | __enable_irq(); |
nightmechanic | 2:59a7d4677474 | 55 | } |
nightmechanic | 2:59a7d4677474 | 56 | |
nightmechanic | 2:59a7d4677474 | 57 | char MPU6050::readByte(uint8_t address, uint8_t subAddress) |
nightmechanic | 2:59a7d4677474 | 58 | { |
nightmechanic | 2:59a7d4677474 | 59 | char data[1]; // `data` will store the register data |
nightmechanic | 2:59a7d4677474 | 60 | char data_write[1]; |
nightmechanic | 2:59a7d4677474 | 61 | data_write[0] = subAddress; |
nightmechanic | 2:59a7d4677474 | 62 | __disable_irq(); |
nightmechanic | 2:59a7d4677474 | 63 | MPU_i2c.write(address, data_write, 1, 1); // no stop |
nightmechanic | 2:59a7d4677474 | 64 | MPU_i2c.read(address, data, 1, 0); |
nightmechanic | 2:59a7d4677474 | 65 | __enable_irq(); |
nightmechanic | 2:59a7d4677474 | 66 | return data[0]; |
nightmechanic | 2:59a7d4677474 | 67 | } |
nightmechanic | 2:59a7d4677474 | 68 | |
nightmechanic | 2:59a7d4677474 | 69 | void MPU6050::readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest) |
nightmechanic | 2:59a7d4677474 | 70 | { |
nightmechanic | 2:59a7d4677474 | 71 | char data[14]; |
nightmechanic | 2:59a7d4677474 | 72 | char data_write[1]; |
nightmechanic | 2:59a7d4677474 | 73 | data_write[0] = subAddress; |
nightmechanic | 2:59a7d4677474 | 74 | __disable_irq(); |
nightmechanic | 2:59a7d4677474 | 75 | MPU_i2c.write(address, data_write, 1, 1); // no stop |
nightmechanic | 2:59a7d4677474 | 76 | MPU_i2c.read(address, data, count, 0); |
nightmechanic | 2:59a7d4677474 | 77 | __enable_irq(); |
nightmechanic | 2:59a7d4677474 | 78 | for(int ii = 0; ii < count; ii++) { |
nightmechanic | 2:59a7d4677474 | 79 | dest[ii] = data[ii]; |
nightmechanic | 2:59a7d4677474 | 80 | } |
nightmechanic | 2:59a7d4677474 | 81 | } |
nightmechanic | 2:59a7d4677474 | 82 | |
nightmechanic | 2:59a7d4677474 | 83 | |
nightmechanic | 2:59a7d4677474 | 84 | void MPU6050::getGres() { |
nightmechanic | 2:59a7d4677474 | 85 | switch (Gscale) |
nightmechanic | 2:59a7d4677474 | 86 | { |
nightmechanic | 2:59a7d4677474 | 87 | // Possible gyro scales (and their register bit settings) are: |
nightmechanic | 2:59a7d4677474 | 88 | // 250 DPS (00), 500 DPS (01), 1000 DPS (10), and 2000 DPS (11). |
nightmechanic | 2:59a7d4677474 | 89 | // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value: |
nightmechanic | 2:59a7d4677474 | 90 | case GFS_250DPS: |
nightmechanic | 2:59a7d4677474 | 91 | gRes = 250.0/32768.0; |
nightmechanic | 2:59a7d4677474 | 92 | break; |
nightmechanic | 2:59a7d4677474 | 93 | case GFS_500DPS: |
nightmechanic | 2:59a7d4677474 | 94 | gRes = 500.0/32768.0; |
nightmechanic | 2:59a7d4677474 | 95 | break; |
nightmechanic | 2:59a7d4677474 | 96 | case GFS_1000DPS: |
nightmechanic | 2:59a7d4677474 | 97 | gRes = 1000.0/32768.0; |
nightmechanic | 2:59a7d4677474 | 98 | break; |
nightmechanic | 2:59a7d4677474 | 99 | case GFS_2000DPS: |
nightmechanic | 2:59a7d4677474 | 100 | gRes = 2000.0/32768.0; |
nightmechanic | 2:59a7d4677474 | 101 | break; |
nightmechanic | 2:59a7d4677474 | 102 | } |
nightmechanic | 2:59a7d4677474 | 103 | } |
nightmechanic | 2:59a7d4677474 | 104 | |
nightmechanic | 2:59a7d4677474 | 105 | void MPU6050::getAres() { |
nightmechanic | 2:59a7d4677474 | 106 | switch (Ascale) |
nightmechanic | 2:59a7d4677474 | 107 | { |
nightmechanic | 2:59a7d4677474 | 108 | // Possible accelerometer scales (and their register bit settings) are: |
nightmechanic | 2:59a7d4677474 | 109 | // 2 Gs (00), 4 Gs (01), 8 Gs (10), and 16 Gs (11). |
nightmechanic | 2:59a7d4677474 | 110 | // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value: |
nightmechanic | 2:59a7d4677474 | 111 | case AFS_2G: |
nightmechanic | 2:59a7d4677474 | 112 | aRes = 2.0/32768.0; |
nightmechanic | 2:59a7d4677474 | 113 | break; |
nightmechanic | 2:59a7d4677474 | 114 | case AFS_4G: |
nightmechanic | 2:59a7d4677474 | 115 | aRes = 4.0/32768.0; |
nightmechanic | 2:59a7d4677474 | 116 | break; |
nightmechanic | 2:59a7d4677474 | 117 | case AFS_8G: |
nightmechanic | 2:59a7d4677474 | 118 | aRes = 8.0/32768.0; |
nightmechanic | 2:59a7d4677474 | 119 | break; |
nightmechanic | 2:59a7d4677474 | 120 | case AFS_16G: |
nightmechanic | 2:59a7d4677474 | 121 | aRes = 16.0/32768.0; |
nightmechanic | 2:59a7d4677474 | 122 | break; |
nightmechanic | 2:59a7d4677474 | 123 | } |
nightmechanic | 2:59a7d4677474 | 124 | } |
nightmechanic | 2:59a7d4677474 | 125 | |
nightmechanic | 2:59a7d4677474 | 126 | |
nightmechanic | 2:59a7d4677474 | 127 | void MPU6050::readAccelData(int16_t * destination) |
nightmechanic | 2:59a7d4677474 | 128 | { |
nightmechanic | 2:59a7d4677474 | 129 | uint8_t rawData[6]; // x/y/z accel register data stored here |
nightmechanic | 2:59a7d4677474 | 130 | readBytes(MPU6050_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]); // Read the six raw data registers into data array |
nightmechanic | 2:59a7d4677474 | 131 | destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value |
nightmechanic | 2:59a7d4677474 | 132 | destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ; |
nightmechanic | 2:59a7d4677474 | 133 | destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ; |
nightmechanic | 2:59a7d4677474 | 134 | } |
nightmechanic | 2:59a7d4677474 | 135 | |
nightmechanic | 2:59a7d4677474 | 136 | void MPU6050::readGyroData(int16_t * destination) |
nightmechanic | 2:59a7d4677474 | 137 | { |
nightmechanic | 2:59a7d4677474 | 138 | uint8_t rawData[6]; // x/y/z gyro register data stored here |
nightmechanic | 2:59a7d4677474 | 139 | readBytes(MPU6050_ADDRESS, GYRO_XOUT_H, 6, &rawData[0]); // Read the six raw data registers sequentially into data array |
nightmechanic | 2:59a7d4677474 | 140 | destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value |
nightmechanic | 2:59a7d4677474 | 141 | destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ; |
nightmechanic | 2:59a7d4677474 | 142 | destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ; |
nightmechanic | 2:59a7d4677474 | 143 | } |
nightmechanic | 2:59a7d4677474 | 144 | |
nightmechanic | 2:59a7d4677474 | 145 | int16_t MPU6050::readTempData() |
nightmechanic | 2:59a7d4677474 | 146 | { |
nightmechanic | 2:59a7d4677474 | 147 | uint8_t rawData[2]; // x/y/z gyro register data stored here |
nightmechanic | 2:59a7d4677474 | 148 | readBytes(MPU6050_ADDRESS, TEMP_OUT_H, 2, &rawData[0]); // Read the two raw data registers sequentially into data array |
nightmechanic | 2:59a7d4677474 | 149 | return (int16_t)(((int16_t)rawData[0]) << 8 | rawData[1]) ; // Turn the MSB and LSB into a 16-bit value |
nightmechanic | 2:59a7d4677474 | 150 | } |
nightmechanic | 2:59a7d4677474 | 151 | |
nightmechanic | 2:59a7d4677474 | 152 | |
nightmechanic | 2:59a7d4677474 | 153 | |
nightmechanic | 2:59a7d4677474 | 154 | // Configure the motion detection control for low power accelerometer mode |
nightmechanic | 2:59a7d4677474 | 155 | void MPU6050::LowPowerAccelOnly() |
nightmechanic | 2:59a7d4677474 | 156 | { |
nightmechanic | 2:59a7d4677474 | 157 | |
nightmechanic | 2:59a7d4677474 | 158 | // The sensor has a high-pass filter necessary to invoke to allow the sensor motion detection algorithms work properly |
nightmechanic | 2:59a7d4677474 | 159 | // Motion detection occurs on free-fall (acceleration below a threshold for some time for all axes), motion (acceleration |
nightmechanic | 2:59a7d4677474 | 160 | // above a threshold for some time on at least one axis), and zero-motion toggle (acceleration on each axis less than a |
nightmechanic | 2:59a7d4677474 | 161 | // threshold for some time sets this flag, motion above the threshold turns it off). The high-pass filter takes gravity out |
nightmechanic | 2:59a7d4677474 | 162 | // consideration for these threshold evaluations; otherwise, the flags would be set all the time! |
nightmechanic | 2:59a7d4677474 | 163 | |
nightmechanic | 2:59a7d4677474 | 164 | uint8_t c = readByte(MPU6050_ADDRESS, PWR_MGMT_1); |
nightmechanic | 2:59a7d4677474 | 165 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c & ~0x30); // Clear sleep and cycle bits [5:6] |
nightmechanic | 2:59a7d4677474 | 166 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c | 0x30); // Set sleep and cycle bits [5:6] to zero to make sure accelerometer is running |
nightmechanic | 2:59a7d4677474 | 167 | |
nightmechanic | 2:59a7d4677474 | 168 | c = readByte(MPU6050_ADDRESS, PWR_MGMT_2); |
nightmechanic | 2:59a7d4677474 | 169 | writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c & ~0x38); // Clear standby XA, YA, and ZA bits [3:5] |
nightmechanic | 2:59a7d4677474 | 170 | writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c | 0x00); // Set XA, YA, and ZA bits [3:5] to zero to make sure accelerometer is running |
nightmechanic | 2:59a7d4677474 | 171 | |
nightmechanic | 2:59a7d4677474 | 172 | c = readByte(MPU6050_ADDRESS, ACCEL_CONFIG); |
nightmechanic | 2:59a7d4677474 | 173 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0x07); // Clear high-pass filter bits [2:0] |
nightmechanic | 2:59a7d4677474 | 174 | // 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 |
nightmechanic | 2:59a7d4677474 | 175 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c | 0x00); // Set ACCEL_HPF to 0; reset mode disbaling high-pass filter |
nightmechanic | 2:59a7d4677474 | 176 | |
nightmechanic | 2:59a7d4677474 | 177 | c = readByte(MPU6050_ADDRESS, CONFIG); |
nightmechanic | 2:59a7d4677474 | 178 | writeByte(MPU6050_ADDRESS, CONFIG, c & ~0x07); // Clear low-pass filter bits [2:0] |
nightmechanic | 2:59a7d4677474 | 179 | writeByte(MPU6050_ADDRESS, CONFIG, c | 0x00); // Set DLPD_CFG to 0; 260 Hz bandwidth, 1 kHz rate |
nightmechanic | 2:59a7d4677474 | 180 | |
nightmechanic | 2:59a7d4677474 | 181 | c = readByte(MPU6050_ADDRESS, INT_ENABLE); |
nightmechanic | 2:59a7d4677474 | 182 | writeByte(MPU6050_ADDRESS, INT_ENABLE, c & ~0xFF); // Clear all interrupts |
nightmechanic | 2:59a7d4677474 | 183 | writeByte(MPU6050_ADDRESS, INT_ENABLE, 0x40); // Enable motion threshold (bits 5) interrupt only |
nightmechanic | 2:59a7d4677474 | 184 | |
nightmechanic | 2:59a7d4677474 | 185 | // Motion detection interrupt requires the absolute value of any axis to lie above the detection threshold |
nightmechanic | 2:59a7d4677474 | 186 | // for at least the counter duration |
nightmechanic | 2:59a7d4677474 | 187 | writeByte(MPU6050_ADDRESS, MOT_THR, 0x80); // Set motion detection to 0.256 g; LSB = 2 mg |
nightmechanic | 2:59a7d4677474 | 188 | writeByte(MPU6050_ADDRESS, MOT_DUR, 0x01); // Set motion detect duration to 1 ms; LSB is 1 ms @ 1 kHz rate |
nightmechanic | 2:59a7d4677474 | 189 | |
nightmechanic | 2:59a7d4677474 | 190 | wait(0.1); // Add delay for accumulation of samples |
nightmechanic | 2:59a7d4677474 | 191 | |
nightmechanic | 2:59a7d4677474 | 192 | c = readByte(MPU6050_ADDRESS, ACCEL_CONFIG); |
nightmechanic | 2:59a7d4677474 | 193 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0x07); // Clear high-pass filter bits [2:0] |
nightmechanic | 2:59a7d4677474 | 194 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c | 0x07); // Set ACCEL_HPF to 7; hold the initial accleration value as a referance |
nightmechanic | 2:59a7d4677474 | 195 | |
nightmechanic | 2:59a7d4677474 | 196 | c = readByte(MPU6050_ADDRESS, PWR_MGMT_2); |
nightmechanic | 2:59a7d4677474 | 197 | writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c & ~0xC7); // Clear standby XA, YA, and ZA bits [3:5] and LP_WAKE_CTRL bits [6:7] |
nightmechanic | 2:59a7d4677474 | 198 | writeByte(MPU6050_ADDRESS, PWR_MGMT_2, c | 0x47); // Set wakeup frequency to 5 Hz, and disable XG, YG, and ZG gyros (bits [0:2]) |
nightmechanic | 2:59a7d4677474 | 199 | |
nightmechanic | 2:59a7d4677474 | 200 | c = readByte(MPU6050_ADDRESS, PWR_MGMT_1); |
nightmechanic | 2:59a7d4677474 | 201 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c & ~0x20); // Clear sleep and cycle bit 5 |
nightmechanic | 2:59a7d4677474 | 202 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, c | 0x20); // Set cycle bit 5 to begin low power accelerometer motion interrupts |
nightmechanic | 2:59a7d4677474 | 203 | |
nightmechanic | 2:59a7d4677474 | 204 | } |
nightmechanic | 2:59a7d4677474 | 205 | |
nightmechanic | 2:59a7d4677474 | 206 | |
nightmechanic | 2:59a7d4677474 | 207 | void MPU6050::resetMPU6050() { |
nightmechanic | 2:59a7d4677474 | 208 | // reset device |
nightmechanic | 2:59a7d4677474 | 209 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device |
nightmechanic | 2:59a7d4677474 | 210 | wait(0.1); |
nightmechanic | 2:59a7d4677474 | 211 | } |
nightmechanic | 2:59a7d4677474 | 212 | |
nightmechanic | 2:59a7d4677474 | 213 | |
nightmechanic | 2:59a7d4677474 | 214 | void MPU6050::initMPU6050() |
nightmechanic | 2:59a7d4677474 | 215 | { |
nightmechanic | 2:59a7d4677474 | 216 | // Initialize MPU6050 device |
nightmechanic | 2:59a7d4677474 | 217 | // wake up device |
nightmechanic | 2:59a7d4677474 | 218 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x00); // Clear sleep mode bit (6), enable all sensors |
nightmechanic | 2:59a7d4677474 | 219 | wait(0.1); // Delay 100 ms for PLL to get established on x-axis gyro; should check for PLL ready interrupt |
nightmechanic | 2:59a7d4677474 | 220 | |
nightmechanic | 2:59a7d4677474 | 221 | // get stable time source |
nightmechanic | 2:59a7d4677474 | 222 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x01); // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001 |
nightmechanic | 2:59a7d4677474 | 223 | |
nightmechanic | 2:59a7d4677474 | 224 | // Configure Gyro and Accelerometer |
nightmechanic | 2:59a7d4677474 | 225 | // Disable FSYNC and set accelerometer and gyro bandwidth to 44 and 42 Hz, respectively; |
nightmechanic | 2:59a7d4677474 | 226 | // DLPF_CFG = bits 2:0 = 010; this sets the sample rate at 1 kHz for both |
nightmechanic | 2:59a7d4677474 | 227 | // Maximum delay is 4.9 ms which is just over a 200 Hz maximum rate |
nightmechanic | 2:59a7d4677474 | 228 | writeByte(MPU6050_ADDRESS, CONFIG, 0x03); |
nightmechanic | 2:59a7d4677474 | 229 | |
nightmechanic | 2:59a7d4677474 | 230 | // Set sample rate = gyroscope output rate/(1 + SMPLRT_DIV) |
nightmechanic | 2:59a7d4677474 | 231 | writeByte(MPU6050_ADDRESS, SMPLRT_DIV, 0x04); // Use a 200 Hz rate; the same rate set in CONFIG above |
nightmechanic | 2:59a7d4677474 | 232 | |
nightmechanic | 2:59a7d4677474 | 233 | // Set gyroscope full scale range |
nightmechanic | 2:59a7d4677474 | 234 | // Range selects FS_SEL and AFS_SEL are 0 - 3, so 2-bit values are left-shifted into positions 4:3 |
nightmechanic | 2:59a7d4677474 | 235 | uint8_t c = readByte(MPU6050_ADDRESS, GYRO_CONFIG); |
nightmechanic | 2:59a7d4677474 | 236 | writeByte(MPU6050_ADDRESS, GYRO_CONFIG, c & ~0xE0); // Clear self-test bits [7:5] |
nightmechanic | 2:59a7d4677474 | 237 | writeByte(MPU6050_ADDRESS, GYRO_CONFIG, c & ~0x18); // Clear AFS bits [4:3] |
nightmechanic | 2:59a7d4677474 | 238 | writeByte(MPU6050_ADDRESS, GYRO_CONFIG, c | Gscale << 3); // Set full scale range for the gyro |
nightmechanic | 2:59a7d4677474 | 239 | |
nightmechanic | 2:59a7d4677474 | 240 | // Set accelerometer configuration |
nightmechanic | 2:59a7d4677474 | 241 | c = readByte(MPU6050_ADDRESS, ACCEL_CONFIG); |
nightmechanic | 2:59a7d4677474 | 242 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0xE0); // Clear self-test bits [7:5] |
nightmechanic | 2:59a7d4677474 | 243 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c & ~0x18); // Clear AFS bits [4:3] |
nightmechanic | 2:59a7d4677474 | 244 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, c | Ascale << 3); // Set full scale range for the accelerometer |
nightmechanic | 2:59a7d4677474 | 245 | |
nightmechanic | 2:59a7d4677474 | 246 | // Configure Interrupts and Bypass Enable |
nightmechanic | 2:59a7d4677474 | 247 | // Set interrupt pin active high, push-pull, and clear on read of INT_STATUS, enable I2C_BYPASS_EN so additional chips |
nightmechanic | 2:59a7d4677474 | 248 | // can join the I2C bus and all can be controlled by the Arduino as master |
nightmechanic | 2:59a7d4677474 | 249 | writeByte(MPU6050_ADDRESS, INT_PIN_CFG, 0x22); |
nightmechanic | 2:59a7d4677474 | 250 | writeByte(MPU6050_ADDRESS, INT_ENABLE, 0x01); // Enable data ready (bit 0) interrupt |
nightmechanic | 2:59a7d4677474 | 251 | } |
nightmechanic | 2:59a7d4677474 | 252 | |
nightmechanic | 2:59a7d4677474 | 253 | // Function which accumulates gyro and accelerometer data after device initialization. It calculates the average |
nightmechanic | 2:59a7d4677474 | 254 | // of the at-rest readings and then loads the resulting offsets into accelerometer and gyro bias registers. |
nightmechanic | 2:59a7d4677474 | 255 | void MPU6050::calibrateMPU6050(float * dest1, float * dest2) |
nightmechanic | 2:59a7d4677474 | 256 | { |
nightmechanic | 2:59a7d4677474 | 257 | uint8_t data[12]; // data array to hold accelerometer and gyro x, y, z, data |
nightmechanic | 2:59a7d4677474 | 258 | uint16_t ii, packet_count, fifo_count; |
nightmechanic | 2:59a7d4677474 | 259 | int32_t gyro_bias[3] = {0, 0, 0}, accel_bias[3] = {0, 0, 0}; |
nightmechanic | 2:59a7d4677474 | 260 | |
nightmechanic | 2:59a7d4677474 | 261 | // reset device, reset all registers, clear gyro and accelerometer bias registers |
nightmechanic | 2:59a7d4677474 | 262 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device |
nightmechanic | 2:59a7d4677474 | 263 | wait(0.1); |
nightmechanic | 2:59a7d4677474 | 264 | |
nightmechanic | 2:59a7d4677474 | 265 | // get stable time source |
nightmechanic | 2:59a7d4677474 | 266 | // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001 |
nightmechanic | 2:59a7d4677474 | 267 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x01); |
nightmechanic | 2:59a7d4677474 | 268 | writeByte(MPU6050_ADDRESS, PWR_MGMT_2, 0x00); |
nightmechanic | 2:59a7d4677474 | 269 | wait(0.2); |
nightmechanic | 2:59a7d4677474 | 270 | |
nightmechanic | 2:59a7d4677474 | 271 | // Configure device for bias calculation |
nightmechanic | 2:59a7d4677474 | 272 | writeByte(MPU6050_ADDRESS, INT_ENABLE, 0x00); // Disable all interrupts |
nightmechanic | 2:59a7d4677474 | 273 | writeByte(MPU6050_ADDRESS, FIFO_EN, 0x00); // Disable FIFO |
nightmechanic | 2:59a7d4677474 | 274 | writeByte(MPU6050_ADDRESS, PWR_MGMT_1, 0x00); // Turn on internal clock source |
nightmechanic | 2:59a7d4677474 | 275 | writeByte(MPU6050_ADDRESS, I2C_MST_CTRL, 0x00); // Disable I2C master |
nightmechanic | 2:59a7d4677474 | 276 | writeByte(MPU6050_ADDRESS, USER_CTRL, 0x00); // Disable FIFO and I2C master modes |
nightmechanic | 2:59a7d4677474 | 277 | writeByte(MPU6050_ADDRESS, USER_CTRL, 0x0C); // Reset FIFO and DMP |
nightmechanic | 2:59a7d4677474 | 278 | wait(0.015); |
nightmechanic | 2:59a7d4677474 | 279 | |
nightmechanic | 2:59a7d4677474 | 280 | // Configure MPU6050 gyro and accelerometer for bias calculation |
nightmechanic | 2:59a7d4677474 | 281 | writeByte(MPU6050_ADDRESS, CONFIG, 0x01); // Set low-pass filter to 188 Hz |
nightmechanic | 2:59a7d4677474 | 282 | writeByte(MPU6050_ADDRESS, SMPLRT_DIV, 0x00); // Set sample rate to 1 kHz |
nightmechanic | 2:59a7d4677474 | 283 | writeByte(MPU6050_ADDRESS, GYRO_CONFIG, 0x00); // Set gyro full-scale to 250 degrees per second, maximum sensitivity |
nightmechanic | 2:59a7d4677474 | 284 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, 0x00); // Set accelerometer full-scale to 2 g, maximum sensitivity |
nightmechanic | 2:59a7d4677474 | 285 | |
nightmechanic | 2:59a7d4677474 | 286 | uint16_t gyrosensitivity = 131; // = 131 LSB/degrees/sec |
nightmechanic | 2:59a7d4677474 | 287 | uint16_t accelsensitivity = 16384; // = 16384 LSB/g |
nightmechanic | 2:59a7d4677474 | 288 | |
nightmechanic | 2:59a7d4677474 | 289 | // Configure FIFO to capture accelerometer and gyro data for bias calculation |
nightmechanic | 2:59a7d4677474 | 290 | writeByte(MPU6050_ADDRESS, USER_CTRL, 0x40); // Enable FIFO |
nightmechanic | 2:59a7d4677474 | 291 | writeByte(MPU6050_ADDRESS, FIFO_EN, 0x78); // Enable gyro and accelerometer sensors for FIFO (max size 1024 bytes in MPU-6050) |
nightmechanic | 2:59a7d4677474 | 292 | wait(0.08); // accumulate 80 samples in 80 milliseconds = 960 bytes |
nightmechanic | 2:59a7d4677474 | 293 | |
nightmechanic | 2:59a7d4677474 | 294 | // At end of sample accumulation, turn off FIFO sensor read |
nightmechanic | 2:59a7d4677474 | 295 | writeByte(MPU6050_ADDRESS, FIFO_EN, 0x00); // Disable gyro and accelerometer sensors for FIFO |
nightmechanic | 2:59a7d4677474 | 296 | readBytes(MPU6050_ADDRESS, FIFO_COUNTH, 2, &data[0]); // read FIFO sample count |
nightmechanic | 2:59a7d4677474 | 297 | fifo_count = ((uint16_t)data[0] << 8) | data[1]; |
nightmechanic | 2:59a7d4677474 | 298 | packet_count = fifo_count/12;// How many sets of full gyro and accelerometer data for averaging |
nightmechanic | 2:59a7d4677474 | 299 | |
nightmechanic | 2:59a7d4677474 | 300 | for (ii = 0; ii < packet_count; ii++) { |
nightmechanic | 2:59a7d4677474 | 301 | int16_t accel_temp[3] = {0, 0, 0}, gyro_temp[3] = {0, 0, 0}; |
nightmechanic | 2:59a7d4677474 | 302 | readBytes(MPU6050_ADDRESS, FIFO_R_W, 12, &data[0]); // read data for averaging |
nightmechanic | 2:59a7d4677474 | 303 | accel_temp[0] = (int16_t) (((int16_t)data[0] << 8) | data[1] ) ; // Form signed 16-bit integer for each sample in FIFO |
nightmechanic | 2:59a7d4677474 | 304 | accel_temp[1] = (int16_t) (((int16_t)data[2] << 8) | data[3] ) ; |
nightmechanic | 2:59a7d4677474 | 305 | accel_temp[2] = (int16_t) (((int16_t)data[4] << 8) | data[5] ) ; |
nightmechanic | 2:59a7d4677474 | 306 | gyro_temp[0] = (int16_t) (((int16_t)data[6] << 8) | data[7] ) ; |
nightmechanic | 2:59a7d4677474 | 307 | gyro_temp[1] = (int16_t) (((int16_t)data[8] << 8) | data[9] ) ; |
nightmechanic | 2:59a7d4677474 | 308 | gyro_temp[2] = (int16_t) (((int16_t)data[10] << 8) | data[11]) ; |
nightmechanic | 2:59a7d4677474 | 309 | |
nightmechanic | 2:59a7d4677474 | 310 | accel_bias[0] += (int32_t) accel_temp[0]; // Sum individual signed 16-bit biases to get accumulated signed 32-bit biases |
nightmechanic | 2:59a7d4677474 | 311 | accel_bias[1] += (int32_t) accel_temp[1]; |
nightmechanic | 2:59a7d4677474 | 312 | accel_bias[2] += (int32_t) accel_temp[2]; |
nightmechanic | 2:59a7d4677474 | 313 | gyro_bias[0] += (int32_t) gyro_temp[0]; |
nightmechanic | 2:59a7d4677474 | 314 | gyro_bias[1] += (int32_t) gyro_temp[1]; |
nightmechanic | 2:59a7d4677474 | 315 | gyro_bias[2] += (int32_t) gyro_temp[2]; |
nightmechanic | 2:59a7d4677474 | 316 | |
nightmechanic | 2:59a7d4677474 | 317 | } |
nightmechanic | 2:59a7d4677474 | 318 | accel_bias[0] /= (int32_t) packet_count; // Normalize sums to get average count biases |
nightmechanic | 2:59a7d4677474 | 319 | accel_bias[1] /= (int32_t) packet_count; |
nightmechanic | 2:59a7d4677474 | 320 | accel_bias[2] /= (int32_t) packet_count; |
nightmechanic | 2:59a7d4677474 | 321 | gyro_bias[0] /= (int32_t) packet_count; |
nightmechanic | 2:59a7d4677474 | 322 | gyro_bias[1] /= (int32_t) packet_count; |
nightmechanic | 2:59a7d4677474 | 323 | gyro_bias[2] /= (int32_t) packet_count; |
nightmechanic | 2:59a7d4677474 | 324 | |
nightmechanic | 2:59a7d4677474 | 325 | if(accel_bias[2] > 0L) {accel_bias[2] -= (int32_t) accelsensitivity;} // Remove gravity from the z-axis accelerometer bias calculation |
nightmechanic | 2:59a7d4677474 | 326 | else {accel_bias[2] += (int32_t) accelsensitivity;} |
nightmechanic | 2:59a7d4677474 | 327 | |
nightmechanic | 2:59a7d4677474 | 328 | // Construct the gyro biases for push to the hardware gyro bias registers, which are reset to zero upon device startup |
nightmechanic | 2:59a7d4677474 | 329 | 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 |
nightmechanic | 2:59a7d4677474 | 330 | data[1] = (-gyro_bias[0]/4) & 0xFF; // Biases are additive, so change sign on calculated average gyro biases |
nightmechanic | 2:59a7d4677474 | 331 | data[2] = (-gyro_bias[1]/4 >> 8) & 0xFF; |
nightmechanic | 2:59a7d4677474 | 332 | data[3] = (-gyro_bias[1]/4) & 0xFF; |
nightmechanic | 2:59a7d4677474 | 333 | data[4] = (-gyro_bias[2]/4 >> 8) & 0xFF; |
nightmechanic | 2:59a7d4677474 | 334 | data[5] = (-gyro_bias[2]/4) & 0xFF; |
nightmechanic | 2:59a7d4677474 | 335 | |
nightmechanic | 2:59a7d4677474 | 336 | // Push gyro biases to hardware registers |
nightmechanic | 2:59a7d4677474 | 337 | writeByte(MPU6050_ADDRESS, XG_OFFS_USRH, data[0]); |
nightmechanic | 2:59a7d4677474 | 338 | writeByte(MPU6050_ADDRESS, XG_OFFS_USRL, data[1]); |
nightmechanic | 2:59a7d4677474 | 339 | writeByte(MPU6050_ADDRESS, YG_OFFS_USRH, data[2]); |
nightmechanic | 2:59a7d4677474 | 340 | writeByte(MPU6050_ADDRESS, YG_OFFS_USRL, data[3]); |
nightmechanic | 2:59a7d4677474 | 341 | writeByte(MPU6050_ADDRESS, ZG_OFFS_USRH, data[4]); |
nightmechanic | 2:59a7d4677474 | 342 | writeByte(MPU6050_ADDRESS, ZG_OFFS_USRL, data[5]); |
nightmechanic | 2:59a7d4677474 | 343 | |
nightmechanic | 2:59a7d4677474 | 344 | dest1[0] = (float) gyro_bias[0]/(float) gyrosensitivity; // construct gyro bias in deg/s for later manual subtraction |
nightmechanic | 2:59a7d4677474 | 345 | dest1[1] = (float) gyro_bias[1]/(float) gyrosensitivity; |
nightmechanic | 2:59a7d4677474 | 346 | dest1[2] = (float) gyro_bias[2]/(float) gyrosensitivity; |
nightmechanic | 2:59a7d4677474 | 347 | |
nightmechanic | 2:59a7d4677474 | 348 | // Construct the accelerometer biases for push to the hardware accelerometer bias registers. These registers contain |
nightmechanic | 2:59a7d4677474 | 349 | // factory trim values which must be added to the calculated accelerometer biases; on boot up these registers will hold |
nightmechanic | 2:59a7d4677474 | 350 | // non-zero values. In addition, bit 0 of the lower byte must be preserved since it is used for temperature |
nightmechanic | 2:59a7d4677474 | 351 | // compensation calculations. Accelerometer bias registers expect bias input as 2048 LSB per g, so that |
nightmechanic | 2:59a7d4677474 | 352 | // the accelerometer biases calculated above must be divided by 8. |
nightmechanic | 2:59a7d4677474 | 353 | |
nightmechanic | 2:59a7d4677474 | 354 | int32_t accel_bias_reg[3] = {0, 0, 0}; // A place to hold the factory accelerometer trim biases |
nightmechanic | 2:59a7d4677474 | 355 | readBytes(MPU6050_ADDRESS, XA_OFFSET_H, 2, &data[0]); // Read factory accelerometer trim values |
nightmechanic | 2:59a7d4677474 | 356 | accel_bias_reg[0] = (int16_t) ((int16_t)data[0] << 8) | data[1]; |
nightmechanic | 2:59a7d4677474 | 357 | readBytes(MPU6050_ADDRESS, YA_OFFSET_H, 2, &data[0]); |
nightmechanic | 2:59a7d4677474 | 358 | accel_bias_reg[1] = (int16_t) ((int16_t)data[0] << 8) | data[1]; |
nightmechanic | 2:59a7d4677474 | 359 | readBytes(MPU6050_ADDRESS, ZA_OFFSET_H, 2, &data[0]); |
nightmechanic | 2:59a7d4677474 | 360 | accel_bias_reg[2] = (int16_t) ((int16_t)data[0] << 8) | data[1]; |
nightmechanic | 2:59a7d4677474 | 361 | |
nightmechanic | 2:59a7d4677474 | 362 | uint32_t mask = 1uL; // Define mask for temperature compensation bit 0 of lower byte of accelerometer bias registers |
nightmechanic | 2:59a7d4677474 | 363 | uint8_t mask_bit[3] = {0, 0, 0}; // Define array to hold mask bit for each accelerometer bias axis |
nightmechanic | 2:59a7d4677474 | 364 | |
nightmechanic | 2:59a7d4677474 | 365 | for(ii = 0; ii < 3; ii++) { |
nightmechanic | 2:59a7d4677474 | 366 | if(accel_bias_reg[ii] & mask) mask_bit[ii] = 0x01; // If temperature compensation bit is set, record that fact in mask_bit |
nightmechanic | 2:59a7d4677474 | 367 | } |
nightmechanic | 2:59a7d4677474 | 368 | |
nightmechanic | 2:59a7d4677474 | 369 | // Construct total accelerometer bias, including calculated average accelerometer bias from above |
nightmechanic | 2:59a7d4677474 | 370 | accel_bias_reg[0] -= (accel_bias[0]/8); // Subtract calculated averaged accelerometer bias scaled to 2048 LSB/g (16 g full scale) |
nightmechanic | 2:59a7d4677474 | 371 | accel_bias_reg[1] -= (accel_bias[1]/8); |
nightmechanic | 2:59a7d4677474 | 372 | accel_bias_reg[2] -= (accel_bias[2]/8); |
nightmechanic | 2:59a7d4677474 | 373 | |
nightmechanic | 2:59a7d4677474 | 374 | data[0] = (accel_bias_reg[0] >> 8) & 0xFF; |
nightmechanic | 2:59a7d4677474 | 375 | data[1] = (accel_bias_reg[0]) & 0xFF; |
nightmechanic | 2:59a7d4677474 | 376 | data[1] = data[1] | mask_bit[0]; // preserve temperature compensation bit when writing back to accelerometer bias registers |
nightmechanic | 2:59a7d4677474 | 377 | data[2] = (accel_bias_reg[1] >> 8) & 0xFF; |
nightmechanic | 2:59a7d4677474 | 378 | data[3] = (accel_bias_reg[1]) & 0xFF; |
nightmechanic | 2:59a7d4677474 | 379 | data[3] = data[3] | mask_bit[1]; // preserve temperature compensation bit when writing back to accelerometer bias registers |
nightmechanic | 2:59a7d4677474 | 380 | data[4] = (accel_bias_reg[2] >> 8) & 0xFF; |
nightmechanic | 2:59a7d4677474 | 381 | data[5] = (accel_bias_reg[2]) & 0xFF; |
nightmechanic | 2:59a7d4677474 | 382 | data[5] = data[5] | mask_bit[2]; // preserve temperature compensation bit when writing back to accelerometer bias registers |
nightmechanic | 2:59a7d4677474 | 383 | |
nightmechanic | 2:59a7d4677474 | 384 | // Push accelerometer biases to hardware registers |
nightmechanic | 2:59a7d4677474 | 385 | // writeByte(MPU6050_ADDRESS, XA_OFFSET_H, data[0]); |
nightmechanic | 2:59a7d4677474 | 386 | // writeByte(MPU6050_ADDRESS, XA_OFFSET_L_TC, data[1]); |
nightmechanic | 2:59a7d4677474 | 387 | // writeByte(MPU6050_ADDRESS, YA_OFFSET_H, data[2]); |
nightmechanic | 2:59a7d4677474 | 388 | // writeByte(MPU6050_ADDRESS, YA_OFFSET_L_TC, data[3]); |
nightmechanic | 2:59a7d4677474 | 389 | // writeByte(MPU6050_ADDRESS, ZA_OFFSET_H, data[4]); |
nightmechanic | 2:59a7d4677474 | 390 | // writeByte(MPU6050_ADDRESS, ZA_OFFSET_L_TC, data[5]); |
nightmechanic | 2:59a7d4677474 | 391 | |
nightmechanic | 2:59a7d4677474 | 392 | // Output scaled accelerometer biases for manual subtraction in the main program |
nightmechanic | 2:59a7d4677474 | 393 | dest2[0] = (float)accel_bias[0]/(float)accelsensitivity; |
nightmechanic | 2:59a7d4677474 | 394 | dest2[1] = (float)accel_bias[1]/(float)accelsensitivity; |
nightmechanic | 2:59a7d4677474 | 395 | dest2[2] = (float)accel_bias[2]/(float)accelsensitivity; |
nightmechanic | 2:59a7d4677474 | 396 | } |
nightmechanic | 2:59a7d4677474 | 397 | |
nightmechanic | 2:59a7d4677474 | 398 | |
nightmechanic | 2:59a7d4677474 | 399 | // Accelerometer and gyroscope self test; check calibration wrt factory settings |
nightmechanic | 2:59a7d4677474 | 400 | void MPU6050::MPU6050SelfTest(float * destination) // Should return percent deviation from factory trim values, +/- 14 or less deviation is a pass |
nightmechanic | 2:59a7d4677474 | 401 | { |
nightmechanic | 2:59a7d4677474 | 402 | uint8_t rawData[4] = {0, 0, 0, 0}; |
nightmechanic | 2:59a7d4677474 | 403 | uint8_t selfTest[6]; |
nightmechanic | 2:59a7d4677474 | 404 | float factoryTrim[6]; |
nightmechanic | 2:59a7d4677474 | 405 | |
nightmechanic | 2:59a7d4677474 | 406 | // Configure the accelerometer for self-test |
nightmechanic | 2:59a7d4677474 | 407 | writeByte(MPU6050_ADDRESS, ACCEL_CONFIG, 0xF0); // Enable self test on all three axes and set accelerometer range to +/- 8 g |
nightmechanic | 2:59a7d4677474 | 408 | writeByte(MPU6050_ADDRESS, GYRO_CONFIG, 0xE0); // Enable self test on all three axes and set gyro range to +/- 250 degrees/s |
nightmechanic | 2:59a7d4677474 | 409 | wait(0.25); // Delay a while to let the device execute the self-test |
nightmechanic | 2:59a7d4677474 | 410 | rawData[0] = readByte(MPU6050_ADDRESS, SELF_TEST_X); // X-axis self-test results |
nightmechanic | 2:59a7d4677474 | 411 | rawData[1] = readByte(MPU6050_ADDRESS, SELF_TEST_Y); // Y-axis self-test results |
nightmechanic | 2:59a7d4677474 | 412 | rawData[2] = readByte(MPU6050_ADDRESS, SELF_TEST_Z); // Z-axis self-test results |
nightmechanic | 2:59a7d4677474 | 413 | rawData[3] = readByte(MPU6050_ADDRESS, SELF_TEST_A); // Mixed-axis self-test results |
nightmechanic | 2:59a7d4677474 | 414 | // Extract the acceleration test results first |
nightmechanic | 2:59a7d4677474 | 415 | selfTest[0] = (rawData[0] >> 3) | (rawData[3] & 0x30) >> 4 ; // XA_TEST result is a five-bit unsigned integer |
nightmechanic | 2:59a7d4677474 | 416 | selfTest[1] = (rawData[1] >> 3) | (rawData[3] & 0x0C) >> 4 ; // YA_TEST result is a five-bit unsigned integer |
nightmechanic | 2:59a7d4677474 | 417 | selfTest[2] = (rawData[2] >> 3) | (rawData[3] & 0x03) >> 4 ; // ZA_TEST result is a five-bit unsigned integer |
nightmechanic | 2:59a7d4677474 | 418 | // Extract the gyration test results first |
nightmechanic | 2:59a7d4677474 | 419 | selfTest[3] = rawData[0] & 0x1F ; // XG_TEST result is a five-bit unsigned integer |
nightmechanic | 2:59a7d4677474 | 420 | selfTest[4] = rawData[1] & 0x1F ; // YG_TEST result is a five-bit unsigned integer |
nightmechanic | 2:59a7d4677474 | 421 | selfTest[5] = rawData[2] & 0x1F ; // ZG_TEST result is a five-bit unsigned integer |
nightmechanic | 2:59a7d4677474 | 422 | // Process results to allow final comparison with factory set values |
nightmechanic | 2:59a7d4677474 | 423 | factoryTrim[0] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[0] - 1.0f)/30.0f))); // FT[Xa] factory trim calculation |
nightmechanic | 2:59a7d4677474 | 424 | factoryTrim[1] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[1] - 1.0f)/30.0f))); // FT[Ya] factory trim calculation |
nightmechanic | 2:59a7d4677474 | 425 | factoryTrim[2] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[2] - 1.0f)/30.0f))); // FT[Za] factory trim calculation |
nightmechanic | 2:59a7d4677474 | 426 | factoryTrim[3] = ( 25.0f*131.0f)*(pow( 1.046f , (selfTest[3] - 1.0f) )); // FT[Xg] factory trim calculation |
nightmechanic | 2:59a7d4677474 | 427 | factoryTrim[4] = (-25.0f*131.0f)*(pow( 1.046f , (selfTest[4] - 1.0f) )); // FT[Yg] factory trim calculation |
nightmechanic | 2:59a7d4677474 | 428 | factoryTrim[5] = ( 25.0f*131.0f)*(pow( 1.046f , (selfTest[5] - 1.0f) )); // FT[Zg] factory trim calculation |
nightmechanic | 2:59a7d4677474 | 429 | |
nightmechanic | 2:59a7d4677474 | 430 | // Output self-test results and factory trim calculation if desired |
nightmechanic | 2:59a7d4677474 | 431 | // Serial.println(selfTest[0]); Serial.println(selfTest[1]); Serial.println(selfTest[2]); |
nightmechanic | 2:59a7d4677474 | 432 | // Serial.println(selfTest[3]); Serial.println(selfTest[4]); Serial.println(selfTest[5]); |
nightmechanic | 2:59a7d4677474 | 433 | // Serial.println(factoryTrim[0]); Serial.println(factoryTrim[1]); Serial.println(factoryTrim[2]); |
nightmechanic | 2:59a7d4677474 | 434 | // Serial.println(factoryTrim[3]); Serial.println(factoryTrim[4]); Serial.println(factoryTrim[5]); |
nightmechanic | 2:59a7d4677474 | 435 | |
nightmechanic | 2:59a7d4677474 | 436 | // Report results as a ratio of (STR - FT)/FT; the change from Factory Trim of the Self-Test Response |
nightmechanic | 2:59a7d4677474 | 437 | // To get to percent, must multiply by 100 and subtract result from 100 |
nightmechanic | 2:59a7d4677474 | 438 | for (int i = 0; i < 6; i++) { |
nightmechanic | 2:59a7d4677474 | 439 | destination[i] = 100.0f + 100.0f*(selfTest[i] - factoryTrim[i])/factoryTrim[i]; // Report percent differences |
nightmechanic | 2:59a7d4677474 | 440 | } |
nightmechanic | 2:59a7d4677474 | 441 | |
nightmechanic | 2:59a7d4677474 | 442 | } |
nightmechanic | 2:59a7d4677474 | 443 | |
nightmechanic | 2:59a7d4677474 | 444 | |
nightmechanic | 2:59a7d4677474 | 445 | // Implementation of Sebastian Madgwick's "...efficient orientation filter for... inertial/magnetic sensor arrays" |
nightmechanic | 2:59a7d4677474 | 446 | // (see http://www.x-io.co.uk/category/open-source/ for examples and more details) |
nightmechanic | 2:59a7d4677474 | 447 | // which fuses acceleration and rotation rate to produce a quaternion-based estimate of relative |
nightmechanic | 2:59a7d4677474 | 448 | // device orientation -- which can be converted to yaw, pitch, and roll. Useful for stabilizing quadcopters, etc. |
nightmechanic | 2:59a7d4677474 | 449 | // The performance of the orientation filter is at least as good as conventional Kalman-based filtering algorithms |
nightmechanic | 2:59a7d4677474 | 450 | // but is much less computationally intensive---it can be performed on a 3.3 V Pro Mini operating at 8 MHz! |
nightmechanic | 2:59a7d4677474 | 451 | void MPU6050::MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz) |
nightmechanic | 2:59a7d4677474 | 452 | { |
nightmechanic | 2:59a7d4677474 | 453 | float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3]; // short name local variable for readability |
nightmechanic | 2:59a7d4677474 | 454 | float norm; // vector norm |
nightmechanic | 2:59a7d4677474 | 455 | float f1, f2, f3; // objective funcyion elements |
nightmechanic | 2:59a7d4677474 | 456 | float J_11or24, J_12or23, J_13or22, J_14or21, J_32, J_33; // objective function Jacobian elements |
nightmechanic | 2:59a7d4677474 | 457 | float qDot1, qDot2, qDot3, qDot4; |
nightmechanic | 2:59a7d4677474 | 458 | float hatDot1, hatDot2, hatDot3, hatDot4; |
nightmechanic | 2:59a7d4677474 | 459 | float gerrx, gerry, gerrz, gbiasx, gbiasy, gbiasz; // gyro bias error |
nightmechanic | 2:59a7d4677474 | 460 | |
nightmechanic | 2:59a7d4677474 | 461 | // Auxiliary variables to avoid repeated arithmetic |
nightmechanic | 2:59a7d4677474 | 462 | float _halfq1 = 0.5f * q1; |
nightmechanic | 2:59a7d4677474 | 463 | float _halfq2 = 0.5f * q2; |
nightmechanic | 2:59a7d4677474 | 464 | float _halfq3 = 0.5f * q3; |
nightmechanic | 2:59a7d4677474 | 465 | float _halfq4 = 0.5f * q4; |
nightmechanic | 2:59a7d4677474 | 466 | float _2q1 = 2.0f * q1; |
nightmechanic | 2:59a7d4677474 | 467 | float _2q2 = 2.0f * q2; |
nightmechanic | 2:59a7d4677474 | 468 | float _2q3 = 2.0f * q3; |
nightmechanic | 2:59a7d4677474 | 469 | float _2q4 = 2.0f * q4; |
nightmechanic | 2:59a7d4677474 | 470 | // float _2q1q3 = 2.0f * q1 * q3; |
nightmechanic | 2:59a7d4677474 | 471 | // float _2q3q4 = 2.0f * q3 * q4; |
nightmechanic | 2:59a7d4677474 | 472 | |
nightmechanic | 2:59a7d4677474 | 473 | // Normalise accelerometer measurement |
nightmechanic | 2:59a7d4677474 | 474 | norm = sqrt(ax * ax + ay * ay + az * az); |
nightmechanic | 2:59a7d4677474 | 475 | if (norm == 0.0f) return; // handle NaN (INF ?) |
nightmechanic | 2:59a7d4677474 | 476 | norm = 1.0f/norm; |
nightmechanic | 2:59a7d4677474 | 477 | ax *= norm; |
nightmechanic | 2:59a7d4677474 | 478 | ay *= norm; |
nightmechanic | 2:59a7d4677474 | 479 | az *= norm; |
nightmechanic | 2:59a7d4677474 | 480 | |
nightmechanic | 2:59a7d4677474 | 481 | // Compute the objective function and Jacobian |
nightmechanic | 2:59a7d4677474 | 482 | f1 = _2q2 * q4 - _2q1 * q3 - ax; |
nightmechanic | 2:59a7d4677474 | 483 | f2 = _2q1 * q2 + _2q3 * q4 - ay; |
nightmechanic | 2:59a7d4677474 | 484 | f3 = 1.0f - _2q2 * q2 - _2q3 * q3 - az; |
nightmechanic | 2:59a7d4677474 | 485 | J_11or24 = _2q3; |
nightmechanic | 2:59a7d4677474 | 486 | J_12or23 = _2q4; |
nightmechanic | 2:59a7d4677474 | 487 | J_13or22 = _2q1; |
nightmechanic | 2:59a7d4677474 | 488 | J_14or21 = _2q2; |
nightmechanic | 2:59a7d4677474 | 489 | J_32 = 2.0f * J_14or21; |
nightmechanic | 2:59a7d4677474 | 490 | J_33 = 2.0f * J_11or24; |
nightmechanic | 2:59a7d4677474 | 491 | |
nightmechanic | 2:59a7d4677474 | 492 | // Compute the gradient (matrix multiplication) |
nightmechanic | 2:59a7d4677474 | 493 | hatDot1 = J_14or21 * f2 - J_11or24 * f1; |
nightmechanic | 2:59a7d4677474 | 494 | hatDot2 = J_12or23 * f1 + J_13or22 * f2 - J_32 * f3; |
nightmechanic | 2:59a7d4677474 | 495 | hatDot3 = J_12or23 * f2 - J_33 *f3 - J_13or22 * f1; |
nightmechanic | 2:59a7d4677474 | 496 | hatDot4 = J_14or21 * f1 + J_11or24 * f2; |
nightmechanic | 2:59a7d4677474 | 497 | |
nightmechanic | 2:59a7d4677474 | 498 | // Normalize the gradient |
nightmechanic | 2:59a7d4677474 | 499 | norm = sqrt(hatDot1 * hatDot1 + hatDot2 * hatDot2 + hatDot3 * hatDot3 + hatDot4 * hatDot4); |
nightmechanic | 2:59a7d4677474 | 500 | if (norm == 0.0f) return; // handle NaN (INF ?) |
nightmechanic | 2:59a7d4677474 | 501 | hatDot1 /= norm; |
nightmechanic | 2:59a7d4677474 | 502 | hatDot2 /= norm; |
nightmechanic | 2:59a7d4677474 | 503 | hatDot3 /= norm; |
nightmechanic | 2:59a7d4677474 | 504 | hatDot4 /= norm; |
nightmechanic | 2:59a7d4677474 | 505 | |
nightmechanic | 2:59a7d4677474 | 506 | // Compute estimated gyroscope biases |
nightmechanic | 2:59a7d4677474 | 507 | gerrx = _2q1 * hatDot2 - _2q2 * hatDot1 - _2q3 * hatDot4 + _2q4 * hatDot3; |
nightmechanic | 2:59a7d4677474 | 508 | gerry = _2q1 * hatDot3 + _2q2 * hatDot4 - _2q3 * hatDot1 - _2q4 * hatDot2; |
nightmechanic | 2:59a7d4677474 | 509 | gerrz = _2q1 * hatDot4 - _2q2 * hatDot3 + _2q3 * hatDot2 - _2q4 * hatDot1; |
nightmechanic | 2:59a7d4677474 | 510 | |
nightmechanic | 2:59a7d4677474 | 511 | // Compute and remove gyroscope biases |
nightmechanic | 2:59a7d4677474 | 512 | gbiasx += gerrx * deltat * zeta; |
nightmechanic | 2:59a7d4677474 | 513 | gbiasy += gerry * deltat * zeta; |
nightmechanic | 2:59a7d4677474 | 514 | gbiasz += gerrz * deltat * zeta; |
nightmechanic | 2:59a7d4677474 | 515 | // gx -= gbiasx; |
nightmechanic | 2:59a7d4677474 | 516 | // gy -= gbiasy; |
nightmechanic | 2:59a7d4677474 | 517 | // gz -= gbiasz; |
nightmechanic | 2:59a7d4677474 | 518 | |
nightmechanic | 2:59a7d4677474 | 519 | // Compute the quaternion derivative |
nightmechanic | 2:59a7d4677474 | 520 | qDot1 = -_halfq2 * gx - _halfq3 * gy - _halfq4 * gz; |
nightmechanic | 2:59a7d4677474 | 521 | qDot2 = _halfq1 * gx + _halfq3 * gz - _halfq4 * gy; |
nightmechanic | 2:59a7d4677474 | 522 | qDot3 = _halfq1 * gy - _halfq2 * gz + _halfq4 * gx; |
nightmechanic | 2:59a7d4677474 | 523 | qDot4 = _halfq1 * gz + _halfq2 * gy - _halfq3 * gx; |
nightmechanic | 2:59a7d4677474 | 524 | |
nightmechanic | 2:59a7d4677474 | 525 | // Compute then integrate estimated quaternion derivative |
nightmechanic | 2:59a7d4677474 | 526 | q1 += (qDot1 -(beta * hatDot1)) * deltat; |
nightmechanic | 2:59a7d4677474 | 527 | q2 += (qDot2 -(beta * hatDot2)) * deltat; |
nightmechanic | 2:59a7d4677474 | 528 | q3 += (qDot3 -(beta * hatDot3)) * deltat; |
nightmechanic | 2:59a7d4677474 | 529 | q4 += (qDot4 -(beta * hatDot4)) * deltat; |
nightmechanic | 2:59a7d4677474 | 530 | |
nightmechanic | 2:59a7d4677474 | 531 | // Normalize the quaternion |
nightmechanic | 2:59a7d4677474 | 532 | norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4); // normalise quaternion |
nightmechanic | 2:59a7d4677474 | 533 | if (norm == 0.0f) return; // handle NaN (INF ?) |
nightmechanic | 2:59a7d4677474 | 534 | norm = 1.0f/norm; |
nightmechanic | 2:59a7d4677474 | 535 | q[0] = q1 * norm; |
nightmechanic | 2:59a7d4677474 | 536 | q[1] = q2 * norm; |
nightmechanic | 2:59a7d4677474 | 537 | q[2] = q3 * norm; |
nightmechanic | 2:59a7d4677474 | 538 | q[3] = q4 * norm; |
nightmechanic | 2:59a7d4677474 | 539 | |
nightmechanic | 2:59a7d4677474 | 540 | } |
nightmechanic | 2:59a7d4677474 | 541 | |
nightmechanic | 2:59a7d4677474 | 542 | bool MPU6050::motion_sensor_init() |
nightmechanic | 2:59a7d4677474 | 543 | { |
nightmechanic | 2:59a7d4677474 | 544 | |
nightmechanic | 2:59a7d4677474 | 545 | |
nightmechanic | 2:59a7d4677474 | 546 | // Read the WHO_AM_I register, this is a good test of communication |
nightmechanic | 2:59a7d4677474 | 547 | uint8_t whoami = readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050 |
nightmechanic | 2:59a7d4677474 | 548 | //serial.printf("I AM 0x%x\n\r", whoami); |
nightmechanic | 2:59a7d4677474 | 549 | //serial.printf("I SHOULD BE 0x68\n\r"); |
nightmechanic | 2:59a7d4677474 | 550 | |
nightmechanic | 2:59a7d4677474 | 551 | if (whoami == 0x68) { // WHO_AM_I should always be 0x68 |
nightmechanic | 2:59a7d4677474 | 552 | // serial.printf("MPU6050 is online..."); |
nightmechanic | 2:59a7d4677474 | 553 | wait(1); |
nightmechanic | 2:59a7d4677474 | 554 | |
nightmechanic | 2:59a7d4677474 | 555 | |
nightmechanic | 2:59a7d4677474 | 556 | MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values |
nightmechanic | 2:59a7d4677474 | 557 | /* |
nightmechanic | 2:59a7d4677474 | 558 | serial.printf("x-axis self test: acceleration trim within : "); |
nightmechanic | 2:59a7d4677474 | 559 | serial.printf("%f", SelfTest[0]); |
nightmechanic | 2:59a7d4677474 | 560 | serial.printf("% of factory value \n\r"); |
nightmechanic | 2:59a7d4677474 | 561 | serial.printf("y-axis self test: acceleration trim within : "); |
nightmechanic | 2:59a7d4677474 | 562 | serial.printf("%f", SelfTest[1]); |
nightmechanic | 2:59a7d4677474 | 563 | serial.printf("% of factory value \n\r"); |
nightmechanic | 2:59a7d4677474 | 564 | serial.printf("z-axis self test: acceleration trim within : "); |
nightmechanic | 2:59a7d4677474 | 565 | serial.printf("%f", SelfTest[2]); |
nightmechanic | 2:59a7d4677474 | 566 | serial.printf("% of factory value \n\r"); |
nightmechanic | 2:59a7d4677474 | 567 | serial.printf("x-axis self test: gyration trim within : "); |
nightmechanic | 2:59a7d4677474 | 568 | serial.printf("%f", SelfTest[3]); |
nightmechanic | 2:59a7d4677474 | 569 | serial.printf("% of factory value \n\r"); |
nightmechanic | 2:59a7d4677474 | 570 | serial.printf("y-axis self test: gyration trim within : "); |
nightmechanic | 2:59a7d4677474 | 571 | serial.printf("%f", SelfTest[4]); |
nightmechanic | 2:59a7d4677474 | 572 | serial.printf("% of factory value \n\r"); |
nightmechanic | 2:59a7d4677474 | 573 | serial.printf("z-axis self test: gyration trim within : "); |
nightmechanic | 2:59a7d4677474 | 574 | serial.printf("%f", SelfTest[5]); |
nightmechanic | 2:59a7d4677474 | 575 | serial.printf("% of factory value \n\r"); |
nightmechanic | 2:59a7d4677474 | 576 | */ |
nightmechanic | 2:59a7d4677474 | 577 | wait(1); |
nightmechanic | 2:59a7d4677474 | 578 | |
nightmechanic | 2:59a7d4677474 | 579 | if(SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f) { |
nightmechanic | 2:59a7d4677474 | 580 | resetMPU6050(); // Reset registers to default in preparation for device calibration |
nightmechanic | 2:59a7d4677474 | 581 | initMPU6050(); |
nightmechanic | 2:59a7d4677474 | 582 | //serial.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature |
nightmechanic | 2:59a7d4677474 | 583 | |
nightmechanic | 2:59a7d4677474 | 584 | return TRUE; |
nightmechanic | 2:59a7d4677474 | 585 | } else { |
nightmechanic | 2:59a7d4677474 | 586 | //serial.printf("Device did not the pass self-test!\n\r"); |
nightmechanic | 2:59a7d4677474 | 587 | return FALSE; |
nightmechanic | 2:59a7d4677474 | 588 | |
nightmechanic | 2:59a7d4677474 | 589 | } |
nightmechanic | 2:59a7d4677474 | 590 | } else { |
nightmechanic | 2:59a7d4677474 | 591 | //serial.printf("Could not connect to MPU6050: \n\r"); |
nightmechanic | 2:59a7d4677474 | 592 | //serial.printf("%#x \n", whoami); |
nightmechanic | 2:59a7d4677474 | 593 | |
nightmechanic | 2:59a7d4677474 | 594 | return FALSE; |
nightmechanic | 2:59a7d4677474 | 595 | } |
nightmechanic | 2:59a7d4677474 | 596 | |
nightmechanic | 2:59a7d4677474 | 597 | |
nightmechanic | 2:59a7d4677474 | 598 | } |
nightmechanic | 2:59a7d4677474 | 599 | |
nightmechanic | 2:59a7d4677474 | 600 | bool MPU6050::motion_update_data(MPU_data_type *new_data, int current_time_us) |
nightmechanic | 2:59a7d4677474 | 601 | { |
nightmechanic | 2:59a7d4677474 | 602 | if(readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { |
nightmechanic | 2:59a7d4677474 | 603 | readAccelData(accelCount); // Read the x/y/z adc values |
nightmechanic | 2:59a7d4677474 | 604 | getAres(); |
nightmechanic | 2:59a7d4677474 | 605 | |
nightmechanic | 2:59a7d4677474 | 606 | // Now we'll calculate the accleration value into actual g's |
nightmechanic | 2:59a7d4677474 | 607 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
nightmechanic | 2:59a7d4677474 | 608 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
nightmechanic | 2:59a7d4677474 | 609 | az = (float)accelCount[2]*aRes - accelBias[2]; |
nightmechanic | 2:59a7d4677474 | 610 | |
nightmechanic | 2:59a7d4677474 | 611 | readGyroData(gyroCount); // Read the x/y/z adc values |
nightmechanic | 2:59a7d4677474 | 612 | getGres(); |
nightmechanic | 2:59a7d4677474 | 613 | |
nightmechanic | 2:59a7d4677474 | 614 | // Calculate the gyro value into actual degrees per second |
nightmechanic | 2:59a7d4677474 | 615 | gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set |
nightmechanic | 2:59a7d4677474 | 616 | gy = (float)gyroCount[1]*gRes; // - gyroBias[1]; |
nightmechanic | 2:59a7d4677474 | 617 | gz = (float)gyroCount[2]*gRes; // - gyroBias[2]; |
nightmechanic | 2:59a7d4677474 | 618 | |
nightmechanic | 2:59a7d4677474 | 619 | tempCount = readTempData(); // Read the x/y/z adc values |
nightmechanic | 2:59a7d4677474 | 620 | temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade |
nightmechanic | 2:59a7d4677474 | 621 | |
nightmechanic | 2:59a7d4677474 | 622 | |
nightmechanic | 2:59a7d4677474 | 623 | Now = current_time_us; |
nightmechanic | 2:59a7d4677474 | 624 | deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update |
nightmechanic | 2:59a7d4677474 | 625 | lastUpdate = Now; |
nightmechanic | 2:59a7d4677474 | 626 | |
nightmechanic | 2:59a7d4677474 | 627 | sum += deltat; |
nightmechanic | 2:59a7d4677474 | 628 | sumCount++; |
nightmechanic | 2:59a7d4677474 | 629 | |
nightmechanic | 2:59a7d4677474 | 630 | if(lastUpdate - firstUpdate > 10000000.0f) { |
nightmechanic | 2:59a7d4677474 | 631 | beta = 0.04; // decrease filter gain after stabilized |
nightmechanic | 2:59a7d4677474 | 632 | zeta = 0.015; // increase bias drift gain after stabilized |
nightmechanic | 2:59a7d4677474 | 633 | } |
nightmechanic | 2:59a7d4677474 | 634 | |
nightmechanic | 2:59a7d4677474 | 635 | // Pass gyro rate as rad/s |
nightmechanic | 2:59a7d4677474 | 636 | MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f); |
nightmechanic | 2:59a7d4677474 | 637 | |
nightmechanic | 2:59a7d4677474 | 638 | |
nightmechanic | 2:59a7d4677474 | 639 | |
nightmechanic | 2:59a7d4677474 | 640 | |
nightmechanic | 2:59a7d4677474 | 641 | // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation. |
nightmechanic | 2:59a7d4677474 | 642 | // In this coordinate system, the positive z-axis is down toward Earth. |
nightmechanic | 2:59a7d4677474 | 643 | // Yaw is the angle between Sensor x-axis and Earth magnetic North (or true North if corrected for local declination, looking down on the sensor positive yaw is counterclockwise. |
nightmechanic | 2:59a7d4677474 | 644 | // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative. |
nightmechanic | 2:59a7d4677474 | 645 | // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll. |
nightmechanic | 2:59a7d4677474 | 646 | // These arise from the definition of the homogeneous rotation matrix constructed from quaternions. |
nightmechanic | 2:59a7d4677474 | 647 | // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be |
nightmechanic | 2:59a7d4677474 | 648 | // applied in the correct order which for this configuration is yaw, pitch, and then roll. |
nightmechanic | 2:59a7d4677474 | 649 | // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links. |
nightmechanic | 2:59a7d4677474 | 650 | yaw = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]); |
nightmechanic | 2:59a7d4677474 | 651 | pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); |
nightmechanic | 2:59a7d4677474 | 652 | roll = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]); |
nightmechanic | 2:59a7d4677474 | 653 | pitch *= 180.0f / PI; |
nightmechanic | 2:59a7d4677474 | 654 | yaw *= 180.0f / PI; |
nightmechanic | 2:59a7d4677474 | 655 | roll *= 180.0f / PI; |
nightmechanic | 2:59a7d4677474 | 656 | /* |
nightmechanic | 2:59a7d4677474 | 657 | new_data->ax = (int) (ax / 16384.0f); |
nightmechanic | 2:59a7d4677474 | 658 | new_data->ay = (int) (ay / 16384.0f); |
nightmechanic | 2:59a7d4677474 | 659 | new_data->az = (int) (az / 16384.0f); |
nightmechanic | 2:59a7d4677474 | 660 | new_data->yaw = (int) (yaw / 16384.0f); |
nightmechanic | 2:59a7d4677474 | 661 | new_data->pitch = (int) (pitch / 16384.0f); |
nightmechanic | 2:59a7d4677474 | 662 | new_data->roll = (int) (roll / 16384.0f); |
nightmechanic | 2:59a7d4677474 | 663 | */ |
nightmechanic | 2:59a7d4677474 | 664 | new_data->ax = (int) (ax * 1000); |
nightmechanic | 2:59a7d4677474 | 665 | new_data->ay = (int) (ay * 1000); |
nightmechanic | 2:59a7d4677474 | 666 | new_data->az = (int) (az * 1000); |
nightmechanic | 2:59a7d4677474 | 667 | new_data->yaw = (int) (yaw * 10); |
nightmechanic | 2:59a7d4677474 | 668 | new_data->pitch = (int) (pitch * 10); |
nightmechanic | 2:59a7d4677474 | 669 | new_data->roll = (int) (roll * 10); |
nightmechanic | 2:59a7d4677474 | 670 | return TRUE; |
nightmechanic | 2:59a7d4677474 | 671 | |
nightmechanic | 2:59a7d4677474 | 672 | } else { |
nightmechanic | 2:59a7d4677474 | 673 | return FALSE; |
nightmechanic | 2:59a7d4677474 | 674 | } |
nightmechanic | 2:59a7d4677474 | 675 | |
nightmechanic | 2:59a7d4677474 | 676 | } |
nightmechanic | 2:59a7d4677474 | 677 | |
nightmechanic | 2:59a7d4677474 | 678 | void MPU6050_set_I2C_freq(int i2c_frequency) |
nightmechanic | 2:59a7d4677474 | 679 | { |
nightmechanic | 2:59a7d4677474 | 680 | MPU_i2c.frequency(i2c_frequency); |
nightmechanic | 2:59a7d4677474 | 681 | } |
nightmechanic | 2:59a7d4677474 | 682 | |
nightmechanic | 2:59a7d4677474 | 683 | |
nightmechanic | 2:59a7d4677474 | 684 | |
nightmechanic | 2:59a7d4677474 | 685 |