GPS

Fork of GPS by Sam Clarke

Committer:
Spilly
Date:
Wed Feb 18 01:46:46 2015 +0000
Revision:
6:94e2037a8bc8
Parent:
5:96d5736d9613
Child:
7:1440e1e56a15
Changed #.00 to #.0f to make compiler happy.  ex: 100.00 to 100.0f; Compiler was complaining about floats being implicitly converted to doubles.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SamClarke 1:0a034c2dbea6 1 #include "GPS.h"
SamClarke 1:0a034c2dbea6 2 GPS::GPS(PinName tx, PinName rx) : _UltimateGps(tx, rx)
SamClarke 1:0a034c2dbea6 3 {
SamClarke 1:0a034c2dbea6 4 _UltimateGps.baud(9600);
SamClarke 1:0a034c2dbea6 5 }
SamClarke 1:0a034c2dbea6 6
SamClarke 1:0a034c2dbea6 7 int GPS::parseData()
SamClarke 1:0a034c2dbea6 8 {
SamClarke 1:0a034c2dbea6 9 while(1) {
SamClarke 1:0a034c2dbea6 10 getData();
SamClarke 1:0a034c2dbea6 11 if(sscanf(NEMA, "GPGGA, %*f, %*f, %*c, %*f, %*c, %d, %d, %*f, %f", &fixtype, &satellites, &altitude) >=1);
SamClarke 4:9ac674d05370 12 if(sscanf(NEMA, "GPRMC, %2d%2d%f, %c, %f, %c, %f, %c, %f, %f, %2d%2d%2d", &hours, &minutes, &seconds, &validity, &latitude, &ns, &longitude, &ew, &speed, &heading, &day, &month, &year) >=1) {
SamClarke 1:0a034c2dbea6 13 if(fixtype == 0) {
SamClarke 1:0a034c2dbea6 14 return 0;
SamClarke 1:0a034c2dbea6 15 }
SamClarke 4:9ac674d05370 16 year += 2000;
SamClarke 1:0a034c2dbea6 17 if(ns =='S') {
SamClarke 1:0a034c2dbea6 18 latitude *= -1.0;
SamClarke 1:0a034c2dbea6 19 }
SamClarke 1:0a034c2dbea6 20 if(ew =='W') {
SamClarke 1:0a034c2dbea6 21 longitude *= -1.0;
SamClarke 1:0a034c2dbea6 22 }
SamClarke 1:0a034c2dbea6 23 float degrees = trunc(latitude / 100.0f);
SamClarke 1:0a034c2dbea6 24 float minutes = latitude - (degrees * 100.0f);
SamClarke 1:0a034c2dbea6 25 latitude = degrees + minutes / 60.0f;
SamClarke 1:0a034c2dbea6 26 degrees = trunc(longitude / 100.0f);
SamClarke 1:0a034c2dbea6 27 minutes = longitude - (degrees * 100.0f);
SamClarke 1:0a034c2dbea6 28 longitude = degrees + minutes / 60.0f;
SamClarke 3:5cb504ce2793 29 if(fixtype == 1) {
SamClarke 3:5cb504ce2793 30 fix = "Positive";
SamClarke 3:5cb504ce2793 31 }
SamClarke 3:5cb504ce2793 32 if(fixtype == 2) {
SamClarke 3:5cb504ce2793 33 fix = "Differential";
SamClarke 3:5cb504ce2793 34 }
Spilly 6:94e2037a8bc8 35 if(heading > 0.0f && heading < 45.0f) {
SamClarke 3:5cb504ce2793 36 cardinal = "NNE";
SamClarke 3:5cb504ce2793 37 }
Spilly 6:94e2037a8bc8 38 if(heading == 45.0f) {
SamClarke 3:5cb504ce2793 39 cardinal = "NE";
SamClarke 3:5cb504ce2793 40 }
Spilly 6:94e2037a8bc8 41 if(heading > 45.0f && heading < 90.0f) {
SamClarke 3:5cb504ce2793 42 cardinal = "ENE";
SamClarke 3:5cb504ce2793 43 }
Spilly 6:94e2037a8bc8 44 if(heading == 90.0f) {
SamClarke 3:5cb504ce2793 45 cardinal = "E";
SamClarke 3:5cb504ce2793 46 }
Spilly 6:94e2037a8bc8 47 if(heading > 90.0f && heading < 135.0f) {
SamClarke 3:5cb504ce2793 48 cardinal = "ESE";
SamClarke 3:5cb504ce2793 49 }
Spilly 6:94e2037a8bc8 50 if(heading == 135.0f) {
SamClarke 3:5cb504ce2793 51 cardinal = "SE";
SamClarke 3:5cb504ce2793 52 }
Spilly 6:94e2037a8bc8 53 if(heading > 135.0f && heading < 180.0f) {
SamClarke 3:5cb504ce2793 54 cardinal = "SSE";
SamClarke 3:5cb504ce2793 55 }
Spilly 6:94e2037a8bc8 56 if(heading == 180.0f) {
SamClarke 3:5cb504ce2793 57 cardinal = "S";
SamClarke 3:5cb504ce2793 58 }
Spilly 6:94e2037a8bc8 59 if(heading > 180.0f && heading < 225.0f) {
SamClarke 3:5cb504ce2793 60 cardinal = "SSW";
SamClarke 3:5cb504ce2793 61 }
Spilly 6:94e2037a8bc8 62 if(heading == 225.0f) {
SamClarke 3:5cb504ce2793 63 cardinal = "SW";
SamClarke 3:5cb504ce2793 64 }
Spilly 6:94e2037a8bc8 65 if(heading > 225.0f && heading < 270.0f) {
SamClarke 3:5cb504ce2793 66 cardinal = "WSW";
SamClarke 3:5cb504ce2793 67 }
Spilly 6:94e2037a8bc8 68 if(heading == 270.0f) {
SamClarke 3:5cb504ce2793 69 cardinal = "W";
SamClarke 3:5cb504ce2793 70 }
Spilly 6:94e2037a8bc8 71 if(heading > 270.0f && heading < 315.0f) {
SamClarke 3:5cb504ce2793 72 cardinal = "WNW";
SamClarke 3:5cb504ce2793 73 }
Spilly 6:94e2037a8bc8 74 if(heading == 315.0f) {
SamClarke 3:5cb504ce2793 75 cardinal = "NW";
SamClarke 3:5cb504ce2793 76 }
Spilly 6:94e2037a8bc8 77 if(heading > 315.0f && heading < 360.0f) {
SamClarke 3:5cb504ce2793 78 cardinal = "NNW";
SamClarke 3:5cb504ce2793 79 }
Spilly 6:94e2037a8bc8 80 if(heading == 360.0f || heading == 0.0f) {
SamClarke 3:5cb504ce2793 81 cardinal = "N";
SamClarke 3:5cb504ce2793 82 }
Spilly 6:94e2037a8bc8 83 kph = speed*1.852f;
SamClarke 1:0a034c2dbea6 84 return 1;
SamClarke 1:0a034c2dbea6 85 }
SamClarke 1:0a034c2dbea6 86 }
SamClarke 1:0a034c2dbea6 87 }
SamClarke 1:0a034c2dbea6 88
SamClarke 1:0a034c2dbea6 89
SamClarke 1:0a034c2dbea6 90 float GPS::trunc(float v)
SamClarke 1:0a034c2dbea6 91 {
Spilly 6:94e2037a8bc8 92 if(v < 0.0f) {
Spilly 6:94e2037a8bc8 93 v*= -1.0f;
SamClarke 1:0a034c2dbea6 94 v = floor(v);
Spilly 6:94e2037a8bc8 95 v*=-1.0f;
SamClarke 1:0a034c2dbea6 96 } else {
SamClarke 1:0a034c2dbea6 97 v = floor(v);
SamClarke 1:0a034c2dbea6 98 }
SamClarke 1:0a034c2dbea6 99 return v;
SamClarke 1:0a034c2dbea6 100 }
SamClarke 1:0a034c2dbea6 101
SamClarke 1:0a034c2dbea6 102 void GPS::getData()
SamClarke 1:0a034c2dbea6 103 {
SamClarke 1:0a034c2dbea6 104 while(_UltimateGps.getc() != '$');
SamClarke 1:0a034c2dbea6 105 for(int i=0; i<256; i++) {
SamClarke 1:0a034c2dbea6 106 NEMA[i] = _UltimateGps.getc();
SamClarke 1:0a034c2dbea6 107 if(NEMA[i] == '\r') {
SamClarke 1:0a034c2dbea6 108 NEMA[i] = 0;
SamClarke 1:0a034c2dbea6 109 return;
SamClarke 1:0a034c2dbea6 110 }
SamClarke 1:0a034c2dbea6 111 }
SamClarke 1:0a034c2dbea6 112 error("overflowed message limit");
SamClarke 1:0a034c2dbea6 113 }
SamClarke 1:0a034c2dbea6 114
SamClarke 1:0a034c2dbea6 115 void GPS::Init()
SamClarke 1:0a034c2dbea6 116 {
SamClarke 5:96d5736d9613 117 wait(1);
SamClarke 2:dcc14e81f8be 118 _UltimateGps.printf("$PMTK220,200*2C\r\n");
SamClarke 5:96d5736d9613 119 wait(0.2);
SamClarke 1:0a034c2dbea6 120 _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 121 wait(1);
SamClarke 1:0a034c2dbea6 122 }