Happy Turkey Day
Dependencies: mbed GPSINT SDFileSystem1 FATDirHandle1 MPU9250
main.cpp@0:ec36896926be, 2019-11-28 (annotated)
- Committer:
- jbeason3
- Date:
- Thu Nov 28 01:39:19 2019 +0000
- Revision:
- 0:ec36896926be
Happy Turkey Day
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbeason3 | 0:ec36896926be | 1 | #include "mbed.h" |
jbeason3 | 0:ec36896926be | 2 | #include "MPU9250.h" |
jbeason3 | 0:ec36896926be | 3 | #include "SDFileSystem.h" |
jbeason3 | 0:ec36896926be | 4 | #include "GPSINT.h" |
jbeason3 | 0:ec36896926be | 5 | DigitalOut myled(LED1); |
jbeason3 | 0:ec36896926be | 6 | MPU9250 mpu9250(p28,p27); // IMU |
jbeason3 | 0:ec36896926be | 7 | GPSINT gps(p13,p14); // GPS |
jbeason3 | 0:ec36896926be | 8 | Serial pc(USBTX, USBRX); // tx, rx |
jbeason3 | 0:ec36896926be | 9 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // defines sd system |
jbeason3 | 0:ec36896926be | 10 | Timer t; |
jbeason3 | 0:ec36896926be | 11 | float sum = 0; |
jbeason3 | 0:ec36896926be | 12 | uint32_t sumCount = 0; |
jbeason3 | 0:ec36896926be | 13 | |
jbeason3 | 0:ec36896926be | 14 | Ticker log_ticker; // creates ticker |
jbeason3 | 0:ec36896926be | 15 | FILE *fp; // defines file |
jbeason3 | 0:ec36896926be | 16 | char file_name[100]; // creates file name character |
jbeason3 | 0:ec36896926be | 17 | |
jbeason3 | 0:ec36896926be | 18 | void mpu9250_initialization(){ |
jbeason3 | 0:ec36896926be | 19 | pc.printf("####CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock); |
jbeason3 | 0:ec36896926be | 20 | //initial com check |
jbeason3 | 0:ec36896926be | 21 | uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250 |
jbeason3 | 0:ec36896926be | 22 | pc.printf("####I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r"); |
jbeason3 | 0:ec36896926be | 23 | if (whoami == 0x71) // WHO_AM_I should always be 0x68 |
jbeason3 | 0:ec36896926be | 24 | { |
jbeason3 | 0:ec36896926be | 25 | pc.printf("####MPU9250 WHO_AM_I is 0x%x\n\r", whoami); |
jbeason3 | 0:ec36896926be | 26 | pc.printf("####MPU9250 is online...\n\r"); |
jbeason3 | 0:ec36896926be | 27 | wait(1); |
jbeason3 | 0:ec36896926be | 28 | //reset MPU and conduct self test// |
jbeason3 | 0:ec36896926be | 29 | pc.printf("####Please wait,IMU Resetting####\r\n"); |
jbeason3 | 0:ec36896926be | 30 | mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration |
jbeason3 | 0:ec36896926be | 31 | pc.printf("####Self Test####\r\n"); |
jbeason3 | 0:ec36896926be | 32 | //initial MPU9250 Parameters |
jbeason3 | 0:ec36896926be | 33 | mpu9250.Ascale = AFS_2G; |
jbeason3 | 0:ec36896926be | 34 | mpu9250.Gscale = GFS_250DPS; // GFS_250DPS, GFS_500DPS, GFS_1000DPS, GFS_2000DPS |
jbeason3 | 0:ec36896926be | 35 | mpu9250.Mscale = MFS_16BITS; // MFS_14BITS or MFS_16BITS, 14-bit or 16-bit magnetometer resolution |
jbeason3 | 0:ec36896926be | 36 | mpu9250.Mmode = 0x06; |
jbeason3 | 0:ec36896926be | 37 | mpu9250.delt_t=0; |
jbeason3 | 0:ec36896926be | 38 | mpu9250.deltat=0.0f; |
jbeason3 | 0:ec36896926be | 39 | mpu9250.lastUpdate = 0; |
jbeason3 | 0:ec36896926be | 40 | mpu9250.firstUpdate = 0; |
jbeason3 | 0:ec36896926be | 41 | mpu9250.Now = 0; |
jbeason3 | 0:ec36896926be | 42 | mpu9250.count=0; |
jbeason3 | 0:ec36896926be | 43 | mpu9250.q[0] = 1.0f; |
jbeason3 | 0:ec36896926be | 44 | mpu9250.q[1] = 0.0f; |
jbeason3 | 0:ec36896926be | 45 | mpu9250.q[2] = 0.0f; |
jbeason3 | 0:ec36896926be | 46 | mpu9250.q[3] = 0.0f; |
jbeason3 | 0:ec36896926be | 47 | // mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values |
jbeason3 | 0:ec36896926be | 48 | //pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]); |
jbeason3 | 0:ec36896926be | 49 | // pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]); |
jbeason3 | 0:ec36896926be | 50 | //pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]); |
jbeason3 | 0:ec36896926be | 51 | //pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]); |
jbeason3 | 0:ec36896926be | 52 | //pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]); |
jbeason3 | 0:ec36896926be | 53 | //pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]); |
jbeason3 | 0:ec36896926be | 54 | pc.printf("####Gyro and accelerometer Calibration will start in 5 seconds####\r\n"); |
jbeason3 | 0:ec36896926be | 55 | pc.printf("####Please keep the IMU still\r\n"); |
jbeason3 | 0:ec36896926be | 56 | wait(5); |
jbeason3 | 0:ec36896926be | 57 | pc.printf("####Calibration starts\r\n"); |
jbeason3 | 0:ec36896926be | 58 | mpu9250.calibrateMPU9250(mpu9250.gyroBias, mpu9250.accelBias); // Calibrate gyro and accelerometers, load biases in bias registers |
jbeason3 | 0:ec36896926be | 59 | pc.printf("x gyro bias = %f\n\r", mpu9250.gyroBias[0]); |
jbeason3 | 0:ec36896926be | 60 | pc.printf("y gyro bias = %f\n\r", mpu9250.gyroBias[1]); |
jbeason3 | 0:ec36896926be | 61 | pc.printf("z gyro bias = %f\n\r", mpu9250.gyroBias[2]); |
jbeason3 | 0:ec36896926be | 62 | pc.printf("x accel bias = %f\n\r", mpu9250.accelBias[0]); |
jbeason3 | 0:ec36896926be | 63 | pc.printf("y accel bias = %f\n\r", mpu9250.accelBias[1]); |
jbeason3 | 0:ec36896926be | 64 | pc.printf("z accel bias = %f\n\r", mpu9250.accelBias[2]); |
jbeason3 | 0:ec36896926be | 65 | wait(2); |
jbeason3 | 0:ec36896926be | 66 | ///initialization |
jbeason3 | 0:ec36896926be | 67 | mpu9250.initMPU9250(); |
jbeason3 | 0:ec36896926be | 68 | pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature |
jbeason3 | 0:ec36896926be | 69 | mpu9250.initAK8963(mpu9250.magCalibration); |
jbeason3 | 0:ec36896926be | 70 | pc.printf("Magnetometer initilized\r\n"); |
jbeason3 | 0:ec36896926be | 71 | // pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer |
jbeason3 | 0:ec36896926be | 72 | // pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale)); |
jbeason3 | 0:ec36896926be | 73 | // pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale)); |
jbeason3 | 0:ec36896926be | 74 | if(mpu9250.Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r"); |
jbeason3 | 0:ec36896926be | 75 | if(mpu9250.Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r"); |
jbeason3 | 0:ec36896926be | 76 | if(mpu9250.Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r"); |
jbeason3 | 0:ec36896926be | 77 | if(mpu9250.Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r"); |
jbeason3 | 0:ec36896926be | 78 | } |
jbeason3 | 0:ec36896926be | 79 | else |
jbeason3 | 0:ec36896926be | 80 | { |
jbeason3 | 0:ec36896926be | 81 | pc.printf("Could not connect to MPU9250: \n\r"); |
jbeason3 | 0:ec36896926be | 82 | pc.printf("%#x \n", whoami); |
jbeason3 | 0:ec36896926be | 83 | while(1) ; // Loop forever if communication doesn't happen |
jbeason3 | 0:ec36896926be | 84 | } |
jbeason3 | 0:ec36896926be | 85 | mpu9250.getAres(); // Get accelerometer sensitivity |
jbeason3 | 0:ec36896926be | 86 | mpu9250.getGres(); // Get gyro sensitivity |
jbeason3 | 0:ec36896926be | 87 | mpu9250.getMres(); // Get magnetometer sensitivity |
jbeason3 | 0:ec36896926be | 88 | pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/mpu9250.aRes); |
jbeason3 | 0:ec36896926be | 89 | pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/mpu9250.gRes); |
jbeason3 | 0:ec36896926be | 90 | pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mpu9250.mRes); |
jbeason3 | 0:ec36896926be | 91 | pc.printf("####IMU initialization done####\r\n"); |
jbeason3 | 0:ec36896926be | 92 | wait(1); |
jbeason3 | 0:ec36896926be | 93 | } |
jbeason3 | 0:ec36896926be | 94 | |
jbeason3 | 0:ec36896926be | 95 | |
jbeason3 | 0:ec36896926be | 96 | void mag_cali(){ |
jbeason3 | 0:ec36896926be | 97 | int32_t mag_bias[3] = {0, 0, 0}, mag_scale[3] = {0, 0, 0}; |
jbeason3 | 0:ec36896926be | 98 | int16_t mag_max[3] = {-32767, -32767, -32767}, mag_min[3] = {32767, 32767, 32767}, mag_temp[3] = {0, 0, 0}; |
jbeason3 | 0:ec36896926be | 99 | //float dest1[3]={0,0,0}, dest2[3]={0,0,0}; |
jbeason3 | 0:ec36896926be | 100 | pc.printf("####Compass Calibration starts in 5 seconds\r\n"); |
jbeason3 | 0:ec36896926be | 101 | wait(5); |
jbeason3 | 0:ec36896926be | 102 | pc.printf("###Start moving your imu in figure 8\r\n"); |
jbeason3 | 0:ec36896926be | 103 | for (int i=0;i<1500;i++){ //1500 for 100 Hz |
jbeason3 | 0:ec36896926be | 104 | mpu9250.readMagData(mag_temp); // Read the x/y/z adc values |
jbeason3 | 0:ec36896926be | 105 | for(int jj=0; jj<3; jj++){ |
jbeason3 | 0:ec36896926be | 106 | if(mag_temp[jj] > mag_max[jj]) mag_max[jj] = mag_temp[jj]; |
jbeason3 | 0:ec36896926be | 107 | if(mag_temp[jj] < mag_min[jj]) mag_min[jj] = mag_temp[jj]; |
jbeason3 | 0:ec36896926be | 108 | } |
jbeason3 | 0:ec36896926be | 109 | wait(0.01);//delay for 10 ms. |
jbeason3 | 0:ec36896926be | 110 | } |
jbeason3 | 0:ec36896926be | 111 | //get hard iron correction |
jbeason3 | 0:ec36896926be | 112 | // Get hard iron correction |
jbeason3 | 0:ec36896926be | 113 | mag_bias[0] = (mag_max[0] + mag_min[0])/2; // get average x mag bias in counts |
jbeason3 | 0:ec36896926be | 114 | mag_bias[1] = (mag_max[1] + mag_min[1])/2; // get average y mag bias in counts |
jbeason3 | 0:ec36896926be | 115 | mag_bias[2] = (mag_max[2] + mag_min[2])/2; // get average z mag bias in counts |
jbeason3 | 0:ec36896926be | 116 | mpu9250.magbias[0] = (float) mag_bias[0]*mpu9250.mRes*mpu9250.magCalibration[0]; // save mag biases in G for main program |
jbeason3 | 0:ec36896926be | 117 | mpu9250.magbias[1] = (float) mag_bias[1]*mpu9250.mRes*mpu9250.magCalibration[1]; |
jbeason3 | 0:ec36896926be | 118 | mpu9250.magbias[2] = (float) mag_bias[2]*mpu9250.mRes*mpu9250.magCalibration[2]; |
jbeason3 | 0:ec36896926be | 119 | pc.printf("####Mag bias =%f,%f,%f\r\n",mpu9250.magbias[0],mpu9250.magbias[1],mpu9250.magbias[2]); |
jbeason3 | 0:ec36896926be | 120 | /*//get soft iron correction |
jbeason3 | 0:ec36896926be | 121 | // Get soft iron correction estimate |
jbeason3 | 0:ec36896926be | 122 | mag_scale[0] = (mag_max[0] - mag_min[0])/2; // get average x axis max chord length in counts |
jbeason3 | 0:ec36896926be | 123 | mag_scale[1] = (mag_max[1] - mag_min[1])/2; // get average y axis max chord length in counts |
jbeason3 | 0:ec36896926be | 124 | mag_scale[2] = (mag_max[2] - mag_min[2])/2; // get average z axis max chord length in counts |
jbeason3 | 0:ec36896926be | 125 | |
jbeason3 | 0:ec36896926be | 126 | float avg_rad = mag_scale[0] + mag_scale[1] + mag_scale[2]; |
jbeason3 | 0:ec36896926be | 127 | avg_rad /= 3.0; |
jbeason3 | 0:ec36896926be | 128 | |
jbeason3 | 0:ec36896926be | 129 | dest2[0] = avg_rad/((float)mag_scale[0]); |
jbeason3 | 0:ec36896926be | 130 | dest2[1] = avg_rad/((float)mag_scale[1]); |
jbeason3 | 0:ec36896926be | 131 | dest2[2] = avg_rad/((float)mag_scale[2]); |
jbeason3 | 0:ec36896926be | 132 | */ |
jbeason3 | 0:ec36896926be | 133 | pc.printf("####Mag Calibration done!\r\n"); |
jbeason3 | 0:ec36896926be | 134 | } |
jbeason3 | 0:ec36896926be | 135 | |
jbeason3 | 0:ec36896926be | 136 | void sdwrite1(){ // Writes first set to SD & to Coolterm to confirm data |
jbeason3 | 0:ec36896926be | 137 | fprintf(fp,"$IMUPS,%f,%f,%f,%f\r\n",t.read(),mpu9250.roll,mpu9250.pitch,mpu9250.yaw); |
jbeason3 | 0:ec36896926be | 138 | pc.printf("$IMUPS,%f,%f,%f,%f\r\n",t.read(),mpu9250.roll,mpu9250.pitch,mpu9250.yaw); |
jbeason3 | 0:ec36896926be | 139 | } |
jbeason3 | 0:ec36896926be | 140 | |
jbeason3 | 0:ec36896926be | 141 | void sdwrite2(){ // Writes 2nd set to SD & Coolterm |
jbeason3 | 0:ec36896926be | 142 | fprintf(fp,"$GPSST,%d,%f,%d\r\n",gps.lock,gps.utc_time,gps.satelites); |
jbeason3 | 0:ec36896926be | 143 | pc.printf("$GPSST,%d,%f,%d\r\n",gps.lock,gps.utc_time,gps.satelites); |
jbeason3 | 0:ec36896926be | 144 | } |
jbeason3 | 0:ec36896926be | 145 | |
jbeason3 | 0:ec36896926be | 146 | void sdwrite3(){ // Writes 3rd set to SD & Coolterm |
jbeason3 | 0:ec36896926be | 147 | fprintf(fp,"$GPSPS,%f,%f,%f,%f,%f\r\n",t.read(),gps.nmea_latitude,gps.nmea_longitude,gps.speed_k,gps.course_d); |
jbeason3 | 0:ec36896926be | 148 | pc.printf("$GPSPS,%f,%f,%f,%f,%f\r\n",t.read(),gps.nmea_latitude,gps.nmea_longitude,gps.speed_k,gps.course_d); |
jbeason3 | 0:ec36896926be | 149 | } |
jbeason3 | 0:ec36896926be | 150 | |
jbeason3 | 0:ec36896926be | 151 | |
jbeason3 | 0:ec36896926be | 152 | int main() { |
jbeason3 | 0:ec36896926be | 153 | char buffer[100]; // creates buffer character |
jbeason3 | 0:ec36896926be | 154 | mkdir("/sd/A5_Beason", 0777); // file location |
jbeason3 | 0:ec36896926be | 155 | pc.printf("Please set a file name\r\n"); // asks user for file name |
jbeason3 | 0:ec36896926be | 156 | pc.scanf("%s",buffer); // looks at the name given |
jbeason3 | 0:ec36896926be | 157 | sprintf(file_name,"/sd/A5_Beason/%s.txt",buffer); // creates file location |
jbeason3 | 0:ec36896926be | 158 | pc.printf("The file name and directory is: %s\r\n",file_name); // tells user the information |
jbeason3 | 0:ec36896926be | 159 | fp = fopen(file_name, "w"); // opens file to be written on |
jbeason3 | 0:ec36896926be | 160 | pc.printf("file_opened \n"); // tells user the file is opened |
jbeason3 | 0:ec36896926be | 161 | if (fp == NULL) { // if the file is not opened |
jbeason3 | 0:ec36896926be | 162 | error("Could not open file for writing\n"); // informs user that the file was not opened |
jbeason3 | 0:ec36896926be | 163 | } |
jbeason3 | 0:ec36896926be | 164 | fclose(fp); // closes file |
jbeason3 | 0:ec36896926be | 165 | |
jbeason3 | 0:ec36896926be | 166 | pc.baud(9600); |
jbeason3 | 0:ec36896926be | 167 | t.start(); |
jbeason3 | 0:ec36896926be | 168 | mpu9250_initialization(); |
jbeason3 | 0:ec36896926be | 169 | mag_cali(); |
jbeason3 | 0:ec36896926be | 170 | pc.printf("####IMU is all set, going to start sensing in 5 seconds\r\n"); |
jbeason3 | 0:ec36896926be | 171 | wait(5); |
jbeason3 | 0:ec36896926be | 172 | while(1){ |
jbeason3 | 0:ec36896926be | 173 | if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt |
jbeason3 | 0:ec36896926be | 174 | mpu9250.readAccelData(mpu9250.accelCount); // Read the x/y/z adc values |
jbeason3 | 0:ec36896926be | 175 | // Now we'll calculate the accleration value into actual g's |
jbeason3 | 0:ec36896926be | 176 | mpu9250.ax = (float)mpu9250.accelCount[0]*mpu9250.aRes - mpu9250.accelBias[0]; // get actual g value, this depends on scale being set |
jbeason3 | 0:ec36896926be | 177 | mpu9250.ay = (float)mpu9250.accelCount[1]*mpu9250.aRes - mpu9250.accelBias[1]; |
jbeason3 | 0:ec36896926be | 178 | mpu9250.az = (float)mpu9250.accelCount[2]*mpu9250.aRes - mpu9250.accelBias[2]; |
jbeason3 | 0:ec36896926be | 179 | |
jbeason3 | 0:ec36896926be | 180 | mpu9250.readGyroData(mpu9250.gyroCount); // Read the x/y/z adc values |
jbeason3 | 0:ec36896926be | 181 | // Calculate the gyro value into actual degrees per second |
jbeason3 | 0:ec36896926be | 182 | mpu9250.gx = (float)mpu9250.gyroCount[0]*mpu9250.gRes - mpu9250.gyroBias[0]; // get actual gyro value, this depends on scale being set |
jbeason3 | 0:ec36896926be | 183 | mpu9250.gy = (float)mpu9250.gyroCount[1]*mpu9250.gRes - mpu9250.gyroBias[1]; |
jbeason3 | 0:ec36896926be | 184 | mpu9250.gz = (float)mpu9250.gyroCount[2]*mpu9250.gRes - mpu9250.gyroBias[2]; |
jbeason3 | 0:ec36896926be | 185 | |
jbeason3 | 0:ec36896926be | 186 | mpu9250.readMagData(mpu9250.magCount); // Read the x/y/z adc values |
jbeason3 | 0:ec36896926be | 187 | // Calculate the magnetometer values in milliGauss |
jbeason3 | 0:ec36896926be | 188 | // Include factory calibration per data sheet and user environmental corrections |
jbeason3 | 0:ec36896926be | 189 | mpu9250.mx = (float)mpu9250.magCount[0]*mpu9250.mRes*mpu9250.magCalibration[0] - mpu9250.magbias[0]; // get actual magnetometer value, this depends on scale being set |
jbeason3 | 0:ec36896926be | 190 | mpu9250.my = (float)mpu9250.magCount[1]*mpu9250.mRes*mpu9250.magCalibration[1] - mpu9250.magbias[1]; |
jbeason3 | 0:ec36896926be | 191 | mpu9250.mz = (float)mpu9250.magCount[2]*mpu9250.mRes*mpu9250.magCalibration[2] - mpu9250.magbias[2]; |
jbeason3 | 0:ec36896926be | 192 | } |
jbeason3 | 0:ec36896926be | 193 | |
jbeason3 | 0:ec36896926be | 194 | mpu9250.Now = t.read_us(); |
jbeason3 | 0:ec36896926be | 195 | mpu9250.deltat = (float)((mpu9250.Now - mpu9250.lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update |
jbeason3 | 0:ec36896926be | 196 | mpu9250.lastUpdate = mpu9250.Now; |
jbeason3 | 0:ec36896926be | 197 | sum += mpu9250.deltat; |
jbeason3 | 0:ec36896926be | 198 | sumCount++; |
jbeason3 | 0:ec36896926be | 199 | //mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, mx, my, mz); |
jbeason3 | 0:ec36896926be | 200 | //compute the quaternion. |
jbeason3 | 0:ec36896926be | 201 | |
jbeason3 | 0:ec36896926be | 202 | mpu9250.MahonyQuaternionUpdate(mpu9250.ax, mpu9250.ay, mpu9250.az, |
jbeason3 | 0:ec36896926be | 203 | mpu9250.gx*PI/180.00, mpu9250.gy*PI/180.00, mpu9250.gz*PI/180.00, |
jbeason3 | 0:ec36896926be | 204 | mpu9250.mx, mpu9250.my, mpu9250.mz); |
jbeason3 | 0:ec36896926be | 205 | |
jbeason3 | 0:ec36896926be | 206 | // pc.printf("Q=%f,%f,%f,%f\r\n",mpu9250.q[0],mpu9250.q[1],mpu9250.q[2],mpu9250.q[3]); |
jbeason3 | 0:ec36896926be | 207 | // Serial print and/or display at 0.5 s rate independent of data rates |
jbeason3 | 0:ec36896926be | 208 | mpu9250.delt_t = t.read_ms() - mpu9250.count; |
jbeason3 | 0:ec36896926be | 209 | if (mpu9250.delt_t > 100) { |
jbeason3 | 0:ec36896926be | 210 | // pc.printf("ax = %f", 1000*mpu9250.ax); |
jbeason3 | 0:ec36896926be | 211 | // pc.printf(" ay = %f", 1000*mpu9250.ay); |
jbeason3 | 0:ec36896926be | 212 | // pc.printf(" az = %f mg\n\r", 1000*mpu9250.az); |
jbeason3 | 0:ec36896926be | 213 | // pc.printf("gx = %f", mpu9250.gx); |
jbeason3 | 0:ec36896926be | 214 | // pc.printf("gy = %f", mpu9250.gy); |
jbeason3 | 0:ec36896926be | 215 | // pc.printf("gz = %f deg/s\n\r", mpu9250.gz); |
jbeason3 | 0:ec36896926be | 216 | //pc.printf("mx = %f", mpu9250.mx); |
jbeason3 | 0:ec36896926be | 217 | //pc.printf(" my = %f", mpu9250.my); |
jbeason3 | 0:ec36896926be | 218 | //pc.printf(" mz = %f mG\n\r", mpu9250.mz); |
jbeason3 | 0:ec36896926be | 219 | // tempCount = mpu9250.readTempData(); // Read the adc values |
jbeason3 | 0:ec36896926be | 220 | // temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade |
jbeason3 | 0:ec36896926be | 221 | // pc.printf(" temperature = %f C\n\r", temperature); |
jbeason3 | 0:ec36896926be | 222 | |
jbeason3 | 0:ec36896926be | 223 | mpu9250.roll = atan2(2.0f * (mpu9250.q[0] * mpu9250.q[1] + mpu9250.q[2] * mpu9250.q[3]), |
jbeason3 | 0:ec36896926be | 224 | mpu9250.q[0] * mpu9250.q[0] - mpu9250.q[1] * mpu9250.q[1] - mpu9250.q[2] * mpu9250.q[2] + mpu9250.q[3] * mpu9250.q[3]); |
jbeason3 | 0:ec36896926be | 225 | mpu9250.pitch = -asin(2.0f * (mpu9250.q[1] * mpu9250.q[3] - mpu9250.q[0] * mpu9250.q[2])); |
jbeason3 | 0:ec36896926be | 226 | mpu9250.yaw = atan2(2.0f * (mpu9250.q[1] * mpu9250.q[2] + mpu9250.q[0] * mpu9250.q[3]), |
jbeason3 | 0:ec36896926be | 227 | mpu9250.q[0] * mpu9250.q[0] + mpu9250.q[1] * mpu9250.q[1] - mpu9250.q[2] * mpu9250.q[2] - mpu9250.q[3] * mpu9250.q[3]); |
jbeason3 | 0:ec36896926be | 228 | mpu9250.pitch *= 180.0f / PI; |
jbeason3 | 0:ec36896926be | 229 | mpu9250.yaw *= 180.0f / PI; |
jbeason3 | 0:ec36896926be | 230 | mpu9250.yaw += 15.0f; // Declination at RI |
jbeason3 | 0:ec36896926be | 231 | mpu9250.roll *= 180.0f / PI; |
jbeason3 | 0:ec36896926be | 232 | |
jbeason3 | 0:ec36896926be | 233 | fp = fopen(file_name,"a"); // Open file |
jbeason3 | 0:ec36896926be | 234 | |
jbeason3 | 0:ec36896926be | 235 | sdwrite1(); // Write $IMUPS to SD card |
jbeason3 | 0:ec36896926be | 236 | sdwrite2(); // Write $GPSST to SD card |
jbeason3 | 0:ec36896926be | 237 | sdwrite3(); // Write $GPSPS to SD card |
jbeason3 | 0:ec36896926be | 238 | fclose(fp); // close file |
jbeason3 | 0:ec36896926be | 239 | |
jbeason3 | 0:ec36896926be | 240 | myled= !myled; |
jbeason3 | 0:ec36896926be | 241 | mpu9250.count = t.read_ms(); |
jbeason3 | 0:ec36896926be | 242 | |
jbeason3 | 0:ec36896926be | 243 | if(mpu9250.count > 1<<21) { |
jbeason3 | 0:ec36896926be | 244 | t.start(); // start the timer over again if ~30 minutes has passed |
jbeason3 | 0:ec36896926be | 245 | mpu9250.count = 0; |
jbeason3 | 0:ec36896926be | 246 | mpu9250.deltat= 0; |
jbeason3 | 0:ec36896926be | 247 | mpu9250.lastUpdate = t.read_us(); |
jbeason3 | 0:ec36896926be | 248 | } |
jbeason3 | 0:ec36896926be | 249 | sum = 0; |
jbeason3 | 0:ec36896926be | 250 | sumCount = 0; |
jbeason3 | 0:ec36896926be | 251 | |
jbeason3 | 0:ec36896926be | 252 | pc.printf("lock=%d %f %f %c %f %c %f %f\r\n",gps.lock,gps.utc_time,gps.nmea_longitude,gps.ns,gps.nmea_latitude,gps.ew,gps.speed_k,gps.course_d); |
jbeason3 | 0:ec36896926be | 253 | wait(1); |
jbeason3 | 0:ec36896926be | 254 | } |
jbeason3 | 0:ec36896926be | 255 | |
jbeason3 | 0:ec36896926be | 256 | } |
jbeason3 | 0:ec36896926be | 257 | |
jbeason3 | 0:ec36896926be | 258 | } |
jbeason3 | 0:ec36896926be | 259 |