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