A basic library for the Adafruit Ultimate GPS module. (MTK3339) http://www.adafruit.com/products/746

Fork of GPS by Sam Clarke

Committer:
GlessConsult
Date:
Tue Mar 15 16:58:07 2016 +0000
Revision:
6:bcbfe060c93e
Parent:
5:96d5736d9613
Ad a new function to save GPS data in a JSON structure.

Who changed what in which revision?

UserRevisionLine numberNew 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
GlessConsult 6:bcbfe060c93e 89 int GPS::structJSON(char * content) {
GlessConsult 6:bcbfe060c93e 90
GlessConsult 6:bcbfe060c93e 91
GlessConsult 6:bcbfe060c93e 92 char contentTemplate[] = "{ \
GlessConsult 6:bcbfe060c93e 93 \"datetime\": \"%04d-%02d-%02dT%02d:%02d:%02.3fZ\",\
GlessConsult 6:bcbfe060c93e 94 \"coordinates\": {\
GlessConsult 6:bcbfe060c93e 95 \"lat\": %f,\
GlessConsult 6:bcbfe060c93e 96 \"lng\": %f},\
GlessConsult 6:bcbfe060c93e 97 \"utc_time\": \"%04d-%02d-%02dT%02d:%02d:%02.3fZ\",\
GlessConsult 6:bcbfe060c93e 98 \"satellites_used\": %d,\
GlessConsult 6:bcbfe060c93e 99 \"gps_quality_indicator\": %d,\
GlessConsult 6:bcbfe060c93e 100 \"altitude\": %d}\0";
GlessConsult 6:bcbfe060c93e 101
GlessConsult 6:bcbfe060c93e 102 if (0 == parseData()) {
GlessConsult 6:bcbfe060c93e 103 return 0;
GlessConsult 6:bcbfe060c93e 104 }
GlessConsult 6:bcbfe060c93e 105 sprintf(content, contentTemplate, year, month, day, hours, minutes, seconds, latitude, longitude, year, month, day, hours, minutes, seconds, satellites, fixtype, (int)altitude);
GlessConsult 6:bcbfe060c93e 106 return 1;
GlessConsult 6:bcbfe060c93e 107 }
SamClarke 1:0a034c2dbea6 108
SamClarke 1:0a034c2dbea6 109 float GPS::trunc(float v)
SamClarke 1:0a034c2dbea6 110 {
SamClarke 1:0a034c2dbea6 111 if(v < 0.0) {
SamClarke 1:0a034c2dbea6 112 v*= -1.0;
SamClarke 1:0a034c2dbea6 113 v = floor(v);
SamClarke 1:0a034c2dbea6 114 v*=-1.0;
SamClarke 1:0a034c2dbea6 115 } else {
SamClarke 1:0a034c2dbea6 116 v = floor(v);
SamClarke 1:0a034c2dbea6 117 }
SamClarke 1:0a034c2dbea6 118 return v;
SamClarke 1:0a034c2dbea6 119 }
SamClarke 1:0a034c2dbea6 120
SamClarke 1:0a034c2dbea6 121 void GPS::getData()
SamClarke 1:0a034c2dbea6 122 {
SamClarke 1:0a034c2dbea6 123 while(_UltimateGps.getc() != '$');
SamClarke 1:0a034c2dbea6 124 for(int i=0; i<256; i++) {
SamClarke 1:0a034c2dbea6 125 NEMA[i] = _UltimateGps.getc();
SamClarke 1:0a034c2dbea6 126 if(NEMA[i] == '\r') {
SamClarke 1:0a034c2dbea6 127 NEMA[i] = 0;
SamClarke 1:0a034c2dbea6 128 return;
SamClarke 1:0a034c2dbea6 129 }
SamClarke 1:0a034c2dbea6 130 }
SamClarke 1:0a034c2dbea6 131 error("overflowed message limit");
SamClarke 1:0a034c2dbea6 132 }
SamClarke 1:0a034c2dbea6 133
SamClarke 1:0a034c2dbea6 134 void GPS::Init()
SamClarke 1:0a034c2dbea6 135 {
SamClarke 5:96d5736d9613 136 wait(1);
SamClarke 2:dcc14e81f8be 137 _UltimateGps.printf("$PMTK220,200*2C\r\n");
SamClarke 5:96d5736d9613 138 wait(0.2);
SamClarke 1:0a034c2dbea6 139 _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 140 wait(1);
SamClarke 1:0a034c2dbea6 141 }