gps
GPS.cpp@0:e239db2b266d, 2018-03-10 (annotated)
- Committer:
- Noah_Newsom
- Date:
- Sat Mar 10 12:15:43 2018 +0000
- Revision:
- 0:e239db2b266d
tracker
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Noah_Newsom | 0:e239db2b266d | 1 | #include "GPS.h" |
Noah_Newsom | 0:e239db2b266d | 2 | GPS::GPS(PinName tx, PinName rx) : _UltimateGps(tx, rx) |
Noah_Newsom | 0:e239db2b266d | 3 | { |
Noah_Newsom | 0:e239db2b266d | 4 | _UltimateGps.baud(4800); |
Noah_Newsom | 0:e239db2b266d | 5 | } |
Noah_Newsom | 0:e239db2b266d | 6 | |
Noah_Newsom | 0:e239db2b266d | 7 | int GPS::parseData() |
Noah_Newsom | 0:e239db2b266d | 8 | { |
Noah_Newsom | 0:e239db2b266d | 9 | while(1) { |
Noah_Newsom | 0:e239db2b266d | 10 | getData(); |
Noah_Newsom | 0:e239db2b266d | 11 | if(sscanf(NEMA, "GPGGA, %*f, %*f, %*c, %*f, %*c, %d, %d, %*f, %f", &fixtype, &satellites, &altitude) >=1); |
Noah_Newsom | 0:e239db2b266d | 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) { |
Noah_Newsom | 0:e239db2b266d | 13 | if(fixtype == 0) { |
Noah_Newsom | 0:e239db2b266d | 14 | return 0; |
Noah_Newsom | 0:e239db2b266d | 15 | } |
Noah_Newsom | 0:e239db2b266d | 16 | year += 2000; |
Noah_Newsom | 0:e239db2b266d | 17 | if(ns =='S') { |
Noah_Newsom | 0:e239db2b266d | 18 | latitude *= -1.0; |
Noah_Newsom | 0:e239db2b266d | 19 | } |
Noah_Newsom | 0:e239db2b266d | 20 | if(ew =='W') { |
Noah_Newsom | 0:e239db2b266d | 21 | longitude *= -1.0; |
Noah_Newsom | 0:e239db2b266d | 22 | } |
Noah_Newsom | 0:e239db2b266d | 23 | float degrees = trunc(latitude / 100.0f); |
Noah_Newsom | 0:e239db2b266d | 24 | float minutes = latitude - (degrees * 100.0f); |
Noah_Newsom | 0:e239db2b266d | 25 | latitude = degrees + minutes / 60.0f; |
Noah_Newsom | 0:e239db2b266d | 26 | degrees = trunc(longitude / 100.0f); |
Noah_Newsom | 0:e239db2b266d | 27 | minutes = longitude - (degrees * 100.0f); |
Noah_Newsom | 0:e239db2b266d | 28 | longitude = degrees + minutes / 60.0f; |
Noah_Newsom | 0:e239db2b266d | 29 | if(fixtype == 1) { |
Noah_Newsom | 0:e239db2b266d | 30 | fix = "Positive"; |
Noah_Newsom | 0:e239db2b266d | 31 | } |
Noah_Newsom | 0:e239db2b266d | 32 | if(fixtype == 2) { |
Noah_Newsom | 0:e239db2b266d | 33 | fix = "Differential"; |
Noah_Newsom | 0:e239db2b266d | 34 | } |
Noah_Newsom | 0:e239db2b266d | 35 | if(heading > 0.00 && heading < 45.00) { |
Noah_Newsom | 0:e239db2b266d | 36 | cardinal = "NNE"; |
Noah_Newsom | 0:e239db2b266d | 37 | } |
Noah_Newsom | 0:e239db2b266d | 38 | if(heading == 45.00) { |
Noah_Newsom | 0:e239db2b266d | 39 | cardinal = "NE"; |
Noah_Newsom | 0:e239db2b266d | 40 | } |
Noah_Newsom | 0:e239db2b266d | 41 | if(heading > 45.00 && heading < 90.00) { |
Noah_Newsom | 0:e239db2b266d | 42 | cardinal = "ENE"; |
Noah_Newsom | 0:e239db2b266d | 43 | } |
Noah_Newsom | 0:e239db2b266d | 44 | if(heading == 90.00) { |
Noah_Newsom | 0:e239db2b266d | 45 | cardinal = "E"; |
Noah_Newsom | 0:e239db2b266d | 46 | } |
Noah_Newsom | 0:e239db2b266d | 47 | if(heading > 90.00 && heading < 135.00) { |
Noah_Newsom | 0:e239db2b266d | 48 | cardinal = "ESE"; |
Noah_Newsom | 0:e239db2b266d | 49 | } |
Noah_Newsom | 0:e239db2b266d | 50 | if(heading == 135.00) { |
Noah_Newsom | 0:e239db2b266d | 51 | cardinal = "SE"; |
Noah_Newsom | 0:e239db2b266d | 52 | } |
Noah_Newsom | 0:e239db2b266d | 53 | if(heading > 135.00 && heading < 180.00) { |
Noah_Newsom | 0:e239db2b266d | 54 | cardinal = "SSE"; |
Noah_Newsom | 0:e239db2b266d | 55 | } |
Noah_Newsom | 0:e239db2b266d | 56 | if(heading == 180.00) { |
Noah_Newsom | 0:e239db2b266d | 57 | cardinal = "S"; |
Noah_Newsom | 0:e239db2b266d | 58 | } |
Noah_Newsom | 0:e239db2b266d | 59 | if(heading > 180.00 && heading < 225.00) { |
Noah_Newsom | 0:e239db2b266d | 60 | cardinal = "SSW"; |
Noah_Newsom | 0:e239db2b266d | 61 | } |
Noah_Newsom | 0:e239db2b266d | 62 | if(heading == 225.00) { |
Noah_Newsom | 0:e239db2b266d | 63 | cardinal = "SW"; |
Noah_Newsom | 0:e239db2b266d | 64 | } |
Noah_Newsom | 0:e239db2b266d | 65 | if(heading > 225.00 && heading < 270.00) { |
Noah_Newsom | 0:e239db2b266d | 66 | cardinal = "WSW"; |
Noah_Newsom | 0:e239db2b266d | 67 | } |
Noah_Newsom | 0:e239db2b266d | 68 | if(heading == 270.00) { |
Noah_Newsom | 0:e239db2b266d | 69 | cardinal = "W"; |
Noah_Newsom | 0:e239db2b266d | 70 | } |
Noah_Newsom | 0:e239db2b266d | 71 | if(heading > 270.00 && heading < 315.00) { |
Noah_Newsom | 0:e239db2b266d | 72 | cardinal = "WNW"; |
Noah_Newsom | 0:e239db2b266d | 73 | } |
Noah_Newsom | 0:e239db2b266d | 74 | if(heading == 315.00) { |
Noah_Newsom | 0:e239db2b266d | 75 | cardinal = "NW"; |
Noah_Newsom | 0:e239db2b266d | 76 | } |
Noah_Newsom | 0:e239db2b266d | 77 | if(heading > 315.00 && heading < 360.00) { |
Noah_Newsom | 0:e239db2b266d | 78 | cardinal = "NNW"; |
Noah_Newsom | 0:e239db2b266d | 79 | } |
Noah_Newsom | 0:e239db2b266d | 80 | if(heading == 360.00 || heading == 0.00) { |
Noah_Newsom | 0:e239db2b266d | 81 | cardinal = "N"; |
Noah_Newsom | 0:e239db2b266d | 82 | } |
Noah_Newsom | 0:e239db2b266d | 83 | kph = speed*1.852; |
Noah_Newsom | 0:e239db2b266d | 84 | return 1; |
Noah_Newsom | 0:e239db2b266d | 85 | } |
Noah_Newsom | 0:e239db2b266d | 86 | } |
Noah_Newsom | 0:e239db2b266d | 87 | } |
Noah_Newsom | 0:e239db2b266d | 88 | |
Noah_Newsom | 0:e239db2b266d | 89 | |
Noah_Newsom | 0:e239db2b266d | 90 | float GPS::trunc(float v) |
Noah_Newsom | 0:e239db2b266d | 91 | { |
Noah_Newsom | 0:e239db2b266d | 92 | if(v < 0.0) { |
Noah_Newsom | 0:e239db2b266d | 93 | v*= -1.0; |
Noah_Newsom | 0:e239db2b266d | 94 | v = floor(v); |
Noah_Newsom | 0:e239db2b266d | 95 | v*=-1.0; |
Noah_Newsom | 0:e239db2b266d | 96 | } else { |
Noah_Newsom | 0:e239db2b266d | 97 | v = floor(v); |
Noah_Newsom | 0:e239db2b266d | 98 | } |
Noah_Newsom | 0:e239db2b266d | 99 | return v; |
Noah_Newsom | 0:e239db2b266d | 100 | } |
Noah_Newsom | 0:e239db2b266d | 101 | |
Noah_Newsom | 0:e239db2b266d | 102 | void GPS::getData() |
Noah_Newsom | 0:e239db2b266d | 103 | { |
Noah_Newsom | 0:e239db2b266d | 104 | while(_UltimateGps.getc() != '$'); |
Noah_Newsom | 0:e239db2b266d | 105 | for(int i=0; i<256; i++) { |
Noah_Newsom | 0:e239db2b266d | 106 | NEMA[i] = _UltimateGps.getc(); |
Noah_Newsom | 0:e239db2b266d | 107 | if(NEMA[i] == '\r') { |
Noah_Newsom | 0:e239db2b266d | 108 | NEMA[i] = 0; |
Noah_Newsom | 0:e239db2b266d | 109 | return; |
Noah_Newsom | 0:e239db2b266d | 110 | } |
Noah_Newsom | 0:e239db2b266d | 111 | } |
Noah_Newsom | 0:e239db2b266d | 112 | error("overflowed message limit"); |
Noah_Newsom | 0:e239db2b266d | 113 | } |
Noah_Newsom | 0:e239db2b266d | 114 | |
Noah_Newsom | 0:e239db2b266d | 115 | void GPS::Init() |
Noah_Newsom | 0:e239db2b266d | 116 | { |
Noah_Newsom | 0:e239db2b266d | 117 | wait(1); |
Noah_Newsom | 0:e239db2b266d | 118 | _UltimateGps.printf("$PMTK220,200*2C\r\n"); |
Noah_Newsom | 0:e239db2b266d | 119 | wait(0.2); |
Noah_Newsom | 0:e239db2b266d | 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"); |
Noah_Newsom | 0:e239db2b266d | 121 | wait(1); |
Noah_Newsom | 0:e239db2b266d | 122 | } |