gps
Revision 0:e239db2b266d, committed 2018-03-10
- Comitter:
- Noah_Newsom
- Date:
- Sat Mar 10 12:15:43 2018 +0000
- Commit message:
- tracker
Changed in this revision
GPS.cpp | Show annotated file Show diff for this revision Revisions of this file |
GPS.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r e239db2b266d GPS.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GPS.cpp Sat Mar 10 12:15:43 2018 +0000 @@ -0,0 +1,122 @@ +#include "GPS.h" +GPS::GPS(PinName tx, PinName rx) : _UltimateGps(tx, rx) +{ + _UltimateGps.baud(4800); +} + +int GPS::parseData() +{ + while(1) { + getData(); + if(sscanf(NEMA, "GPGGA, %*f, %*f, %*c, %*f, %*c, %d, %d, %*f, %f", &fixtype, &satellites, &altitude) >=1); + 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) { + if(fixtype == 0) { + return 0; + } + year += 2000; + if(ns =='S') { + latitude *= -1.0; + } + if(ew =='W') { + longitude *= -1.0; + } + float degrees = trunc(latitude / 100.0f); + float minutes = latitude - (degrees * 100.0f); + latitude = degrees + minutes / 60.0f; + degrees = trunc(longitude / 100.0f); + minutes = longitude - (degrees * 100.0f); + longitude = degrees + minutes / 60.0f; + if(fixtype == 1) { + fix = "Positive"; + } + if(fixtype == 2) { + fix = "Differential"; + } + if(heading > 0.00 && heading < 45.00) { + cardinal = "NNE"; + } + if(heading == 45.00) { + cardinal = "NE"; + } + if(heading > 45.00 && heading < 90.00) { + cardinal = "ENE"; + } + if(heading == 90.00) { + cardinal = "E"; + } + if(heading > 90.00 && heading < 135.00) { + cardinal = "ESE"; + } + if(heading == 135.00) { + cardinal = "SE"; + } + if(heading > 135.00 && heading < 180.00) { + cardinal = "SSE"; + } + if(heading == 180.00) { + cardinal = "S"; + } + if(heading > 180.00 && heading < 225.00) { + cardinal = "SSW"; + } + if(heading == 225.00) { + cardinal = "SW"; + } + if(heading > 225.00 && heading < 270.00) { + cardinal = "WSW"; + } + if(heading == 270.00) { + cardinal = "W"; + } + if(heading > 270.00 && heading < 315.00) { + cardinal = "WNW"; + } + if(heading == 315.00) { + cardinal = "NW"; + } + if(heading > 315.00 && heading < 360.00) { + cardinal = "NNW"; + } + if(heading == 360.00 || heading == 0.00) { + cardinal = "N"; + } + kph = speed*1.852; + return 1; + } + } +} + + +float GPS::trunc(float v) +{ + if(v < 0.0) { + v*= -1.0; + v = floor(v); + v*=-1.0; + } else { + v = floor(v); + } + return v; +} + +void GPS::getData() +{ + while(_UltimateGps.getc() != '$'); + for(int i=0; i<256; i++) { + NEMA[i] = _UltimateGps.getc(); + if(NEMA[i] == '\r') { + NEMA[i] = 0; + return; + } + } + error("overflowed message limit"); +} + +void GPS::Init() +{ + wait(1); + _UltimateGps.printf("$PMTK220,200*2C\r\n"); + wait(0.2); + _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"); + wait(1); +} \ No newline at end of file
diff -r 000000000000 -r e239db2b266d GPS.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GPS.h Sat Mar 10 12:15:43 2018 +0000 @@ -0,0 +1,68 @@ +/****************************************************** +* A basic mbed library for the Adafruit ultimate * +* GPS module NOTE: the LOCUS functions are yet to be * +* implemented * +* * +* Written by: Sam Clarke * +******************************************************/ + +#include "mbed.h" +#include <string> + +#ifndef GPS_H +#define GPS_H + +// EXAMPLE OUTPUTS +// +// $GPRMC,064951.000,A,2307.1256,N,12016.4438,E,0.03,165.48,260406,3.05,W,A*2C +// $GPRMC, time, status, latitude, N/S, longitude, E/W, speed(knots), heading, date, N/A, N/A, MODE*CHECKSUM +// +// $GPGGA,064951.000,2307.1256,N,12016.4438,E,1,8,0.95,39.9,M,17.8,M,,*65 +// $GPGGA, time, latitude, N/S, longitude, E/W, fix, satellites, hdop, altitude, M, geoidal sep , M,,*CHECKSUM +// $GPGGA, %f, %*f, %*c, %*f, %*c, %d, %d, %*f, %*f, %*c, %*f , %*c,,%*c%*c%*c0 + +class GPS +{ +public: + + GPS(PinName tx, PinName rx); + void Init(); + int parseData(); + float time; // UTC time + int hours; + int minutes; + float seconds; + char validity,ns,ew;// RMC data status A = Data Valid; V = Data Not valid; + float latitude; // + float longitude; // + float speed; // speed in knots + float heading; // heading in degrees derived from previous & current location + string date; // + int day; + int month; + int year; + int fixtype; // 0 = no fix; 1 = fix; 2=differential fix + int satellites; // number of satellites used + float altitude; // + string fix; + string cardinal; + float kph; + +private: + + float trunc ( float v); + void getData(); + Serial _UltimateGps; + char NEMA[256]; +}; +#endif + +/* +#define 1HZ_STREAM "$PMTK220,1000*1F\r\n" // 1.0 second interval +#define 5HZ_STREAM "$PMTK220,200*2C\r\n" // 0.2 second interval +#define 10HZ_STREAM "$PMTK220,100*2F\r\n" // 0.1 second interval + +#define OUTPUT_RMC "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n" +#define OUTPUT_RMCGGA "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" +#define OUTPUT_OFF "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" +*/ \ No newline at end of file