this version has all of Jim's fixes for reading the GPS and IMU data synchronously

Dependencies:   MODSERIAL SDFileSystem mbed SDShell CRC CommHandler FP LinkedList LogUtil

Revision:
1:8e24e633f8d8
Parent:
0:432b860b6ff7
Child:
2:7039be3daf6e
--- a/PCMessaging.h	Mon Apr 22 21:26:04 2013 +0000
+++ b/PCMessaging.h	Tue Apr 30 19:59:43 2013 +0000
@@ -8,12 +8,14 @@
 const unsigned char  STOPSTREAM_MSG     =5;
 const unsigned char  STARTLOGINFO_MSG   =6;
 const unsigned char  STOPLOGINFO_MSG    =7;
+const unsigned char  FIRE_TRIGGER_MSG   =8;
 
 const double  DEGREES_TO_RADIANS = acos(-1.0)/180.0;
 const double eccen          = 0.0818191908426;  //WGS84 earth eccentricity
 const double earthRadius    = 6378137;          //WGS84 earthRadius in meters
 
-char serBuf[128];
+const unsigned short serBuffMax = 1024;
+char serBuf[serBuffMax];
 int serBufChars=0;
 
 //flags to control the PC command actions
@@ -25,9 +27,10 @@
 bool logMsgInfo     =false;
 bool sendLogMsgInfo =false;
 bool recordData     =false;
+bool fireTrigger    =false;
 
 
-const unsigned char numMessages = 8;    //number of potential messages
+const unsigned char numMessages = 9;    //number of potential messages
 char msgList[numMessages][32];          //text array storing the command messages from the PC
 char minMessageSize = 11;               //minimum size of a text message
 unsigned char CR = 0x0d;                //ASCII Carriage Return
@@ -44,6 +47,7 @@
     sprintf(msgList[STOPSTREAM_MSG],    "WMsg POSSTREAM N");
     sprintf(msgList[STARTLOGINFO_MSG],  "WMsg LOGINFO Y");
     sprintf(msgList[STOPLOGINFO_MSG],   "WMsg LOGINFO N");
+    sprintf(msgList[FIRE_TRIGGER_MSG],  "WMsg TRIGGER");
     //message length is from 10 to 16 chars
     
     toPC.printf(" finished setting up messages \n");
@@ -57,7 +61,8 @@
     {
         
         // Read in next character
-        char inChar = toPC.getc();  //read char from the USB serial link to the PC
+        unsigned char inChar = 0;
+        if(toPC.readable() ) inChar = toPC.getc();  //read char from the USB serial link to the PC
         //toPC.printf("%02x ",inChar);
         
         //incoming messages will end witb a CR / LF -- disregard these chars
@@ -70,6 +75,7 @@
                 
         // Append end of string
         //We always assume we have a complete message string and test for this below
+        if (serBufChars >= serBuffMax) {toPC.printf(" overun char buff \n"); serBufChars = 0; return; };
         serBuf[serBufChars] = '\0';
         
         bool validMessage = false;
@@ -86,6 +92,7 @@
             {
             
                 //toPC.printf( "\n       found valid message %s  \n\n", serBuf);
+                rxMsg = !rxMsg;
 
                 validMessage = true;
                 serBufChars = 0; //reset the character count to reset for next message
@@ -117,6 +124,9 @@
                         logMsgInfo = (m == STARTLOGINFO_MSG);
                         sendLogMsgInfo = true;
                     break;  
+                    case FIRE_TRIGGER_MSG:
+                        fireTrigger = true;
+                    break;
                 }  //end Switch statement
                 break;
             } //end test for a valid message
@@ -172,7 +182,7 @@
             double elTime = (double)PPSTimeOffset + timeFromPPS.read();
             
             // Position time -- GPSTime is the time of the last valid GPS position message
-            double posTime = GPSTime + elTime;
+            double posTime = GPSTimemsecs/1000 + elTime;
             
             //toPC.printf(" elTime = %6.3f PPSimeOffset = %6.3f \n", elTime, PPSTimeOffset);
             //toPC.printf(" latRateFac = %10.3f  lonRateFac = %10.3f \n", latRateFac, lonRateFac);
@@ -205,11 +215,14 @@
                          eVel,
                          velMsg.verticalSpeed
                          );
+             txMsg = !txMsg;
 }
 
 void processPCmessages(FILE* &fpNav, OEM615BESTPOS posMsg, OEM615BESTVEL velMsg)
 {
 
+
+        
         //we should put the below stuff into the readPC() procedure.
         //only do these actions in response to a command so no need for the tests w/o an inoput byte from the PC
         //perform the activities as a response to the commands
@@ -222,6 +235,7 @@
         //all this does is assess the GPS convergence -- really available in the above
         if (sendStatus)  //send the status message to the PC
         {
+            txMsg = !txMsg;
             sendStatus=false;
             char solReady = 'N';
             //solStatus 
@@ -230,7 +244,7 @@
                 solReady = 'Y';
             }
             toPC.printf("WMsg STATUS %5.3lf %c\n", 
-                         GPSTime, 
+                         GPSTimemsecs, 
                          solReady
                          );
         }
@@ -238,36 +252,41 @@
         //should just record ALL the data -- can pick over it in the post-processing
         if (sendRecData)  //begin to (or stop) record the serial data
         {
-            sendRecData=false;
+            sendRecData=false;  //reset the flag so we dont continue to come through here
             char recChar = 'N';
-            if (recordData)
+            //recordData set to true only if we receive a STARTDATA from the PC
+            if (recordData)  //here we have received a message to record the data
             {
-                if ((fpNav == NULL))
+                if ((fpNav == NULL))  //if file not opened -- open it
                 {
+                    toPC.printf(" opening the SD card file \n");
                     fpNav = fopen("/sd/Data/NAV.bin", "wb");
-                    setvbuf(fpNav, NULL, _IONBF, 512); 
-                    //toPC.printf("\n opened the SD card file recordData=%10d \n\n", fpNav);
+                    wait_ms(1000);
                 }
-                if (fpNav != NULL)
+                if (fpNav != NULL)  //if the fie was already opened we will respond to the PC with a Y 
                 {
                     recChar = 'Y';
                 }
-                else
+                else //is the file was not opened we will write a message to the PC
                 {
                     toPC.printf(" Could not open the SD card \n\n");
                 }
             }
-            else
+            //recordData set to false only if we receive a STOPDATA from the PC
+            else //here we have received a message to stop the recording
             {
-                if (fpNav != NULL)
+                if (fpNav != NULL)  //if the file is open -- close it
                 {
                     toPC.printf(" closing the SD card file \n\n");
+                    fflush(fpNav);
                     fclose(fpNav);
-                    wait(1.0);
+                    wait_ms(1000);
                     //toPC.printf("\n after closing the SD card file \n\n");
-                    recordData = false;
                     fpNav = NULL;
                 }
+                //if stop recording received and the file is already closed -- just do nothing
+                recordData = false;
+
             }
             toPC.printf("WMsg RECORDDATA %c\n", 
                          recChar