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

Files at this revision

API Documentation at this revision

Comitter:
jekain314
Date:
Sun May 05 14:35:25 2013 +0000
Parent:
5:2ce1be9d4bef
Child:
7:1bec23c68a3c
Child:
9:893481b2e488
Commit message:
the logic for receiving PC messages was revised. Now we look for a 4-byte preamble (WMsg) as a message start and do a reset if we don't find a sequence of these 4 bytes at an assumed start of a message.

Changed in this revision

ADIS16488.h Show annotated file Show diff for this revision Revisions of this file
PCMessaging.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ADIS16488.h	Sat May 04 23:43:53 2013 +0000
+++ b/ADIS16488.h	Sun May 05 14:35:25 2013 +0000
@@ -111,12 +111,12 @@
     //change the page to 0 to get the data
     spi.write((int)0x8000);  //change to page 0
     
-    toPC.printf(" setting the default values\n");
+    //toPC.printf(" setting the default values\n");
     
     //set the IMU synch and message ID
     tempRec.synch = 0x1C1244AA;  //same as the GPS synch words
     tempRec.msgID = 111;  //IMU record ID
     
-    toPC.printf(" finished setting the default values\n");
+    //toPC.printf(" finished setting the default values\n");
 
 }
--- a/PCMessaging.h	Sat May 04 23:43:53 2013 +0000
+++ b/PCMessaging.h	Sun May 05 14:35:25 2013 +0000
@@ -1,20 +1,20 @@
 //these are defines for the messages that are sent from the PC across the USB
 //these messages produce reactions on the mbed
-const unsigned char  STATUS_MSG         =0;
-const unsigned char  POSVEL_MSG         =1;
-const unsigned char  STARTDATA_MSG      =2;
-const unsigned char  STOPDATA_MSG       =3;
-const unsigned char  STARTSTREAM_MSG    =4;
-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 unsigned char  POSVEL_MSG         =0;
+const unsigned char  FIRE_TRIGGER_MSG   =1;
+const unsigned char  STATUS_MSG         =2;
+const unsigned char  STARTDATA_MSG      =3;
+const unsigned char  STOPDATA_MSG       =4;
+const unsigned char  STARTSTREAM_MSG    =5;
+const unsigned char  STOPSTREAM_MSG     =6;
+const unsigned char  STARTLOGINFO_MSG   =7;
+const unsigned char  STOPLOGINFO_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
 
-const unsigned short serBuffMax = 1024;
+const unsigned short serBuffMax = 18;
 char serBuf[serBuffMax];
 int serBufChars=0;
 
@@ -36,6 +36,9 @@
 unsigned char CR = 0x0d;                //ASCII Carriage Return
 unsigned char LF = 0x0a;                //ASCII Line Feed
 
+char preamble[5] = "WMsg";
+char testPreamble[5];
+
 void setUpMessages(void)
 { 
     //set up the ASCII text records that are candidates to be passed from the PC
@@ -50,40 +53,59 @@
     sprintf(msgList[FIRE_TRIGGER_MSG],  "WMsg TRIGGER");
     //message length is from 10 to 16 chars
     
-    toPC.printf(" finished setting up messages \n");
+ //   toPC.printf(" finished setting up messages \n");
 }
 
 void readFromPC()
 {
-     //The received commands only occur at the initialization stage -- time is not that critical there.
-    //during the real-time action, we will never pass the followong if test ( no characters received)
+    //  should this be a while rather than if ??? -- may have multiple bytes in buffer
     if (toPC.readable()) //read a PC serial byte and test it for a command
     {
-        
-        // Read in next character
+        // Read in next character  -- why not read all available??
         unsigned char inChar = 0;
-        if(toPC.readable() ) inChar = toPC.getc();  //read char from the USB serial link to the PC
+        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
         if (inChar == CR || inChar == LF)  return; //CR is a 0x0a
         
-        // serBuffMax = 1024 -- largest serBuffMax is 1023 -- but we add one below for the '\0'
-        if (serBufChars >= (serBuffMax-2)) {toPC.printf("WMsg overun char buff \n"); serBufChars = 0; };
-        serBuf[serBufChars] = inChar; //set this char in a char array
+        //all received messages assumed to have a WMsg preamble
+        //if we have read 4 chars, test these for "WMsg", if they are not WMsg, reset the buffer counter
+        //if we receive an occasional bad byte that messes up a message, this will resynch fast to a next message
+        //this will let us miss a message --- but hopefully only one
+        
+        // serBuffMax = 18 -- largest serBuffMax is 16 -- but we add one below for the '\0'
+        // if the following occurs we have had a trash byte following a WMsg header
+        if (serBufChars >= (serBuffMax-2)) {toPC.printf("WMsg restart message search\n"); serBufChars = 0; }
+        
+        serBuf[serBufChars] = inChar; //set this char in a char array for testing complete message
+  
+        testPreamble[serBufChars] = inChar;  //char array for testing the preamble
         serBufChars++;
         
+        if (serBufChars == 4)  //four initial chars detected (0, 1, 2, 3)
+        {
+            testPreamble[4] = '\0';  //form null-terminated string for compare
+            if (strncmp(testPreamble, preamble, 5) != 0)   //compare the chars to the WMsg preamble
+            {
+                toPC.printf("WMsg preamble mismatch \n");
+                serBufChars = 0;  //if they dont match -- restart the search for a preamble
+                return;
+            }
+        }
+        
+        //if we get here, we have found a preamble and we are looking for a complete message
         //no need to continue if numChars are less than the shortest candidate message
         if (serBufChars < minMessageSize) return;
                 
         // Append end of string
-        //We always assume we have a complete message string and test for this below
+        // We always assume we have a complete message string and test for this below
         serBuf[serBufChars] = '\0';
         
         bool validMessage = false;
         
-        
         // Check for valid message -- there are numMessages possible messages
+        //this assumes that the message buffer contains an exact message
         for (int m = 0; m < numMessages && !validMessage; m++) //check for all messages ... 
         {
             //toPC.printf(" \n\n found chars to test %3d %3d %s \n\n\n ", serBufChars, strlen(msgList[m]), serBuf );
@@ -99,7 +121,6 @@
                 validMessage = true;
                 serBufChars = 0; //reset the character count to reset for next message
                 
-    
                 //set programmatic action flags based on the message
                 switch(m)
                 {
@@ -130,11 +151,14 @@
                     break;  
                     case FIRE_TRIGGER_MSG:
                         fireTrigger = true;
+                        toPC.printf("MBED received trigger command \n");
                     break;
                 }  //end Switch statement
                 break;
             } //end test for a valid message
+            
         }  //end message text loop
+        
     }  //end pc.readable
 };
 
@@ -236,7 +260,7 @@
             //th file is closed(in main) if we dont receive POSVAL messages for 60 secs
             if (fpNav == NULL)
             { 
-                toPC.printf(" opening the SD card file \n");
+                toPC.printf("WMsg opening the SD card file \n");
                 fpNav = fopen("/sd/Data/NAV.bin", "wb");
                 wait_ms(10);
                 recordData = true;
--- a/main.cpp	Sat May 04 23:43:53 2013 +0000
+++ b/main.cpp	Sun May 05 14:35:25 2013 +0000
@@ -93,7 +93,7 @@
     //toPC.baud(9600); 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");
+    //toPC.printf("\n\n released GPS from RESET and set to high baud rate \n\n");
     
     //just wait to launch the GPS receiver
     for (int i=0; i<5; i++) { toPC.printf("WMsg start: %3d \n", 4-i); wait(1); }
@@ -106,7 +106,7 @@
     }
     else
     {
-        toPC.printf(" SD card not present \n");
+        toPC.printf("WMsg SD card not present \n");
     }
     
     //NOTE:  we do not assume that the GPS receiver has been pre-set up for the WALDO_FCS functionality
@@ -236,6 +236,7 @@
         //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
@@ -244,6 +245,7 @@
             sendRecData = true;
             recordData  = false;
         }
+        */
                 
         processPCmessages(fpNav, posMsg, velMsg);
         
@@ -259,6 +261,8 @@
             triggerInterval.start();
         }
         
+        //the trigger requires a pulse -- the above portion lowers the signal and the below raises it
+        //this has been tested at 50 msecs and it will not fire at that pulse duration
         if(finishTrigger && triggerInterval.read_ms() > 100)
         {
             fire = 1;