Changeset - e5c11279c2a0
[Not reviewed]
default
0 1 0
mkanning@CL-ENS241-10.cedarville.edu - 12 years ago 2013-01-10 15:03:18
mkanning@CL-ENS241-10.cedarville.edu
checksum calculator and comparator for NMEA
1 file changed with 20 insertions and 2 deletions:
0 comments (0 inline, 0 general)
master/master/lib/trackuinoGPS/gpsMKa.c
Show inline comments
 
/*
 
* gpsMKa.c
 
*
 
* Created: 11/15/2012 12:02:38 PM
 
*  Author: mkanning
 
*/
 
 
#include <stdbool.h>
 
#include <avr/io.h>
 
#include "gpsMKa.h"
 
#include "../serial.h"
 

	
 

	
 
// holds the byte ALREADY PARSED. includes starting character
 
int bytesReceived = 0;
 

	
 
//data (and checksum) of most recent transmission
 
char data[16];
 

	
 
//used to skip over bytes during parse
 
int skipBytes = 0;
 

	
 
//used to index data arrays during data collection
 
int numBytes = 0;
 

	
 
//variables to store data from transmission
 
//least significant digit is stored at location 0 of arrays
 
uint8_t tramsmissionType[5];
 
uint8_t timestamp[9];	//hhmmss.ss
 
uint8_t latitude[8];	//lllll.lla
 
uint8_t longitude[8];	//yyyyy.yyb
 
uint8_t quality;		//quality for GGA and validity for RMC
 
uint8_t numSatellites[2];
 
uint8_t hdop[4];		//xx.x
 
uint8_t altitude[8];	//xxxxxx.x
 
uint8_t wgs84Height[6];	//sxxx.x
 
uint8_t lastUpdated[6];	//blank - included for testing
 
uint8_t stationID[6];	//blank - included for testing
 
uint8_t checksum[2];	//xx
 
uint8_t knots[5];		//xxx.xx
 
uint8_t course[5];		//xxx.x
 
uint8_t datestamp[6];	//ddmmyy
 
uint8_t variation[6];	//xxx.xb
 
int calculatedChecksum;
 
int receivedChecksum;
 

	
 
// transmission state machine
 
enum decodeState {
 
	//shared fields
 
	INITIALIZE=0,
 
	GET_TYPE,
 
	GPS_CHECKSUM,	//need to find out how this is calculated/compared
 
	GPS_CHECKSUM,	//XOR of all the bytes between the $ and the * (not including the delimiters themselves), written in hexadecimal
 
	//GGA data fields
 
	GGA_TIME,
 
	GGA_LATITUDE,
 
	GGA_LONGITUDE,
 
	GGA_QUALITY,
 
	GGA_SATELLITES,
 
	GGA_HDOP,
 
	GGA_ALTITUDE,
 
	GGA_WGS84,
 
	GGA_LAST_UPDATE,
 
	GGA_STATION_ID,
 
	//RMC data fields
 
	RMC_TIME,
 
	RMC_VALIDITY,
 
	RMC_LATITUDE,
 
	RMC_LONGITUDE,
 
	RMC_KNOTS,
 
	RMC_COURSE,
 
	RMC_DATE,
 
	RMC_MAG_VARIATION,
 
	
 
}decodeState;
 

	
 

	
 

	
 
//// MKa GPS transmission parser START
 
void parse_gps_transmission(void){
 
	
 
	// Pull byte off of the buffer
 
	char byte = 'a';// serial1_readChar();
 
	
 
	if(byte == '$') //start of transmission sentence
 
	{
 
		decodeState = GET_TYPE;
 
		numBytes = 0; //prep for next phases
 
		skipBytes = 0;
 
		calculatedChecksum = 0;
 
	}
 
	
 
	//parse transmission type
 
	else if (decodeState == GET_TYPE)
 
	{
 
		tramsmissionType[numBytes] = byte;
 
		numBytes++;
 
		
 
		if(byte == ',') //end of this data type
 
		{
 
			if (tramsmissionType[2] == 'G' &&
 
@@ -365,86 +366,103 @@ void parse_gps_transmission(void){
 
	else if(decodeState == RMC_KNOTS)
 
	{
 
		if (byte == ',') //end of this data type
 
		{
 
			decodeState = RMC_COURSE;
 
			numBytes = 0; //prep for next phase of parse
 
		}
 
		else //store data
 
		{
 
			knots[numBytes]= byte; //adjust number of bytes to fit array
 
			numBytes++;
 
		}
 
	}
 
	
 
	//course
 
	else if(decodeState == RMC_COURSE)
 
	{
 
		if (byte == ',') //end of this data type
 
		{
 
			decodeState = RMC_DATE;
 
			numBytes = 0; //prep for next phase of parse
 
		}
 
		else //store data
 
		{
 
			course[numBytes] = byte; //adjust number of bytes to fit array
 
			numBytes++;
 
		}
 
	}
 
	
 
	//date
 
	else if(decodeState == RMC_DATE)
 
	{
 
		if (byte == ',') //end of this data type
 
		{
 
			decodeState = RMC_MAG_VARIATION;
 
			skipBytes = 0; //prep for next phase of parse
 
			numBytes = 0;
 
		}
 
		else //store data
 
		{
 
			datestamp[numBytes] = byte; //adjust number of bytes to fit array
 
			numBytes++;
 
		}
 
	}
 
	
 
	//magnetic variation
 
	else if(decodeState == RMC_MAG_VARIATION)
 
	{
 
		if (byte == ',') //end of this data type
 
		if (byte == '*') //end of this data type
 
		{
 
			decodeState = GPS_CHECKSUM;
 
			numBytes = 0; //prep for next phase of parse
 
		}
 
		else //store data
 
		{
 
			variation[numBytes] = byte; //adjust number of bytes to fit array
 
			numBytes++;
 
		}
 
	}
 
	///parses RMC transmissions END
 
	
 
	
 
	//checksum
 
	else if (decodeState == GPS_CHECKSUM)
 
	{
 
		if (numBytes == 2) //end of data - terminating character ??
 
		{
 
			//checksum calculator for testing http://www.hhhh.org/wiml/proj/nmeaxor.html
 
			//TODO: must determine what to do with correct and incorrect messages
 
			receivedChecksum = checksum[0] + (checksum[1]*16);	//convert bytes to int
 
			if(calculatedChecksum==receivedChecksum)
 
			{
 
				
 
			}
 
			else
 
			{
 
				
 
			}
 
			
 
			decodeState = INITIALIZE;
 
			numBytes = 0; //prep for next phase of parse
 
		}
 
		else //store data
 
		{
 
			checksum[numBytes] = byte; //adjust number of bytes to fit array
 
			numBytes++;
 
		}
 
	}
 
	
 
	if (decodeState!=GPS_CHECKSUM && decodeState!=INITIALIZE)	//want bytes between '$' and '*'
 
	{
 
	//input byte into running checksum
 
	XORbyteWithChecksum(byte);
 
}
 
	
 

	
 
}
 
/// MKa GPS transmission parser END
 

	
 
void XORbyteWithChecksum(uint8_t byte){
 
	calculatedChecksum ^= (int)byte; //this may need to be re-coded
 
}
 

	
0 comments (0 inline, 0 general)