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