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