A basic library for the Adafruit Ultimate GPS module. (MTK3339) http://www.adafruit.com/products/746
Fork of GPS by
GPS.cpp@1:0a034c2dbea6, 2012-10-09 (annotated)
- Committer:
- SamClarke
- Date:
- Tue Oct 09 18:47:58 2012 +0000
- Revision:
- 1:0a034c2dbea6
- Parent:
- 0:697a7215cc10
- Child:
- 2:dcc14e81f8be
cleaned up :)
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 | 1:0a034c2dbea6 | 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 | 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 | 1:0a034c2dbea6 | 30 | return 1; |
SamClarke | 1:0a034c2dbea6 | 31 | } |
SamClarke | 1:0a034c2dbea6 | 32 | } |
SamClarke | 1:0a034c2dbea6 | 33 | } |
SamClarke | 1:0a034c2dbea6 | 34 | |
SamClarke | 1:0a034c2dbea6 | 35 | |
SamClarke | 1:0a034c2dbea6 | 36 | float GPS::trunc(float v) |
SamClarke | 1:0a034c2dbea6 | 37 | { |
SamClarke | 1:0a034c2dbea6 | 38 | if(v < 0.0) { |
SamClarke | 1:0a034c2dbea6 | 39 | v*= -1.0; |
SamClarke | 1:0a034c2dbea6 | 40 | v = floor(v); |
SamClarke | 1:0a034c2dbea6 | 41 | v*=-1.0; |
SamClarke | 1:0a034c2dbea6 | 42 | } else { |
SamClarke | 1:0a034c2dbea6 | 43 | v = floor(v); |
SamClarke | 1:0a034c2dbea6 | 44 | } |
SamClarke | 1:0a034c2dbea6 | 45 | return v; |
SamClarke | 1:0a034c2dbea6 | 46 | } |
SamClarke | 1:0a034c2dbea6 | 47 | |
SamClarke | 1:0a034c2dbea6 | 48 | void GPS::getData() |
SamClarke | 1:0a034c2dbea6 | 49 | { |
SamClarke | 1:0a034c2dbea6 | 50 | while(_UltimateGps.getc() != '$'); |
SamClarke | 1:0a034c2dbea6 | 51 | for(int i=0; i<256; i++) { |
SamClarke | 1:0a034c2dbea6 | 52 | NEMA[i] = _UltimateGps.getc(); |
SamClarke | 1:0a034c2dbea6 | 53 | if(NEMA[i] == '\r') { |
SamClarke | 1:0a034c2dbea6 | 54 | NEMA[i] = 0; |
SamClarke | 1:0a034c2dbea6 | 55 | return; |
SamClarke | 1:0a034c2dbea6 | 56 | } |
SamClarke | 1:0a034c2dbea6 | 57 | } |
SamClarke | 1:0a034c2dbea6 | 58 | error("overflowed message limit"); |
SamClarke | 1:0a034c2dbea6 | 59 | } |
SamClarke | 1:0a034c2dbea6 | 60 | |
SamClarke | 1:0a034c2dbea6 | 61 | void GPS::Init() |
SamClarke | 1:0a034c2dbea6 | 62 | { |
SamClarke | 1:0a034c2dbea6 | 63 | _UltimateGps.printf("$PMTK220,1000*1F\r\n"); |
SamClarke | 1:0a034c2dbea6 | 64 | wait(1); |
SamClarke | 1:0a034c2dbea6 | 65 | _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 | 66 | wait(1); |
SamClarke | 1:0a034c2dbea6 | 67 | } |