otamesi

Dependencies:   mbed

Committer:
seangshim
Date:
Sat Feb 16 05:40:24 2019 +0000
Revision:
41:7c537a922510
Parent:
18:2a47ed430cfe
nei

Who changed what in which revision?

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