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
Diff: PCMessaging.h
- Revision:
- 6:71da5b99de97
- Parent:
- 5:2ce1be9d4bef
- Child:
- 7:1bec23c68a3c
- Child:
- 9:893481b2e488
--- 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;