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