GPS
Fork of GPS by
GPS.cpp@4:9ac674d05370, 2014-01-19 (annotated)
- Committer:
- SamClarke
- Date:
- Sun Jan 19 07:44:51 2014 +0000
- Revision:
- 4:9ac674d05370
- Parent:
- 3:5cb504ce2793
- Child:
- 5:96d5736d9613
Further breakdown of date into usable variables. This has made is suitable for my satellite tracking project.
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 | 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 | } |
SamClarke | 3:5cb504ce2793 | 35 | if(heading > 0.00 && heading < 45.00) { |
SamClarke | 3:5cb504ce2793 | 36 | cardinal = "NNE"; |
SamClarke | 3:5cb504ce2793 | 37 | } |
SamClarke | 3:5cb504ce2793 | 38 | if(heading == 45.00) { |
SamClarke | 3:5cb504ce2793 | 39 | cardinal = "NE"; |
SamClarke | 3:5cb504ce2793 | 40 | } |
SamClarke | 3:5cb504ce2793 | 41 | if(heading > 45.00 && heading < 90.00) { |
SamClarke | 3:5cb504ce2793 | 42 | cardinal = "ENE"; |
SamClarke | 3:5cb504ce2793 | 43 | } |
SamClarke | 3:5cb504ce2793 | 44 | if(heading == 90.00) { |
SamClarke | 3:5cb504ce2793 | 45 | cardinal = "E"; |
SamClarke | 3:5cb504ce2793 | 46 | } |
SamClarke | 3:5cb504ce2793 | 47 | if(heading > 90.00 && heading < 135.00) { |
SamClarke | 3:5cb504ce2793 | 48 | cardinal = "ESE"; |
SamClarke | 3:5cb504ce2793 | 49 | } |
SamClarke | 3:5cb504ce2793 | 50 | if(heading == 135.00) { |
SamClarke | 3:5cb504ce2793 | 51 | cardinal = "SE"; |
SamClarke | 3:5cb504ce2793 | 52 | } |
SamClarke | 3:5cb504ce2793 | 53 | if(heading > 135.00 && heading < 180.00) { |
SamClarke | 3:5cb504ce2793 | 54 | cardinal = "SSE"; |
SamClarke | 3:5cb504ce2793 | 55 | } |
SamClarke | 3:5cb504ce2793 | 56 | if(heading == 180.00) { |
SamClarke | 3:5cb504ce2793 | 57 | cardinal = "S"; |
SamClarke | 3:5cb504ce2793 | 58 | } |
SamClarke | 3:5cb504ce2793 | 59 | if(heading > 180.00 && heading < 225.00) { |
SamClarke | 3:5cb504ce2793 | 60 | cardinal = "SSW"; |
SamClarke | 3:5cb504ce2793 | 61 | } |
SamClarke | 3:5cb504ce2793 | 62 | if(heading == 225.00) { |
SamClarke | 3:5cb504ce2793 | 63 | cardinal = "SW"; |
SamClarke | 3:5cb504ce2793 | 64 | } |
SamClarke | 3:5cb504ce2793 | 65 | if(heading > 225.00 && heading < 270.00) { |
SamClarke | 3:5cb504ce2793 | 66 | cardinal = "WSW"; |
SamClarke | 3:5cb504ce2793 | 67 | } |
SamClarke | 3:5cb504ce2793 | 68 | if(heading == 270.00) { |
SamClarke | 3:5cb504ce2793 | 69 | cardinal = "W"; |
SamClarke | 3:5cb504ce2793 | 70 | } |
SamClarke | 3:5cb504ce2793 | 71 | if(heading > 270.00 && heading < 315.00) { |
SamClarke | 3:5cb504ce2793 | 72 | cardinal = "WNW"; |
SamClarke | 3:5cb504ce2793 | 73 | } |
SamClarke | 3:5cb504ce2793 | 74 | if(heading == 315.00) { |
SamClarke | 3:5cb504ce2793 | 75 | cardinal = "NW"; |
SamClarke | 3:5cb504ce2793 | 76 | } |
SamClarke | 3:5cb504ce2793 | 77 | if(heading > 315.00 && heading < 360.00) { |
SamClarke | 3:5cb504ce2793 | 78 | cardinal = "NNW"; |
SamClarke | 3:5cb504ce2793 | 79 | } |
SamClarke | 3:5cb504ce2793 | 80 | if(heading == 360.00 || heading == 0.00) { |
SamClarke | 3:5cb504ce2793 | 81 | cardinal = "N"; |
SamClarke | 3:5cb504ce2793 | 82 | } |
SamClarke | 3:5cb504ce2793 | 83 | kph = speed*1.852; |
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 | { |
SamClarke | 1:0a034c2dbea6 | 92 | if(v < 0.0) { |
SamClarke | 1:0a034c2dbea6 | 93 | v*= -1.0; |
SamClarke | 1:0a034c2dbea6 | 94 | v = floor(v); |
SamClarke | 1:0a034c2dbea6 | 95 | v*=-1.0; |
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 | 2:dcc14e81f8be | 117 | _UltimateGps.printf("$PMTK220,200*2C\r\n"); |
SamClarke | 1:0a034c2dbea6 | 118 | wait(1); |
SamClarke | 1:0a034c2dbea6 | 119 | _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 | 120 | wait(1); |
SamClarke | 1:0a034c2dbea6 | 121 | } |