![](/media/cache/group/11700697_10200701668420946_8214886085951071495_o.jpg.50x50_q85.jpg)
Class of MPU9250
Dependencies: AHRS_fillter mbed
Fork of MPU9250AHRS by
MPU9250.cpp@8:928673148b55, 2016-01-20 (annotated)
- Committer:
- icyzkungz
- Date:
- Wed Jan 20 02:42:22 2016 +0000
- Revision:
- 8:928673148b55
- Parent:
- 6:5665d427bceb
add tx,rx in AHRS class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icyzkungz | 6:5665d427bceb | 1 | #include "MPU9250.h" |
icyzkungz | 6:5665d427bceb | 2 | |
icyzkungz | 6:5665d427bceb | 3 | |
icyzkungz | 6:5665d427bceb | 4 | MPU9250::MPU9250(PinName sda, PinName scl, PinName tx, PinName rx, int address) : i2c(sda, scl), pc(tx,rx) |
icyzkungz | 6:5665d427bceb | 5 | { |
icyzkungz | 6:5665d427bceb | 6 | if(address == 0) |
icyzkungz | 6:5665d427bceb | 7 | MPU9250_ADDRESS = MPU9250_ADDRESS_68; |
icyzkungz | 6:5665d427bceb | 8 | else if(address == 1) MPU9250_ADDRESS = MPU9250_ADDRESS_69; |
icyzkungz | 6:5665d427bceb | 9 | else { |
icyzkungz | 6:5665d427bceb | 10 | printf("Wrong Address\n"); |
icyzkungz | 6:5665d427bceb | 11 | while(1); |
icyzkungz | 6:5665d427bceb | 12 | } |
icyzkungz | 6:5665d427bceb | 13 | |
icyzkungz | 6:5665d427bceb | 14 | i2c.frequency(400000); |
icyzkungz | 6:5665d427bceb | 15 | |
icyzkungz | 6:5665d427bceb | 16 | for(int i=0; i<=3; i++) { |
icyzkungz | 6:5665d427bceb | 17 | magCalibration[i] = 0; |
icyzkungz | 6:5665d427bceb | 18 | magbias[i] = 0; |
icyzkungz | 6:5665d427bceb | 19 | gyroBias[i] = 0; |
icyzkungz | 6:5665d427bceb | 20 | accelBias[i] = 0; |
icyzkungz | 6:5665d427bceb | 21 | } |
icyzkungz | 6:5665d427bceb | 22 | Mmode = 0x06; // Either 8 Hz 0x02) or 100 Hz (0x06) magnetometer data ODR |
icyzkungz | 6:5665d427bceb | 23 | } |
icyzkungz | 6:5665d427bceb | 24 | |
icyzkungz | 6:5665d427bceb | 25 | void MPU9250::Start() |
icyzkungz | 6:5665d427bceb | 26 | { |
icyzkungz | 6:5665d427bceb | 27 | whoami = readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250 |
icyzkungz | 6:5665d427bceb | 28 | pc.printf("I AM 0x%x\n\r", whoami); |
icyzkungz | 6:5665d427bceb | 29 | pc.printf("I SHOULD BE 0x71\n\r"); |
icyzkungz | 6:5665d427bceb | 30 | |
icyzkungz | 6:5665d427bceb | 31 | if (whoami == 0x71) { // WHO_AM_I should always be 0x68 |
icyzkungz | 6:5665d427bceb | 32 | pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami); |
icyzkungz | 6:5665d427bceb | 33 | pc.printf("MPU9250 is online...\n\r"); |
icyzkungz | 6:5665d427bceb | 34 | wait(1); |
icyzkungz | 6:5665d427bceb | 35 | |
icyzkungz | 6:5665d427bceb | 36 | resetMPU9250(); // Reset registers to default in preparation for device calibration |
icyzkungz | 6:5665d427bceb | 37 | MPU9250SelfTest(); // Start by performing self test and reporting values |
icyzkungz | 6:5665d427bceb | 38 | /*pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]); |
icyzkungz | 6:5665d427bceb | 39 | pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]); |
icyzkungz | 6:5665d427bceb | 40 | pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]); |
icyzkungz | 6:5665d427bceb | 41 | pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]); |
icyzkungz | 6:5665d427bceb | 42 | pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]); |
icyzkungz | 6:5665d427bceb | 43 | pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]);*/ |
icyzkungz | 6:5665d427bceb | 44 | calibrateMPU9250(); // Calibrate gyro and accelerometers, load biases in bias registers |
icyzkungz | 6:5665d427bceb | 45 | /*pc.printf("x gyro bias = %f\n\r", gyroBias[0]); |
icyzkungz | 6:5665d427bceb | 46 | pc.printf("y gyro bias = %f\n\r", gyroBias[1]); |
icyzkungz | 6:5665d427bceb | 47 | pc.printf("z gyro bias = %f\n\r", gyroBias[2]); |
icyzkungz | 6:5665d427bceb | 48 | pc.printf("x accel bias = %f\n\r", accelBias[0]); |
icyzkungz | 6:5665d427bceb | 49 | pc.printf("y accel bias = %f\n\r", accelBias[1]); |
icyzkungz | 6:5665d427bceb | 50 | pc.printf("z accel bias = %f\n\r", accelBias[2]);*/ |
icyzkungz | 6:5665d427bceb | 51 | wait(2); |
icyzkungz | 6:5665d427bceb | 52 | initMPU9250(); |
icyzkungz | 6:5665d427bceb | 53 | pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature |
icyzkungz | 6:5665d427bceb | 54 | initAK8963(); |
icyzkungz | 6:5665d427bceb | 55 | pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer |
icyzkungz | 6:5665d427bceb | 56 | |
icyzkungz | 6:5665d427bceb | 57 | whoami = readByte(AK8963_ADDRESS, WHO_AM_I_AK8963); // Read WHO_AM_I register for MPU-9250 |
icyzkungz | 6:5665d427bceb | 58 | pc.printf("I AM 0x%x\n\r", whoami); |
icyzkungz | 6:5665d427bceb | 59 | pc.printf("I SHOULD BE 0x48\n\r"); |
icyzkungz | 6:5665d427bceb | 60 | if(whoami != 0x48) { |
icyzkungz | 6:5665d427bceb | 61 | while(1); |
icyzkungz | 6:5665d427bceb | 62 | } |
icyzkungz | 6:5665d427bceb | 63 | /*pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale)); |
icyzkungz | 6:5665d427bceb | 64 | pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale)); |
icyzkungz | 6:5665d427bceb | 65 | if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r"); |
icyzkungz | 6:5665d427bceb | 66 | if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r"); |
icyzkungz | 6:5665d427bceb | 67 | if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r"); |
icyzkungz | 6:5665d427bceb | 68 | if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r");*/ |
icyzkungz | 6:5665d427bceb | 69 | wait(1); |
icyzkungz | 6:5665d427bceb | 70 | } else { |
icyzkungz | 6:5665d427bceb | 71 | pc.printf("Could not connect to MPU9250: \n\r"); |
icyzkungz | 6:5665d427bceb | 72 | pc.printf("%#x \n", whoami); |
icyzkungz | 6:5665d427bceb | 73 | |
icyzkungz | 6:5665d427bceb | 74 | while(1) ; // Loop forever if communication doesn't happen |
icyzkungz | 6:5665d427bceb | 75 | } |
icyzkungz | 6:5665d427bceb | 76 | |
icyzkungz | 6:5665d427bceb | 77 | |
icyzkungz | 6:5665d427bceb | 78 | getAres(); // Get accelerometer sensitivity |
icyzkungz | 6:5665d427bceb | 79 | getGres(); // Get gyro sensitivity |
icyzkungz | 6:5665d427bceb | 80 | getMres(); // Get magnetometer sensitivity |
icyzkungz | 6:5665d427bceb | 81 | /*pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes); |
icyzkungz | 6:5665d427bceb | 82 | pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes); |
icyzkungz | 6:5665d427bceb | 83 | pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes);*/ |
icyzkungz | 6:5665d427bceb | 84 | |
icyzkungz | 6:5665d427bceb | 85 | MagCal(); |
icyzkungz | 6:5665d427bceb | 86 | } |
icyzkungz | 6:5665d427bceb | 87 | |
icyzkungz | 6:5665d427bceb | 88 | void MPU9250::ReadRawAccGyroMag() |
icyzkungz | 6:5665d427bceb | 89 | { |
icyzkungz | 6:5665d427bceb | 90 | // If intPin goes high, all data registers have new data |
icyzkungz | 6:5665d427bceb | 91 | if(readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt |
icyzkungz | 6:5665d427bceb | 92 | |
icyzkungz | 6:5665d427bceb | 93 | readAccelData(); // Read the x/y/z adc values |
icyzkungz | 6:5665d427bceb | 94 | AccelXYZCal(); |
icyzkungz | 6:5665d427bceb | 95 | // Now we'll calculate the accleration value into actual g's |
icyzkungz | 6:5665d427bceb | 96 | /*ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
icyzkungz | 6:5665d427bceb | 97 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
icyzkungz | 6:5665d427bceb | 98 | az = (float)accelCount[2]*aRes - accelBias[2];*/ |
icyzkungz | 6:5665d427bceb | 99 | |
icyzkungz | 6:5665d427bceb | 100 | readGyroData(); // Read the x/y/z adc values |
icyzkungz | 6:5665d427bceb | 101 | GyroXYZCal(); |
icyzkungz | 6:5665d427bceb | 102 | // Calculate the gyro value into actual degrees per second |
icyzkungz | 6:5665d427bceb | 103 | /*gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set |
icyzkungz | 6:5665d427bceb | 104 | gy = (float)gyroCount[1]*gRes - gyroBias[1]; |
icyzkungz | 6:5665d427bceb | 105 | gz = (float)gyroCount[2]*gRes - gyroBias[2];*/ |
icyzkungz | 6:5665d427bceb | 106 | |
icyzkungz | 6:5665d427bceb | 107 | readMagData(); // Read the x/y/z adc values |
icyzkungz | 6:5665d427bceb | 108 | MagXYZCal(); |
icyzkungz | 6:5665d427bceb | 109 | /*mx = ((float)magCount[0]-xmin)*magCalibration[0] + magbias[0]; // get actual magnetometer value, this depends on scale being set |
icyzkungz | 6:5665d427bceb | 110 | my = ((float)magCount[1]-ymin)*magCalibration[1] + magbias[1]; |
icyzkungz | 6:5665d427bceb | 111 | mz = ((float)magCount[2]-zmin)*magCalibration[2] + magbias[2];*/ |
icyzkungz | 6:5665d427bceb | 112 | } |
icyzkungz | 6:5665d427bceb | 113 | } |
icyzkungz | 6:5665d427bceb | 114 | |
icyzkungz | 6:5665d427bceb | 115 | void MPU9250::writeByte(uint8_t address, uint8_t subAddress, uint8_t data) |
icyzkungz | 6:5665d427bceb | 116 | { |
icyzkungz | 6:5665d427bceb | 117 | char data_write[2]; |
icyzkungz | 6:5665d427bceb | 118 | data_write[0] = subAddress; |
icyzkungz | 6:5665d427bceb | 119 | data_write[1] = data; |
icyzkungz | 6:5665d427bceb | 120 | i2c.write(address, data_write, 2, 0); |
icyzkungz | 6:5665d427bceb | 121 | } |
icyzkungz | 6:5665d427bceb | 122 | |
icyzkungz | 6:5665d427bceb | 123 | char MPU9250::readByte(uint8_t address, uint8_t subAddress) |
icyzkungz | 6:5665d427bceb | 124 | { |
icyzkungz | 6:5665d427bceb | 125 | char data[1]; // `data` will store the register data |
icyzkungz | 6:5665d427bceb | 126 | char data_write[1]; |
icyzkungz | 6:5665d427bceb | 127 | data_write[0] = subAddress; |
icyzkungz | 6:5665d427bceb | 128 | i2c.write(address, data_write, 1, 1); // no stop |
icyzkungz | 6:5665d427bceb | 129 | i2c.read(address, data, 1, 0); |
icyzkungz | 6:5665d427bceb | 130 | return data[0]; |
icyzkungz | 6:5665d427bceb | 131 | } |
icyzkungz | 6:5665d427bceb | 132 | |
icyzkungz | 6:5665d427bceb | 133 | void MPU9250::readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest) |
icyzkungz | 6:5665d427bceb | 134 | { |
icyzkungz | 6:5665d427bceb | 135 | char data[14]; |
icyzkungz | 6:5665d427bceb | 136 | char data_write[1]; |
icyzkungz | 6:5665d427bceb | 137 | data_write[0] = subAddress; |
icyzkungz | 6:5665d427bceb | 138 | i2c.write(address, data_write, 1, 1); // no stop |
icyzkungz | 6:5665d427bceb | 139 | i2c.read(address, data, count, 0); |
icyzkungz | 6:5665d427bceb | 140 | for(int ii = 0; ii < count; ii++) { |
icyzkungz | 6:5665d427bceb | 141 | dest[ii] = data[ii]; |
icyzkungz | 6:5665d427bceb | 142 | } |
icyzkungz | 6:5665d427bceb | 143 | } |
icyzkungz | 6:5665d427bceb | 144 | |
icyzkungz | 6:5665d427bceb | 145 | |
icyzkungz | 6:5665d427bceb | 146 | void MPU9250::setMres() |
icyzkungz | 6:5665d427bceb | 147 | { |
icyzkungz | 6:5665d427bceb | 148 | getMres(); |
icyzkungz | 6:5665d427bceb | 149 | switch (Mscale) { |
icyzkungz | 6:5665d427bceb | 150 | // Possible magnetometer scales (and their register bit settings) are: |
icyzkungz | 6:5665d427bceb | 151 | // 14 bit resolution (0) and 16 bit resolution (1) |
icyzkungz | 6:5665d427bceb | 152 | case MFS_14BITS: |
icyzkungz | 6:5665d427bceb | 153 | mRes = 10.0*4219.0/8190.0; // Proper scale to return milliGauss |
icyzkungz | 6:5665d427bceb | 154 | break; |
icyzkungz | 6:5665d427bceb | 155 | case MFS_16BITS: |
icyzkungz | 6:5665d427bceb | 156 | mRes = 10.0*4219.0/32760.0; // Proper scale to return milliGauss |
icyzkungz | 6:5665d427bceb | 157 | break; |
icyzkungz | 6:5665d427bceb | 158 | } |
icyzkungz | 6:5665d427bceb | 159 | } |
icyzkungz | 6:5665d427bceb | 160 | |
icyzkungz | 6:5665d427bceb | 161 | |
icyzkungz | 6:5665d427bceb | 162 | void MPU9250::setGres() |
icyzkungz | 6:5665d427bceb | 163 | { |
icyzkungz | 6:5665d427bceb | 164 | getGres(); |
icyzkungz | 6:5665d427bceb | 165 | switch (Gscale) { |
icyzkungz | 6:5665d427bceb | 166 | // Possible gyro scales (and their register bit settings) are: |
icyzkungz | 6:5665d427bceb | 167 | // 250 DPS (00), 500 DPS (01), 1000 DPS (10), and 2000 DPS (11). |
icyzkungz | 6:5665d427bceb | 168 | // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value: |
icyzkungz | 6:5665d427bceb | 169 | case GFS_250DPS: |
icyzkungz | 6:5665d427bceb | 170 | gRes = 250.0/32768.0; |
icyzkungz | 6:5665d427bceb | 171 | break; |
icyzkungz | 6:5665d427bceb | 172 | case GFS_500DPS: |
icyzkungz | 6:5665d427bceb | 173 | gRes = 500.0/32768.0; |
icyzkungz | 6:5665d427bceb | 174 | break; |
icyzkungz | 6:5665d427bceb | 175 | case GFS_1000DPS: |
icyzkungz | 6:5665d427bceb | 176 | gRes = 1000.0/32768.0; |
icyzkungz | 6:5665d427bceb | 177 | break; |
icyzkungz | 6:5665d427bceb | 178 | case GFS_2000DPS: |
icyzkungz | 6:5665d427bceb | 179 | gRes = 2000.0/32768.0; |
icyzkungz | 6:5665d427bceb | 180 | break; |
icyzkungz | 6:5665d427bceb | 181 | } |
icyzkungz | 6:5665d427bceb | 182 | } |
icyzkungz | 6:5665d427bceb | 183 | |
icyzkungz | 6:5665d427bceb | 184 | void MPU9250::setAres() |
icyzkungz | 6:5665d427bceb | 185 | { |
icyzkungz | 6:5665d427bceb | 186 | getAres(); |
icyzkungz | 6:5665d427bceb | 187 | switch (Ascale) { |
icyzkungz | 6:5665d427bceb | 188 | // Possible accelerometer scales (and their register bit settings) are: |
icyzkungz | 6:5665d427bceb | 189 | // 2 Gs (00), 4 Gs (01), 8 Gs (10), and 16 Gs (11). |
icyzkungz | 6:5665d427bceb | 190 | // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value: |
icyzkungz | 6:5665d427bceb | 191 | case AFS_2G: |
icyzkungz | 6:5665d427bceb | 192 | aRes = 2.0/32768.0; |
icyzkungz | 6:5665d427bceb | 193 | break; |
icyzkungz | 6:5665d427bceb | 194 | case AFS_4G: |
icyzkungz | 6:5665d427bceb | 195 | aRes = 4.0/32768.0; |
icyzkungz | 6:5665d427bceb | 196 | break; |
icyzkungz | 6:5665d427bceb | 197 | case AFS_8G: |
icyzkungz | 6:5665d427bceb | 198 | aRes = 8.0/32768.0; |
icyzkungz | 6:5665d427bceb | 199 | break; |
icyzkungz | 6:5665d427bceb | 200 | case AFS_16G: |
icyzkungz | 6:5665d427bceb | 201 | aRes = 16.0/32768.0; |
icyzkungz | 6:5665d427bceb | 202 | break; |
icyzkungz | 6:5665d427bceb | 203 | } |
icyzkungz | 6:5665d427bceb | 204 | } |
icyzkungz | 6:5665d427bceb | 205 | |
icyzkungz | 6:5665d427bceb | 206 | void MPU9250::getMres() |
icyzkungz | 6:5665d427bceb | 207 | { |
icyzkungz | 6:5665d427bceb | 208 | Mscale = MFS_16BITS; // MFS_14BITS or MFS_16BITS, 14-bit or 16-bit magnetometer resolution |
icyzkungz | 6:5665d427bceb | 209 | } |
icyzkungz | 6:5665d427bceb | 210 | |
icyzkungz | 6:5665d427bceb | 211 | |
icyzkungz | 6:5665d427bceb | 212 | void MPU9250::getGres() |
icyzkungz | 6:5665d427bceb | 213 | { |
icyzkungz | 6:5665d427bceb | 214 | Gscale = GFS_250DPS; // GFS_250DPS, GFS_500DPS, GFS_1000DPS, GFS_2000DPS |
icyzkungz | 6:5665d427bceb | 215 | } |
icyzkungz | 6:5665d427bceb | 216 | |
icyzkungz | 6:5665d427bceb | 217 | void MPU9250::getAres() |
icyzkungz | 6:5665d427bceb | 218 | { |
icyzkungz | 6:5665d427bceb | 219 | Ascale = AFS_2G; // AFS_2G, AFS_4G, AFS_8G, AFS_16G |
icyzkungz | 6:5665d427bceb | 220 | } |
icyzkungz | 6:5665d427bceb | 221 | |
icyzkungz | 6:5665d427bceb | 222 | void MPU9250::MagCal() |
icyzkungz | 6:5665d427bceb | 223 | { |
icyzkungz | 6:5665d427bceb | 224 | printf("START scan mag\n\r\n\r\n\r"); |
icyzkungz | 6:5665d427bceb | 225 | |
icyzkungz | 6:5665d427bceb | 226 | //Assign random value before calibrate |
icyzkungz | 6:5665d427bceb | 227 | /*xmax = -4914.0f; |
icyzkungz | 6:5665d427bceb | 228 | xmin = 4914.0f; |
icyzkungz | 6:5665d427bceb | 229 | |
icyzkungz | 6:5665d427bceb | 230 | ymax = -4914.0; |
icyzkungz | 6:5665d427bceb | 231 | ymin = 4914.0f; |
icyzkungz | 6:5665d427bceb | 232 | |
icyzkungz | 6:5665d427bceb | 233 | zmax = -4914.0; |
icyzkungz | 6:5665d427bceb | 234 | zmin = 4914.0f; |
icyzkungz | 6:5665d427bceb | 235 | |
icyzkungz | 6:5665d427bceb | 236 | change=false; |
icyzkungz | 6:5665d427bceb | 237 | |
icyzkungz | 6:5665d427bceb | 238 | while(1) { |
icyzkungz | 6:5665d427bceb | 239 | readMagData(magCount); |
icyzkungz | 6:5665d427bceb | 240 | |
icyzkungz | 6:5665d427bceb | 241 | if(magCount[0]<xmin) { |
icyzkungz | 6:5665d427bceb | 242 | xmin = magCount[0]; |
icyzkungz | 6:5665d427bceb | 243 | change = true; |
icyzkungz | 6:5665d427bceb | 244 | } |
icyzkungz | 6:5665d427bceb | 245 | if(magCount[0]>xmax) { |
icyzkungz | 6:5665d427bceb | 246 | xmax = magCount[0]; |
icyzkungz | 6:5665d427bceb | 247 | change = true; |
icyzkungz | 6:5665d427bceb | 248 | } |
icyzkungz | 6:5665d427bceb | 249 | |
icyzkungz | 6:5665d427bceb | 250 | if(magCount[1]<ymin) { |
icyzkungz | 6:5665d427bceb | 251 | ymin = magCount[1]; |
icyzkungz | 6:5665d427bceb | 252 | change = true; |
icyzkungz | 6:5665d427bceb | 253 | } |
icyzkungz | 6:5665d427bceb | 254 | if(magCount[1]>ymax) { |
icyzkungz | 6:5665d427bceb | 255 | ymax = magCount[1]; |
icyzkungz | 6:5665d427bceb | 256 | change = true; |
icyzkungz | 6:5665d427bceb | 257 | } |
icyzkungz | 6:5665d427bceb | 258 | |
icyzkungz | 6:5665d427bceb | 259 | |
icyzkungz | 6:5665d427bceb | 260 | if(magCount[2]<zmin) { |
icyzkungz | 6:5665d427bceb | 261 | zmin = magCount[2]; |
icyzkungz | 6:5665d427bceb | 262 | change = true; |
icyzkungz | 6:5665d427bceb | 263 | } |
icyzkungz | 6:5665d427bceb | 264 | if(magCount[2]>zmax) { |
icyzkungz | 6:5665d427bceb | 265 | zmax = magCount[2]; |
icyzkungz | 6:5665d427bceb | 266 | change = true; |
icyzkungz | 6:5665d427bceb | 267 | } |
icyzkungz | 6:5665d427bceb | 268 | |
icyzkungz | 6:5665d427bceb | 269 | if(change==true) { |
icyzkungz | 6:5665d427bceb | 270 | printf("Mx Max= %f Min= %f\n\r",xmax,xmin); |
icyzkungz | 6:5665d427bceb | 271 | printf("My Max= %f Min= %f\n\r",ymax,ymin); |
icyzkungz | 6:5665d427bceb | 272 | printf("Mz Max= %f Min= %f\n\r",zmax,zmin); |
icyzkungz | 6:5665d427bceb | 273 | change=false; |
icyzkungz | 6:5665d427bceb | 274 | }*/ |
icyzkungz | 6:5665d427bceb | 275 | |
icyzkungz | 6:5665d427bceb | 276 | //Out of Calibration loop |
icyzkungz | 6:5665d427bceb | 277 | /*if(button==1) { |
icyzkungz | 6:5665d427bceb | 278 | while(button==1); |
icyzkungz | 6:5665d427bceb | 279 | break; |
icyzkungz | 6:5665d427bceb | 280 | }*/ |
icyzkungz | 6:5665d427bceb | 281 | //} |
icyzkungz | 6:5665d427bceb | 282 | |
icyzkungz | 6:5665d427bceb | 283 | |
icyzkungz | 6:5665d427bceb | 284 | xmax = 188.000000; |
icyzkungz | 6:5665d427bceb | 285 | xmin = -316.000000; |
icyzkungz | 6:5665d427bceb | 286 | ymax = 485.000000; |
icyzkungz | 6:5665d427bceb | 287 | ymin = -26.000000; |
icyzkungz | 6:5665d427bceb | 288 | zmax = 165.000000; |
icyzkungz | 6:5665d427bceb | 289 | xmin = -230.000000; |
icyzkungz | 6:5665d427bceb | 290 | |
icyzkungz | 6:5665d427bceb | 291 | magbias[0] = -1.0; |
icyzkungz | 6:5665d427bceb | 292 | magbias[1] = -1.0; |
icyzkungz | 6:5665d427bceb | 293 | magbias[2] = -1.0; |
icyzkungz | 6:5665d427bceb | 294 | |
icyzkungz | 6:5665d427bceb | 295 | magCalibration[0] = 2.0f / (xmax -xmin); |
icyzkungz | 6:5665d427bceb | 296 | magCalibration[1] = 2.0f / (ymax -ymin); |
icyzkungz | 6:5665d427bceb | 297 | magCalibration[2] = 2.0f / (zmax -zmin); |
icyzkungz | 6:5665d427bceb | 298 | |
icyzkungz | 6:5665d427bceb | 299 | printf("mag[0] %f",magbias[0]); |
icyzkungz | 6:5665d427bceb | 300 | printf("mag[1] %f",magbias[1]); |
icyzkungz | 6:5665d427bceb | 301 | printf("mag[2] %f\n\r",magbias[2]); |
icyzkungz | 6:5665d427bceb | 302 | } |
icyzkungz | 6:5665d427bceb | 303 | |
icyzkungz | 6:5665d427bceb | 304 | void MPU9250::AccelXYZCal() |
icyzkungz | 6:5665d427bceb | 305 | { |
icyzkungz | 6:5665d427bceb | 306 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
icyzkungz | 6:5665d427bceb | 307 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
icyzkungz | 6:5665d427bceb | 308 | az = (float)accelCount[2]*aRes - accelBias[2]; |
icyzkungz | 6:5665d427bceb | 309 | } |
icyzkungz | 6:5665d427bceb | 310 | |
icyzkungz | 6:5665d427bceb | 311 | void MPU9250::GyroXYZCal() |
icyzkungz | 6:5665d427bceb | 312 | { |
icyzkungz | 6:5665d427bceb | 313 | gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set |
icyzkungz | 6:5665d427bceb | 314 | gy = (float)gyroCount[1]*gRes - gyroBias[1]; |
icyzkungz | 6:5665d427bceb | 315 | gz = (float)gyroCount[2]*gRes - gyroBias[2]; |
icyzkungz | 6:5665d427bceb | 316 | } |
icyzkungz | 6:5665d427bceb | 317 | |
icyzkungz | 6:5665d427bceb | 318 | void MPU9250::MagXYZCal() |
icyzkungz | 6:5665d427bceb | 319 | { |
icyzkungz | 6:5665d427bceb | 320 | mx = ((float)magCount[0]-xmin)*magCalibration[0] + magbias[0]; // get actual magnetometer value, this depends on scale being set |
icyzkungz | 6:5665d427bceb | 321 | my = ((float)magCount[1]-ymin)*magCalibration[1] + magbias[1]; |
icyzkungz | 6:5665d427bceb | 322 | mz = ((float)magCount[2]-zmin)*magCalibration[2] + magbias[2]; |
icyzkungz | 6:5665d427bceb | 323 | } |
icyzkungz | 6:5665d427bceb | 324 | |
icyzkungz | 6:5665d427bceb | 325 | |
icyzkungz | 6:5665d427bceb | 326 | void MPU9250::readAccelData() |
icyzkungz | 6:5665d427bceb | 327 | { |
icyzkungz | 6:5665d427bceb | 328 | float destination[3] = {0,0,0}; |
icyzkungz | 6:5665d427bceb | 329 | uint8_t rawData[6]; // x/y/z accel register data stored here |
icyzkungz | 6:5665d427bceb | 330 | readBytes(MPU9250_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]); // Read the six raw data registers into data array |
icyzkungz | 6:5665d427bceb | 331 | destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value |
icyzkungz | 6:5665d427bceb | 332 | destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ; |
icyzkungz | 6:5665d427bceb | 333 | destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ; |
icyzkungz | 6:5665d427bceb | 334 | |
icyzkungz | 6:5665d427bceb | 335 | for(int i=0; i<=2; i++) |
icyzkungz | 6:5665d427bceb | 336 | accelCount[i] = (float)destination[i]; |
icyzkungz | 6:5665d427bceb | 337 | } |
icyzkungz | 6:5665d427bceb | 338 | |
icyzkungz | 6:5665d427bceb | 339 | void MPU9250::readGyroData() |
icyzkungz | 6:5665d427bceb | 340 | { |
icyzkungz | 6:5665d427bceb | 341 | float destination[3] = {0,0,0}; |
icyzkungz | 6:5665d427bceb | 342 | uint8_t rawData[6]; // x/y/z gyro register data stored here |
icyzkungz | 6:5665d427bceb | 343 | readBytes(MPU9250_ADDRESS, GYRO_XOUT_H, 6, &rawData[0]); // Read the six raw data registers sequentially into data array |
icyzkungz | 6:5665d427bceb | 344 | destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value |
icyzkungz | 6:5665d427bceb | 345 | destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ; |
icyzkungz | 6:5665d427bceb | 346 | destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ; |
icyzkungz | 6:5665d427bceb | 347 | |
icyzkungz | 6:5665d427bceb | 348 | for(int i=0; i<=2; i++) |
icyzkungz | 6:5665d427bceb | 349 | gyroCount[i] = (float)destination[i]; |
icyzkungz | 6:5665d427bceb | 350 | } |
icyzkungz | 6:5665d427bceb | 351 | |
icyzkungz | 6:5665d427bceb | 352 | void MPU9250::readMagData() |
icyzkungz | 6:5665d427bceb | 353 | { |
icyzkungz | 6:5665d427bceb | 354 | float destination[3] = {0,0,0}; |
icyzkungz | 6:5665d427bceb | 355 | uint8_t rawData[7]; // x/y/z gyro register data, ST2 register stored here, must read ST2 at end of data acquisition |
icyzkungz | 6:5665d427bceb | 356 | if(readByte(AK8963_ADDRESS, AK8963_ST1) & 0x01) { // wait for magnetometer data ready bit to be set |
icyzkungz | 6:5665d427bceb | 357 | readBytes(AK8963_ADDRESS, AK8963_XOUT_L, 7, &rawData[0]); // Read the six raw data and ST2 registers sequentially into data array |
icyzkungz | 6:5665d427bceb | 358 | uint8_t c = rawData[6]; // End data read by reading ST2 register |
icyzkungz | 6:5665d427bceb | 359 | if(!(c & 0x08)) { // Check if magnetic sensor overflow set, if not then report data |
icyzkungz | 6:5665d427bceb | 360 | destination[0] = (int16_t)(((int16_t)rawData[1] << 8) | rawData[0]); // Turn the MSB and LSB into a signed 16-bit value |
icyzkungz | 6:5665d427bceb | 361 | destination[1] = (int16_t)(((int16_t)rawData[3] << 8) | rawData[2]) ; // Data stored as little Endian |
icyzkungz | 6:5665d427bceb | 362 | destination[2] = (int16_t)(((int16_t)rawData[5] << 8) | rawData[4]) ; |
icyzkungz | 6:5665d427bceb | 363 | } |
icyzkungz | 6:5665d427bceb | 364 | } |
icyzkungz | 6:5665d427bceb | 365 | |
icyzkungz | 6:5665d427bceb | 366 | for(int i=0; i<=2; i++) |
icyzkungz | 6:5665d427bceb | 367 | magCount[i] = (float)destination[i]; |
icyzkungz | 6:5665d427bceb | 368 | } |
icyzkungz | 6:5665d427bceb | 369 | |
icyzkungz | 6:5665d427bceb | 370 | void MPU9250::readTempData() |
icyzkungz | 6:5665d427bceb | 371 | { |
icyzkungz | 6:5665d427bceb | 372 | int16_t destination; |
icyzkungz | 6:5665d427bceb | 373 | uint8_t rawData[2]; // x/y/z gyro register data stored here |
icyzkungz | 6:5665d427bceb | 374 | readBytes(MPU9250_ADDRESS, TEMP_OUT_H, 2, &rawData[0]); // Read the two raw data registers sequentially into data array |
icyzkungz | 6:5665d427bceb | 375 | destination = (int16_t)(((int16_t)rawData[0]) << 8 | rawData[1]) ; // Turn the MSB and LSB into a 16-bit value |
icyzkungz | 6:5665d427bceb | 376 | destination = ((float) destination) / 333.87f + 21.0f; |
icyzkungz | 6:5665d427bceb | 377 | temperature = destination; |
icyzkungz | 6:5665d427bceb | 378 | } |
icyzkungz | 6:5665d427bceb | 379 | |
icyzkungz | 6:5665d427bceb | 380 | |
icyzkungz | 6:5665d427bceb | 381 | void MPU9250::resetMPU9250() |
icyzkungz | 6:5665d427bceb | 382 | { |
icyzkungz | 6:5665d427bceb | 383 | // reset device |
icyzkungz | 6:5665d427bceb | 384 | writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device |
icyzkungz | 6:5665d427bceb | 385 | wait(0.1); |
icyzkungz | 6:5665d427bceb | 386 | } |
icyzkungz | 6:5665d427bceb | 387 | |
icyzkungz | 6:5665d427bceb | 388 | void MPU9250::initAK8963() |
icyzkungz | 6:5665d427bceb | 389 | { |
icyzkungz | 6:5665d427bceb | 390 | float destination[3] = {0,0,0}; |
icyzkungz | 6:5665d427bceb | 391 | // First extract the factory calibration for each magnetometer axis |
icyzkungz | 6:5665d427bceb | 392 | uint8_t rawData[3]; // x/y/z gyro calibration data stored here |
icyzkungz | 6:5665d427bceb | 393 | writeByte(AK8963_ADDRESS, AK8963_CNTL, 0x00); // Power down magnetometer |
icyzkungz | 6:5665d427bceb | 394 | wait(0.01); |
icyzkungz | 6:5665d427bceb | 395 | writeByte(AK8963_ADDRESS, AK8963_CNTL, 0x0F); // Enter Fuse ROM access mode |
icyzkungz | 6:5665d427bceb | 396 | wait(0.01); |
icyzkungz | 6:5665d427bceb | 397 | readBytes(AK8963_ADDRESS, AK8963_ASAX, 3, &rawData[0]); // Read the x-, y-, and z-axis calibration values |
icyzkungz | 6:5665d427bceb | 398 | destination[0] = (float)(rawData[0] - 128)/256.0f + 1.0f; // Return x-axis sensitivity adjustment values, etc. |
icyzkungz | 6:5665d427bceb | 399 | destination[1] = (float)(rawData[1] - 128)/256.0f + 1.0f; |
icyzkungz | 6:5665d427bceb | 400 | destination[2] = (float)(rawData[2] - 128)/256.0f + 1.0f; |
icyzkungz | 6:5665d427bceb | 401 | writeByte(AK8963_ADDRESS, AK8963_CNTL, 0x00); // Power down magnetometer |
icyzkungz | 6:5665d427bceb | 402 | wait(0.01); |
icyzkungz | 6:5665d427bceb | 403 | // Configure the magnetometer for continuous read and highest resolution |
icyzkungz | 6:5665d427bceb | 404 | // set Mscale bit 4 to 1 (0) to enable 16 (14) bit resolution in CNTL register, |
icyzkungz | 6:5665d427bceb | 405 | // and enable continuous mode data acquisition Mmode (bits [3:0]), 0010 for 8 Hz and 0110 for 100 Hz sample rates |
icyzkungz | 6:5665d427bceb | 406 | writeByte(AK8963_ADDRESS, AK8963_CNTL, Mscale << 4 | Mmode); // Set magnetometer data resolution and sample ODR |
icyzkungz | 6:5665d427bceb | 407 | wait(0.01); |
icyzkungz | 6:5665d427bceb | 408 | |
icyzkungz | 6:5665d427bceb | 409 | for(int i=0; i<=2; i++) |
icyzkungz | 6:5665d427bceb | 410 | magCalibration[i] = destination[i]; |
icyzkungz | 6:5665d427bceb | 411 | } |
icyzkungz | 6:5665d427bceb | 412 | |
icyzkungz | 6:5665d427bceb | 413 | |
icyzkungz | 6:5665d427bceb | 414 | void MPU9250::initMPU9250() |
icyzkungz | 6:5665d427bceb | 415 | { |
icyzkungz | 6:5665d427bceb | 416 | // Initialize MPU9250 device |
icyzkungz | 6:5665d427bceb | 417 | // wake up device |
icyzkungz | 6:5665d427bceb | 418 | writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x00); // Clear sleep mode bit (6), enable all sensors |
icyzkungz | 6:5665d427bceb | 419 | wait(0.1); // Delay 100 ms for PLL to get established on x-axis gyro; should check for PLL ready interrupt |
icyzkungz | 6:5665d427bceb | 420 | |
icyzkungz | 6:5665d427bceb | 421 | // get stable time source |
icyzkungz | 6:5665d427bceb | 422 | writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x01); // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001 |
icyzkungz | 6:5665d427bceb | 423 | |
icyzkungz | 6:5665d427bceb | 424 | // Configure Gyro and Accelerometer |
icyzkungz | 6:5665d427bceb | 425 | // Disable FSYNC and set accelerometer and gyro bandwidth to 44 and 42 Hz, respectively; |
icyzkungz | 6:5665d427bceb | 426 | // DLPF_CFG = bits 2:0 = 010; this sets the sample rate at 1 kHz for both |
icyzkungz | 6:5665d427bceb | 427 | // Maximum delay is 4.9 ms which is just over a 200 Hz maximum rate |
icyzkungz | 6:5665d427bceb | 428 | writeByte(MPU9250_ADDRESS, CONFIG, 0x03); |
icyzkungz | 6:5665d427bceb | 429 | |
icyzkungz | 6:5665d427bceb | 430 | // Set sample rate = gyroscope output rate/(1 + SMPLRT_DIV) |
icyzkungz | 6:5665d427bceb | 431 | writeByte(MPU9250_ADDRESS, SMPLRT_DIV, 0x04); // Use a 200 Hz rate; the same rate set in CONFIG above |
icyzkungz | 6:5665d427bceb | 432 | |
icyzkungz | 6:5665d427bceb | 433 | // Set gyroscope full scale range |
icyzkungz | 6:5665d427bceb | 434 | // Range selects FS_SEL and AFS_SEL are 0 - 3, so 2-bit values are left-shifted into positions 4:3 |
icyzkungz | 6:5665d427bceb | 435 | uint8_t c = readByte(MPU9250_ADDRESS, GYRO_CONFIG); |
icyzkungz | 6:5665d427bceb | 436 | writeByte(MPU9250_ADDRESS, GYRO_CONFIG, c & ~0xE0); // Clear self-test bits [7:5] |
icyzkungz | 6:5665d427bceb | 437 | writeByte(MPU9250_ADDRESS, GYRO_CONFIG, c & ~0x18); // Clear AFS bits [4:3] |
icyzkungz | 6:5665d427bceb | 438 | writeByte(MPU9250_ADDRESS, GYRO_CONFIG, c | Gscale << 3); // Set full scale range for the gyro |
icyzkungz | 6:5665d427bceb | 439 | |
icyzkungz | 6:5665d427bceb | 440 | // Set accelerometer configuration |
icyzkungz | 6:5665d427bceb | 441 | c = readByte(MPU9250_ADDRESS, ACCEL_CONFIG); |
icyzkungz | 6:5665d427bceb | 442 | writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, c & ~0xE0); // Clear self-test bits [7:5] |
icyzkungz | 6:5665d427bceb | 443 | writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, c & ~0x18); // Clear AFS bits [4:3] |
icyzkungz | 6:5665d427bceb | 444 | writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, c | Ascale << 3); // Set full scale range for the accelerometer |
icyzkungz | 6:5665d427bceb | 445 | |
icyzkungz | 6:5665d427bceb | 446 | // Set accelerometer sample rate configuration |
icyzkungz | 6:5665d427bceb | 447 | // It is possible to get a 4 kHz sample rate from the accelerometer by choosing 1 for |
icyzkungz | 6:5665d427bceb | 448 | // accel_fchoice_b bit [3]; in this case the bandwidth is 1.13 kHz |
icyzkungz | 6:5665d427bceb | 449 | c = readByte(MPU9250_ADDRESS, ACCEL_CONFIG2); |
icyzkungz | 6:5665d427bceb | 450 | writeByte(MPU9250_ADDRESS, ACCEL_CONFIG2, c & ~0x0F); // Clear accel_fchoice_b (bit 3) and A_DLPFG (bits [2:0]) |
icyzkungz | 6:5665d427bceb | 451 | writeByte(MPU9250_ADDRESS, ACCEL_CONFIG2, c | 0x03); // Set accelerometer rate to 1 kHz and bandwidth to 41 Hz |
icyzkungz | 6:5665d427bceb | 452 | |
icyzkungz | 6:5665d427bceb | 453 | // The accelerometer, gyro, and thermometer are set to 1 kHz sample rates, |
icyzkungz | 6:5665d427bceb | 454 | // but all these rates are further reduced by a factor of 5 to 200 Hz because of the SMPLRT_DIV setting |
icyzkungz | 6:5665d427bceb | 455 | |
icyzkungz | 6:5665d427bceb | 456 | // Configure Interrupts and Bypass Enable |
icyzkungz | 6:5665d427bceb | 457 | // Set interrupt pin active high, push-pull, and clear on read of INT_STATUS, enable I2C_BYPASS_EN so additional chips |
icyzkungz | 6:5665d427bceb | 458 | // can join the I2C bus and all can be controlled by the Arduino as master |
icyzkungz | 6:5665d427bceb | 459 | writeByte(MPU9250_ADDRESS, INT_PIN_CFG, 0x22); |
icyzkungz | 6:5665d427bceb | 460 | writeByte(MPU9250_ADDRESS, INT_ENABLE, 0x01); // Enable data ready (bit 0) interrupt |
icyzkungz | 6:5665d427bceb | 461 | } |
icyzkungz | 6:5665d427bceb | 462 | |
icyzkungz | 6:5665d427bceb | 463 | // Function which accumulates gyro and accelerometer data after device initialization. It calculates the average |
icyzkungz | 6:5665d427bceb | 464 | // of the at-rest readings and then loads the resulting offsets into accelerometer and gyro bias registers. |
icyzkungz | 6:5665d427bceb | 465 | void MPU9250::calibrateMPU9250() |
icyzkungz | 6:5665d427bceb | 466 | { |
icyzkungz | 6:5665d427bceb | 467 | uint8_t data[12]; // data array to hold accelerometer and gyro x, y, z, data |
icyzkungz | 6:5665d427bceb | 468 | uint16_t ii, packet_count, fifo_count; |
icyzkungz | 6:5665d427bceb | 469 | int32_t gyro_bias[3] = {0, 0, 0}, accel_bias[3] = {0, 0, 0}; |
icyzkungz | 6:5665d427bceb | 470 | |
icyzkungz | 6:5665d427bceb | 471 | // reset device, reset all registers, clear gyro and accelerometer bias registers |
icyzkungz | 6:5665d427bceb | 472 | writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device |
icyzkungz | 6:5665d427bceb | 473 | wait(0.1); |
icyzkungz | 6:5665d427bceb | 474 | |
icyzkungz | 6:5665d427bceb | 475 | // get stable time source |
icyzkungz | 6:5665d427bceb | 476 | // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001 |
icyzkungz | 6:5665d427bceb | 477 | writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x01); |
icyzkungz | 6:5665d427bceb | 478 | writeByte(MPU9250_ADDRESS, PWR_MGMT_2, 0x00); |
icyzkungz | 6:5665d427bceb | 479 | wait(0.2); |
icyzkungz | 6:5665d427bceb | 480 | |
icyzkungz | 6:5665d427bceb | 481 | // Configure device for bias calculation |
icyzkungz | 6:5665d427bceb | 482 | writeByte(MPU9250_ADDRESS, INT_ENABLE, 0x00); // Disable all interrupts |
icyzkungz | 6:5665d427bceb | 483 | writeByte(MPU9250_ADDRESS, FIFO_EN, 0x00); // Disable FIFO |
icyzkungz | 6:5665d427bceb | 484 | writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x00); // Turn on internal clock source |
icyzkungz | 6:5665d427bceb | 485 | writeByte(MPU9250_ADDRESS, I2C_MST_CTRL, 0x00); // Disable I2C master |
icyzkungz | 6:5665d427bceb | 486 | writeByte(MPU9250_ADDRESS, USER_CTRL, 0x00); // Disable FIFO and I2C master modes |
icyzkungz | 6:5665d427bceb | 487 | writeByte(MPU9250_ADDRESS, USER_CTRL, 0x0C); // Reset FIFO and DMP |
icyzkungz | 6:5665d427bceb | 488 | wait(0.015); |
icyzkungz | 6:5665d427bceb | 489 | |
icyzkungz | 6:5665d427bceb | 490 | // Configure MPU9250 gyro and accelerometer for bias calculation |
icyzkungz | 6:5665d427bceb | 491 | writeByte(MPU9250_ADDRESS, CONFIG, 0x01); // Set low-pass filter to 188 Hz |
icyzkungz | 6:5665d427bceb | 492 | writeByte(MPU9250_ADDRESS, SMPLRT_DIV, 0x00); // Set sample rate to 1 kHz |
icyzkungz | 6:5665d427bceb | 493 | writeByte(MPU9250_ADDRESS, GYRO_CONFIG, 0x00); // Set gyro full-scale to 250 degrees per second, maximum sensitivity |
icyzkungz | 6:5665d427bceb | 494 | writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, 0x00); // Set accelerometer full-scale to 2 g, maximum sensitivity |
icyzkungz | 6:5665d427bceb | 495 | |
icyzkungz | 6:5665d427bceb | 496 | uint16_t gyrosensitivity = 131; // = 131 LSB/degrees/sec |
icyzkungz | 6:5665d427bceb | 497 | uint16_t accelsensitivity = 16384; // = 16384 LSB/g |
icyzkungz | 6:5665d427bceb | 498 | |
icyzkungz | 6:5665d427bceb | 499 | // Configure FIFO to capture accelerometer and gyro data for bias calculation |
icyzkungz | 6:5665d427bceb | 500 | writeByte(MPU9250_ADDRESS, USER_CTRL, 0x40); // Enable FIFO |
icyzkungz | 6:5665d427bceb | 501 | writeByte(MPU9250_ADDRESS, FIFO_EN, 0x78); // Enable gyro and accelerometer sensors for FIFO (max size 512 bytes in MPU-9250) |
icyzkungz | 6:5665d427bceb | 502 | wait(0.04); // accumulate 40 samples in 80 milliseconds = 480 bytes |
icyzkungz | 6:5665d427bceb | 503 | |
icyzkungz | 6:5665d427bceb | 504 | // At end of sample accumulation, turn off FIFO sensor read |
icyzkungz | 6:5665d427bceb | 505 | writeByte(MPU9250_ADDRESS, FIFO_EN, 0x00); // Disable gyro and accelerometer sensors for FIFO |
icyzkungz | 6:5665d427bceb | 506 | readBytes(MPU9250_ADDRESS, FIFO_COUNTH, 2, &data[0]); // read FIFO sample count |
icyzkungz | 6:5665d427bceb | 507 | fifo_count = ((uint16_t)data[0] << 8) | data[1]; |
icyzkungz | 6:5665d427bceb | 508 | packet_count = fifo_count/12;// How many sets of full gyro and accelerometer data for averaging |
icyzkungz | 6:5665d427bceb | 509 | |
icyzkungz | 6:5665d427bceb | 510 | for (ii = 0; ii < packet_count; ii++) { |
icyzkungz | 6:5665d427bceb | 511 | int16_t accel_temp[3] = {0, 0, 0}, gyro_temp[3] = {0, 0, 0}; |
icyzkungz | 6:5665d427bceb | 512 | readBytes(MPU9250_ADDRESS, FIFO_R_W, 12, &data[0]); // read data for averaging |
icyzkungz | 6:5665d427bceb | 513 | accel_temp[0] = (int16_t) (((int16_t)data[0] << 8) | data[1] ) ; // Form signed 16-bit integer for each sample in FIFO |
icyzkungz | 6:5665d427bceb | 514 | accel_temp[1] = (int16_t) (((int16_t)data[2] << 8) | data[3] ) ; |
icyzkungz | 6:5665d427bceb | 515 | accel_temp[2] = (int16_t) (((int16_t)data[4] << 8) | data[5] ) ; |
icyzkungz | 6:5665d427bceb | 516 | gyro_temp[0] = (int16_t) (((int16_t)data[6] << 8) | data[7] ) ; |
icyzkungz | 6:5665d427bceb | 517 | gyro_temp[1] = (int16_t) (((int16_t)data[8] << 8) | data[9] ) ; |
icyzkungz | 6:5665d427bceb | 518 | gyro_temp[2] = (int16_t) (((int16_t)data[10] << 8) | data[11]) ; |
icyzkungz | 6:5665d427bceb | 519 | |
icyzkungz | 6:5665d427bceb | 520 | accel_bias[0] += (int32_t) accel_temp[0]; // Sum individual signed 16-bit biases to get accumulated signed 32-bit biases |
icyzkungz | 6:5665d427bceb | 521 | accel_bias[1] += (int32_t) accel_temp[1]; |
icyzkungz | 6:5665d427bceb | 522 | accel_bias[2] += (int32_t) accel_temp[2]; |
icyzkungz | 6:5665d427bceb | 523 | gyro_bias[0] += (int32_t) gyro_temp[0]; |
icyzkungz | 6:5665d427bceb | 524 | gyro_bias[1] += (int32_t) gyro_temp[1]; |
icyzkungz | 6:5665d427bceb | 525 | gyro_bias[2] += (int32_t) gyro_temp[2]; |
icyzkungz | 6:5665d427bceb | 526 | |
icyzkungz | 6:5665d427bceb | 527 | } |
icyzkungz | 6:5665d427bceb | 528 | accel_bias[0] /= (int32_t) packet_count; // Normalize sums to get average count biases |
icyzkungz | 6:5665d427bceb | 529 | accel_bias[1] /= (int32_t) packet_count; |
icyzkungz | 6:5665d427bceb | 530 | accel_bias[2] /= (int32_t) packet_count; |
icyzkungz | 6:5665d427bceb | 531 | gyro_bias[0] /= (int32_t) packet_count; |
icyzkungz | 6:5665d427bceb | 532 | gyro_bias[1] /= (int32_t) packet_count; |
icyzkungz | 6:5665d427bceb | 533 | gyro_bias[2] /= (int32_t) packet_count; |
icyzkungz | 6:5665d427bceb | 534 | |
icyzkungz | 6:5665d427bceb | 535 | if(accel_bias[2] > 0L) { |
icyzkungz | 6:5665d427bceb | 536 | accel_bias[2] -= (int32_t) accelsensitivity; // Remove gravity from the z-axis accelerometer bias calculation |
icyzkungz | 6:5665d427bceb | 537 | } else { |
icyzkungz | 6:5665d427bceb | 538 | accel_bias[2] += (int32_t) accelsensitivity; |
icyzkungz | 6:5665d427bceb | 539 | } |
icyzkungz | 6:5665d427bceb | 540 | |
icyzkungz | 6:5665d427bceb | 541 | // Construct the gyro biases for push to the hardware gyro bias registers, which are reset to zero upon device startup |
icyzkungz | 6:5665d427bceb | 542 | 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 |
icyzkungz | 6:5665d427bceb | 543 | data[1] = (-gyro_bias[0]/4) & 0xFF; // Biases are additive, so change sign on calculated average gyro biases |
icyzkungz | 6:5665d427bceb | 544 | data[2] = (-gyro_bias[1]/4 >> 8) & 0xFF; |
icyzkungz | 6:5665d427bceb | 545 | data[3] = (-gyro_bias[1]/4) & 0xFF; |
icyzkungz | 6:5665d427bceb | 546 | data[4] = (-gyro_bias[2]/4 >> 8) & 0xFF; |
icyzkungz | 6:5665d427bceb | 547 | data[5] = (-gyro_bias[2]/4) & 0xFF; |
icyzkungz | 6:5665d427bceb | 548 | |
icyzkungz | 6:5665d427bceb | 549 | /// Push gyro biases to hardware registers |
icyzkungz | 6:5665d427bceb | 550 | /* writeByte(MPU9250_ADDRESS, XG_OFFSET_H, data[0]); |
icyzkungz | 6:5665d427bceb | 551 | writeByte(MPU9250_ADDRESS, XG_OFFSET_L, data[1]); |
icyzkungz | 6:5665d427bceb | 552 | writeByte(MPU9250_ADDRESS, YG_OFFSET_H, data[2]); |
icyzkungz | 6:5665d427bceb | 553 | writeByte(MPU9250_ADDRESS, YG_OFFSET_L, data[3]); |
icyzkungz | 6:5665d427bceb | 554 | writeByte(MPU9250_ADDRESS, ZG_OFFSET_H, data[4]); |
icyzkungz | 6:5665d427bceb | 555 | writeByte(MPU9250_ADDRESS, ZG_OFFSET_L, data[5]); |
icyzkungz | 6:5665d427bceb | 556 | */ |
icyzkungz | 6:5665d427bceb | 557 | gyroBias[0] = (float) gyro_bias[0]/(float) gyrosensitivity; // construct gyro bias in deg/s for later manual subtraction |
icyzkungz | 6:5665d427bceb | 558 | gyroBias[1] = (float) gyro_bias[1]/(float) gyrosensitivity; |
icyzkungz | 6:5665d427bceb | 559 | gyroBias[2] = (float) gyro_bias[2]/(float) gyrosensitivity; |
icyzkungz | 6:5665d427bceb | 560 | |
icyzkungz | 6:5665d427bceb | 561 | // Construct the accelerometer biases for push to the hardware accelerometer bias registers. These registers contain |
icyzkungz | 6:5665d427bceb | 562 | // factory trim values which must be added to the calculated accelerometer biases; on boot up these registers will hold |
icyzkungz | 6:5665d427bceb | 563 | // non-zero values. In addition, bit 0 of the lower byte must be preserved since it is used for temperature |
icyzkungz | 6:5665d427bceb | 564 | // compensation calculations. Accelerometer bias registers expect bias input as 2048 LSB per g, so that |
icyzkungz | 6:5665d427bceb | 565 | // the accelerometer biases calculated above must be divided by 8. |
icyzkungz | 6:5665d427bceb | 566 | |
icyzkungz | 6:5665d427bceb | 567 | int32_t accel_bias_reg[3] = {0, 0, 0}; // A place to hold the factory accelerometer trim biases |
icyzkungz | 6:5665d427bceb | 568 | readBytes(MPU9250_ADDRESS, XA_OFFSET_H, 2, &data[0]); // Read factory accelerometer trim values |
icyzkungz | 6:5665d427bceb | 569 | accel_bias_reg[0] = (int16_t) ((int16_t)data[0] << 8) | data[1]; |
icyzkungz | 6:5665d427bceb | 570 | readBytes(MPU9250_ADDRESS, YA_OFFSET_H, 2, &data[0]); |
icyzkungz | 6:5665d427bceb | 571 | accel_bias_reg[1] = (int16_t) ((int16_t)data[0] << 8) | data[1]; |
icyzkungz | 6:5665d427bceb | 572 | readBytes(MPU9250_ADDRESS, ZA_OFFSET_H, 2, &data[0]); |
icyzkungz | 6:5665d427bceb | 573 | accel_bias_reg[2] = (int16_t) ((int16_t)data[0] << 8) | data[1]; |
icyzkungz | 6:5665d427bceb | 574 | |
icyzkungz | 6:5665d427bceb | 575 | uint32_t mask = 1uL; // Define mask for temperature compensation bit 0 of lower byte of accelerometer bias registers |
icyzkungz | 6:5665d427bceb | 576 | uint8_t mask_bit[3] = {0, 0, 0}; // Define array to hold mask bit for each accelerometer bias axis |
icyzkungz | 6:5665d427bceb | 577 | |
icyzkungz | 6:5665d427bceb | 578 | for(ii = 0; ii < 3; ii++) { |
icyzkungz | 6:5665d427bceb | 579 | if(accel_bias_reg[ii] & mask) mask_bit[ii] = 0x01; // If temperature compensation bit is set, record that fact in mask_bit |
icyzkungz | 6:5665d427bceb | 580 | } |
icyzkungz | 6:5665d427bceb | 581 | |
icyzkungz | 6:5665d427bceb | 582 | // Construct total accelerometer bias, including calculated average accelerometer bias from above |
icyzkungz | 6:5665d427bceb | 583 | accel_bias_reg[0] -= (accel_bias[0]/8); // Subtract calculated averaged accelerometer bias scaled to 2048 LSB/g (16 g full scale) |
icyzkungz | 6:5665d427bceb | 584 | accel_bias_reg[1] -= (accel_bias[1]/8); |
icyzkungz | 6:5665d427bceb | 585 | accel_bias_reg[2] -= (accel_bias[2]/8); |
icyzkungz | 6:5665d427bceb | 586 | |
icyzkungz | 6:5665d427bceb | 587 | data[0] = (accel_bias_reg[0] >> 8) & 0xFF; |
icyzkungz | 6:5665d427bceb | 588 | data[1] = (accel_bias_reg[0]) & 0xFF; |
icyzkungz | 6:5665d427bceb | 589 | data[1] = data[1] | mask_bit[0]; // preserve temperature compensation bit when writing back to accelerometer bias registers |
icyzkungz | 6:5665d427bceb | 590 | data[2] = (accel_bias_reg[1] >> 8) & 0xFF; |
icyzkungz | 6:5665d427bceb | 591 | data[3] = (accel_bias_reg[1]) & 0xFF; |
icyzkungz | 6:5665d427bceb | 592 | data[3] = data[3] | mask_bit[1]; // preserve temperature compensation bit when writing back to accelerometer bias registers |
icyzkungz | 6:5665d427bceb | 593 | data[4] = (accel_bias_reg[2] >> 8) & 0xFF; |
icyzkungz | 6:5665d427bceb | 594 | data[5] = (accel_bias_reg[2]) & 0xFF; |
icyzkungz | 6:5665d427bceb | 595 | data[5] = data[5] | mask_bit[2]; // preserve temperature compensation bit when writing back to accelerometer bias registers |
icyzkungz | 6:5665d427bceb | 596 | |
icyzkungz | 6:5665d427bceb | 597 | // Apparently this is not working for the acceleration biases in the MPU-9250 |
icyzkungz | 6:5665d427bceb | 598 | // Are we handling the temperature correction bit properly? |
icyzkungz | 6:5665d427bceb | 599 | // Push accelerometer biases to hardware registers |
icyzkungz | 6:5665d427bceb | 600 | /* writeByte(MPU9250_ADDRESS, XA_OFFSET_H, data[0]); |
icyzkungz | 6:5665d427bceb | 601 | writeByte(MPU9250_ADDRESS, XA_OFFSET_L, data[1]); |
icyzkungz | 6:5665d427bceb | 602 | writeByte(MPU9250_ADDRESS, YA_OFFSET_H, data[2]); |
icyzkungz | 6:5665d427bceb | 603 | writeByte(MPU9250_ADDRESS, YA_OFFSET_L, data[3]); |
icyzkungz | 6:5665d427bceb | 604 | writeByte(MPU9250_ADDRESS, ZA_OFFSET_H, data[4]); |
icyzkungz | 6:5665d427bceb | 605 | writeByte(MPU9250_ADDRESS, ZA_OFFSET_L, data[5]); |
icyzkungz | 6:5665d427bceb | 606 | */ |
icyzkungz | 6:5665d427bceb | 607 | // Output scaled accelerometer biases for manual subtraction in the main program |
icyzkungz | 6:5665d427bceb | 608 | accelBias[0] = (float)accel_bias[0]/(float)accelsensitivity; |
icyzkungz | 6:5665d427bceb | 609 | accelBias[1] = (float)accel_bias[1]/(float)accelsensitivity; |
icyzkungz | 6:5665d427bceb | 610 | accelBias[2] = (float)accel_bias[2]/(float)accelsensitivity; |
icyzkungz | 6:5665d427bceb | 611 | } |
icyzkungz | 6:5665d427bceb | 612 | |
icyzkungz | 6:5665d427bceb | 613 | |
icyzkungz | 6:5665d427bceb | 614 | // Accelerometer and gyroscope self test; check calibration wrt factory settings |
icyzkungz | 6:5665d427bceb | 615 | void MPU9250::MPU9250SelfTest() // Should return percent deviation from factory trim values, +/- 14 or less deviation is a pass |
icyzkungz | 6:5665d427bceb | 616 | { |
icyzkungz | 6:5665d427bceb | 617 | //float destination[6] = {0,0,0,0,0,0}; |
icyzkungz | 6:5665d427bceb | 618 | uint8_t rawData[6] = {0, 0, 0, 0, 0, 0}; |
icyzkungz | 6:5665d427bceb | 619 | uint8_t selfTest[6]; |
icyzkungz | 6:5665d427bceb | 620 | int16_t gAvg[3], aAvg[3], aSTAvg[3], gSTAvg[3]; |
icyzkungz | 6:5665d427bceb | 621 | float factoryTrim[6]; |
icyzkungz | 6:5665d427bceb | 622 | uint8_t FS = 0; |
icyzkungz | 6:5665d427bceb | 623 | |
icyzkungz | 6:5665d427bceb | 624 | writeByte(MPU9250_ADDRESS, SMPLRT_DIV, 0x00); // Set gyro sample rate to 1 kHz |
icyzkungz | 6:5665d427bceb | 625 | writeByte(MPU9250_ADDRESS, CONFIG, 0x02); // Set gyro sample rate to 1 kHz and DLPF to 92 Hz |
icyzkungz | 6:5665d427bceb | 626 | writeByte(MPU9250_ADDRESS, GYRO_CONFIG, 1<<FS); // Set full scale range for the gyro to 250 dps |
icyzkungz | 6:5665d427bceb | 627 | writeByte(MPU9250_ADDRESS, ACCEL_CONFIG2, 0x02); // Set accelerometer rate to 1 kHz and bandwidth to 92 Hz |
icyzkungz | 6:5665d427bceb | 628 | writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, 1<<FS); // Set full scale range for the accelerometer to 2 g |
icyzkungz | 6:5665d427bceb | 629 | |
icyzkungz | 6:5665d427bceb | 630 | for( int ii = 0; ii < 200; ii++) { // get average current values of gyro and acclerometer |
icyzkungz | 6:5665d427bceb | 631 | |
icyzkungz | 6:5665d427bceb | 632 | readBytes(MPU9250_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]); // Read the six raw data registers into data array |
icyzkungz | 6:5665d427bceb | 633 | aAvg[0] += (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value |
icyzkungz | 6:5665d427bceb | 634 | aAvg[1] += (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ; |
icyzkungz | 6:5665d427bceb | 635 | aAvg[2] += (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ; |
icyzkungz | 6:5665d427bceb | 636 | |
icyzkungz | 6:5665d427bceb | 637 | readBytes(MPU9250_ADDRESS, GYRO_XOUT_H, 6, &rawData[0]); // Read the six raw data registers sequentially into data array |
icyzkungz | 6:5665d427bceb | 638 | gAvg[0] += (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value |
icyzkungz | 6:5665d427bceb | 639 | gAvg[1] += (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ; |
icyzkungz | 6:5665d427bceb | 640 | gAvg[2] += (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ; |
icyzkungz | 6:5665d427bceb | 641 | } |
icyzkungz | 6:5665d427bceb | 642 | |
icyzkungz | 6:5665d427bceb | 643 | for (int ii =0; ii < 3; ii++) { // Get average of 200 values and store as average current readings |
icyzkungz | 6:5665d427bceb | 644 | aAvg[ii] /= 200; |
icyzkungz | 6:5665d427bceb | 645 | gAvg[ii] /= 200; |
icyzkungz | 6:5665d427bceb | 646 | } |
icyzkungz | 6:5665d427bceb | 647 | |
icyzkungz | 6:5665d427bceb | 648 | // Configure the accelerometer for self-test |
icyzkungz | 6:5665d427bceb | 649 | writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, 0xE0); // Enable self test on all three axes and set accelerometer range to +/- 2 g |
icyzkungz | 6:5665d427bceb | 650 | writeByte(MPU9250_ADDRESS, GYRO_CONFIG, 0xE0); // Enable self test on all three axes and set gyro range to +/- 250 degrees/s |
icyzkungz | 6:5665d427bceb | 651 | //delay(25); // Delay a while to let the device stabilize |
icyzkungz | 6:5665d427bceb | 652 | |
icyzkungz | 6:5665d427bceb | 653 | for( int ii = 0; ii < 200; ii++) { // get average self-test values of gyro and acclerometer |
icyzkungz | 6:5665d427bceb | 654 | |
icyzkungz | 6:5665d427bceb | 655 | readBytes(MPU9250_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]); // Read the six raw data registers into data array |
icyzkungz | 6:5665d427bceb | 656 | aSTAvg[0] += (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value |
icyzkungz | 6:5665d427bceb | 657 | aSTAvg[1] += (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ; |
icyzkungz | 6:5665d427bceb | 658 | aSTAvg[2] += (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ; |
icyzkungz | 6:5665d427bceb | 659 | |
icyzkungz | 6:5665d427bceb | 660 | readBytes(MPU9250_ADDRESS, GYRO_XOUT_H, 6, &rawData[0]); // Read the six raw data registers sequentially into data array |
icyzkungz | 6:5665d427bceb | 661 | gSTAvg[0] += (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value |
icyzkungz | 6:5665d427bceb | 662 | gSTAvg[1] += (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ; |
icyzkungz | 6:5665d427bceb | 663 | gSTAvg[2] += (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ; |
icyzkungz | 6:5665d427bceb | 664 | } |
icyzkungz | 6:5665d427bceb | 665 | |
icyzkungz | 6:5665d427bceb | 666 | for (int ii =0; ii < 3; ii++) { // Get average of 200 values and store as average self-test readings |
icyzkungz | 6:5665d427bceb | 667 | aSTAvg[ii] /= 200; |
icyzkungz | 6:5665d427bceb | 668 | gSTAvg[ii] /= 200; |
icyzkungz | 6:5665d427bceb | 669 | } |
icyzkungz | 6:5665d427bceb | 670 | |
icyzkungz | 6:5665d427bceb | 671 | // Configure the gyro and accelerometer for normal operation |
icyzkungz | 6:5665d427bceb | 672 | writeByte(MPU9250_ADDRESS, ACCEL_CONFIG, 0x00); |
icyzkungz | 6:5665d427bceb | 673 | writeByte(MPU9250_ADDRESS, GYRO_CONFIG, 0x00); |
icyzkungz | 6:5665d427bceb | 674 | //delay(25); // Delay a while to let the device stabilize |
icyzkungz | 6:5665d427bceb | 675 | |
icyzkungz | 6:5665d427bceb | 676 | // Retrieve accelerometer and gyro factory Self-Test Code from USR_Reg |
icyzkungz | 6:5665d427bceb | 677 | selfTest[0] = readByte(MPU9250_ADDRESS, SELF_TEST_X_ACCEL); // X-axis accel self-test results |
icyzkungz | 6:5665d427bceb | 678 | selfTest[1] = readByte(MPU9250_ADDRESS, SELF_TEST_Y_ACCEL); // Y-axis accel self-test results |
icyzkungz | 6:5665d427bceb | 679 | selfTest[2] = readByte(MPU9250_ADDRESS, SELF_TEST_Z_ACCEL); // Z-axis accel self-test results |
icyzkungz | 6:5665d427bceb | 680 | selfTest[3] = readByte(MPU9250_ADDRESS, SELF_TEST_X_GYRO); // X-axis gyro self-test results |
icyzkungz | 6:5665d427bceb | 681 | selfTest[4] = readByte(MPU9250_ADDRESS, SELF_TEST_Y_GYRO); // Y-axis gyro self-test results |
icyzkungz | 6:5665d427bceb | 682 | selfTest[5] = readByte(MPU9250_ADDRESS, SELF_TEST_Z_GYRO); // Z-axis gyro self-test results |
icyzkungz | 6:5665d427bceb | 683 | |
icyzkungz | 6:5665d427bceb | 684 | // Retrieve factory self-test value from self-test code reads |
icyzkungz | 6:5665d427bceb | 685 | factoryTrim[0] = (float)(2620/1<<FS)*(pow( (float)1.01 , ((float)selfTest[0] - (float)1.0) )); // FT[Xa] factory trim calculation |
icyzkungz | 6:5665d427bceb | 686 | factoryTrim[1] = (float)(2620/1<<FS)*(pow( (float)1.01 , ((float)selfTest[1] - (float)1.0) )); // FT[Ya] factory trim calculation |
icyzkungz | 6:5665d427bceb | 687 | factoryTrim[2] = (float)(2620/1<<FS)*(pow( (float)1.01 , ((float)selfTest[2] - (float)1.0) )); // FT[Za] factory trim calculation |
icyzkungz | 6:5665d427bceb | 688 | factoryTrim[3] = (float)(2620/1<<FS)*(pow( (float)1.01 , ((float)selfTest[3] - (float)1.0) )); // FT[Xg] factory trim calculation |
icyzkungz | 6:5665d427bceb | 689 | factoryTrim[4] = (float)(2620/1<<FS)*(pow( (float)1.01 , ((float)selfTest[4] - (float)1.0) )); // FT[Yg] factory trim calculation |
icyzkungz | 6:5665d427bceb | 690 | factoryTrim[5] = (float)(2620/1<<FS)*(pow( (float)1.01 , ((float)selfTest[5] - (float)1.0) )); // FT[Zg] factory trim calculation |
icyzkungz | 6:5665d427bceb | 691 | |
icyzkungz | 6:5665d427bceb | 692 | // Report results as a ratio of (STR - FT)/FT; the change from Factory Trim of the Self-Test Response |
icyzkungz | 6:5665d427bceb | 693 | // To get percent, must multiply by 100 |
icyzkungz | 6:5665d427bceb | 694 | for (int i = 0; i < 3; i++) { |
icyzkungz | 6:5665d427bceb | 695 | SelfTest[i] = (float)100.0*((float)(aSTAvg[i] - aAvg[i]))/factoryTrim[i]; // Report percent differences |
icyzkungz | 6:5665d427bceb | 696 | SelfTest[i+3] = (float)100.0*((float)(gSTAvg[i] - gAvg[i]))/factoryTrim[i+3]; // Report percent differences |
icyzkungz | 6:5665d427bceb | 697 | } |
icyzkungz | 6:5665d427bceb | 698 | |
icyzkungz | 6:5665d427bceb | 699 | } |