GPS
Fork of GPS by
GPS.cpp@3:5cb504ce2793, 2012-10-21 (annotated)
- Committer:
- SamClarke
- Date:
- Sun Oct 21 09:28:41 2012 +0000
- Revision:
- 3:5cb504ce2793
- Parent:
- 2:dcc14e81f8be
- Child:
- 4:9ac674d05370
Added cardinal directions / converted gps.speed knots to gps.kph
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:5cb504ce2793 | 30 | if(fixtype == 1) { |
SamClarke | 3:5cb504ce2793 | 31 | fix = "Positive"; |
SamClarke | 3:5cb504ce2793 | 32 | } |
SamClarke | 3:5cb504ce2793 | 33 | if(fixtype == 2) { |
SamClarke | 3:5cb504ce2793 | 34 | fix = "Differential"; |
SamClarke | 3:5cb504ce2793 | 35 | } |
SamClarke | 3:5cb504ce2793 | 36 | if(heading > 0.00 && heading < 45.00) { |
SamClarke | 3:5cb504ce2793 | 37 | cardinal = "NNE"; |
SamClarke | 3:5cb504ce2793 | 38 | } |
SamClarke | 3:5cb504ce2793 | 39 | if(heading == 45.00) { |
SamClarke | 3:5cb504ce2793 | 40 | cardinal = "NE"; |
SamClarke | 3:5cb504ce2793 | 41 | } |
SamClarke | 3:5cb504ce2793 | 42 | if(heading > 45.00 && heading < 90.00) { |
SamClarke | 3:5cb504ce2793 | 43 | cardinal = "ENE"; |
SamClarke | 3:5cb504ce2793 | 44 | } |
SamClarke | 3:5cb504ce2793 | 45 | if(heading == 90.00) { |
SamClarke | 3:5cb504ce2793 | 46 | cardinal = "E"; |
SamClarke | 3:5cb504ce2793 | 47 | } |
SamClarke | 3:5cb504ce2793 | 48 | if(heading > 90.00 && heading < 135.00) { |
SamClarke | 3:5cb504ce2793 | 49 | cardinal = "ESE"; |
SamClarke | 3:5cb504ce2793 | 50 | } |
SamClarke | 3:5cb504ce2793 | 51 | if(heading == 135.00) { |
SamClarke | 3:5cb504ce2793 | 52 | cardinal = "SE"; |
SamClarke | 3:5cb504ce2793 | 53 | } |
SamClarke | 3:5cb504ce2793 | 54 | if(heading > 135.00 && heading < 180.00) { |
SamClarke | 3:5cb504ce2793 | 55 | cardinal = "SSE"; |
SamClarke | 3:5cb504ce2793 | 56 | } |
SamClarke | 3:5cb504ce2793 | 57 | if(heading == 180.00) { |
SamClarke | 3:5cb504ce2793 | 58 | cardinal = "S"; |
SamClarke | 3:5cb504ce2793 | 59 | } |
SamClarke | 3:5cb504ce2793 | 60 | if(heading > 180.00 && heading < 225.00) { |
SamClarke | 3:5cb504ce2793 | 61 | cardinal = "SSW"; |
SamClarke | 3:5cb504ce2793 | 62 | } |
SamClarke | 3:5cb504ce2793 | 63 | if(heading == 225.00) { |
SamClarke | 3:5cb504ce2793 | 64 | cardinal = "SW"; |
SamClarke | 3:5cb504ce2793 | 65 | } |
SamClarke | 3:5cb504ce2793 | 66 | if(heading > 225.00 && heading < 270.00) { |
SamClarke | 3:5cb504ce2793 | 67 | cardinal = "WSW"; |
SamClarke | 3:5cb504ce2793 | 68 | } |
SamClarke | 3:5cb504ce2793 | 69 | if(heading == 270.00) { |
SamClarke | 3:5cb504ce2793 | 70 | cardinal = "W"; |
SamClarke | 3:5cb504ce2793 | 71 | } |
SamClarke | 3:5cb504ce2793 | 72 | if(heading > 270.00 && heading < 315.00) { |
SamClarke | 3:5cb504ce2793 | 73 | cardinal = "WNW"; |
SamClarke | 3:5cb504ce2793 | 74 | } |
SamClarke | 3:5cb504ce2793 | 75 | if(heading == 315.00) { |
SamClarke | 3:5cb504ce2793 | 76 | cardinal = "NW"; |
SamClarke | 3:5cb504ce2793 | 77 | } |
SamClarke | 3:5cb504ce2793 | 78 | if(heading > 315.00 && heading < 360.00) { |
SamClarke | 3:5cb504ce2793 | 79 | cardinal = "NNW"; |
SamClarke | 3:5cb504ce2793 | 80 | } |
SamClarke | 3:5cb504ce2793 | 81 | if(heading == 360.00 || heading == 0.00) { |
SamClarke | 3:5cb504ce2793 | 82 | cardinal = "N"; |
SamClarke | 3:5cb504ce2793 | 83 | } |
SamClarke | 3:5cb504ce2793 | 84 | kph = speed*1.852; |
SamClarke | 1:0a034c2dbea6 | 85 | return 1; |
SamClarke | 1:0a034c2dbea6 | 86 | } |
SamClarke | 1:0a034c2dbea6 | 87 | } |
SamClarke | 1:0a034c2dbea6 | 88 | } |
SamClarke | 1:0a034c2dbea6 | 89 | |
SamClarke | 1:0a034c2dbea6 | 90 | |
SamClarke | 1:0a034c2dbea6 | 91 | float GPS::trunc(float v) |
SamClarke | 1:0a034c2dbea6 | 92 | { |
SamClarke | 1:0a034c2dbea6 | 93 | if(v < 0.0) { |
SamClarke | 1:0a034c2dbea6 | 94 | v*= -1.0; |
SamClarke | 1:0a034c2dbea6 | 95 | v = floor(v); |
SamClarke | 1:0a034c2dbea6 | 96 | v*=-1.0; |
SamClarke | 1:0a034c2dbea6 | 97 | } else { |
SamClarke | 1:0a034c2dbea6 | 98 | v = floor(v); |
SamClarke | 1:0a034c2dbea6 | 99 | } |
SamClarke | 1:0a034c2dbea6 | 100 | return v; |
SamClarke | 1:0a034c2dbea6 | 101 | } |
SamClarke | 1:0a034c2dbea6 | 102 | |
SamClarke | 1:0a034c2dbea6 | 103 | void GPS::getData() |
SamClarke | 1:0a034c2dbea6 | 104 | { |
SamClarke | 1:0a034c2dbea6 | 105 | while(_UltimateGps.getc() != '$'); |
SamClarke | 1:0a034c2dbea6 | 106 | for(int i=0; i<256; i++) { |
SamClarke | 1:0a034c2dbea6 | 107 | NEMA[i] = _UltimateGps.getc(); |
SamClarke | 1:0a034c2dbea6 | 108 | if(NEMA[i] == '\r') { |
SamClarke | 1:0a034c2dbea6 | 109 | NEMA[i] = 0; |
SamClarke | 1:0a034c2dbea6 | 110 | return; |
SamClarke | 1:0a034c2dbea6 | 111 | } |
SamClarke | 1:0a034c2dbea6 | 112 | } |
SamClarke | 1:0a034c2dbea6 | 113 | error("overflowed message limit"); |
SamClarke | 1:0a034c2dbea6 | 114 | } |
SamClarke | 1:0a034c2dbea6 | 115 | |
SamClarke | 1:0a034c2dbea6 | 116 | void GPS::Init() |
SamClarke | 1:0a034c2dbea6 | 117 | { |
SamClarke | 2:dcc14e81f8be | 118 | _UltimateGps.printf("$PMTK220,200*2C\r\n"); |
SamClarke | 1:0a034c2dbea6 | 119 | wait(1); |
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 | } |