GPS
Fork of GPS by
GPS.cpp@0:697a7215cc10, 2012-10-06 (annotated)
- Committer:
- SamClarke
- Date:
- Sat Oct 06 22:41:59 2012 +0000
- Revision:
- 0:697a7215cc10
- Child:
- 1:0a034c2dbea6
Basic version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SamClarke | 0:697a7215cc10 | 1 | #include "GPS.h" |
SamClarke | 0:697a7215cc10 | 2 | |
SamClarke | 0:697a7215cc10 | 3 | GPS::GPS(PinName tx, PinName rx) : _UltimateGps(tx, rx) |
SamClarke | 0:697a7215cc10 | 4 | { |
SamClarke | 0:697a7215cc10 | 5 | _UltimateGps.baud(9600); |
SamClarke | 0:697a7215cc10 | 6 | } |
SamClarke | 0:697a7215cc10 | 7 | |
SamClarke | 0:697a7215cc10 | 8 | int GPS::parseData() |
SamClarke | 0:697a7215cc10 | 9 | { |
SamClarke | 0:697a7215cc10 | 10 | |
SamClarke | 0:697a7215cc10 | 11 | while(1) { |
SamClarke | 0:697a7215cc10 | 12 | getData(); |
SamClarke | 0:697a7215cc10 | 13 | if(sscanf(NEMA, "GPGGA, %*f, %*f, %*c, %*f, %*c, %d, %d, %*f, %f", &fixtype, &satellites, &altitude) >=1); |
SamClarke | 0:697a7215cc10 | 14 | if(sscanf(NEMA, "GPRMC, %f, %c, %f, %c, %f, %c, %f, %f, %d", &time, &validity, &latitude, &ns, &longitude, &ew, &speed, &heading, &date) >=1) { |
SamClarke | 0:697a7215cc10 | 15 | if(fixtype == '0') { |
SamClarke | 0:697a7215cc10 | 16 | return 0; |
SamClarke | 0:697a7215cc10 | 17 | } |
SamClarke | 0:697a7215cc10 | 18 | if(fixtype == '1') { |
SamClarke | 0:697a7215cc10 | 19 | fixname = "Positive"; |
SamClarke | 0:697a7215cc10 | 20 | } |
SamClarke | 0:697a7215cc10 | 21 | if(fixtype == '2') { |
SamClarke | 0:697a7215cc10 | 22 | fixname = "Differential"; |
SamClarke | 0:697a7215cc10 | 23 | } |
SamClarke | 0:697a7215cc10 | 24 | if(ns =='S') { |
SamClarke | 0:697a7215cc10 | 25 | latitude *= -1.0; |
SamClarke | 0:697a7215cc10 | 26 | } |
SamClarke | 0:697a7215cc10 | 27 | if(ew =='W') { |
SamClarke | 0:697a7215cc10 | 28 | longitude *= -1.0; |
SamClarke | 0:697a7215cc10 | 29 | } |
SamClarke | 0:697a7215cc10 | 30 | float degrees = trunc(latitude / 100.0f); |
SamClarke | 0:697a7215cc10 | 31 | float minutes = latitude - (degrees * 100.0f); |
SamClarke | 0:697a7215cc10 | 32 | latitude = degrees + minutes / 60.0f; |
SamClarke | 0:697a7215cc10 | 33 | degrees = trunc(longitude / 100.0f); |
SamClarke | 0:697a7215cc10 | 34 | minutes = longitude - (degrees * 100.0f); |
SamClarke | 0:697a7215cc10 | 35 | longitude = degrees + minutes / 60.0f; |
SamClarke | 0:697a7215cc10 | 36 | return 1; |
SamClarke | 0:697a7215cc10 | 37 | } |
SamClarke | 0:697a7215cc10 | 38 | } |
SamClarke | 0:697a7215cc10 | 39 | } |
SamClarke | 0:697a7215cc10 | 40 | |
SamClarke | 0:697a7215cc10 | 41 | |
SamClarke | 0:697a7215cc10 | 42 | float GPS::trunc(float v) |
SamClarke | 0:697a7215cc10 | 43 | { |
SamClarke | 0:697a7215cc10 | 44 | if(v < 0.0) { |
SamClarke | 0:697a7215cc10 | 45 | v*= -1.0; |
SamClarke | 0:697a7215cc10 | 46 | v = floor(v); |
SamClarke | 0:697a7215cc10 | 47 | v*=-1.0; |
SamClarke | 0:697a7215cc10 | 48 | } else { |
SamClarke | 0:697a7215cc10 | 49 | v = floor(v); |
SamClarke | 0:697a7215cc10 | 50 | } |
SamClarke | 0:697a7215cc10 | 51 | return v; |
SamClarke | 0:697a7215cc10 | 52 | } |
SamClarke | 0:697a7215cc10 | 53 | |
SamClarke | 0:697a7215cc10 | 54 | void GPS::getData() |
SamClarke | 0:697a7215cc10 | 55 | { |
SamClarke | 0:697a7215cc10 | 56 | while(_UltimateGps.getc() != '$'); |
SamClarke | 0:697a7215cc10 | 57 | for(int i=0; i<256; i++) { |
SamClarke | 0:697a7215cc10 | 58 | NEMA[i] = _UltimateGps.getc(); |
SamClarke | 0:697a7215cc10 | 59 | if(NEMA[i] == '\r') { |
SamClarke | 0:697a7215cc10 | 60 | NEMA[i] = 0; |
SamClarke | 0:697a7215cc10 | 61 | return; |
SamClarke | 0:697a7215cc10 | 62 | } |
SamClarke | 0:697a7215cc10 | 63 | } |
SamClarke | 0:697a7215cc10 | 64 | error("overflowed message limit"); |
SamClarke | 0:697a7215cc10 | 65 | } |
SamClarke | 0:697a7215cc10 | 66 | |
SamClarke | 0:697a7215cc10 | 67 | void GPS::Init() |
SamClarke | 0:697a7215cc10 | 68 | { |
SamClarke | 0:697a7215cc10 | 69 | _UltimateGps.printf("$PMTK220,1000*1F\r\n"); |
SamClarke | 0:697a7215cc10 | 70 | wait(1); |
SamClarke | 0:697a7215cc10 | 71 | _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 | 0:697a7215cc10 | 72 | wait(1); |
SamClarke | 0:697a7215cc10 | 73 | } |