JEK changes enabling proper recording of IMU/GPS datastrams - 02-APR-2013

Dependencies:   mbed

Fork of GPS_Incremental by Dan Matthews

Committer:
jekain314
Date:
Fri Apr 19 16:21:27 2013 +0000
Revision:
9:b45feb91ba38
Parent:
8:13724ed3f825
update to allow better imu gps data collection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jekain314 6:2a8486283198 1 //these are defines for the messages that are sent from the PC across the USB
jekain314 6:2a8486283198 2 //these messages produce reactions on the mbed
jekain314 7:2e20e4cf53e6 3 const unsigned char STATUS_MSG =0;
jekain314 7:2e20e4cf53e6 4 const unsigned char POSVEL_MSG =1;
jekain314 6:2a8486283198 5 const unsigned char STARTDATA_MSG =2;
jekain314 6:2a8486283198 6 const unsigned char STOPDATA_MSG =3;
jekain314 6:2a8486283198 7 const unsigned char STARTSTREAM_MSG =4;
jekain314 6:2a8486283198 8 const unsigned char STOPSTREAM_MSG =5;
jekain314 6:2a8486283198 9 const unsigned char STARTLOGINFO_MSG =6;
jekain314 6:2a8486283198 10 const unsigned char STOPLOGINFO_MSG =7;
jekain314 6:2a8486283198 11
jekain314 6:2a8486283198 12 const double DEGREES_TO_RADIANS = acos(-1.0)/180.0;
jekain314 6:2a8486283198 13 const double eccen = 0.0818191908426; //WGS84 earth eccentricity
jekain314 6:2a8486283198 14 const double earthRadius = 6378137; //WGS84 earthRadius in meters
jekain314 6:2a8486283198 15
jekain314 6:2a8486283198 16 char serBuf[128];
jekain314 6:2a8486283198 17 int serBufChars=0;
jekain314 6:2a8486283198 18
jekain314 6:2a8486283198 19 //flags to control the PC command actions
jekain314 6:2a8486283198 20 bool sendPosVel =false;
jekain314 6:2a8486283198 21 bool sendStatus =false;
jekain314 6:2a8486283198 22 bool sendRecData =false;
jekain314 6:2a8486283198 23 bool streamPos =false;
jekain314 6:2a8486283198 24 bool sendStreamPos =false;
jekain314 6:2a8486283198 25 bool logMsgInfo =false;
jekain314 6:2a8486283198 26 bool sendLogMsgInfo =false;
jekain314 6:2a8486283198 27 bool recordData =false;
jekain314 6:2a8486283198 28
jekain314 6:2a8486283198 29
jekain314 6:2a8486283198 30 const unsigned char numMessages = 8; //number of potential messages
jekain314 6:2a8486283198 31 char msgList[numMessages][32]; //text array storing the command messages from the PC
jekain314 7:2e20e4cf53e6 32 char minMessageSize = 11; //minimum size of a text message
jekain314 6:2a8486283198 33 unsigned char CR = 0x0d; //ASCII Carriage Return
jekain314 6:2a8486283198 34 unsigned char LF = 0x0a; //ASCII Line Feed
jekain314 6:2a8486283198 35
jekain314 6:2a8486283198 36 void setUpMessages(void)
jekain314 6:2a8486283198 37 {
jekain314 6:2a8486283198 38 //set up the ASCII text records that are candidates to be passed from the PC
jekain314 6:2a8486283198 39 sprintf(msgList[STATUS_MSG], "WMsg STATUS");
jekain314 6:2a8486283198 40 sprintf(msgList[POSVEL_MSG], "WMsg POSVEL");
jekain314 6:2a8486283198 41 sprintf(msgList[STARTDATA_MSG], "WMsg RECORDDATA Y");
jekain314 6:2a8486283198 42 sprintf(msgList[STOPDATA_MSG], "WMsg RECORDDATA N");
jekain314 6:2a8486283198 43 sprintf(msgList[STARTSTREAM_MSG], "WMsg POSSTREAM Y");
jekain314 6:2a8486283198 44 sprintf(msgList[STOPSTREAM_MSG], "WMsg POSSTREAM N");
jekain314 6:2a8486283198 45 sprintf(msgList[STARTLOGINFO_MSG], "WMsg LOGINFO Y");
jekain314 6:2a8486283198 46 sprintf(msgList[STOPLOGINFO_MSG], "WMsg LOGINFO N");
jekain314 6:2a8486283198 47 //message length is from 10 to 16 chars
jekain314 6:2a8486283198 48
jekain314 6:2a8486283198 49 toPC.printf(" finished setting up messages \n");
jekain314 6:2a8486283198 50 }
jekain314 6:2a8486283198 51
jekain314 6:2a8486283198 52 void readFromPC()
jekain314 6:2a8486283198 53 {
jekain314 6:2a8486283198 54 //The received commands only occur at the initialization stage -- time is not that critical there.
jekain314 6:2a8486283198 55 //during the real-time action, we will never pass the followong if test ( no characters received)
jekain314 6:2a8486283198 56 if (toPC.readable()) //read a PC serial byte and test it for a command
jekain314 6:2a8486283198 57 {
jekain314 6:2a8486283198 58
jekain314 6:2a8486283198 59 // Read in next character
jekain314 6:2a8486283198 60 char inChar = toPC.getc(); //read char from the USB serial link to the PC
jekain314 7:2e20e4cf53e6 61 //toPC.printf("%02x ",inChar);
jekain314 6:2a8486283198 62
jekain314 6:2a8486283198 63 //incoming messages will end witb a CR / LF -- disregard these chars
jekain314 7:2e20e4cf53e6 64 if (inChar == CR || inChar == LF) return; //CR is a 0x0a
jekain314 6:2a8486283198 65
jekain314 6:2a8486283198 66 serBuf[serBufChars++] = inChar; //set this char in a char array
jekain314 6:2a8486283198 67
jekain314 6:2a8486283198 68 //no need to continue if numChars are less than the shortest candidate message
jekain314 7:2e20e4cf53e6 69 //if (serBufChars < minMessageSize) return;
jekain314 7:2e20e4cf53e6 70
jekain314 6:2a8486283198 71 // Append end of string
jekain314 6:2a8486283198 72 //We always assume we have a complete message string and test for this below
jekain314 6:2a8486283198 73 serBuf[serBufChars] = '\0';
jekain314 6:2a8486283198 74
jekain314 6:2a8486283198 75 bool validMessage = false;
jekain314 6:2a8486283198 76
jekain314 7:2e20e4cf53e6 77
jekain314 6:2a8486283198 78 // Check for valid message -- there are numMessages possible messages
jekain314 6:2a8486283198 79 for (int m = 0; m < numMessages && !validMessage; m++) //check for all messages ...
jekain314 6:2a8486283198 80 {
jekain314 6:2a8486283198 81 //toPC.printf(" \n\n found chars to test %3d %3d %s \n\n\n ", serBufChars, strlen(msgList[m]), serBuf );
jekain314 6:2a8486283198 82
jekain314 6:2a8486283198 83 //check the current partial message against ALL possible messages
jekain314 6:2a8486283198 84 //messages must match in both strength length and text
jekain314 6:2a8486283198 85 if (serBufChars == strlen(msgList[m]) && strncmp(serBuf, msgList[m], serBufChars) == 0 )
jekain314 6:2a8486283198 86 {
jekain314 6:2a8486283198 87
jekain314 7:2e20e4cf53e6 88 //toPC.printf( "\n found valid message %s \n\n", serBuf);
jekain314 6:2a8486283198 89
jekain314 6:2a8486283198 90 validMessage = true;
jekain314 6:2a8486283198 91 serBufChars = 0; //reset the character count to reset for next message
jekain314 6:2a8486283198 92
jekain314 6:2a8486283198 93 //set programmatic action flags based on the message
jekain314 6:2a8486283198 94 switch(m)
jekain314 6:2a8486283198 95 {
jekain314 6:2a8486283198 96 case STATUS_MSG:
jekain314 6:2a8486283198 97 sendStatus = true; //send a status message back to PC
jekain314 6:2a8486283198 98 break;
jekain314 6:2a8486283198 99 case POSVEL_MSG:
jekain314 6:2a8486283198 100 sendPosVel = true; //send a posvel message back to PC
jekain314 6:2a8486283198 101 break;
jekain314 6:2a8486283198 102 case STARTDATA_MSG: //start the data recording to the SD card
jekain314 6:2a8486283198 103 recordData = true;
jekain314 6:2a8486283198 104 sendRecData = true;
jekain314 6:2a8486283198 105 break;
jekain314 6:2a8486283198 106 case STOPDATA_MSG: //stop the data recording to the SD card
jekain314 6:2a8486283198 107 recordData = false;
jekain314 6:2a8486283198 108 sendRecData = true;
jekain314 6:2a8486283198 109 break;
jekain314 6:2a8486283198 110 case STARTSTREAM_MSG:
jekain314 6:2a8486283198 111 case STOPSTREAM_MSG:
jekain314 6:2a8486283198 112 streamPos = (m == STARTSTREAM_MSG);
jekain314 6:2a8486283198 113 sendStreamPos = true;
jekain314 6:2a8486283198 114 break;
jekain314 6:2a8486283198 115 case STARTLOGINFO_MSG:
jekain314 6:2a8486283198 116 case STOPLOGINFO_MSG:
jekain314 6:2a8486283198 117 logMsgInfo = (m == STARTLOGINFO_MSG);
jekain314 6:2a8486283198 118 sendLogMsgInfo = true;
jekain314 6:2a8486283198 119 break;
jekain314 6:2a8486283198 120 } //end Switch statement
jekain314 6:2a8486283198 121 break;
jekain314 6:2a8486283198 122 } //end test for a valid message
jekain314 6:2a8486283198 123 } //end message text loop
jekain314 6:2a8486283198 124 } //end pc.readable
jekain314 6:2a8486283198 125 };
jekain314 6:2a8486283198 126
jekain314 6:2a8486283198 127 void earthCoefficients(double latitudeRad, double longitudeRad, double height, double &latRateFac, double &lonRateFac)
jekain314 6:2a8486283198 128 {
jekain314 6:2a8486283198 129 //compute the lat and lon factors for use in the interpolation of the lat and lon between 1 sec epochs
jekain314 6:2a8486283198 130 //latRateFac & lonRateFac multiplied by Vnorth or VEast to get latRate and lonRate
jekain314 6:2a8486283198 131 //see this document (page 32) www.fas.org/spp/military/program/nav/basicnav.pdf
jekain314 6:2a8486283198 132
jekain314 6:2a8486283198 133 double eccenSinLat = eccen * sin(latitudeRad);
jekain314 6:2a8486283198 134 double temp1 = 1.0 - eccenSinLat*eccenSinLat;
jekain314 6:2a8486283198 135 double temp2 = sqrt(temp1);
jekain314 6:2a8486283198 136 double r_meridian = earthRadius * ( 1.0 - eccen*eccen)/ (temp1 * temp2);
jekain314 6:2a8486283198 137 double r_normal = earthRadius / temp2;
jekain314 6:2a8486283198 138
jekain314 6:2a8486283198 139 //divide Vnorth by latRateFac to get the latitude rate in deg per sec
jekain314 6:2a8486283198 140 latRateFac = (r_meridian + height)* DEGREES_TO_RADIANS;
jekain314 6:2a8486283198 141
jekain314 6:2a8486283198 142 //divide VEast by lonRateFac to get the longitude rate in deg per sec
jekain314 6:2a8486283198 143 lonRateFac = (r_normal + height) * cos(latitudeRad)* DEGREES_TO_RADIANS;
jekain314 6:2a8486283198 144 }
jekain314 6:2a8486283198 145
jekain314 6:2a8486283198 146 void sendPosVelMessageToPC(OEM615BESTPOS posMsg, OEM615BESTVEL velMsg)
jekain314 6:2a8486283198 147 {
jekain314 6:2a8486283198 148 //north and east velocity from the horizontal speed and heading
jekain314 6:2a8486283198 149 //velMsg may not be the "current" message --- but is the one also associated with a position message
jekain314 6:2a8486283198 150 double nVel = velMsg.horizontalSpeed*cos(velMsg.heading*DEGREES_TO_RADIANS);
jekain314 6:2a8486283198 151 double eVel = velMsg.horizontalSpeed*sin(velMsg.heading*DEGREES_TO_RADIANS);
jekain314 6:2a8486283198 152
jekain314 6:2a8486283198 153 double latRateFac;
jekain314 6:2a8486283198 154 double lonRateFac;
jekain314 6:2a8486283198 155
jekain314 6:2a8486283198 156 earthCoefficients( posMsg.latitude*DEGREES_TO_RADIANS,
jekain314 6:2a8486283198 157 posMsg.longitude*DEGREES_TO_RADIANS,
jekain314 6:2a8486283198 158 posMsg.height,
jekain314 6:2a8486283198 159 latRateFac, lonRateFac);
jekain314 6:2a8486283198 160
jekain314 6:2a8486283198 161 //commented calculations are for a spherical earth (Chris's original computation)
jekain314 6:2a8486283198 162 // For the 1 second deltas with which we are dealing
jekain314 6:2a8486283198 163 // This calculation should be close enough for now
jekain314 6:2a8486283198 164 // Approximately 1 nautical mile / minute latitude, 60 minutes/degree, 1852 meters/nautical mile
jekain314 6:2a8486283198 165 //double latMetersPerDeg = 60.0*1852.0;
jekain314 6:2a8486283198 166 // longitude separation is approximately equal to latitude separation * cosine of latitude
jekain314 6:2a8486283198 167 //double lonMetersPerDeg = latMetersPerDeg*cos(posMsg.latitude*DEGREES_TO_RADIANS);
jekain314 6:2a8486283198 168
jekain314 6:2a8486283198 169 // Elapsed time since last known GPS position
jekain314 6:2a8486283198 170 //PPSTimeOffset is a result of possibly missing a prior GPS position message
jekain314 6:2a8486283198 171 // timeFromPPS.read() is always the time from the moset recent 1PPS
jekain314 6:2a8486283198 172 double elTime = (double)PPSTimeOffset + timeFromPPS.read();
jekain314 6:2a8486283198 173
jekain314 6:2a8486283198 174 // Position time -- GPSTime is the time of the last valid GPS position message
jekain314 6:2a8486283198 175 double posTime = GPSTime + elTime;
jekain314 6:2a8486283198 176
jekain314 6:2a8486283198 177 //toPC.printf(" elTime = %6.3f PPSimeOffset = %6.3f \n", elTime, PPSTimeOffset);
jekain314 6:2a8486283198 178 //toPC.printf(" latRateFac = %10.3f lonRateFac = %10.3f \n", latRateFac, lonRateFac);
jekain314 6:2a8486283198 179 //toPC.printf(" latRateFac = %10.3f lonRateFac = %10.3f \n", latMetersPerDeg, lonMetersPerDeg);
jekain314 6:2a8486283198 180
jekain314 6:2a8486283198 181 // Estimated position based on previous position and velocity
jekain314 6:2a8486283198 182 // posMsg is the last time when the BESTVEL and BESTPOS messages had identical times
jekain314 6:2a8486283198 183 //double latPos = posMsg.latitude + (nVel/latMetersPerDeg)*elTime;
jekain314 6:2a8486283198 184 //double lonPos = posMsg.longitude + (eVel/lonMetersPerDeg)*elTime;
jekain314 6:2a8486283198 185
jekain314 6:2a8486283198 186 double latPos = posMsg.latitude + (nVel/latRateFac)*elTime;
jekain314 6:2a8486283198 187 double lonPos = posMsg.longitude + (eVel/lonRateFac)*elTime;
jekain314 6:2a8486283198 188 double htPos = posMsg.height + velMsg.verticalSpeed/(60*1852)*elTime;
jekain314 6:2a8486283198 189
jekain314 7:2e20e4cf53e6 190 char solReady = 'N';
jekain314 7:2e20e4cf53e6 191 //solStatus
jekain314 7:2e20e4cf53e6 192 if (posMsg.solStatus == 0) //see description of solution status in OEMV615.h
jekain314 7:2e20e4cf53e6 193 {
jekain314 7:2e20e4cf53e6 194 solReady = 'Y';
jekain314 7:2e20e4cf53e6 195 }
jekain314 7:2e20e4cf53e6 196
jekain314 7:2e20e4cf53e6 197 toPC.printf("WMsg POSVEL %5.3lf %1d %c %8.5lf %9.5lf %4.3lf %4.3lf %4.3lf %4.3lf\n",
jekain314 6:2a8486283198 198 posTime,
jekain314 6:2a8486283198 199 posMsg.numSolSV,
jekain314 7:2e20e4cf53e6 200 solReady,
jekain314 6:2a8486283198 201 latPos,
jekain314 6:2a8486283198 202 lonPos,
jekain314 6:2a8486283198 203 htPos,
jekain314 6:2a8486283198 204 nVel,
jekain314 6:2a8486283198 205 eVel,
jekain314 6:2a8486283198 206 velMsg.verticalSpeed
jekain314 6:2a8486283198 207 );
jekain314 6:2a8486283198 208 }
jekain314 6:2a8486283198 209
jekain314 6:2a8486283198 210 void processPCmessages(FILE* &fpNav, OEM615BESTPOS posMsg, OEM615BESTVEL velMsg)
jekain314 6:2a8486283198 211 {
jekain314 6:2a8486283198 212
jekain314 6:2a8486283198 213 //we should put the below stuff into the readPC() procedure.
jekain314 6:2a8486283198 214 //only do these actions in response to a command so no need for the tests w/o an inoput byte from the PC
jekain314 6:2a8486283198 215 //perform the activities as a response to the commands
jekain314 6:2a8486283198 216 if (sendPosVel) //true if we want to return a position solution
jekain314 6:2a8486283198 217 {
jekain314 6:2a8486283198 218 sendPosVel=false; //set to true if a POSVEL is requested from the PC
jekain314 6:2a8486283198 219 sendPosVelMessageToPC(posMsg, velMsg);
jekain314 6:2a8486283198 220 }
jekain314 6:2a8486283198 221
jekain314 6:2a8486283198 222 //all this does is assess the GPS convergence -- really available in the above
jekain314 6:2a8486283198 223 if (sendStatus) //send the status message to the PC
jekain314 6:2a8486283198 224 {
jekain314 6:2a8486283198 225 sendStatus=false;
jekain314 6:2a8486283198 226 char solReady = 'N';
jekain314 6:2a8486283198 227 //solStatus
jekain314 6:2a8486283198 228 if (posMsg.solStatus == 0) //see description of solution status in OEMV615.h
jekain314 6:2a8486283198 229 {
jekain314 6:2a8486283198 230 solReady = 'Y';
jekain314 6:2a8486283198 231 }
jekain314 6:2a8486283198 232 toPC.printf("WMsg STATUS %5.3lf %c\n",
jekain314 6:2a8486283198 233 GPSTime,
jekain314 6:2a8486283198 234 solReady
jekain314 6:2a8486283198 235 );
jekain314 6:2a8486283198 236 }
jekain314 6:2a8486283198 237
jekain314 6:2a8486283198 238 //should just record ALL the data -- can pick over it in the post-processing
jekain314 6:2a8486283198 239 if (sendRecData) //begin to (or stop) record the serial data
jekain314 6:2a8486283198 240 {
jekain314 6:2a8486283198 241 sendRecData=false;
jekain314 6:2a8486283198 242 char recChar = 'N';
jekain314 6:2a8486283198 243 if (recordData)
jekain314 6:2a8486283198 244 {
jekain314 6:2a8486283198 245 if ((fpNav == NULL))
jekain314 6:2a8486283198 246 {
jekain314 6:2a8486283198 247 fpNav = fopen("/sd/Data/NAV.bin", "wb");
jekain314 6:2a8486283198 248 //toPC.printf("\n opened the SD card file recordData=%10d \n\n", fpNav);
jekain314 6:2a8486283198 249 }
jekain314 6:2a8486283198 250 if (fpNav != NULL)
jekain314 6:2a8486283198 251 {
jekain314 6:2a8486283198 252 recChar = 'Y';
jekain314 6:2a8486283198 253 }
jekain314 6:2a8486283198 254 else
jekain314 6:2a8486283198 255 {
jekain314 6:2a8486283198 256 toPC.printf(" Could not open the SD card \n\n");
jekain314 6:2a8486283198 257 }
jekain314 6:2a8486283198 258 }
jekain314 6:2a8486283198 259 else
jekain314 6:2a8486283198 260 {
jekain314 6:2a8486283198 261 if (fpNav != NULL)
jekain314 6:2a8486283198 262 {
jekain314 6:2a8486283198 263 toPC.printf(" closing the SD card file \n\n");
jekain314 6:2a8486283198 264 fclose(fpNav);
jekain314 8:13724ed3f825 265 wait(1.0);
jekain314 8:13724ed3f825 266 //toPC.printf("\n after closing the SD card file \n\n");
jekain314 6:2a8486283198 267 recordData = false;
jekain314 6:2a8486283198 268 fpNav = NULL;
jekain314 6:2a8486283198 269 }
jekain314 6:2a8486283198 270 }
jekain314 6:2a8486283198 271 toPC.printf("WMsg RECORDDATA %c\n",
jekain314 6:2a8486283198 272 recChar
jekain314 6:2a8486283198 273 );
jekain314 6:2a8486283198 274 }
jekain314 6:2a8486283198 275
jekain314 6:2a8486283198 276 if (sendStreamPos) //stream the position data to the PC
jekain314 6:2a8486283198 277 {
jekain314 6:2a8486283198 278 sendStreamPos=false;
jekain314 6:2a8486283198 279 char streamChar = 'N';
jekain314 6:2a8486283198 280 if (streamPos)
jekain314 6:2a8486283198 281 {
jekain314 6:2a8486283198 282 streamChar = 'Y';
jekain314 6:2a8486283198 283 }
jekain314 6:2a8486283198 284 toPC.printf("WMsg POSSTREAM %c\n",
jekain314 6:2a8486283198 285 streamChar
jekain314 6:2a8486283198 286 );
jekain314 6:2a8486283198 287 }
jekain314 6:2a8486283198 288
jekain314 6:2a8486283198 289 //not sure this is ever used ..
jekain314 6:2a8486283198 290 if (sendLogMsgInfo) //send log info to the PC
jekain314 6:2a8486283198 291 {
jekain314 6:2a8486283198 292 sendLogMsgInfo=false;
jekain314 6:2a8486283198 293 char logChar = 'N';
jekain314 6:2a8486283198 294 if (logMsgInfo)
jekain314 6:2a8486283198 295 {
jekain314 6:2a8486283198 296 logChar = 'Y';
jekain314 6:2a8486283198 297 }
jekain314 6:2a8486283198 298 toPC.printf("WMsg LOGINFO %c\n",
jekain314 6:2a8486283198 299 logChar
jekain314 6:2a8486283198 300 );
jekain314 6:2a8486283198 301 }
jekain314 6:2a8486283198 302
jekain314 6:2a8486283198 303
jekain314 6:2a8486283198 304 }