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

Committer:
nightmechanic
Date:
Sat Mar 26 21:36:24 2016 +0000
Revision:
4:7e4bb0c29d3b
Parent:
2:59a7d4677474
Added source info to the MPU6050 files

Who changed what in which revision?

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