Files @ c715dbef3de5
Branch filter:

Location: seniordesign-firmware/master/master/lib/gps.c

ethanzonca@CL-ENS241-08.cedarville.edu
Master repository restructuring and cleanup (major path changes)
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
* gpsMKa.c
*
* Created: 11/15/2012 12:02:38 PM
*  Author: mkanning
*/

#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "gps.h"
#include "serial.h"
#include "../config.h"
#include "led.h"



// Circular buffer for incoming data
uint8_t nmeaBuffer[NMEABUFFER_SIZE];

// Location of parser in the buffer
uint8_t nmeaBufferParsePosition = 0;

// Location of receive byte interrupt in the buffer
volatile uint16_t nmeaBufferDataPosition = 0;


/*
volatile uint8_t charTest;
ISR(USART1_RX_vect)
{
	serial0_sendChar(UDR1);
	
}*/

// 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
char tramsmissionType[7];

char timestamp[12];	//hhmmss.ss
char* get_timestamp() {
	return timestamp;
}
	
char latitude[14];	//lllll.lla
char* get_latitude() {
	return latitude;
}

char longitude[14];	//yyyyy.yyb
char* get_longitude() {
	return longitude;
}


char quality;		//quality for GGA and validity for RMC
char numSatellites[4];
char* getNumSatelites() {
	return numSatellites;
}

char hdop[6];		//xx.x
char* get_hdop() {
	return hdop;
}

char altitude[10];	//xxxxxx.x
char wgs84Height[8];	//sxxx.x
char lastUpdated[8];	//blank - included for testing
char stationID[8];	//blank - included for testing
char checksum[3];	//xx

char knots[8];		//xxx.xx
char* get_speedKnots() {
	return knots;
}

char course[8];		//xxx.x
char* get_course() {
	return course;
}
	
char dayofmonth[9];	//ddmmyy
char* get_dayofmonth() {
	return dayofmonth;
}

char variation[9];	//xxx.xb
int calculatedChecksum;
int receivedChecksum;

char convertBuf1[15];
char convertBuf2[15];

// transmission state machine
enum decodeState {
	//shared fields
	INITIALIZE=0,
	GET_TYPE,
	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;


char debugBuff[128];

ISR(USART1_RX_vect)
{
	nmeaBuffer[nmeaBufferDataPosition % NMEABUFFER_SIZE] = UDR1;
	nmeaBufferDataPosition = (nmeaBufferDataPosition + 1) % NMEABUFFER_SIZE;
	//serial0_sendChar(UDR1);
	//snprintf(debugBuff, 32, "GPS: bdp: %d, bpp: %d decodestate: %u \r\n", nmeaBufferDataPosition, nmeaBufferParsePosition, decodeState);
	//serial0_sendString((debugBuff));
}


// Could inline if program space available
static void setParserState(uint8_t state)
{
	decodeState = state;

	
	// If resetting, clear vars
	if(state == INITIALIZE)
	{
		calculatedChecksum = 0;
	}
	
	// Every time we change state, we have parsed a byte
	nmeaBufferParsePosition = (nmeaBufferParsePosition + 1) % NMEABUFFER_SIZE;
}



//// MKa GPS transmission parser START
void parse_gps_transmission(void){
	
	// Pull byte off of the buffer
	char byte;
	
	
	
	while(nmeaBufferDataPosition != nmeaBufferParsePosition) {
	led_on(LED_ACTIVITY);
		
		byte = nmeaBuffer[nmeaBufferParsePosition];
		
		//snprintf(debugBuff, 64, "GPSParse: byte [%c] decodestate: %u bp: %u pp: %u\r\n",  byte, decodeState, nmeaBufferDataPosition, nmeaBufferParsePosition);
		//serial0_sendString((debugBuff));
		
		if(decodeState == INITIALIZE) //start of transmission sentence
		{
			if(byte == '$') {
				#ifdef DEBUG_NMEA
				serial0_sendString("found $\r\n");
				#endif
				
				setParserState(GET_TYPE);
				numBytes = 0; //prep for next phases
				skipBytes = 0;
				calculatedChecksum = 0;
			}		
			
			else {
				setParserState(INITIALIZE);
			}
		}
	
		//parse transmission type
		else if (decodeState == GET_TYPE)
		{
			tramsmissionType[numBytes] = byte;
			numBytes++;
			
			#ifdef DEBUG_NMEA
			serial0_sendString("stored a type byte\r\n");
			#endif
			
			if(byte == ',') //end of this data type
			{
				tramsmissionType[5] = 0x00;
				
				if (tramsmissionType[2] == 'G' &&
				tramsmissionType[3] == 'G' &&
				tramsmissionType[4] == 'A')
				{
					setParserState(GGA_TIME);
					numBytes = 0;
				}
				else if (tramsmissionType[2] == 'R' &&
				tramsmissionType[3] == 'M' &&
				tramsmissionType[4] == 'C')
				{
					setParserState(RMC_TIME);
					numBytes = 0;
				}
				else //this is an invalid transmission type
				{
					setParserState(INITIALIZE);
				}
			}
			else {
				// continue
				setParserState(GET_TYPE);
			}
			
		}
	
		///parses GGA transmissions START
		/// $--GGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*xx
		//timestamp
		else if (decodeState == GGA_TIME)
		{
			if (byte == ',') //end of this data type
			{
				timestamp[4] = 0x00; // Cut off at 4 (no seconds) for APRS
				setParserState(GGA_LATITUDE);
				skipBytes = 0; //prep for next phase of parse
				numBytes = 0;
			}
			else //store data
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found GGA time byte\r\n");
				#endif
				
				setParserState(GGA_TIME);
				timestamp[numBytes] = byte; //byte; //adjust number of bytes to fit array
				numBytes++;
			}
		}
	
		//latitude
		else if (decodeState == GGA_LATITUDE)
		{
			if (byte == ',' && skipBytes == 0) //discard this byte
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found lat skip byte\r\n");
				#endif
				
				skipBytes = 1;
				setParserState(GGA_LATITUDE);
			}
			else if (byte == ',') //end of this data type
			{
				
				latitude[7] = 0x00; // null terminate
				
				
				// First 2 bytes
				//convertBuf1[0] = latitude[0];
				//convertBuf1[1] = latitude[1];
				//convertBuf1[2] = '\0';
				//strncpy(convertBuf2, latitude, 7);
				//convertBuf2[7] = '\0';
				
				
				setParserState(GGA_LONGITUDE);
				skipBytes = 0; //prep for next phase of parse
				numBytes = 0;
			}
			else //store data
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found lat byte\r\n");
				#endif
				
				latitude[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(GGA_LATITUDE);
			}
		}
	
		//longitude
		else if (decodeState == GGA_LONGITUDE)
		{
			if (byte == ',' && skipBytes == 0) //discard this byte
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found long skip byte\r\n");
				#endif
				
				skipBytes = 1;
				setParserState(GGA_LONGITUDE);
			}
			else if (byte == ',') //end of this data type
			{
				longitude[8] = 0x00;
				setParserState(GGA_QUALITY);
				numBytes = 0; //prep for next phase of parse
				skipBytes = 0;
			}
			else //store data
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found long byte\r\n");
				#endif
				
				longitude[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(GGA_LONGITUDE);
			}
		}
	
		//GGA quality
		else if (decodeState == GGA_QUALITY)
		{
			if (byte == ',') //end of this data type
			{
				setParserState(GGA_SATELLITES);
				numBytes = 0; //prep for next phase of parse
			}
			else //store data
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found quality byte\r\n");
				#endif
				
				quality = byte; //maybe reset if invalid data ??
				setParserState(GGA_QUALITY);
			}
		}
	
		//number of satellites
		else if (decodeState == GGA_SATELLITES)
		{
			if (byte == ',') //end of this data type
			{
				numSatellites[numBytes] = 0x00;
				setParserState(GGA_HDOP);
				numBytes = 0; //prep for next phase of parse
			}
			else //store data
			{
				numSatellites[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(GGA_SATELLITES);
			}
		}
	
		//HDOP
		else if (decodeState == GGA_HDOP)
		{
			if (byte == ',' ) //end of this data type
			{
				hdop[numBytes] = 0x00;
				setParserState(GGA_ALTITUDE);
				numBytes = 0; //prep for next phase of parse
				skipBytes = 0;
			}
			else //store data
			{
				hdop[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(GGA_HDOP);
			}
		}
	
		//altitude
		else if (decodeState == GGA_ALTITUDE)
		{
			if (byte == ',' && skipBytes == 0) //discard this byte
			{
				skipBytes = 1;
				setParserState(GGA_ALTITUDE);
			}
			else if(byte == ',') //end of this data type
			{
				altitude[numBytes] = 0x00;
				setParserState(GGA_WGS84);
				numBytes = 0; //prep for next phase of parse
			}
			else //store data
			{
				altitude[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(GGA_ALTITUDE);
			}
		}
	
		//WGS84 Height
		else if (decodeState == GGA_WGS84)
		{
			if (byte == ',' && skipBytes == 0) //discard this byte
			{
				skipBytes = 1;
				setParserState(GGA_WGS84);
			}
			else if(byte == ',') //end of this data type
			{
				wgs84Height[numBytes] = 0x00;
				setParserState(GGA_LAST_UPDATE);
				skipBytes = 0; //prep for next phase of parse
				numBytes = 0;
			}
			else //store data
			{
				wgs84Height[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(GGA_WGS84);
			}
		}
	
		//last GGA DGPS update
		else if (decodeState == GGA_LAST_UPDATE)
		{
			if (byte == ',') //end of this data type
			{
				lastUpdated[numBytes] = 0x00;
				setParserState(GGA_STATION_ID);
				numBytes = 0; //prep for next phase of parse
			}
			else //store data - this should be blank
			{
				lastUpdated[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(GGA_LAST_UPDATE);
			}
		}
	
		//GGA DGPS station ID
		else if (decodeState == GGA_STATION_ID)
		{
			if (byte == ',' || byte == '*') //end of this data type
			{
				stationID[numBytes] = 0x00;
				setParserState(GPS_CHECKSUM);
				numBytes = 0; //prep for next phase of parse
			}
			else //store data - this should be blank
			{
				stationID[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(GGA_STATION_ID);
			}
		}
		///parses GGA transmissions END
	
		/// $GPRMC,hhmmss.ss,A,llll.ll,a,yyyyy.yy,a,x.x,x.x,ddmmyy,x.x,a*hh
		///parses RMC transmissions
		//time
		// emz: commented setter, GMC time better?
		else if(decodeState == RMC_TIME)
		{
			if (byte == ',') //end of this data type
			{
				//timestamp[numBytes] = 0x00;
				setParserState(RMC_VALIDITY);
				numBytes = 0; //prep for next phase of parse
			}
			else //store data
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found time byte\r\n");
				#endif
				
				//timestamp[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(RMC_TIME);
			}
		}
	
		//validity
		// not needed? dupe gga
		else if(decodeState == RMC_VALIDITY)
		{
			if (byte == ',') //end of this data type
			{
				setParserState(RMC_LATITUDE);
				skipBytes = 0; //prep for next phase of parse
				numBytes = 0;
			}
			else //store data
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found valid byte\r\n");
				#endif
				
				//quality = byte;
				numBytes++;
				setParserState(RMC_VALIDITY);
			}
		}
	
		//latitude RMC (we don't need this, commented out setter)
		else if(decodeState == RMC_LATITUDE)
		{
			if (byte == ',' && skipBytes == 0) //discard this byte
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found lat skip byte\r\n");
				#endif
				
				skipBytes = 1; 
				setParserState(RMC_LATITUDE);
			}
			else if (byte == ',') //end of this data type
			{
				setParserState(RMC_LONGITUDE);
				skipBytes = 0; //prep for next phase of parse
				numBytes = 0;
			}
			else //store data
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found lat byte\r\n");
				#endif
				
				//latitude[numBytes]= byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(RMC_LATITUDE);
			}
		}
	
		//longitude RMC (we don't need this, commented out setter)
		else if(decodeState == RMC_LONGITUDE)
		{
			if (byte == ',' && skipBytes == 0) //discard this byte
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found long byte\r\n");
				#endif
				
				skipBytes = 1; 
				setParserState(RMC_LONGITUDE);
			}
			else if (byte == ',') //end of this data type
			{
				setParserState(RMC_KNOTS);
				skipBytes = 0;
				numBytes = 0;
			}
			else //store data
			{
				#ifdef DEBUG_NMEA
				serial0_sendString("found long byte\r\n");
				#endif
				
				//longitude[numBytes]= byte; //adjust number of bytes to fit array
				numBytes++;
				setParserState(RMC_LONGITUDE);
			}
		}
	
		//knots
		else if(decodeState == RMC_KNOTS)
		{
			if (byte == ',') //end of this data type
			{
				knots[numBytes] = 0x00;
				setParserState(RMC_COURSE);
				numBytes = 0; //prep for next phase of parse
			}
			else //store data
			{
				setParserState(RMC_KNOTS);
				knots[numBytes]= byte; //adjust number of bytes to fit array
				numBytes++;
			}
		}
	
		//course
		else if(decodeState == RMC_COURSE)
		{
			if (byte == ',') //end of this data type
			{
				course[numBytes] = 0x00;
				setParserState(RMC_DATE);
				numBytes = 0; //prep for next phase of parse
			}
			else //store data
			{
				setParserState(RMC_COURSE);
				course[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
			}
		}
	
		//date
		else if(decodeState == RMC_DATE)
		{
			if (byte == ',') //end of this data type
			{
				// Cut it off at day of month. Also has month and year if we ever need it.
				dayofmonth[2] = 0x00;
				setParserState(RMC_MAG_VARIATION);
				skipBytes = 0; //prep for next phase of parse
				numBytes = 0;
			}
			else //store data
			{
				setParserState(RMC_DATE);
				dayofmonth[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
			{
				variation[numBytes] = 0x00;
				setParserState(GPS_CHECKSUM);
				numBytes = 0; //prep for next phase of parse
			}
			else //store data
			{
				setParserState(RMC_MAG_VARIATION);
				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
				{
				
				}
				#ifdef DEBUG_NMEA
				serial0_sendString("OMG GOT TO CHECKSUM!\r\n");
				#endif
				
				serial0_sendString((debugBuff));
				setParserState(INITIALIZE);
				numBytes = 0; //prep for next phase of parse
			}
			else //store data
			{
				setParserState(GPS_CHECKSUM);
				checksum[numBytes] = byte; //adjust number of bytes to fit array
				numBytes++;
			}
		}
		else {
			setParserState(INITIALIZE);
		}
	
		if (decodeState!=GPS_CHECKSUM && decodeState!=INITIALIZE)	//want bytes between '$' and '*'
		{
			//input byte into running checksum
			XORbyteWithChecksum(byte);
		}
		led_off(LED_ACTIVITY);
	}	
	

}
/// MKa GPS transmission parser END

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