Updated version with new commands and sleep mode
Fork of GPS by
GPS.cpp@6:af055728e564, 2015-04-05 (annotated)
- Committer:
- Spilly
- Date:
- Sun Apr 05 01:41:34 2015 +0000
- Revision:
- 6:af055728e564
- Parent:
- 5:96d5736d9613
- Child:
- 7:7aac669b0075
Added several new initialization commands and a sleep function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Spilly | 6:af055728e564 | 1 | /************************************************************************************************************************************************************** |
Spilly | 6:af055728e564 | 2 | // This is a modified version of mbed /users/SamClarke/code/GPS/ for MTK3339 GPS module |
Spilly | 6:af055728e564 | 3 | // |
Spilly | 6:af055728e564 | 4 | // Changes made by Ryan Spillman: |
Spilly | 6:af055728e564 | 5 | // added commands to the initialization function to set baud rate, set update rate, enabled DGPS WAAS, and set minimum navigation speed |
Spilly | 6:af055728e564 | 6 | // added a function to put the unit into sleep mode |
Spilly | 6:af055728e564 | 7 | // added several zeros after the decimal places to fix weird rounding errors |
Spilly | 6:af055728e564 | 8 | // added information about checksums |
Spilly | 6:af055728e564 | 9 | // |
Spilly | 6:af055728e564 | 10 | // IMPORTANT: |
Spilly | 6:af055728e564 | 11 | // Any changes made to commands sent to GPS unit require corresponding changes to the |
Spilly | 6:af055728e564 | 12 | // checksum at the end of the command |
Spilly | 6:af055728e564 | 13 | // Here is a tool for calculating the checksum: |
Spilly | 6:af055728e564 | 14 | // http://siliconsparrow.com/demos/nmeachecksum.php |
Spilly | 6:af055728e564 | 15 | **************************************************************************************************************************************************************/ |
Spilly | 6:af055728e564 | 16 | |
SamClarke | 1:0a034c2dbea6 | 17 | #include "GPS.h" |
SamClarke | 1:0a034c2dbea6 | 18 | GPS::GPS(PinName tx, PinName rx) : _UltimateGps(tx, rx) |
SamClarke | 1:0a034c2dbea6 | 19 | { |
Spilly | 6:af055728e564 | 20 | //_UltimateGps.baud(9600); //use this for default MTK3339 adafruit firmware |
Spilly | 6:af055728e564 | 21 | _UltimateGps.baud(38400); //Changed firmware of MTK3339 to 38400 baud with update rate of 5Hz (GlobalTopFlashTool to change firmware) |
SamClarke | 1:0a034c2dbea6 | 22 | } |
SamClarke | 1:0a034c2dbea6 | 23 | |
SamClarke | 1:0a034c2dbea6 | 24 | int GPS::parseData() |
SamClarke | 1:0a034c2dbea6 | 25 | { |
SamClarke | 1:0a034c2dbea6 | 26 | while(1) { |
SamClarke | 1:0a034c2dbea6 | 27 | getData(); |
SamClarke | 1:0a034c2dbea6 | 28 | if(sscanf(NEMA, "GPGGA, %*f, %*f, %*c, %*f, %*c, %d, %d, %*f, %f", &fixtype, &satellites, &altitude) >=1); |
SamClarke | 4:9ac674d05370 | 29 | 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 | 30 | if(fixtype == 0) { |
SamClarke | 1:0a034c2dbea6 | 31 | return 0; |
SamClarke | 1:0a034c2dbea6 | 32 | } |
SamClarke | 4:9ac674d05370 | 33 | year += 2000; |
SamClarke | 1:0a034c2dbea6 | 34 | if(ns =='S') { |
Spilly | 6:af055728e564 | 35 | latitude *= -1.00000000000000000f; |
SamClarke | 1:0a034c2dbea6 | 36 | } |
SamClarke | 1:0a034c2dbea6 | 37 | if(ew =='W') { |
Spilly | 6:af055728e564 | 38 | longitude *= -1.00000000000000000f; |
SamClarke | 1:0a034c2dbea6 | 39 | } |
Spilly | 6:af055728e564 | 40 | float degrees = trunc(latitude / 100.0000000000000000f); |
Spilly | 6:af055728e564 | 41 | float minutes = latitude - (degrees * 100.00000000000000000f); |
Spilly | 6:af055728e564 | 42 | latitude = degrees + minutes / 60.00000000000000000f; |
Spilly | 6:af055728e564 | 43 | degrees = trunc(longitude / 100.0000000000000000f); |
Spilly | 6:af055728e564 | 44 | minutes = longitude - (degrees * 100.00000000000000000f); |
Spilly | 6:af055728e564 | 45 | longitude = degrees + minutes / 60.00000000000000000f; |
SamClarke | 3:5cb504ce2793 | 46 | if(fixtype == 1) { |
SamClarke | 3:5cb504ce2793 | 47 | fix = "Positive"; |
SamClarke | 3:5cb504ce2793 | 48 | } |
SamClarke | 3:5cb504ce2793 | 49 | if(fixtype == 2) { |
SamClarke | 3:5cb504ce2793 | 50 | fix = "Differential"; |
SamClarke | 3:5cb504ce2793 | 51 | } |
Spilly | 6:af055728e564 | 52 | if(heading > 0.00f && heading < 45.00f) { |
SamClarke | 3:5cb504ce2793 | 53 | cardinal = "NNE"; |
SamClarke | 3:5cb504ce2793 | 54 | } |
Spilly | 6:af055728e564 | 55 | if(heading == 45.00f) { |
SamClarke | 3:5cb504ce2793 | 56 | cardinal = "NE"; |
SamClarke | 3:5cb504ce2793 | 57 | } |
Spilly | 6:af055728e564 | 58 | if(heading > 45.00f && heading < 90.00f) { |
SamClarke | 3:5cb504ce2793 | 59 | cardinal = "ENE"; |
SamClarke | 3:5cb504ce2793 | 60 | } |
Spilly | 6:af055728e564 | 61 | if(heading == 90.00f) { |
SamClarke | 3:5cb504ce2793 | 62 | cardinal = "E"; |
SamClarke | 3:5cb504ce2793 | 63 | } |
Spilly | 6:af055728e564 | 64 | if(heading > 90.00f && heading < 135.00f) { |
SamClarke | 3:5cb504ce2793 | 65 | cardinal = "ESE"; |
SamClarke | 3:5cb504ce2793 | 66 | } |
Spilly | 6:af055728e564 | 67 | if(heading == 135.00f) { |
SamClarke | 3:5cb504ce2793 | 68 | cardinal = "SE"; |
SamClarke | 3:5cb504ce2793 | 69 | } |
Spilly | 6:af055728e564 | 70 | if(heading > 135.00f && heading < 180.00f) { |
SamClarke | 3:5cb504ce2793 | 71 | cardinal = "SSE"; |
SamClarke | 3:5cb504ce2793 | 72 | } |
Spilly | 6:af055728e564 | 73 | if(heading == 180.00f) { |
SamClarke | 3:5cb504ce2793 | 74 | cardinal = "S"; |
SamClarke | 3:5cb504ce2793 | 75 | } |
Spilly | 6:af055728e564 | 76 | if(heading > 180.00f && heading < 225.00f) { |
SamClarke | 3:5cb504ce2793 | 77 | cardinal = "SSW"; |
SamClarke | 3:5cb504ce2793 | 78 | } |
Spilly | 6:af055728e564 | 79 | if(heading == 225.00f) { |
SamClarke | 3:5cb504ce2793 | 80 | cardinal = "SW"; |
SamClarke | 3:5cb504ce2793 | 81 | } |
Spilly | 6:af055728e564 | 82 | if(heading > 225.00f && heading < 270.00f) { |
SamClarke | 3:5cb504ce2793 | 83 | cardinal = "WSW"; |
SamClarke | 3:5cb504ce2793 | 84 | } |
Spilly | 6:af055728e564 | 85 | if(heading == 270.00f) { |
SamClarke | 3:5cb504ce2793 | 86 | cardinal = "W"; |
SamClarke | 3:5cb504ce2793 | 87 | } |
Spilly | 6:af055728e564 | 88 | if(heading > 270.00f && heading < 315.00f) { |
SamClarke | 3:5cb504ce2793 | 89 | cardinal = "WNW"; |
SamClarke | 3:5cb504ce2793 | 90 | } |
Spilly | 6:af055728e564 | 91 | if(heading == 315.00f) { |
SamClarke | 3:5cb504ce2793 | 92 | cardinal = "NW"; |
SamClarke | 3:5cb504ce2793 | 93 | } |
Spilly | 6:af055728e564 | 94 | if(heading > 315.00f && heading < 360.00f) { |
SamClarke | 3:5cb504ce2793 | 95 | cardinal = "NNW"; |
SamClarke | 3:5cb504ce2793 | 96 | } |
Spilly | 6:af055728e564 | 97 | if(heading == 360.00f || heading == 0.00f) { |
SamClarke | 3:5cb504ce2793 | 98 | cardinal = "N"; |
SamClarke | 3:5cb504ce2793 | 99 | } |
Spilly | 6:af055728e564 | 100 | kph = speed*1.852f; |
SamClarke | 1:0a034c2dbea6 | 101 | return 1; |
SamClarke | 1:0a034c2dbea6 | 102 | } |
SamClarke | 1:0a034c2dbea6 | 103 | } |
SamClarke | 1:0a034c2dbea6 | 104 | } |
SamClarke | 1:0a034c2dbea6 | 105 | |
SamClarke | 1:0a034c2dbea6 | 106 | |
SamClarke | 1:0a034c2dbea6 | 107 | float GPS::trunc(float v) |
SamClarke | 1:0a034c2dbea6 | 108 | { |
Spilly | 6:af055728e564 | 109 | if(v < 0.0f) { |
Spilly | 6:af055728e564 | 110 | v*= -1.0f; |
SamClarke | 1:0a034c2dbea6 | 111 | v = floor(v); |
Spilly | 6:af055728e564 | 112 | v*=-1.0f; |
SamClarke | 1:0a034c2dbea6 | 113 | } else { |
SamClarke | 1:0a034c2dbea6 | 114 | v = floor(v); |
SamClarke | 1:0a034c2dbea6 | 115 | } |
SamClarke | 1:0a034c2dbea6 | 116 | return v; |
SamClarke | 1:0a034c2dbea6 | 117 | } |
SamClarke | 1:0a034c2dbea6 | 118 | |
SamClarke | 1:0a034c2dbea6 | 119 | void GPS::getData() |
SamClarke | 1:0a034c2dbea6 | 120 | { |
SamClarke | 1:0a034c2dbea6 | 121 | while(_UltimateGps.getc() != '$'); |
SamClarke | 1:0a034c2dbea6 | 122 | for(int i=0; i<256; i++) { |
SamClarke | 1:0a034c2dbea6 | 123 | NEMA[i] = _UltimateGps.getc(); |
SamClarke | 1:0a034c2dbea6 | 124 | if(NEMA[i] == '\r') { |
SamClarke | 1:0a034c2dbea6 | 125 | NEMA[i] = 0; |
SamClarke | 1:0a034c2dbea6 | 126 | return; |
SamClarke | 1:0a034c2dbea6 | 127 | } |
SamClarke | 1:0a034c2dbea6 | 128 | } |
SamClarke | 1:0a034c2dbea6 | 129 | error("overflowed message limit"); |
SamClarke | 1:0a034c2dbea6 | 130 | } |
SamClarke | 1:0a034c2dbea6 | 131 | |
SamClarke | 1:0a034c2dbea6 | 132 | void GPS::Init() |
SamClarke | 1:0a034c2dbea6 | 133 | { |
Spilly | 6:af055728e564 | 134 | _UltimateGps.printf("$PMTK251,38400*27\r\n"); //set baud (any higher and the serial buffer overflows) |
Spilly | 6:af055728e564 | 135 | //_UltimateGps.printf("$PMTK220,100*2F\r\n"); //10 Hz update |
Spilly | 6:af055728e564 | 136 | _UltimateGps.printf("$PMTK220,200*2C\r\n"); //5 Hz udpate |
Spilly | 6:af055728e564 | 137 | _UltimateGps.printf("$PMTK225,0*2bt\r\n"); //disable always locate (datasheet indicates that this negatively affects accuracy) |
Spilly | 6:af055728e564 | 138 | _UltimateGps.printf("$PMTK301,2*2et\r\n"); //set DGPS to use WAAS |
Spilly | 6:af055728e564 | 139 | _UltimateGps.printf("$PMTK386,0.8*35\r\n"); //set Nav Speed threshold to 0.8 m/s |
Spilly | 6:af055728e564 | 140 | _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"); |
Spilly | 6:af055728e564 | 141 | } |
Spilly | 6:af055728e564 | 142 | |
Spilly | 6:af055728e564 | 143 | void GPS::Sleep(int sleep) |
Spilly | 6:af055728e564 | 144 | { |
Spilly | 6:af055728e564 | 145 | if(sleep == 1) |
Spilly | 6:af055728e564 | 146 | { |
Spilly | 6:af055728e564 | 147 | _UltimateGps.printf("$$PMTK161,0*28\r\n"); //go to sleep |
Spilly | 6:af055728e564 | 148 | } |
Spilly | 6:af055728e564 | 149 | else |
Spilly | 6:af055728e564 | 150 | { |
Spilly | 6:af055728e564 | 151 | _UltimateGps.printf("$$PMTK161,1*28\r\n"); //wake up |
Spilly | 6:af055728e564 | 152 | _UltimateGps.printf("$PMTK220,200*2C\r\n"); //5 Hz udpate |
SamClarke | 1:0a034c2dbea6 | 153 | _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"); |
Spilly | 6:af055728e564 | 154 | } |
SamClarke | 1:0a034c2dbea6 | 155 | } |