GPS

Fork of GPS by Sam Clarke

Committer:
SamClarke
Date:
Sat Oct 13 04:27:18 2012 +0000
Revision:
2:dcc14e81f8be
Parent:
1:0a034c2dbea6
Child:
3:5cb504ce2793
Format for time from hhmmss.msms to hh:mm:ss.msms; gps.hours; gps.minutes; gps.seconds;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SamClarke 1:0a034c2dbea6 1 #include "GPS.h"
SamClarke 1:0a034c2dbea6 2
SamClarke 1:0a034c2dbea6 3 GPS::GPS(PinName tx, PinName rx) : _UltimateGps(tx, rx)
SamClarke 1:0a034c2dbea6 4 {
SamClarke 1:0a034c2dbea6 5 _UltimateGps.baud(9600);
SamClarke 1:0a034c2dbea6 6 }
SamClarke 1:0a034c2dbea6 7
SamClarke 1:0a034c2dbea6 8 int GPS::parseData()
SamClarke 1:0a034c2dbea6 9 {
SamClarke 1:0a034c2dbea6 10
SamClarke 1:0a034c2dbea6 11 while(1) {
SamClarke 1:0a034c2dbea6 12 getData();
SamClarke 1:0a034c2dbea6 13 if(sscanf(NEMA, "GPGGA, %*f, %*f, %*c, %*f, %*c, %d, %d, %*f, %f", &fixtype, &satellites, &altitude) >=1);
SamClarke 2:dcc14e81f8be 14 if(sscanf(NEMA, "GPRMC, %2d%2d%f, %c, %f, %c, %f, %c, %f, %f, %d", &hours, &minutes, &seconds, &validity, &latitude, &ns, &longitude, &ew, &speed, &heading, &date) >=1) {
SamClarke 1:0a034c2dbea6 15 if(fixtype == 0) {
SamClarke 1:0a034c2dbea6 16 return 0;
SamClarke 1:0a034c2dbea6 17 }
SamClarke 1:0a034c2dbea6 18 if(ns =='S') {
SamClarke 1:0a034c2dbea6 19 latitude *= -1.0;
SamClarke 1:0a034c2dbea6 20 }
SamClarke 1:0a034c2dbea6 21 if(ew =='W') {
SamClarke 1:0a034c2dbea6 22 longitude *= -1.0;
SamClarke 1:0a034c2dbea6 23 }
SamClarke 1:0a034c2dbea6 24 float degrees = trunc(latitude / 100.0f);
SamClarke 1:0a034c2dbea6 25 float minutes = latitude - (degrees * 100.0f);
SamClarke 1:0a034c2dbea6 26 latitude = degrees + minutes / 60.0f;
SamClarke 1:0a034c2dbea6 27 degrees = trunc(longitude / 100.0f);
SamClarke 1:0a034c2dbea6 28 minutes = longitude - (degrees * 100.0f);
SamClarke 1:0a034c2dbea6 29 longitude = degrees + minutes / 60.0f;
SamClarke 1:0a034c2dbea6 30 return 1;
SamClarke 1:0a034c2dbea6 31 }
SamClarke 1:0a034c2dbea6 32 }
SamClarke 1:0a034c2dbea6 33 }
SamClarke 1:0a034c2dbea6 34
SamClarke 1:0a034c2dbea6 35
SamClarke 1:0a034c2dbea6 36 float GPS::trunc(float v)
SamClarke 1:0a034c2dbea6 37 {
SamClarke 1:0a034c2dbea6 38 if(v < 0.0) {
SamClarke 1:0a034c2dbea6 39 v*= -1.0;
SamClarke 1:0a034c2dbea6 40 v = floor(v);
SamClarke 1:0a034c2dbea6 41 v*=-1.0;
SamClarke 1:0a034c2dbea6 42 } else {
SamClarke 1:0a034c2dbea6 43 v = floor(v);
SamClarke 1:0a034c2dbea6 44 }
SamClarke 1:0a034c2dbea6 45 return v;
SamClarke 1:0a034c2dbea6 46 }
SamClarke 1:0a034c2dbea6 47
SamClarke 1:0a034c2dbea6 48 void GPS::getData()
SamClarke 1:0a034c2dbea6 49 {
SamClarke 1:0a034c2dbea6 50 while(_UltimateGps.getc() != '$');
SamClarke 1:0a034c2dbea6 51 for(int i=0; i<256; i++) {
SamClarke 1:0a034c2dbea6 52 NEMA[i] = _UltimateGps.getc();
SamClarke 1:0a034c2dbea6 53 if(NEMA[i] == '\r') {
SamClarke 1:0a034c2dbea6 54 NEMA[i] = 0;
SamClarke 1:0a034c2dbea6 55 return;
SamClarke 1:0a034c2dbea6 56 }
SamClarke 1:0a034c2dbea6 57 }
SamClarke 1:0a034c2dbea6 58 error("overflowed message limit");
SamClarke 1:0a034c2dbea6 59 }
SamClarke 1:0a034c2dbea6 60
SamClarke 1:0a034c2dbea6 61 void GPS::Init()
SamClarke 1:0a034c2dbea6 62 {
SamClarke 2:dcc14e81f8be 63 _UltimateGps.printf("$PMTK220,200*2C\r\n");
SamClarke 1:0a034c2dbea6 64 wait(1);
SamClarke 1:0a034c2dbea6 65 _UltimateGps.printf("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n");
SamClarke 1:0a034c2dbea6 66 wait(1);
SamClarke 1:0a034c2dbea6 67 }