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:
25:2287bd8c9877
Parent:
24:353322495742
Child:
26:c2208b0ff78b
--- a/main.cpp	Mon May 27 19:30:57 2013 +0000
+++ b/main.cpp	Thu Jun 20 15:33:38 2013 +0000
@@ -107,7 +107,7 @@
     //This the serial communication back to the the PC host
     //Launch the C++ serial port read program there to catch the ASCII characters
     //toPC.baud(9600); wait_ms(100);    
-    toPC.baud(2*115200); wait_ms(100);
+    toPC.baud(8*115200); wait_ms(100);
     //toPC.baud(1*115200); wait_ms(100);
     //toPC.printf("\n\n released GPS from RESET and set to high baud rate \n\n");
     
@@ -212,9 +212,9 @@
     //set up the ADIS16488 
     setupADIS();
 
-    setUpMessages();  //set up the expected text message commands frm the PC 
+    setUpMessages();  //set up the expected text message commands from the PC 
     
-    //set up the interrupt to catch the GPS receiver serial bytes as they are presented
+    //initiate the interrupt to catch the GPS receiver serial bytes as they are presented
     GPS_COM1.attach(&readSerialByte, MODSERIAL::RxIrq);
     
     timeFromPPS.start();  //start the time for measuring time from 1PPS events
@@ -224,7 +224,7 @@
     
     int totalBytesWritten = 0;
     
-    /*establish the initial value for the CRC recursion atter the header
+    /*establish the initial value for the CRC recursion after the header signature bytes
     unsigned long CRC = 0;
     CRC32Value(CRC, 0xAA);
     CRC32Value(CRC, 0x44);
@@ -234,12 +234,12 @@
     toPC.printf(" CRC after AA44121C header: %08x \n", CRC);
     wait(20);
     */
-        
-    //why do we always start with recording the data ???
+    
+    //at the start we do not record the data
     recordData = false;
     sendRecData = false;     
     
-    unsigned long cyclesPerSec = 0;
+    unsigned long cyclesPerSec = 0;  //main while() loop cycles per GPS sec
     bool GPSdataWritten = false;
     bool finishTrigger = false;
     Timer triggerInterval;
@@ -255,23 +255,23 @@
     {        
             
         //read the USB serial data from the PC to check for commands
-        //in the primary real-time portion, there are no bytes from the PC so this has no impact
         readFromPC();
         
-        //
         //this will close the fpNav file on the SD card if the file is open 
         //and the elapsed time from PosVel messages is > 60 secs
         //this prevents loosing the fpNav file if the PC goes down
+        //   !!!! timeFromPosVelMessageReceipt !!! was never started
         if (fpNav && (timeFromPosVelMessageReceipt.read() > 10) )
         {
             sendRecData = true;
             recordData  = false;
         }
-        //
                 
+        // for any received PC message, take the appropriate action
         processPCmessages(fpNav, posMsg, velMsg);
         
-        if (get_file_msg)
+        //if we receive a "GETFILE" message from the PC -- close the fpNavFile and break from the while() loop
+        if (get_file_msg)  
         {
             if (fpNav != NULL) fclose(fpNav);
             break;  //terminate the while loop when we receive this message from the PC
@@ -283,11 +283,10 @@
             toPC.printf("WMsg TRIGGERTIME %10d \n", triggerTime);
             //pre-fire the trigger using the mid-body 2.5mm connection (T2i)
             pre_fire = 0;  //pin30 (midbody of connector) set to zero
-            wait(.01f);  //wait for 0.25 secs
+            wait(.01f);  // not sure what this does
             fire = 0; //fire the trigger using the tip connection
-            //wait(0.10);
-            fireTrigger = false;
-            finishTrigger = true;
+            fireTrigger = false;  //finished the setup -- but wait to do the actual fire 
+            finishTrigger = true; //set to false after firing the trigger
             triggerInterval.start();
         }
         
@@ -298,19 +297,18 @@
             fire = 1;
             pre_fire = 1;
             triggerInterval.reset();
-            finishTrigger = false;
+            finishTrigger = false;  //completes the trigger firing pulse definition
         }
         
         cyclesPerSec++;
 
-//
         ////////////////////////////////////////////////////////////////////////////
         //below is where we process the complete stored GPS message for the second
         //The !IMUDataReady test prevents the IMU and GPS data from being written 
         //to disk on the same pass through this loop  
-        /////////////////////////////////////////////////////////////////////////////
+        /////////////////////////////////////////////////////////////////////////////        
         
-        
+        //there are three potential messages and all messages have a header
         if (completeMessageAvailable && !IMUDataReady)
         {
             
@@ -322,26 +320,35 @@
             //Thus GPSTimemsecs increments by 1 here while GPSTimemsecs effectively decrements by 1.
             //This handles IMU time tagging between the 1PPS event and the first receipt of a new GPS time.
 
-            GPSTimemsecs = msgHdr.GPSTime_msecs;  //time in GPS message header
-            PPSTimeOffset = 0;                    //incremented by 1 in the PPS ISR
             
+            //message header length is 28 -- right side is pointer to the receiver-computed CRC for this record 
             unsigned long msgCRC = *((unsigned long*)&msgBuffer[messageLocation[savedMessageCounter-1] + 28 + msgHdr.messageLength]);
             
             //toPC.printf("tmeFrom1PPS= %5d  ID= %3d Ln = %3d computedCRC= %08x msgCRC= %08x msgCntr = %3d CRCerr=%4d\n", 
             //    timeFromPPS.read_us(), msgHdr.messageID, msgHdr.messageLength, computedCRC, msgCRC, savedMessageCounter, TotalBadCRCmatches);
                 
-            if ( msgCRC != computedCRC)
+            if ( msgCRC != computedCRC)  //computedCRC is performed as we read each bvyte
             {
                 toPC.printf("WMsg bad CRC match for messageID %3d total CRC errors = %4d \n",  
                     msgHdr.messageLength, TotalBadCRCmatches++);
             }
+            else
+            {
+                //if the CRC check is valid -- then get the time from the header
+                //we get three messages each sec -- does it matter that we do this three times?
+                GPSTimemsecs = msgHdr.GPSTime_msecs;  //time in GPS message header
+                
+                //the PPSTimeOffset accounts for the occurrence where we do not get any GPS messages over a sec -- but PPS is still operative
+                PPSTimeOffset = 0;                    //incremented by 1 in the PPS ISR
+            }
                      
-            if      (msgHdr.messageID == 42)
+            if      (msgHdr.messageID == 42) //this is the position message (lat, lon, alt)
             {
+                //map the starting record byte index to the record structure
                 curPos = *((OEM615BESTPOS*)&msgBuffer[messageLocation[savedMessageCounter-1]]);
                 posMsg = curPos;
                 
-                if (streamPos)
+                if (streamPos)  // we no longer use this functionality
                 {
                         toPC.printf("BESTPOS %5d %1d %8.6lf %9.6lf %5.3lf %d %d\n",
                                           curPos.msgHeader.GPSTime_msecs,  curPos.solStatus,
@@ -350,7 +357,7 @@
                 }
                 
             } 
-            else if (msgHdr.messageID == 99)  
+            else if (msgHdr.messageID == 99)  //this is the velocity message
             {
                 curVel = *((OEM615BESTVEL*)&msgBuffer[ messageLocation[savedMessageCounter-1] ]);
                 //toPC.printf("BESTVEL  vel: horizontalSpeed= %5.3f heading=%5.1f verticalSpeed=%4.2f \n", 
@@ -358,10 +365,13 @@
                 velMsg = curVel;
             }
  
+            //below is set to true when we detect that we have received a complete GPS message
             completeMessageAvailable = false;
         }
         
         //write the GPS data to the SD card
+        //NOTE:  this is valid only for a once-per-sec GPS message
+        //for this case, all messages come out well prior to 0.5 secs after the 1PPS
         if (!IMUDataReady && !GPSdataWritten && timeFromPPS.read_us() > 500000 && recordData && (fpNav != NULL))
         {
                 totalBytesWritten += fwrite(&msgBuffer, 1, byteCounter, fpNav);
@@ -387,6 +397,7 @@
             IMUDataReady = false;
         }
         
+        //this is a command from the PC to fire a trigger
         if (camera1EventDetected)  //we have detected a camera trigger event
         {
             toPC.printf("WMsg TRIGGERTIME %5.3lf\n", camera1Time);
@@ -407,10 +418,12 @@
             IMURecordCounter = 0;
             detectedGPS1PPS = false;
             
-            rxMsg = !rxMsg;
+            rxMsg = !rxMsg;  //flash the lights to make sure the mbed loop is operative
             txMsg = !txMsg;
         }
-    }
+    ///////////////////////////////////////////
+    }  //end of the major while() loop 
+    ///////////////////////////////////////////
       
 
      if (fpNav != NULL)
@@ -432,22 +445,4 @@
      
      NVIC_SystemReset();
     
-    /*
-    //at this point we have terminated the dat collection and we need to send the stored nav file to the PC 
-    FILE* fpNavStored = fopen("/sd/Data/NAV.bin", "rb");
-    unsigned char tempArray[512];
-    toPC.printf(" beginning the file transfer \n");
-    int i = 0;
-    while(!feof(fpNavStored) )
-    {
-        
-        toPC.printf(" reading the 512 bytes: %3d  \n", i);
-        fread(tempArray, 1, 512, fpNavStored);
-        toPC.printf(" writing the 512 bytes: %3d  \n", i);
-        fwrite(tempArray, 1, 512, toPC); 
-        i++;
-    }
-    */
-
-    
 }
\ No newline at end of file