航空研究会 / Mbed 2 deprecated MPU_check

Dependencies:   mbed MPU6050_2

Committer:
taknokolat
Date:
Wed Feb 06 10:54:28 2019 +0000
Revision:
0:eae101ae93c0
a;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
taknokolat 0:eae101ae93c0 1 #include "mbed.h"
taknokolat 0:eae101ae93c0 2 #include "math.h"
taknokolat 0:eae101ae93c0 3 #include "MPU9250.h"
taknokolat 0:eae101ae93c0 4
taknokolat 0:eae101ae93c0 5
taknokolat 0:eae101ae93c0 6 MPU9250::MPU9250(PinName sda, PinName scl, RawSerial* serial_p)
taknokolat 0:eae101ae93c0 7 :
taknokolat 0:eae101ae93c0 8 i2c_p(new I2C(sda,scl)),
taknokolat 0:eae101ae93c0 9 i2c(*i2c_p),
taknokolat 0:eae101ae93c0 10 pc_p(serial_p)
taknokolat 0:eae101ae93c0 11 {
taknokolat 0:eae101ae93c0 12 initializeValue();
taknokolat 0:eae101ae93c0 13 }
taknokolat 0:eae101ae93c0 14
taknokolat 0:eae101ae93c0 15 MPU9250::~MPU9250(){}
taknokolat 0:eae101ae93c0 16
taknokolat 0:eae101ae93c0 17
taknokolat 0:eae101ae93c0 18 /*---------- public function ----------*/
taknokolat 0:eae101ae93c0 19 bool MPU9250::Initialize(void){
taknokolat 0:eae101ae93c0 20 uint8_t whoami;
taknokolat 0:eae101ae93c0 21
taknokolat 0:eae101ae93c0 22 i2c.frequency(400000); // use fast (400 kHz) I2C
taknokolat 0:eae101ae93c0 23 timer.start();
taknokolat 0:eae101ae93c0 24
taknokolat 0:eae101ae93c0 25 whoami = Whoami_MPU9250();
taknokolat 0:eae101ae93c0 26 pc_p->printf("I AM 0x%x\n\r", whoami); pc_p->printf("I SHOULD BE 0x71\n\r");
taknokolat 0:eae101ae93c0 27
taknokolat 0:eae101ae93c0 28 if(whoami == IAM_MPU9250){
taknokolat 0:eae101ae93c0 29 resetMPU9250(); // Reset registers to default in preparation for device calibration
taknokolat 0:eae101ae93c0 30 calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
taknokolat 0:eae101ae93c0 31 wait(1);
taknokolat 0:eae101ae93c0 32
taknokolat 0:eae101ae93c0 33 initMPU9250();
taknokolat 0:eae101ae93c0 34 initAK8963(magCalibration);
taknokolat 0:eae101ae93c0 35
taknokolat 0:eae101ae93c0 36 pc_p->printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale));
taknokolat 0:eae101ae93c0 37 pc_p->printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale));
taknokolat 0:eae101ae93c0 38
taknokolat 0:eae101ae93c0 39 if(Mscale == 0) pc_p->printf("Magnetometer resolution = 14 bits\n\r");
taknokolat 0:eae101ae93c0 40 if(Mscale == 1) pc_p->printf("Magnetometer resolution = 16 bits\n\r");
taknokolat 0:eae101ae93c0 41 if(Mmode == 2) pc_p->printf("Magnetometer ODR = 8 Hz\n\r");
taknokolat 0:eae101ae93c0 42 if(Mmode == 6) pc_p->printf("Magnetometer ODR = 100 Hz\n\r");
taknokolat 0:eae101ae93c0 43
taknokolat 0:eae101ae93c0 44 getAres();
taknokolat 0:eae101ae93c0 45 getGres();
taknokolat 0:eae101ae93c0 46 getMres();
taknokolat 0:eae101ae93c0 47
taknokolat 0:eae101ae93c0 48 pc_p->printf("mpu9250 initialized\r\n");
taknokolat 0:eae101ae93c0 49 return true;
taknokolat 0:eae101ae93c0 50 }else return false;
taknokolat 0:eae101ae93c0 51 }
taknokolat 0:eae101ae93c0 52
taknokolat 0:eae101ae93c0 53 bool MPU9250::sensingAcGyMg(){
taknokolat 0:eae101ae93c0 54 if(readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt
taknokolat 0:eae101ae93c0 55 sensingAccel();
taknokolat 0:eae101ae93c0 56 sensingGyro();
taknokolat 0:eae101ae93c0 57 sensingMag();
taknokolat 0:eae101ae93c0 58 return true;
taknokolat 0:eae101ae93c0 59 }else return false;
taknokolat 0:eae101ae93c0 60 }
taknokolat 0:eae101ae93c0 61
taknokolat 0:eae101ae93c0 62
taknokolat 0:eae101ae93c0 63 void MPU9250::calculatePostureAngle(float degree[3]){
taknokolat 0:eae101ae93c0 64 Now = timer.read_us();
taknokolat 0:eae101ae93c0 65 deltat = (float)((Now - lastUpdate)/1000000.0f); // set integration time by time elapsed since last filter update
taknokolat 0:eae101ae93c0 66 lastUpdate = Now;
taknokolat 0:eae101ae93c0 67
taknokolat 0:eae101ae93c0 68 // if(lastUpdate - firstUpdate > 10000000.0f) {
taknokolat 0:eae101ae93c0 69 // beta = 0.04; // decrease filter gain after stabilized
taknokolat 0:eae101ae93c0 70 // zeta = 0.015; // increasey bias drift gain after stabilized
taknokolat 0:eae101ae93c0 71 // }
taknokolat 0:eae101ae93c0 72
taknokolat 0:eae101ae93c0 73 // Pass gyro rate as rad/s
taknokolat 0:eae101ae93c0 74 MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
taknokolat 0:eae101ae93c0 75 MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); //my, mx, mzになってるけどセンサの設置上の都合だろうか
taknokolat 0:eae101ae93c0 76
taknokolat 0:eae101ae93c0 77 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
taknokolat 0:eae101ae93c0 78 // In this coordinate system, the positive z-axis is down toward Earth.
taknokolat 0:eae101ae93c0 79 // Yaw is the angle between Sensor x-axis and Earth magnetic North (or true North if corrected for local declination, looking down on the sensor positive yaw is counterclockwise.
taknokolat 0:eae101ae93c0 80 // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
taknokolat 0:eae101ae93c0 81 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
taknokolat 0:eae101ae93c0 82 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
taknokolat 0:eae101ae93c0 83 // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
taknokolat 0:eae101ae93c0 84 // applied in the correct order which for this configuration is yaw, pitch, and then roll.
taknokolat 0:eae101ae93c0 85 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
taknokolat 0:eae101ae93c0 86 translateQuaternionToDeg(q);
taknokolat 0:eae101ae93c0 87 calibrateDegree();
taknokolat 0:eae101ae93c0 88 degree[0] = roll;
taknokolat 0:eae101ae93c0 89 degree[1] = pitch;
taknokolat 0:eae101ae93c0 90 degree[2] = yaw;
taknokolat 0:eae101ae93c0 91 }
taknokolat 0:eae101ae93c0 92
taknokolat 0:eae101ae93c0 93
taknokolat 0:eae101ae93c0 94 float MPU9250::calculateYawByMg(){
taknokolat 0:eae101ae93c0 95 transformCoordinateFromCompassToMPU();
taknokolat 0:eae101ae93c0 96 lpmag[0] = LPGAIN_MAG *lpmag[0] + (1 - LPGAIN_MAG)*mx;
taknokolat 0:eae101ae93c0 97 lpmag[1] = LPGAIN_MAG *lpmag[1] + (1 - LPGAIN_MAG)*my;
taknokolat 0:eae101ae93c0 98 lpmag[2] = LPGAIN_MAG *lpmag[2] + (1 - LPGAIN_MAG)*mz;
taknokolat 0:eae101ae93c0 99
taknokolat 0:eae101ae93c0 100 float radroll = PI/180.0f * roll;
taknokolat 0:eae101ae93c0 101 float radpitch = PI/180.0f * pitch;
taknokolat 0:eae101ae93c0 102
taknokolat 0:eae101ae93c0 103 return 180.0f/PI * atan2(lpmag[2]*sin(radpitch) - lpmag[1]*cos(radpitch),
taknokolat 0:eae101ae93c0 104 lpmag[0]*cos(radroll) - lpmag[1]*sin(radroll)*sin(radpitch) + lpmag[2]*sin(radroll)*cos(radpitch));
taknokolat 0:eae101ae93c0 105 }
taknokolat 0:eae101ae93c0 106
taknokolat 0:eae101ae93c0 107
taknokolat 0:eae101ae93c0 108 // Accelerometer and gyroscope self test; check calibration wrt factory settings
taknokolat 0:eae101ae93c0 109 void MPU9250::MPU9250SelfTest(float * destination) // Should return percent deviation from factory trim values, +/- 14 or less deviation is a pass
taknokolat 0:eae101ae93c0 110 {
taknokolat 0:eae101ae93c0 111 uint8_t rawData[6] = {0, 0, 0, 0, 0, 0};
taknokolat 0:eae101ae93c0 112 uint8_t selfTest[6];
taknokolat 0:eae101ae93c0 113 int16_t gAvg[3], aAvg[3], aSTAvg[3], gSTAvg[3];
taknokolat 0:eae101ae93c0 114 float factoryTrim[6];
taknokolat 0:eae101ae93c0 115 uint8_t FS = 0;
taknokolat 0:eae101ae93c0 116
taknokolat 0:eae101ae93c0 117 writeByte(MPU9250_ADDRESS, SMPLRT_DIV, 0x00); // Set gyro sample rate to 1 kHz
taknokolat 0:eae101ae93c0 118 writeByte(MPU9250_ADDRESS, CONFIG, 0x02); // Set gyro sample rate to 1 kHz and DLPF to 92 Hz
taknokolat 0:eae101ae93c0 119 writeByte(MPU9250_ADDRESS, GYRO_CONFIG, 1<<FS); // Set full scale range for the gyro to 250 dps
taknokolat 0:eae101ae93c0 120 writeByte(MPU9250_ADDRESS, ACCEL_CONFIG2, 0x02); // Set accelerometer rate to 1 kHz and bandwidth to 92 Hz
taknokolat 0:eae101ae93c0 121 writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, 1<<FS); // Set full scale range for the accelerometer to 2 g
taknokolat 0:eae101ae93c0 122
taknokolat 0:eae101ae93c0 123 for( int ii = 0; ii < 200; ii++) { // get average current values of gyro and acclerometer
taknokolat 0:eae101ae93c0 124 readBytes(MPU9250_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]); // Read the six raw data registers into data array
taknokolat 0:eae101ae93c0 125 aAvg[0] += (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
taknokolat 0:eae101ae93c0 126 aAvg[1] += (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ;
taknokolat 0:eae101ae93c0 127 aAvg[2] += (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ;
taknokolat 0:eae101ae93c0 128
taknokolat 0:eae101ae93c0 129 readBytes(MPU9250_ADDRESS, GYRO_XOUT_H, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
taknokolat 0:eae101ae93c0 130 gAvg[0] += (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
taknokolat 0:eae101ae93c0 131 gAvg[1] += (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ;
taknokolat 0:eae101ae93c0 132 gAvg[2] += (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ;
taknokolat 0:eae101ae93c0 133 }
taknokolat 0:eae101ae93c0 134
taknokolat 0:eae101ae93c0 135 for (int ii =0; ii < 3; ii++) { // Get average of 200 values and store as average current readings
taknokolat 0:eae101ae93c0 136 aAvg[ii] /= 200;
taknokolat 0:eae101ae93c0 137 gAvg[ii] /= 200;
taknokolat 0:eae101ae93c0 138 }
taknokolat 0:eae101ae93c0 139
taknokolat 0:eae101ae93c0 140 // Configure the accelerometer for self-test
taknokolat 0:eae101ae93c0 141 writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, 0xE0); // Enable self test on all three axes and set accelerometer range to +/- 2 g
taknokolat 0:eae101ae93c0 142 writeByte(MPU9250_ADDRESS, GYRO_CONFIG, 0xE0); // Enable self test on all three axes and set gyro range to +/- 250 degrees/s
taknokolat 0:eae101ae93c0 143 //delay(55); // Delay a while to let the device stabilize
taknokolat 0:eae101ae93c0 144
taknokolat 0:eae101ae93c0 145 for( int ii = 0; ii < 200; ii++) { // get average self-test values of gyro and acclerometer
taknokolat 0:eae101ae93c0 146 readBytes(MPU9250_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]); // Read the six raw data registers into data array
taknokolat 0:eae101ae93c0 147 aSTAvg[0] += (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
taknokolat 0:eae101ae93c0 148 aSTAvg[1] += (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ;
taknokolat 0:eae101ae93c0 149 aSTAvg[2] += (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ;
taknokolat 0:eae101ae93c0 150
taknokolat 0:eae101ae93c0 151 readBytes(MPU9250_ADDRESS, GYRO_XOUT_H, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
taknokolat 0:eae101ae93c0 152 gSTAvg[0] += (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
taknokolat 0:eae101ae93c0 153 gSTAvg[1] += (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ;
taknokolat 0:eae101ae93c0 154 gSTAvg[2] += (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ;
taknokolat 0:eae101ae93c0 155 }
taknokolat 0:eae101ae93c0 156
taknokolat 0:eae101ae93c0 157 for (int ii =0; ii < 3; ii++) { // Get average of 200 values and store as average self-test readings
taknokolat 0:eae101ae93c0 158 aSTAvg[ii] /= 200;
taknokolat 0:eae101ae93c0 159 gSTAvg[ii] /= 200;
taknokolat 0:eae101ae93c0 160 }
taknokolat 0:eae101ae93c0 161
taknokolat 0:eae101ae93c0 162 // Configure the gyro and accelerometer for normal operation
taknokolat 0:eae101ae93c0 163 writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, 0x00);
taknokolat 0:eae101ae93c0 164 writeByte(MPU9250_ADDRESS, GYRO_CONFIG, 0x00);
taknokolat 0:eae101ae93c0 165 //delay(45); // Delay a while to let the device stabilize
taknokolat 0:eae101ae93c0 166
taknokolat 0:eae101ae93c0 167 // Retrieve accelerometer and gyro factory Self-Test Code from USR_Reg
taknokolat 0:eae101ae93c0 168 selfTest[0] = readByte(MPU9250_ADDRESS, SELF_TEST_X_ACCEL); // X-axis accel self-test results
taknokolat 0:eae101ae93c0 169 selfTest[1] = readByte(MPU9250_ADDRESS, SELF_TEST_Y_ACCEL); // Y-axis accel self-test results
taknokolat 0:eae101ae93c0 170 selfTest[2] = readByte(MPU9250_ADDRESS, SELF_TEST_Z_ACCEL); // Z-axis accel self-test results
taknokolat 0:eae101ae93c0 171 selfTest[3] = readByte(MPU9250_ADDRESS, SELF_TEST_X_GYRO); // X-axis gyro self-test results
taknokolat 0:eae101ae93c0 172 selfTest[4] = readByte(MPU9250_ADDRESS, SELF_TEST_Y_GYRO); // Y-axis gyro self-test results
taknokolat 0:eae101ae93c0 173 selfTest[5] = readByte(MPU9250_ADDRESS, SELF_TEST_Z_GYRO); // Z-axis gyro self-test results
taknokolat 0:eae101ae93c0 174
taknokolat 0:eae101ae93c0 175 // Retrieve factory self-test value from self-test code reads
taknokolat 0:eae101ae93c0 176 factoryTrim[0] = (float)(2620/1<<FS)*(pow( 1.01 , ((float)selfTest[0] - 1.0) )); // FT[Xa] factory trim calculation
taknokolat 0:eae101ae93c0 177 factoryTrim[1] = (float)(2620/1<<FS)*(pow( 1.01 , ((float)selfTest[1] - 1.0) )); // FT[Ya] factory trim calculation
taknokolat 0:eae101ae93c0 178 factoryTrim[2] = (float)(2620/1<<FS)*(pow( 1.01 , ((float)selfTest[2] - 1.0) )); // FT[Za] factory trim calculation
taknokolat 0:eae101ae93c0 179 factoryTrim[3] = (float)(2620/1<<FS)*(pow( 1.01 , ((float)selfTest[3] - 1.0) )); // FT[Xg] factory trim calculation
taknokolat 0:eae101ae93c0 180 factoryTrim[4] = (float)(2620/1<<FS)*(pow( 1.01 , ((float)selfTest[4] - 1.0) )); // FT[Yg] factory trim calculation
taknokolat 0:eae101ae93c0 181 factoryTrim[5] = (float)(2620/1<<FS)*(pow( 1.01 , ((float)selfTest[5] - 1.0) )); // FT[Zg] factory trim calculation
taknokolat 0:eae101ae93c0 182
taknokolat 0:eae101ae93c0 183 // Report results as a ratio of (STR - FT)/FT; the change from Factory Trim of the Self-Test Response
taknokolat 0:eae101ae93c0 184 // To get percent, must multiply by 100
taknokolat 0:eae101ae93c0 185 for (int i = 0; i < 3; i++) {
taknokolat 0:eae101ae93c0 186 destination[i] = 100.0*((float)(aSTAvg[i] - aAvg[i]))/factoryTrim[i]; // Report percent differences
taknokolat 0:eae101ae93c0 187 destination[i+3] = 100.0*((float)(gSTAvg[i] - gAvg[i]))/factoryTrim[i+3]; // Report percent differences
taknokolat 0:eae101ae93c0 188 }
taknokolat 0:eae101ae93c0 189 }
taknokolat 0:eae101ae93c0 190
taknokolat 0:eae101ae93c0 191 void MPU9250::pickupAccel(float accel[3]){
taknokolat 0:eae101ae93c0 192 sensingAccel();
taknokolat 0:eae101ae93c0 193 accel[0] = ax;
taknokolat 0:eae101ae93c0 194 accel[1] = ay;
taknokolat 0:eae101ae93c0 195 accel[2] = az;
taknokolat 0:eae101ae93c0 196 }
taknokolat 0:eae101ae93c0 197
taknokolat 0:eae101ae93c0 198 void MPU9250::pickupGyro(float gyro[3]){
taknokolat 0:eae101ae93c0 199 sensingGyro();
taknokolat 0:eae101ae93c0 200 gyro[0] = gx;
taknokolat 0:eae101ae93c0 201 gyro[1] = gy;
taknokolat 0:eae101ae93c0 202 gyro[2] = gz;
taknokolat 0:eae101ae93c0 203 }
taknokolat 0:eae101ae93c0 204
taknokolat 0:eae101ae93c0 205 void MPU9250::pickupMag(float mag[3]){
taknokolat 0:eae101ae93c0 206 sensingMag();
taknokolat 0:eae101ae93c0 207 mag[0] = mx;
taknokolat 0:eae101ae93c0 208 mag[1] = my;
taknokolat 0:eae101ae93c0 209 mag[2] = mz;
taknokolat 0:eae101ae93c0 210 }
taknokolat 0:eae101ae93c0 211
taknokolat 0:eae101ae93c0 212 float MPU9250::pickupTemp(void){
taknokolat 0:eae101ae93c0 213 sensingTemp();
taknokolat 0:eae101ae93c0 214 return temperature;
taknokolat 0:eae101ae93c0 215 }
taknokolat 0:eae101ae93c0 216
taknokolat 0:eae101ae93c0 217 void MPU9250::displayAccel(void){
taknokolat 0:eae101ae93c0 218 pc_p->printf("ax = %f", 1000*ax);
taknokolat 0:eae101ae93c0 219 pc_p->printf(" ay = %f", 1000*ay);
taknokolat 0:eae101ae93c0 220 pc_p->printf(" az = %f mg\n\r", 1000*az);
taknokolat 0:eae101ae93c0 221 }
taknokolat 0:eae101ae93c0 222
taknokolat 0:eae101ae93c0 223 void MPU9250::displayGyro(void){
taknokolat 0:eae101ae93c0 224 pc_p->printf("gx = %f", gx);
taknokolat 0:eae101ae93c0 225 pc_p->printf(" gy = %f", gy);
taknokolat 0:eae101ae93c0 226 pc_p->printf(" gz = %f deg/s\n\r", gz);
taknokolat 0:eae101ae93c0 227 }
taknokolat 0:eae101ae93c0 228
taknokolat 0:eae101ae93c0 229 void MPU9250::displayMag(void){
taknokolat 0:eae101ae93c0 230 pc_p->printf("mx = %f,", mx);
taknokolat 0:eae101ae93c0 231 pc_p->printf(" my = %f,", my);
taknokolat 0:eae101ae93c0 232 pc_p->printf(" mz = %f mG\n\r", mz);
taknokolat 0:eae101ae93c0 233 }
taknokolat 0:eae101ae93c0 234
taknokolat 0:eae101ae93c0 235 void MPU9250::displayQuaternion(void){
taknokolat 0:eae101ae93c0 236 pc_p->printf("q0 = %f\n\r", q[0]);
taknokolat 0:eae101ae93c0 237 pc_p->printf("q1 = %f\n\r", q[1]);
taknokolat 0:eae101ae93c0 238 pc_p->printf("q2 = %f\n\r", q[2]);
taknokolat 0:eae101ae93c0 239 pc_p->printf("q3 = %f\n\r", q[3]);
taknokolat 0:eae101ae93c0 240 }
taknokolat 0:eae101ae93c0 241
taknokolat 0:eae101ae93c0 242 void MPU9250::displayAngle(void){
taknokolat 0:eae101ae93c0 243 //pc_p->printf("$%d %d %d;",(int)(yaw*100),(int)(pitch*100),(int)(roll*100));
taknokolat 0:eae101ae93c0 244 pc_p->printf("Roll: %f\tPitch: %f\tYaw: %f\n\r", roll, pitch, yaw);
taknokolat 0:eae101ae93c0 245 }
taknokolat 0:eae101ae93c0 246
taknokolat 0:eae101ae93c0 247 void MPU9250::displayTemperature(void){
taknokolat 0:eae101ae93c0 248 pc_p->printf(" temperature = %f C\n\r", temperature);
taknokolat 0:eae101ae93c0 249 }
taknokolat 0:eae101ae93c0 250
taknokolat 0:eae101ae93c0 251 void MPU9250::setMagBias(float bias_x, float bias_y, float bias_z){
taknokolat 0:eae101ae93c0 252 magbias[0] = bias_x;
taknokolat 0:eae101ae93c0 253 magbias[1] = bias_y;
taknokolat 0:eae101ae93c0 254 magbias[2] = bias_z;
taknokolat 0:eae101ae93c0 255 }
taknokolat 0:eae101ae93c0 256
taknokolat 0:eae101ae93c0 257 /*---------- private function ----------*/
taknokolat 0:eae101ae93c0 258
taknokolat 0:eae101ae93c0 259 void MPU9250::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
taknokolat 0:eae101ae93c0 260 {
taknokolat 0:eae101ae93c0 261 char data_write[2];
taknokolat 0:eae101ae93c0 262
taknokolat 0:eae101ae93c0 263 data_write[0] = subAddress;
taknokolat 0:eae101ae93c0 264 data_write[1] = data;
taknokolat 0:eae101ae93c0 265 i2c.write(address, data_write, 2, 0);
taknokolat 0:eae101ae93c0 266 }
taknokolat 0:eae101ae93c0 267
taknokolat 0:eae101ae93c0 268 char MPU9250::readByte(uint8_t address, uint8_t subAddress)
taknokolat 0:eae101ae93c0 269 {
taknokolat 0:eae101ae93c0 270 char data[1]; // `data` will store the register data
taknokolat 0:eae101ae93c0 271 char data_write[1];
taknokolat 0:eae101ae93c0 272
taknokolat 0:eae101ae93c0 273 data_write[0] = subAddress;
taknokolat 0:eae101ae93c0 274 i2c.write(address, data_write, 1, 1); // no stop
taknokolat 0:eae101ae93c0 275 i2c.read(address, data, 1, 0);
taknokolat 0:eae101ae93c0 276 return data[0];
taknokolat 0:eae101ae93c0 277 }
taknokolat 0:eae101ae93c0 278
taknokolat 0:eae101ae93c0 279 void MPU9250::readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest)
taknokolat 0:eae101ae93c0 280 {
taknokolat 0:eae101ae93c0 281 char data[14];
taknokolat 0:eae101ae93c0 282 char data_write[1];
taknokolat 0:eae101ae93c0 283
taknokolat 0:eae101ae93c0 284 data_write[0] = subAddress;
taknokolat 0:eae101ae93c0 285 i2c.write(address, data_write, 1, 1); // no stop
taknokolat 0:eae101ae93c0 286 i2c.read(address, data, count, 0);
taknokolat 0:eae101ae93c0 287 for(int ii = 0; ii < count; ii++) {
taknokolat 0:eae101ae93c0 288 dest[ii] = data[ii];
taknokolat 0:eae101ae93c0 289 }
taknokolat 0:eae101ae93c0 290 }
taknokolat 0:eae101ae93c0 291
taknokolat 0:eae101ae93c0 292 void MPU9250::initializeValue(void){
taknokolat 0:eae101ae93c0 293 Ascale = AFS_2G; // AFS_2G, AFS_4G, AFS_8G, AFS_16G
taknokolat 0:eae101ae93c0 294 Gscale = GFS_250DPS; // GFS_250DPS, GFS_500DPS, GFS_1000DPS, GFS_2000DPS
taknokolat 0:eae101ae93c0 295 Mscale = MFS_16BITS; // MFS_14BITS or MFS_16BITS, 14-bit or 16-bit magnetometer resolution
taknokolat 0:eae101ae93c0 296 Mmode = 0x06; // Either 8 Hz 0x02) or 100 Hz (0x06) magnetometer data ODR
taknokolat 0:eae101ae93c0 297
taknokolat 0:eae101ae93c0 298 GyroMeasError = PI * (60.0f / 180.0f);
taknokolat 0:eae101ae93c0 299 beta = sqrt(3.0f / 4.0f) * GyroMeasError; // compute beta
taknokolat 0:eae101ae93c0 300 GyroMeasDrift = PI * (1.0f / 180.0f); // gyroscope measurement drift in rad/s/s (start at 0.0 deg/s/s)
taknokolat 0:eae101ae93c0 301 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
taknokolat 0:eae101ae93c0 302
taknokolat 0:eae101ae93c0 303 deltat = 0.0f; // integration interval for both filter schemes
taknokolat 0:eae101ae93c0 304 lastUpdate = 0, firstUpdate = 0, Now = 0; // used to calculate integration interval // used to calculate integration interval
taknokolat 0:eae101ae93c0 305
taknokolat 0:eae101ae93c0 306 for(int i=0; i<3; i++){
taknokolat 0:eae101ae93c0 307 magCalibration[i] = 0;
taknokolat 0:eae101ae93c0 308 gyroBias[i] = 0;
taknokolat 0:eae101ae93c0 309 accelBias[i] = 0;
taknokolat 0:eae101ae93c0 310 magbias[i] = 0;
taknokolat 0:eae101ae93c0 311
taknokolat 0:eae101ae93c0 312 eInt[i] = 0.0f;
taknokolat 0:eae101ae93c0 313
taknokolat 0:eae101ae93c0 314 lpmag[i] = 0.0f;
taknokolat 0:eae101ae93c0 315 }
taknokolat 0:eae101ae93c0 316
taknokolat 0:eae101ae93c0 317 q[0] = 1.0f;
taknokolat 0:eae101ae93c0 318 q[1] = 0.0f;
taknokolat 0:eae101ae93c0 319 q[2] = 0.0f;
taknokolat 0:eae101ae93c0 320 q[3] = 0.0f;
taknokolat 0:eae101ae93c0 321 }
taknokolat 0:eae101ae93c0 322
taknokolat 0:eae101ae93c0 323 void MPU9250::initMPU9250(void)
taknokolat 0:eae101ae93c0 324 {
taknokolat 0:eae101ae93c0 325 // Initialize MPU9250 device
taknokolat 0:eae101ae93c0 326 // wake up device
taknokolat 0:eae101ae93c0 327 writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x00); // Clear sleep mode bit (6), enable all sensors
taknokolat 0:eae101ae93c0 328 wait(0.1); // Delay 100 ms for PLL to get established on x-axis gyro; should check for PLL ready interrupt
taknokolat 0:eae101ae93c0 329
taknokolat 0:eae101ae93c0 330 // get stable time source
taknokolat 0:eae101ae93c0 331 writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x01); // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001
taknokolat 0:eae101ae93c0 332
taknokolat 0:eae101ae93c0 333 // Configure Gyro and Accelerometer
taknokolat 0:eae101ae93c0 334 // Disable FSYNC and set accelerometer and gyro bandwidth to 44 and 42 Hz, respectively;
taknokolat 0:eae101ae93c0 335 // DLPF_CFG = bits 2:0 = 010; this sets the sample rate at 1 kHz for both
taknokolat 0:eae101ae93c0 336 // Maximum delay is 4.9 ms which is just over a 200 Hz maximum rate
taknokolat 0:eae101ae93c0 337 writeByte(MPU9250_ADDRESS, CONFIG, 0x03);
taknokolat 0:eae101ae93c0 338
taknokolat 0:eae101ae93c0 339 // Set sample rate = gyroscope output rate/(1 + SMPLRT_DIV)
taknokolat 0:eae101ae93c0 340 writeByte(MPU9250_ADDRESS, SMPLRT_DIV, 0x04); // Use a 200 Hz rate; the same rate set in CONFIG above
taknokolat 0:eae101ae93c0 341
taknokolat 0:eae101ae93c0 342 // Set gyroscope full scale range
taknokolat 0:eae101ae93c0 343 // Range selects FS_SEL and AFS_SEL are 0 - 3, so 2-bit values are left-shifted into positions 4:3
taknokolat 0:eae101ae93c0 344 uint8_t c = readByte(MPU9250_ADDRESS, GYRO_CONFIG); // get current GYRO_CONFIG register value
taknokolat 0:eae101ae93c0 345 // c = c & ~0xE0; // Clear self-test bits [7:5]
taknokolat 0:eae101ae93c0 346 c = c & ~0x02; // Clear Fchoice bits [1:0]
taknokolat 0:eae101ae93c0 347 c = c & ~0x18; // Clear AFS bits [4:3]
taknokolat 0:eae101ae93c0 348 c = c | Gscale << 3; // Set full scale range for the gyro
taknokolat 0:eae101ae93c0 349 // c =| 0x00; // Set Fchoice for the gyro to 11 by writing its inverse to bits 1:0 of GYRO_CONFIG
taknokolat 0:eae101ae93c0 350 writeByte(MPU9250_ADDRESS, GYRO_CONFIG, c ); // Write new GYRO_CONFIG value to register
taknokolat 0:eae101ae93c0 351
taknokolat 0:eae101ae93c0 352 // Set accelerometer full-scale range configuration
taknokolat 0:eae101ae93c0 353 c = readByte(MPU9250_ADDRESS, ACCEL_CONFIG); // get current ACCEL_CONFIG register value
taknokolat 0:eae101ae93c0 354 // c = c & ~0xE0; // Clear self-test bits [7:5]
taknokolat 0:eae101ae93c0 355 c = c & ~0x18; // Clear AFS bits [4:3]
taknokolat 0:eae101ae93c0 356 c = c | Ascale << 3; // Set full scale range for the accelerometer
taknokolat 0:eae101ae93c0 357 writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, c); // Write new ACCEL_CONFIG register value
taknokolat 0:eae101ae93c0 358
taknokolat 0:eae101ae93c0 359 // Set accelerometer sample rate configuration
taknokolat 0:eae101ae93c0 360 // It is possible to get a 4 kHz sample rate from the accelerometer by choosing 1 for
taknokolat 0:eae101ae93c0 361 // accel_fchoice_b bit [3]; in this case the bandwidth is 1.13 kHz
taknokolat 0:eae101ae93c0 362 c = readByte(MPU9250_ADDRESS, ACCEL_CONFIG2); // get current ACCEL_CONFIG2 register value
taknokolat 0:eae101ae93c0 363 c = c & ~0x0F; // Clear accel_fchoice_b (bit 3) and A_DLPFG (bits [2:0])
taknokolat 0:eae101ae93c0 364 c = c | 0x03; // Set accelerometer rate to 1 kHz and bandwidth to 41 Hz
taknokolat 0:eae101ae93c0 365 writeByte(MPU9250_ADDRESS, ACCEL_CONFIG2, c); // Write new ACCEL_CONFIG2 register value
taknokolat 0:eae101ae93c0 366
taknokolat 0:eae101ae93c0 367 // The accelerometer, gyro, and thermometer are set to 1 kHz sample rates,
taknokolat 0:eae101ae93c0 368 // but all these rates are further reduced by a factor of 5 to 200 Hz because of the SMPLRT_DIV setting
taknokolat 0:eae101ae93c0 369
taknokolat 0:eae101ae93c0 370 // Configure Interrupts and Bypass Enable
taknokolat 0:eae101ae93c0 371 // Set interrupt pin active high, push-pull, and clear on read of INT_STATUS, enable I2C_BYPASS_EN so additional chips
taknokolat 0:eae101ae93c0 372 // can join the I2C bus and all can be controlled by the Arduino as master
taknokolat 0:eae101ae93c0 373 writeByte(MPU9250_ADDRESS, INT_PIN_CFG, 0x22);
taknokolat 0:eae101ae93c0 374 writeByte(MPU9250_ADDRESS, INT_ENABLE, 0x01); // Enable data ready (bit 0) interrupt
taknokolat 0:eae101ae93c0 375 }
taknokolat 0:eae101ae93c0 376
taknokolat 0:eae101ae93c0 377 void MPU9250::initAK8963(float * destination)
taknokolat 0:eae101ae93c0 378 {
taknokolat 0:eae101ae93c0 379 // First extract the factory calibration for each magnetometer axis
taknokolat 0:eae101ae93c0 380 uint8_t rawData[3]; // x/y/z gyro calibration data stored here
taknokolat 0:eae101ae93c0 381
taknokolat 0:eae101ae93c0 382 writeByte(AK8963_ADDRESS, AK8963_CNTL, 0x00); // Power down magnetometer
taknokolat 0:eae101ae93c0 383 wait(0.01);
taknokolat 0:eae101ae93c0 384
taknokolat 0:eae101ae93c0 385 writeByte(AK8963_ADDRESS, AK8963_CNTL, 0x0F); // Enter Fuse ROM access mode
taknokolat 0:eae101ae93c0 386 wait(0.01);
taknokolat 0:eae101ae93c0 387
taknokolat 0:eae101ae93c0 388 readBytes(AK8963_ADDRESS, AK8963_ASAX, 3, &rawData[0]); // Read the x-, y-, and z-axis calibration values
taknokolat 0:eae101ae93c0 389 destination[0] = (float)(rawData[0] - 128)/256.0f + 1.0f; // Return x-axis sensitivity adjustment values, etc.
taknokolat 0:eae101ae93c0 390 destination[1] = (float)(rawData[1] - 128)/256.0f + 1.0f;
taknokolat 0:eae101ae93c0 391 destination[2] = (float)(rawData[2] - 128)/256.0f + 1.0f;
taknokolat 0:eae101ae93c0 392 writeByte(AK8963_ADDRESS, AK8963_CNTL, 0x00); // Power down magnetometer
taknokolat 0:eae101ae93c0 393 wait(0.01);
taknokolat 0:eae101ae93c0 394
taknokolat 0:eae101ae93c0 395 // Configure the magnetometer for continuous read and highest resolution
taknokolat 0:eae101ae93c0 396 // set Mscale bit 4 to 1 (0) to enable 16 (14) bit resolution in CNTL register,
taknokolat 0:eae101ae93c0 397 // and enable continuous mode data acquisition Mmode (bits [3:0]), 0010 for 8 Hz and 0110 for 100 Hz sample rates
taknokolat 0:eae101ae93c0 398 writeByte(AK8963_ADDRESS, AK8963_CNTL, Mscale << 4 | Mmode); // Set magnetometer data resolution and sample ODR
taknokolat 0:eae101ae93c0 399 wait(0.01);
taknokolat 0:eae101ae93c0 400 }
taknokolat 0:eae101ae93c0 401
taknokolat 0:eae101ae93c0 402 void MPU9250::resetMPU9250(void)
taknokolat 0:eae101ae93c0 403 {
taknokolat 0:eae101ae93c0 404 // reset device
taknokolat 0:eae101ae93c0 405 writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device
taknokolat 0:eae101ae93c0 406 wait(0.1);
taknokolat 0:eae101ae93c0 407 }
taknokolat 0:eae101ae93c0 408
taknokolat 0:eae101ae93c0 409 // Function which accumulates gyro and accelerometer data after device initialization. It calculates the average
taknokolat 0:eae101ae93c0 410 // of the at-rest readings and then loads the resulting offsets into accelerometer and gyro bias registers.
taknokolat 0:eae101ae93c0 411 void MPU9250::calibrateMPU9250(float * dest1, float * dest2)
taknokolat 0:eae101ae93c0 412 {
taknokolat 0:eae101ae93c0 413 uint8_t data[12]; // data array to hold accelerometer and gyro x, y, z, data
taknokolat 0:eae101ae93c0 414 uint16_t ii, packet_count, fifo_count;
taknokolat 0:eae101ae93c0 415 int32_t gyro_bias[3] = {0, 0, 0}, accel_bias[3] = {0, 0, 0};
taknokolat 0:eae101ae93c0 416 int32_t accel_bias_reg[3] = {0, 0, 0}; // A place to hold the factory accelerometer trim biases
taknokolat 0:eae101ae93c0 417
taknokolat 0:eae101ae93c0 418 // reset device, reset all registers, clear gyro and accelerometer bias registers
taknokolat 0:eae101ae93c0 419 writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device
taknokolat 0:eae101ae93c0 420 wait(0.1);
taknokolat 0:eae101ae93c0 421
taknokolat 0:eae101ae93c0 422 // get stable time source
taknokolat 0:eae101ae93c0 423 // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001
taknokolat 0:eae101ae93c0 424 writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x01);
taknokolat 0:eae101ae93c0 425 writeByte(MPU9250_ADDRESS, PWR_MGMT_2, 0x00);
taknokolat 0:eae101ae93c0 426 wait(0.2);
taknokolat 0:eae101ae93c0 427
taknokolat 0:eae101ae93c0 428 // Configure device for bias calculation
taknokolat 0:eae101ae93c0 429 writeByte(MPU9250_ADDRESS, INT_ENABLE, 0x00); // Disable all interrupts
taknokolat 0:eae101ae93c0 430 writeByte(MPU9250_ADDRESS, FIFO_EN, 0x00); // Disable FIFO
taknokolat 0:eae101ae93c0 431 writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x00); // Turn on internal clock source
taknokolat 0:eae101ae93c0 432 writeByte(MPU9250_ADDRESS, I2C_MST_CTRL, 0x00); // Disable I2C master
taknokolat 0:eae101ae93c0 433 writeByte(MPU9250_ADDRESS, USER_CTRL, 0x00); // Disable FIFO and I2C master modes
taknokolat 0:eae101ae93c0 434 writeByte(MPU9250_ADDRESS, USER_CTRL, 0x0C); // Reset FIFO and DMP
taknokolat 0:eae101ae93c0 435 wait(0.015);
taknokolat 0:eae101ae93c0 436
taknokolat 0:eae101ae93c0 437 // Configure MPU9250 gyro and accelerometer for bias calculation
taknokolat 0:eae101ae93c0 438 writeByte(MPU9250_ADDRESS, CONFIG, 0x01); // Set low-pass filter to 188 Hz
taknokolat 0:eae101ae93c0 439 writeByte(MPU9250_ADDRESS, SMPLRT_DIV, 0x00); // Set sample rate to 1 kHz
taknokolat 0:eae101ae93c0 440 writeByte(MPU9250_ADDRESS, GYRO_CONFIG, 0x00); // Set gyro full-scale to 250 degrees per second, maximum sensitivity
taknokolat 0:eae101ae93c0 441 writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, 0x00); // Set accelerometer full-scale to 2 g, maximum sensitivity
taknokolat 0:eae101ae93c0 442
taknokolat 0:eae101ae93c0 443 uint16_t gyrosensitivity = 131; // = 131 LSB/degrees/sec
taknokolat 0:eae101ae93c0 444 uint16_t accelsensitivity = 16384; // = 16384 LSB/g
taknokolat 0:eae101ae93c0 445
taknokolat 0:eae101ae93c0 446 // Configure FIFO to capture accelerometer and gyro data for bias calculation
taknokolat 0:eae101ae93c0 447 writeByte(MPU9250_ADDRESS, USER_CTRL, 0x40); // Enable FIFO
taknokolat 0:eae101ae93c0 448 writeByte(MPU9250_ADDRESS, FIFO_EN, 0x78); // Enable gyro and accelerometer sensors for FIFO (max size 512 bytes in MPU-9250)
taknokolat 0:eae101ae93c0 449 wait(0.04); // accumulate 40 samples in 80 milliseconds = 480 bytes
taknokolat 0:eae101ae93c0 450
taknokolat 0:eae101ae93c0 451 // At end of sample accumulation, turn off FIFO sensor read
taknokolat 0:eae101ae93c0 452 writeByte(MPU9250_ADDRESS, FIFO_EN, 0x00); // Disable gyro and accelerometer sensors for FIFO
taknokolat 0:eae101ae93c0 453 readBytes(MPU9250_ADDRESS, FIFO_COUNTH, 2, &data[0]); // read FIFO sample count
taknokolat 0:eae101ae93c0 454 fifo_count = ((uint16_t)data[0] << 8) | data[1];
taknokolat 0:eae101ae93c0 455 packet_count = fifo_count/12;// How many sets of full gyro and accelerometer data for averaging
taknokolat 0:eae101ae93c0 456
taknokolat 0:eae101ae93c0 457 for (ii = 0; ii < packet_count; ii++) {
taknokolat 0:eae101ae93c0 458 int16_t accel_temp[3] = {0, 0, 0}, gyro_temp[3] = {0, 0, 0};
taknokolat 0:eae101ae93c0 459 readBytes(MPU9250_ADDRESS, FIFO_R_W, 12, &data[0]); // read data for averaging
taknokolat 0:eae101ae93c0 460 accel_temp[0] = (int16_t) (((int16_t)data[0] << 8) | data[1] ) ; // Form signed 16-bit integer for each sample in FIFO
taknokolat 0:eae101ae93c0 461 accel_temp[1] = (int16_t) (((int16_t)data[2] << 8) | data[3] ) ;
taknokolat 0:eae101ae93c0 462 accel_temp[2] = (int16_t) (((int16_t)data[4] << 8) | data[5] ) ;
taknokolat 0:eae101ae93c0 463 gyro_temp[0] = (int16_t) (((int16_t)data[6] << 8) | data[7] ) ;
taknokolat 0:eae101ae93c0 464 gyro_temp[1] = (int16_t) (((int16_t)data[8] << 8) | data[9] ) ;
taknokolat 0:eae101ae93c0 465 gyro_temp[2] = (int16_t) (((int16_t)data[10] << 8) | data[11]) ;
taknokolat 0:eae101ae93c0 466
taknokolat 0:eae101ae93c0 467 accel_bias[0] += (int32_t) accel_temp[0]; // Sum individual signed 16-bit biases to get accumulated signed 32-bit biases
taknokolat 0:eae101ae93c0 468 accel_bias[1] += (int32_t) accel_temp[1];
taknokolat 0:eae101ae93c0 469 accel_bias[2] += (int32_t) accel_temp[2];
taknokolat 0:eae101ae93c0 470 gyro_bias[0] += (int32_t) gyro_temp[0];
taknokolat 0:eae101ae93c0 471 gyro_bias[1] += (int32_t) gyro_temp[1];
taknokolat 0:eae101ae93c0 472 gyro_bias[2] += (int32_t) gyro_temp[2];
taknokolat 0:eae101ae93c0 473
taknokolat 0:eae101ae93c0 474 }
taknokolat 0:eae101ae93c0 475 accel_bias[0] /= (int32_t) packet_count; // Normalize sums to get average count biases
taknokolat 0:eae101ae93c0 476 accel_bias[1] /= (int32_t) packet_count;
taknokolat 0:eae101ae93c0 477 accel_bias[2] /= (int32_t) packet_count;
taknokolat 0:eae101ae93c0 478 gyro_bias[0] /= (int32_t) packet_count;
taknokolat 0:eae101ae93c0 479 gyro_bias[1] /= (int32_t) packet_count;
taknokolat 0:eae101ae93c0 480 gyro_bias[2] /= (int32_t) packet_count;
taknokolat 0:eae101ae93c0 481
taknokolat 0:eae101ae93c0 482 if(accel_bias[2] > 0L) {accel_bias[2] -= (int32_t) accelsensitivity;} // Remove gravity from the z-axis accelerometer bias calculation
taknokolat 0:eae101ae93c0 483 else {accel_bias[2] += (int32_t) accelsensitivity;}
taknokolat 0:eae101ae93c0 484
taknokolat 0:eae101ae93c0 485 // Construct the gyro biases for push to the hardware gyro bias registers, which are reset to zero upon device startup
taknokolat 0:eae101ae93c0 486 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
taknokolat 0:eae101ae93c0 487 data[1] = (-gyro_bias[0]/4) & 0xFF; // Biases are additive, so change sign on calculated average gyro biases
taknokolat 0:eae101ae93c0 488 data[2] = (-gyro_bias[1]/4 >> 8) & 0xFF;
taknokolat 0:eae101ae93c0 489 data[3] = (-gyro_bias[1]/4) & 0xFF;
taknokolat 0:eae101ae93c0 490 data[4] = (-gyro_bias[2]/4 >> 8) & 0xFF;
taknokolat 0:eae101ae93c0 491 data[5] = (-gyro_bias[2]/4) & 0xFF;
taknokolat 0:eae101ae93c0 492
taknokolat 0:eae101ae93c0 493 /// Push gyro biases to hardware registers
taknokolat 0:eae101ae93c0 494 /*
taknokolat 0:eae101ae93c0 495 writeByte(MPU9250_ADDRESS, XG_OFFSET_H, data[0]);
taknokolat 0:eae101ae93c0 496 writeByte(MPU9250_ADDRESS, XG_OFFSET_L, data[1]);
taknokolat 0:eae101ae93c0 497 writeByte(MPU9250_ADDRESS, YG_OFFSET_H, data[2]);
taknokolat 0:eae101ae93c0 498 writeByte(MPU9250_ADDRESS, YG_OFFSET_L, data[3]);
taknokolat 0:eae101ae93c0 499 writeByte(MPU9250_ADDRESS, ZG_OFFSET_H, data[4]);
taknokolat 0:eae101ae93c0 500 writeByte(MPU9250_ADDRESS, ZG_OFFSET_L, data[5]);
taknokolat 0:eae101ae93c0 501 */
taknokolat 0:eae101ae93c0 502 dest1[0] = (float) gyro_bias[0]/(float) gyrosensitivity; // construct gyro bias in deg/s for later manual subtraction
taknokolat 0:eae101ae93c0 503 dest1[1] = (float) gyro_bias[1]/(float) gyrosensitivity;
taknokolat 0:eae101ae93c0 504 dest1[2] = (float) gyro_bias[2]/(float) gyrosensitivity;
taknokolat 0:eae101ae93c0 505
taknokolat 0:eae101ae93c0 506 // Construct the accelerometer biases for push to the hardware accelerometer bias registers. These registers contain
taknokolat 0:eae101ae93c0 507 // factory trim values which must be added to the calculated accelerometer biases; on boot up these registers will hold
taknokolat 0:eae101ae93c0 508 // non-zero values. In addition, bit 0 of the lower byte must be preserved since it is used for temperature
taknokolat 0:eae101ae93c0 509 // compensation calculations. Accelerometer bias registers expect bias input as 2048 LSB per g, so that
taknokolat 0:eae101ae93c0 510 // the accelerometer biases calculated above must be divided by 8.
taknokolat 0:eae101ae93c0 511
taknokolat 0:eae101ae93c0 512 readBytes(MPU9250_ADDRESS, XA_OFFSET_H, 2, &data[0]); // Read factory accelerometer trim values
taknokolat 0:eae101ae93c0 513 accel_bias_reg[0] = (int16_t) ((int16_t)data[0] << 8) | data[1];
taknokolat 0:eae101ae93c0 514 readBytes(MPU9250_ADDRESS, YA_OFFSET_H, 2, &data[0]);
taknokolat 0:eae101ae93c0 515 accel_bias_reg[1] = (int16_t) ((int16_t)data[0] << 8) | data[1];
taknokolat 0:eae101ae93c0 516 readBytes(MPU9250_ADDRESS, ZA_OFFSET_H, 2, &data[0]);
taknokolat 0:eae101ae93c0 517 accel_bias_reg[2] = (int16_t) ((int16_t)data[0] << 8) | data[1];
taknokolat 0:eae101ae93c0 518
taknokolat 0:eae101ae93c0 519 uint32_t mask = 1uL; // Define mask for temperature compensation bit 0 of lower byte of accelerometer bias registers
taknokolat 0:eae101ae93c0 520 uint8_t mask_bit[3] = {0, 0, 0}; // Define array to hold mask bit for each accelerometer bias axis
taknokolat 0:eae101ae93c0 521
taknokolat 0:eae101ae93c0 522 for(ii = 0; ii < 3; ii++) {
taknokolat 0:eae101ae93c0 523 if(accel_bias_reg[ii] & mask) mask_bit[ii] = 0x01; // If temperature compensation bit is set, record that fact in mask_bit
taknokolat 0:eae101ae93c0 524 }
taknokolat 0:eae101ae93c0 525
taknokolat 0:eae101ae93c0 526 // Construct total accelerometer bias, including calculated average accelerometer bias from above
taknokolat 0:eae101ae93c0 527 accel_bias_reg[0] -= (accel_bias[0]/8); // Subtract calculated averaged accelerometer bias scaled to 2048 LSB/g (16 g full scale)
taknokolat 0:eae101ae93c0 528 accel_bias_reg[1] -= (accel_bias[1]/8);
taknokolat 0:eae101ae93c0 529 accel_bias_reg[2] -= (accel_bias[2]/8);
taknokolat 0:eae101ae93c0 530
taknokolat 0:eae101ae93c0 531 data[0] = (accel_bias_reg[0] >> 8) & 0xFF;
taknokolat 0:eae101ae93c0 532 data[1] = (accel_bias_reg[0]) & 0xFF;
taknokolat 0:eae101ae93c0 533 data[1] = data[1] | mask_bit[0]; // preserve temperature compensation bit when writing back to accelerometer bias registers
taknokolat 0:eae101ae93c0 534 data[2] = (accel_bias_reg[1] >> 8) & 0xFF;
taknokolat 0:eae101ae93c0 535 data[3] = (accel_bias_reg[1]) & 0xFF;
taknokolat 0:eae101ae93c0 536 data[3] = data[3] | mask_bit[1]; // preserve temperature compensation bit when writing back to accelerometer bias registers
taknokolat 0:eae101ae93c0 537 data[4] = (accel_bias_reg[2] >> 8) & 0xFF;
taknokolat 0:eae101ae93c0 538 data[5] = (accel_bias_reg[2]) & 0xFF;
taknokolat 0:eae101ae93c0 539 data[5] = data[5] | mask_bit[2]; // preserve temperature compensation bit when writing back to accelerometer bias registers
taknokolat 0:eae101ae93c0 540
taknokolat 0:eae101ae93c0 541 // Apparently this is not working for the acceleration biases in the MPU-9250
taknokolat 0:eae101ae93c0 542 // Are we handling the temperature correction bit properly?
taknokolat 0:eae101ae93c0 543 // Push accelerometer biases to hardware registers
taknokolat 0:eae101ae93c0 544 /*
taknokolat 0:eae101ae93c0 545 writeByte(MPU9250_ADDRESS, XA_OFFSET_H, data[0]);
taknokolat 0:eae101ae93c0 546 writeByte(MPU9250_ADDRESS, XA_OFFSET_L, data[1]);
taknokolat 0:eae101ae93c0 547 writeByte(MPU9250_ADDRESS, YA_OFFSET_H, data[2]);
taknokolat 0:eae101ae93c0 548 writeByte(MPU9250_ADDRESS, YA_OFFSET_L, data[3]);
taknokolat 0:eae101ae93c0 549 writeByte(MPU9250_ADDRESS, ZA_OFFSET_H, data[4]);
taknokolat 0:eae101ae93c0 550 writeByte(MPU9250_ADDRESS, ZA_OFFSET_L, data[5]);
taknokolat 0:eae101ae93c0 551 */
taknokolat 0:eae101ae93c0 552 // Output scaled accelerometer biases for manual subtraction in the main program
taknokolat 0:eae101ae93c0 553 dest2[0] = (float)accel_bias[0]/(float)accelsensitivity;
taknokolat 0:eae101ae93c0 554 dest2[1] = (float)accel_bias[1]/(float)accelsensitivity;
taknokolat 0:eae101ae93c0 555 dest2[2] = (float)accel_bias[2]/(float)accelsensitivity;
taknokolat 0:eae101ae93c0 556 }
taknokolat 0:eae101ae93c0 557
taknokolat 0:eae101ae93c0 558 void MPU9250::getMres(void)
taknokolat 0:eae101ae93c0 559 {
taknokolat 0:eae101ae93c0 560 switch (Mscale)
taknokolat 0:eae101ae93c0 561 {
taknokolat 0:eae101ae93c0 562 // Possible magnetometer scales (and their register bit settings) are:
taknokolat 0:eae101ae93c0 563 // 14 bit resolution (0) and 16 bit resolution (1)
taknokolat 0:eae101ae93c0 564 case MFS_14BITS:
taknokolat 0:eae101ae93c0 565 mRes = 10.0*4219.0/8190.0; // Proper scale to return milliGauss
taknokolat 0:eae101ae93c0 566 break;
taknokolat 0:eae101ae93c0 567 case MFS_16BITS:
taknokolat 0:eae101ae93c0 568 mRes = 10.0*4219.0/32760.0; // Proper scale to return milliGauss
taknokolat 0:eae101ae93c0 569 break;
taknokolat 0:eae101ae93c0 570 }
taknokolat 0:eae101ae93c0 571 }
taknokolat 0:eae101ae93c0 572
taknokolat 0:eae101ae93c0 573 void MPU9250::getGres(void) {
taknokolat 0:eae101ae93c0 574 switch (Gscale)
taknokolat 0:eae101ae93c0 575 {
taknokolat 0:eae101ae93c0 576 // Possible gyro scales (and their register bit settings) are:
taknokolat 0:eae101ae93c0 577 // 250 DPS (00), 500 DPS (01), 1000 DPS (10), and 2000 DPS (11).
taknokolat 0:eae101ae93c0 578 // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value:
taknokolat 0:eae101ae93c0 579 case GFS_250DPS:
taknokolat 0:eae101ae93c0 580 gRes = 250.0/32768.0;
taknokolat 0:eae101ae93c0 581 break;
taknokolat 0:eae101ae93c0 582 case GFS_500DPS:
taknokolat 0:eae101ae93c0 583 gRes = 500.0/32768.0;
taknokolat 0:eae101ae93c0 584 break;
taknokolat 0:eae101ae93c0 585 case GFS_1000DPS:
taknokolat 0:eae101ae93c0 586 gRes = 1000.0/32768.0;
taknokolat 0:eae101ae93c0 587 break;
taknokolat 0:eae101ae93c0 588 case GFS_2000DPS:
taknokolat 0:eae101ae93c0 589 gRes = 2000.0/32768.0;
taknokolat 0:eae101ae93c0 590 break;
taknokolat 0:eae101ae93c0 591 }
taknokolat 0:eae101ae93c0 592 }
taknokolat 0:eae101ae93c0 593
taknokolat 0:eae101ae93c0 594
taknokolat 0:eae101ae93c0 595 void MPU9250::getAres(void)
taknokolat 0:eae101ae93c0 596 {
taknokolat 0:eae101ae93c0 597 switch (Ascale)
taknokolat 0:eae101ae93c0 598 {
taknokolat 0:eae101ae93c0 599 // Possible accelerometer scales (and their register bit settings) are:
taknokolat 0:eae101ae93c0 600 // 2 Gs (00), 4 Gs (01), 8 Gs (10), and 16 Gs (11).
taknokolat 0:eae101ae93c0 601 // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value:
taknokolat 0:eae101ae93c0 602 case AFS_2G:
taknokolat 0:eae101ae93c0 603 aRes = 2.0/32768.0;
taknokolat 0:eae101ae93c0 604 break;
taknokolat 0:eae101ae93c0 605 case AFS_4G:
taknokolat 0:eae101ae93c0 606 aRes = 4.0/32768.0;
taknokolat 0:eae101ae93c0 607 break;
taknokolat 0:eae101ae93c0 608 case AFS_8G:
taknokolat 0:eae101ae93c0 609 aRes = 8.0/32768.0;
taknokolat 0:eae101ae93c0 610 break;
taknokolat 0:eae101ae93c0 611 case AFS_16G:
taknokolat 0:eae101ae93c0 612 aRes = 16.0/32768.0;
taknokolat 0:eae101ae93c0 613 break;
taknokolat 0:eae101ae93c0 614 }
taknokolat 0:eae101ae93c0 615 }
taknokolat 0:eae101ae93c0 616
taknokolat 0:eae101ae93c0 617 void MPU9250::readAccelData(int16_t * destination)
taknokolat 0:eae101ae93c0 618 {
taknokolat 0:eae101ae93c0 619 uint8_t rawData[6]; // x/y/z accel register data stored here
taknokolat 0:eae101ae93c0 620
taknokolat 0:eae101ae93c0 621 readBytes(MPU9250_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]); // Read the six raw data registers into data array
taknokolat 0:eae101ae93c0 622 destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
taknokolat 0:eae101ae93c0 623 destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ;
taknokolat 0:eae101ae93c0 624 destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ;
taknokolat 0:eae101ae93c0 625 }
taknokolat 0:eae101ae93c0 626
taknokolat 0:eae101ae93c0 627 void MPU9250::readGyroData(int16_t * destination)
taknokolat 0:eae101ae93c0 628 {
taknokolat 0:eae101ae93c0 629 uint8_t rawData[6]; // x/y/z gyro register data stored here
taknokolat 0:eae101ae93c0 630
taknokolat 0:eae101ae93c0 631 readBytes(MPU9250_ADDRESS, GYRO_XOUT_H, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
taknokolat 0:eae101ae93c0 632 destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
taknokolat 0:eae101ae93c0 633 destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ;
taknokolat 0:eae101ae93c0 634 destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ;
taknokolat 0:eae101ae93c0 635 }
taknokolat 0:eae101ae93c0 636
taknokolat 0:eae101ae93c0 637 void MPU9250::readMagData(int16_t * destination)
taknokolat 0:eae101ae93c0 638 {
taknokolat 0:eae101ae93c0 639 uint8_t rawData[7]; // x/y/z gyro register data, ST2 register stored here, must read ST2 at end of data acquisition
taknokolat 0:eae101ae93c0 640
taknokolat 0:eae101ae93c0 641 if(readByte(AK8963_ADDRESS, AK8963_ST1) & 0x01) { // wait for magnetometer data ready bit to be set
taknokolat 0:eae101ae93c0 642 readBytes(AK8963_ADDRESS, AK8963_XOUT_L, 7, &rawData[0]); // Read the six raw data and ST2 registers sequentially into data array
taknokolat 0:eae101ae93c0 643 uint8_t c = rawData[6]; // End data read by reading ST2 register
taknokolat 0:eae101ae93c0 644 if(!(c & 0x08)) { // Check if magnetic sensor overflow set, if not then report data
taknokolat 0:eae101ae93c0 645 destination[0] = (int16_t)(((int16_t)rawData[1] << 8) | rawData[0]); // Turn the MSB and LSB into a signed 16-bit value
taknokolat 0:eae101ae93c0 646 destination[1] = (int16_t)(((int16_t)rawData[3] << 8) | rawData[2]) ; // Data stored as little Endian
taknokolat 0:eae101ae93c0 647 destination[2] = (int16_t)(((int16_t)rawData[5] << 8) | rawData[4]) ;
taknokolat 0:eae101ae93c0 648 }
taknokolat 0:eae101ae93c0 649 }
taknokolat 0:eae101ae93c0 650 }
taknokolat 0:eae101ae93c0 651
taknokolat 0:eae101ae93c0 652 int16_t MPU9250::readTempData(void)
taknokolat 0:eae101ae93c0 653 {
taknokolat 0:eae101ae93c0 654 uint8_t rawData[2]; // x/y/z gyro register data stored here
taknokolat 0:eae101ae93c0 655
taknokolat 0:eae101ae93c0 656 readBytes(MPU9250_ADDRESS, TEMP_OUT_H, 2, &rawData[0]); // Read the two raw data registers sequentially into data array
taknokolat 0:eae101ae93c0 657
taknokolat 0:eae101ae93c0 658 return (int16_t)(((int16_t)rawData[0]) << 8 | rawData[1]) ; // Turn the MSB and LSB into a 16-bit value
taknokolat 0:eae101ae93c0 659 }
taknokolat 0:eae101ae93c0 660
taknokolat 0:eae101ae93c0 661 uint8_t MPU9250::Whoami_MPU9250(void){
taknokolat 0:eae101ae93c0 662 return readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
taknokolat 0:eae101ae93c0 663 }
taknokolat 0:eae101ae93c0 664
taknokolat 0:eae101ae93c0 665 uint8_t MPU9250::Whoami_AK8963(void){
taknokolat 0:eae101ae93c0 666 return readByte(WHO_AM_I_AK8963, WHO_AM_I_AK8963);
taknokolat 0:eae101ae93c0 667 }
taknokolat 0:eae101ae93c0 668
taknokolat 0:eae101ae93c0 669 void MPU9250::sensingAccel(void){
taknokolat 0:eae101ae93c0 670 readAccelData(accelCount);
taknokolat 0:eae101ae93c0 671 ax = (float)accelCount[0]*aRes - accelBias[0];
taknokolat 0:eae101ae93c0 672 ay = (float)accelCount[1]*aRes - accelBias[1];
taknokolat 0:eae101ae93c0 673 az = (float)accelCount[2]*aRes - accelBias[2];
taknokolat 0:eae101ae93c0 674 }
taknokolat 0:eae101ae93c0 675
taknokolat 0:eae101ae93c0 676 void MPU9250::sensingGyro(void){
taknokolat 0:eae101ae93c0 677 readGyroData(gyroCount);
taknokolat 0:eae101ae93c0 678 gx = (float)gyroCount[0]*gRes - gyroBias[0];
taknokolat 0:eae101ae93c0 679 gy = (float)gyroCount[1]*gRes - gyroBias[1];
taknokolat 0:eae101ae93c0 680 gz = (float)gyroCount[2]*gRes - gyroBias[2];
taknokolat 0:eae101ae93c0 681 }
taknokolat 0:eae101ae93c0 682
taknokolat 0:eae101ae93c0 683 void MPU9250::sensingMag(void){
taknokolat 0:eae101ae93c0 684 readMagData(magCount);
taknokolat 0:eae101ae93c0 685 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0];
taknokolat 0:eae101ae93c0 686 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1];
taknokolat 0:eae101ae93c0 687 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
taknokolat 0:eae101ae93c0 688 }
taknokolat 0:eae101ae93c0 689
taknokolat 0:eae101ae93c0 690 void MPU9250::sensingTemp(void){
taknokolat 0:eae101ae93c0 691 tempCount = readTempData();
taknokolat 0:eae101ae93c0 692 temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
taknokolat 0:eae101ae93c0 693 }
taknokolat 0:eae101ae93c0 694
taknokolat 0:eae101ae93c0 695 // Implementation of Sebastian Madgwick's "...efficient orientation filter for... inertial/magnetic sensor arrays"
taknokolat 0:eae101ae93c0 696 // (see http://www.x-io.co.uk/category/open-source/ for examples and more details)
taknokolat 0:eae101ae93c0 697 // which fuses acceleration, rotation rate, and magnetic moments to produce a quaternion-based estimate of absolute
taknokolat 0:eae101ae93c0 698 // device orientation -- which can be converted to yaw, pitch, and roll. Useful for stabilizing quadcopters, etc.
taknokolat 0:eae101ae93c0 699 // The performance of the orientation filter is at least as good as conventional Kalman-based filtering algorithms
taknokolat 0:eae101ae93c0 700 // but is much less computationally intensive---it can be performed on a 3.3 V Pro Mini operating at 8 MHz!
taknokolat 0:eae101ae93c0 701 void MPU9250::MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz)
taknokolat 0:eae101ae93c0 702 {
taknokolat 0:eae101ae93c0 703 float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3]; // short name local variable for readability
taknokolat 0:eae101ae93c0 704 float norm;
taknokolat 0:eae101ae93c0 705 float hx, hy, _2bx, _2bz;
taknokolat 0:eae101ae93c0 706 float s1, s2, s3, s4;
taknokolat 0:eae101ae93c0 707 float qDot1, qDot2, qDot3, qDot4;
taknokolat 0:eae101ae93c0 708
taknokolat 0:eae101ae93c0 709 // Auxiliary variables to avoid repeated arithmetic
taknokolat 0:eae101ae93c0 710 float _2q1mx;
taknokolat 0:eae101ae93c0 711 float _2q1my;
taknokolat 0:eae101ae93c0 712 float _2q1mz;
taknokolat 0:eae101ae93c0 713 float _2q2mx;
taknokolat 0:eae101ae93c0 714 float _4bx;
taknokolat 0:eae101ae93c0 715 float _4bz;
taknokolat 0:eae101ae93c0 716 float _2q1 = 2.0f * q1;
taknokolat 0:eae101ae93c0 717 float _2q2 = 2.0f * q2;
taknokolat 0:eae101ae93c0 718 float _2q3 = 2.0f * q3;
taknokolat 0:eae101ae93c0 719 float _2q4 = 2.0f * q4;
taknokolat 0:eae101ae93c0 720 float _2q1q3 = 2.0f * q1 * q3;
taknokolat 0:eae101ae93c0 721 float _2q3q4 = 2.0f * q3 * q4;
taknokolat 0:eae101ae93c0 722 float q1q1 = q1 * q1;
taknokolat 0:eae101ae93c0 723 float q1q2 = q1 * q2;
taknokolat 0:eae101ae93c0 724 float q1q3 = q1 * q3;
taknokolat 0:eae101ae93c0 725 float q1q4 = q1 * q4;
taknokolat 0:eae101ae93c0 726 float q2q2 = q2 * q2;
taknokolat 0:eae101ae93c0 727 float q2q3 = q2 * q3;
taknokolat 0:eae101ae93c0 728 float q2q4 = q2 * q4;
taknokolat 0:eae101ae93c0 729 float q3q3 = q3 * q3;
taknokolat 0:eae101ae93c0 730 float q3q4 = q3 * q4;
taknokolat 0:eae101ae93c0 731 float q4q4 = q4 * q4;
taknokolat 0:eae101ae93c0 732
taknokolat 0:eae101ae93c0 733 // Normalise accelerometer measurement
taknokolat 0:eae101ae93c0 734 norm = sqrt(ax * ax + ay * ay + az * az);
taknokolat 0:eae101ae93c0 735 if (norm == 0.0f) return; // handle NaN
taknokolat 0:eae101ae93c0 736 norm = 1.0f/norm;
taknokolat 0:eae101ae93c0 737 ax *= norm;
taknokolat 0:eae101ae93c0 738 ay *= norm;
taknokolat 0:eae101ae93c0 739 az *= norm;
taknokolat 0:eae101ae93c0 740
taknokolat 0:eae101ae93c0 741 // Normalise magnetometer measurement
taknokolat 0:eae101ae93c0 742 norm = sqrt(mx * mx + my * my + mz * mz);
taknokolat 0:eae101ae93c0 743 if (norm == 0.0f) return; // handle NaN
taknokolat 0:eae101ae93c0 744 norm = 1.0f/norm;
taknokolat 0:eae101ae93c0 745 mx *= norm;
taknokolat 0:eae101ae93c0 746 my *= norm;
taknokolat 0:eae101ae93c0 747 mz *= norm;
taknokolat 0:eae101ae93c0 748
taknokolat 0:eae101ae93c0 749 // Reference direction of Earth's magnetic field
taknokolat 0:eae101ae93c0 750 _2q1mx = 2.0f * q1 * mx;
taknokolat 0:eae101ae93c0 751 _2q1my = 2.0f * q1 * my;
taknokolat 0:eae101ae93c0 752 _2q1mz = 2.0f * q1 * mz;
taknokolat 0:eae101ae93c0 753 _2q2mx = 2.0f * q2 * mx;
taknokolat 0:eae101ae93c0 754 hx = mx * q1q1 - _2q1my * q4 + _2q1mz * q3 + mx * q2q2 + _2q2 * my * q3 + _2q2 * mz * q4 - mx * q3q3 - mx * q4q4;
taknokolat 0:eae101ae93c0 755 hy = _2q1mx * q4 + my * q1q1 - _2q1mz * q2 + _2q2mx * q3 - my * q2q2 + my * q3q3 + _2q3 * mz * q4 - my * q4q4;
taknokolat 0:eae101ae93c0 756 _2bx = sqrt(hx * hx + hy * hy);
taknokolat 0:eae101ae93c0 757 _2bz = -_2q1mx * q3 + _2q1my * q2 + mz * q1q1 + _2q2mx * q4 - mz * q2q2 + _2q3 * my * q4 - mz * q3q3 + mz * q4q4;
taknokolat 0:eae101ae93c0 758 _4bx = 2.0f * _2bx;
taknokolat 0:eae101ae93c0 759 _4bz = 2.0f * _2bz;
taknokolat 0:eae101ae93c0 760
taknokolat 0:eae101ae93c0 761 // Gradient decent algorithm corrective step
taknokolat 0:eae101ae93c0 762 s1 = -_2q3 * (2.0f * q2q4 - _2q1q3 - ax) + _2q2 * (2.0f * q1q2 + _2q3q4 - ay) - _2bz * q3 * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (-_2bx * q4 + _2bz * q2) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + _2bx * q3 * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
taknokolat 0:eae101ae93c0 763 s2 = _2q4 * (2.0f * q2q4 - _2q1q3 - ax) + _2q1 * (2.0f * q1q2 + _2q3q4 - ay) - 4.0f * q2 * (1.0f - 2.0f * q2q2 - 2.0f * q3q3 - az) + _2bz * q4 * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (_2bx * q3 + _2bz * q1) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + (_2bx * q4 - _4bz * q2) * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
taknokolat 0:eae101ae93c0 764 s3 = -_2q1 * (2.0f * q2q4 - _2q1q3 - ax) + _2q4 * (2.0f * q1q2 + _2q3q4 - ay) - 4.0f * q3 * (1.0f - 2.0f * q2q2 - 2.0f * q3q3 - az) + (-_4bx * q3 - _2bz * q1) * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (_2bx * q2 + _2bz * q4) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + (_2bx * q1 - _4bz * q3) * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
taknokolat 0:eae101ae93c0 765 s4 = _2q2 * (2.0f * q2q4 - _2q1q3 - ax) + _2q3 * (2.0f * q1q2 + _2q3q4 - ay) + (-_4bx * q4 + _2bz * q2) * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (-_2bx * q1 + _2bz * q3) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + _2bx * q2 * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
taknokolat 0:eae101ae93c0 766 norm = sqrt(s1 * s1 + s2 * s2 + s3 * s3 + s4 * s4); // normalise step magnitude
taknokolat 0:eae101ae93c0 767 norm = 1.0f/norm;
taknokolat 0:eae101ae93c0 768 s1 *= norm;
taknokolat 0:eae101ae93c0 769 s2 *= norm;
taknokolat 0:eae101ae93c0 770 s3 *= norm;
taknokolat 0:eae101ae93c0 771 s4 *= norm;
taknokolat 0:eae101ae93c0 772
taknokolat 0:eae101ae93c0 773 // Compute rate of change of quaternion
taknokolat 0:eae101ae93c0 774 qDot1 = 0.5f * (-q2 * gx - q3 * gy - q4 * gz) - beta * s1;
taknokolat 0:eae101ae93c0 775 qDot2 = 0.5f * (q1 * gx + q3 * gz - q4 * gy) - beta * s2;
taknokolat 0:eae101ae93c0 776 qDot3 = 0.5f * (q1 * gy - q2 * gz + q4 * gx) - beta * s3;
taknokolat 0:eae101ae93c0 777 qDot4 = 0.5f * (q1 * gz + q2 * gy - q3 * gx) - beta * s4;
taknokolat 0:eae101ae93c0 778
taknokolat 0:eae101ae93c0 779 // Integrate to yield quaternion
taknokolat 0:eae101ae93c0 780 q1 += qDot1 * deltat;
taknokolat 0:eae101ae93c0 781 q2 += qDot2 * deltat;
taknokolat 0:eae101ae93c0 782 q3 += qDot3 * deltat;
taknokolat 0:eae101ae93c0 783 q4 += qDot4 * deltat;
taknokolat 0:eae101ae93c0 784 norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4); // normalise quaternion
taknokolat 0:eae101ae93c0 785 norm = 1.0f/norm;
taknokolat 0:eae101ae93c0 786 q[0] = q1 * norm;
taknokolat 0:eae101ae93c0 787 q[1] = q2 * norm;
taknokolat 0:eae101ae93c0 788 q[2] = q3 * norm;
taknokolat 0:eae101ae93c0 789 q[3] = q4 * norm;
taknokolat 0:eae101ae93c0 790
taknokolat 0:eae101ae93c0 791 }
taknokolat 0:eae101ae93c0 792
taknokolat 0:eae101ae93c0 793 void MPU9250::MahonyQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz)
taknokolat 0:eae101ae93c0 794 {
taknokolat 0:eae101ae93c0 795 float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3]; // short name local variable for readability
taknokolat 0:eae101ae93c0 796 float norm;
taknokolat 0:eae101ae93c0 797 float hx, hy, bx, bz;
taknokolat 0:eae101ae93c0 798 float vx, vy, vz, wx, wy, wz;
taknokolat 0:eae101ae93c0 799 float ex, ey, ez;
taknokolat 0:eae101ae93c0 800 float pa, pb, pc;
taknokolat 0:eae101ae93c0 801
taknokolat 0:eae101ae93c0 802 // Auxiliary variables to avoid repeated arithmetic
taknokolat 0:eae101ae93c0 803 float q1q1 = q1 * q1;
taknokolat 0:eae101ae93c0 804 float q1q2 = q1 * q2;
taknokolat 0:eae101ae93c0 805 float q1q3 = q1 * q3;
taknokolat 0:eae101ae93c0 806 float q1q4 = q1 * q4;
taknokolat 0:eae101ae93c0 807 float q2q2 = q2 * q2;
taknokolat 0:eae101ae93c0 808 float q2q3 = q2 * q3;
taknokolat 0:eae101ae93c0 809 float q2q4 = q2 * q4;
taknokolat 0:eae101ae93c0 810 float q3q3 = q3 * q3;
taknokolat 0:eae101ae93c0 811 float q3q4 = q3 * q4;
taknokolat 0:eae101ae93c0 812 float q4q4 = q4 * q4;
taknokolat 0:eae101ae93c0 813
taknokolat 0:eae101ae93c0 814 // Normalise accelerometer measurement
taknokolat 0:eae101ae93c0 815 norm = sqrt(ax * ax + ay * ay + az * az);
taknokolat 0:eae101ae93c0 816 if (norm == 0.0f) return; // handle NaN
taknokolat 0:eae101ae93c0 817 norm = 1.0f / norm; // use reciprocal for division
taknokolat 0:eae101ae93c0 818 ax *= norm;
taknokolat 0:eae101ae93c0 819 ay *= norm;
taknokolat 0:eae101ae93c0 820 az *= norm;
taknokolat 0:eae101ae93c0 821
taknokolat 0:eae101ae93c0 822 // Normalise magnetometer measurement
taknokolat 0:eae101ae93c0 823 norm = sqrt(mx * mx + my * my + mz * mz);
taknokolat 0:eae101ae93c0 824 if (norm == 0.0f) return; // handle NaN
taknokolat 0:eae101ae93c0 825 norm = 1.0f / norm; // use reciprocal for division
taknokolat 0:eae101ae93c0 826 mx *= norm;
taknokolat 0:eae101ae93c0 827 my *= norm;
taknokolat 0:eae101ae93c0 828 mz *= norm;
taknokolat 0:eae101ae93c0 829
taknokolat 0:eae101ae93c0 830 // Reference direction of Earth's magnetic field
taknokolat 0:eae101ae93c0 831 hx = 2.0f * mx * (0.5f - q3q3 - q4q4) + 2.0f * my * (q2q3 - q1q4) + 2.0f * mz * (q2q4 + q1q3);
taknokolat 0:eae101ae93c0 832 hy = 2.0f * mx * (q2q3 + q1q4) + 2.0f * my * (0.5f - q2q2 - q4q4) + 2.0f * mz * (q3q4 - q1q2);
taknokolat 0:eae101ae93c0 833 bx = sqrt((hx * hx) + (hy * hy));
taknokolat 0:eae101ae93c0 834 bz = 2.0f * mx * (q2q4 - q1q3) + 2.0f * my * (q3q4 + q1q2) + 2.0f * mz * (0.5f - q2q2 - q3q3);
taknokolat 0:eae101ae93c0 835
taknokolat 0:eae101ae93c0 836 // Estimated direction of gravity and magnetic field
taknokolat 0:eae101ae93c0 837 vx = 2.0f * (q2q4 - q1q3);
taknokolat 0:eae101ae93c0 838 vy = 2.0f * (q1q2 + q3q4);
taknokolat 0:eae101ae93c0 839 vz = q1q1 - q2q2 - q3q3 + q4q4;
taknokolat 0:eae101ae93c0 840 wx = 2.0f * bx * (0.5f - q3q3 - q4q4) + 2.0f * bz * (q2q4 - q1q3);
taknokolat 0:eae101ae93c0 841 wy = 2.0f * bx * (q2q3 - q1q4) + 2.0f * bz * (q1q2 + q3q4);
taknokolat 0:eae101ae93c0 842 wz = 2.0f * bx * (q1q3 + q2q4) + 2.0f * bz * (0.5f - q2q2 - q3q3);
taknokolat 0:eae101ae93c0 843
taknokolat 0:eae101ae93c0 844 // Error is cross product between estimated direction and measured direction of gravity
taknokolat 0:eae101ae93c0 845 ex = (ay * vz - az * vy) + (my * wz - mz * wy);
taknokolat 0:eae101ae93c0 846 ey = (az * vx - ax * vz) + (mz * wx - mx * wz);
taknokolat 0:eae101ae93c0 847 ez = (ax * vy - ay * vx) + (mx * wy - my * wx);
taknokolat 0:eae101ae93c0 848 if (Ki > 0.0f){
taknokolat 0:eae101ae93c0 849 eInt[0] += ex; // accumulate integral error
taknokolat 0:eae101ae93c0 850 eInt[1] += ey;
taknokolat 0:eae101ae93c0 851 eInt[2] += ez;
taknokolat 0:eae101ae93c0 852
taknokolat 0:eae101ae93c0 853 }else{
taknokolat 0:eae101ae93c0 854 eInt[0] = 0.0f; // prevent integral wind up
taknokolat 0:eae101ae93c0 855 eInt[1] = 0.0f;
taknokolat 0:eae101ae93c0 856 eInt[2] = 0.0f;
taknokolat 0:eae101ae93c0 857 }
taknokolat 0:eae101ae93c0 858
taknokolat 0:eae101ae93c0 859 // Apply feedback terms
taknokolat 0:eae101ae93c0 860 gx = gx + Kp * ex + Ki * eInt[0];
taknokolat 0:eae101ae93c0 861 gy = gy + Kp * ey + Ki * eInt[1];
taknokolat 0:eae101ae93c0 862 gz = gz + Kp * ez + Ki * eInt[2];
taknokolat 0:eae101ae93c0 863
taknokolat 0:eae101ae93c0 864 // Integrate rate of change of quaternion
taknokolat 0:eae101ae93c0 865 pa = q2;
taknokolat 0:eae101ae93c0 866 pb = q3;
taknokolat 0:eae101ae93c0 867 pc = q4;
taknokolat 0:eae101ae93c0 868 q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * deltat);
taknokolat 0:eae101ae93c0 869 q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * deltat);
taknokolat 0:eae101ae93c0 870 q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * deltat);
taknokolat 0:eae101ae93c0 871 q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * deltat);
taknokolat 0:eae101ae93c0 872
taknokolat 0:eae101ae93c0 873 // Normalise quaternion
taknokolat 0:eae101ae93c0 874 norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4);
taknokolat 0:eae101ae93c0 875 norm = 1.0f / norm;
taknokolat 0:eae101ae93c0 876 q[0] = q1 * norm;
taknokolat 0:eae101ae93c0 877 q[1] = q2 * norm;
taknokolat 0:eae101ae93c0 878 q[2] = q3 * norm;
taknokolat 0:eae101ae93c0 879 q[3] = q4 * norm;
taknokolat 0:eae101ae93c0 880
taknokolat 0:eae101ae93c0 881 }
taknokolat 0:eae101ae93c0 882
taknokolat 0:eae101ae93c0 883 void MPU9250::translateQuaternionToDeg(float quaternion[4]){
taknokolat 0:eae101ae93c0 884 yaw = atan2(2.0f * (quaternion[1] * quaternion[2] + quaternion[0] * quaternion[3]), quaternion[0] * quaternion[0] + quaternion[1] * quaternion[1] - quaternion[2] * quaternion[2] - quaternion[3] * quaternion[3]);
taknokolat 0:eae101ae93c0 885 roll = -asin(2.0f * (quaternion[1] * quaternion[3] - quaternion[0] * quaternion[2]));
taknokolat 0:eae101ae93c0 886 pitch = atan2(2.0f * (quaternion[0] * quaternion[1] + quaternion[2] * quaternion[3]), quaternion[0] * quaternion[0] - quaternion[1] * quaternion[1] - quaternion[2] * quaternion[2] + quaternion[3] * quaternion[3]);
taknokolat 0:eae101ae93c0 887 }
taknokolat 0:eae101ae93c0 888
taknokolat 0:eae101ae93c0 889 void MPU9250::calibrateDegree(void){
taknokolat 0:eae101ae93c0 890 pitch *= 180.0f / PI;
taknokolat 0:eae101ae93c0 891 yaw *= 180.0f / PI;
taknokolat 0:eae101ae93c0 892 yaw -= DECLINATION;
taknokolat 0:eae101ae93c0 893 roll *= 180.0f / PI;
taknokolat 0:eae101ae93c0 894 }
taknokolat 0:eae101ae93c0 895
taknokolat 0:eae101ae93c0 896 void MPU9250::transformCoordinateFromCompassToMPU(){
taknokolat 0:eae101ae93c0 897 float buf = mx;
taknokolat 0:eae101ae93c0 898 mx = my;
taknokolat 0:eae101ae93c0 899 my = buf;
taknokolat 0:eae101ae93c0 900 mz = -mz;
taknokolat 0:eae101ae93c0 901 }