Library for ublox neo 6m

Dependents:   GPS-Tracking-Velo

Committer:
QuangAnhLe
Date:
Tue Apr 16 21:53:03 2019 +0000
Revision:
2:2b9befe69d07
Parent:
1:9a8a3f68b005
SMS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fookies 0:8f2e256775d7 1 /* mbed EM-406 GPS Module Library
fookies 0:8f2e256775d7 2 * Copyright (c) 2008-2010, sford
fookies 0:8f2e256775d7 3 *
fookies 0:8f2e256775d7 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
fookies 0:8f2e256775d7 5 * of this software and associated documentation files (the "Software"), to deal
fookies 0:8f2e256775d7 6 * in the Software without restriction, including without limitation the rights
fookies 0:8f2e256775d7 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
fookies 0:8f2e256775d7 8 * copies of the Software, and to permit persons to whom the Software is
fookies 0:8f2e256775d7 9 * furnished to do so, subject to the following conditions:
fookies 0:8f2e256775d7 10 *
fookies 0:8f2e256775d7 11 * The above copyright notice and this permission notice shall be included in
fookies 0:8f2e256775d7 12 * all copies or substantial portions of the Software.
fookies 0:8f2e256775d7 13 *
fookies 0:8f2e256775d7 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
fookies 0:8f2e256775d7 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
fookies 0:8f2e256775d7 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
fookies 0:8f2e256775d7 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
fookies 0:8f2e256775d7 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
fookies 0:8f2e256775d7 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
fookies 0:8f2e256775d7 20 * THE SOFTWARE.
fookies 0:8f2e256775d7 21 */
fookies 0:8f2e256775d7 22
fookies 0:8f2e256775d7 23 #include "GPS.h"
fookies 0:8f2e256775d7 24
fookies 0:8f2e256775d7 25 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
fookies 0:8f2e256775d7 26 //_gps.baud(4800);
fookies 0:8f2e256775d7 27 longitude = 0.0;
fookies 0:8f2e256775d7 28 latitude = 0.0;
QuangAnhLe 1:9a8a3f68b005 29 altitude = 0.0;
QuangAnhLe 1:9a8a3f68b005 30
fookies 0:8f2e256775d7 31 }
fookies 0:8f2e256775d7 32
fookies 0:8f2e256775d7 33 int GPS::sample() {
QuangAnhLe 1:9a8a3f68b005 34 float time,horizontal;
QuangAnhLe 1:9a8a3f68b005 35 float time2,longitude2,latitude2,Speed,track;
QuangAnhLe 1:9a8a3f68b005 36 char ns,ew,aut,ac,ns2,ew2;
QuangAnhLe 1:9a8a3f68b005 37 //int data;
QuangAnhLe 2:2b9befe69d07 38 //float Hdop, Pdop;
QuangAnhLe 1:9a8a3f68b005 39 char Aut;
QuangAnhLe 1:9a8a3f68b005 40 //char star;
QuangAnhLe 1:9a8a3f68b005 41 //int check_data;
QuangAnhLe 1:9a8a3f68b005 42 //char Pdop;
QuangAnhLe 1:9a8a3f68b005 43 //int fix_3d;
QuangAnhLe 1:9a8a3f68b005 44 //int Id1,Id2,Id3,Id4,Id5,Id6,Id7,Id8,Id9,Id10,Id11,Id12;
fookies 0:8f2e256775d7 45
fookies 0:8f2e256775d7 46 //return 1; //testing by Jigar
fookies 0:8f2e256775d7 47 while (1) {
fookies 0:8f2e256775d7 48 getline();
fookies 0:8f2e256775d7 49 //printf("\n\rentered the GPS.sample while loop \n\r %s\n\r", msg);
fookies 0:8f2e256775d7 50
fookies 0:8f2e256775d7 51
QuangAnhLe 1:9a8a3f68b005 52 /* $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
fookies 0:8f2e256775d7 53
QuangAnhLe 1:9a8a3f68b005 54 Where:
QuangAnhLe 1:9a8a3f68b005 55 GGA Global Positioning System Fix Data
QuangAnhLe 1:9a8a3f68b005 56 123519 Fix taken at 12:35:19 UTC
QuangAnhLe 1:9a8a3f68b005 57 4807.038,N Latitude 48 deg 07.038' N
QuangAnhLe 1:9a8a3f68b005 58 01131.000,E Longitude 11 deg 31.000' E
QuangAnhLe 1:9a8a3f68b005 59 1 Fix quality: 0 = invalid
QuangAnhLe 1:9a8a3f68b005 60 1 = GPS fix (SPS)
QuangAnhLe 1:9a8a3f68b005 61 2 = DGPS fix
QuangAnhLe 1:9a8a3f68b005 62 3 = PPS fix
QuangAnhLe 1:9a8a3f68b005 63 4 = Real Time Kinematic
QuangAnhLe 1:9a8a3f68b005 64 5 = Float RTK
QuangAnhLe 1:9a8a3f68b005 65 6 = estimated (dead reckoning) (2.3 feature)
QuangAnhLe 1:9a8a3f68b005 66 7 = Manual input mode
QuangAnhLe 1:9a8a3f68b005 67 8 = Simulation mode
QuangAnhLe 1:9a8a3f68b005 68 08 Number of satellites being tracked
QuangAnhLe 1:9a8a3f68b005 69 0.9 Horizontal dilution of position
QuangAnhLe 1:9a8a3f68b005 70 545.4,M Altitude, Meters, above mean sea level
QuangAnhLe 1:9a8a3f68b005 71 46.9,M Height of geoid (mean sea level) above WGS84
QuangAnhLe 1:9a8a3f68b005 72 ellipsoid
QuangAnhLe 1:9a8a3f68b005 73 (empty field) time in seconds since last DGPS update
QuangAnhLe 1:9a8a3f68b005 74 (empty field) DGPS station ID number
QuangAnhLe 1:9a8a3f68b005 75 *47 the checksum data, always begins with **/
fookies 0:8f2e256775d7 76 // if (sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%*d,%*d, %*f,%*f,%*f,%*f,%*f,%*f,%d ", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) {
QuangAnhLe 1:9a8a3f68b005 77 if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f", &time, &latitude, &ns, &longitude, &ew, &fix,& sat_tracked,&horizontal,&altitude) >= 1) {
fookies 0:8f2e256775d7 78
QuangAnhLe 1:9a8a3f68b005 79 if (!fix)
fookies 0:8f2e256775d7 80 {
fookies 0:8f2e256775d7 81 longitude = 0.0;
fookies 0:8f2e256775d7 82 latitude = 0.0;
QuangAnhLe 1:9a8a3f68b005 83 altitude = 0.0;
fookies 0:8f2e256775d7 84 utc=0.0;
QuangAnhLe 1:9a8a3f68b005 85 sat_tracked=0;
fookies 0:8f2e256775d7 86 return 0;
fookies 0:8f2e256775d7 87 }
fookies 0:8f2e256775d7 88 else
fookies 0:8f2e256775d7 89 {
fookies 0:8f2e256775d7 90 if (ns == 'S') {
fookies 0:8f2e256775d7 91 latitude *= -1.0;
fookies 0:8f2e256775d7 92 }
fookies 0:8f2e256775d7 93 if (ew == 'W') {
fookies 0:8f2e256775d7 94 longitude *= -1.0;
fookies 0:8f2e256775d7 95 }
fookies 0:8f2e256775d7 96 float degrees = trunc(latitude / 100.0f);
fookies 0:8f2e256775d7 97 float minutes = latitude - (degrees * 100.0f);
fookies 0:8f2e256775d7 98 latitude = degrees + minutes / 60.0f;
QuangAnhLe 1:9a8a3f68b005 99 degrees = trunc(longitude / 100.0f ); //degrees = trunc(longitude / 100.0f * 0.01f);
fookies 0:8f2e256775d7 100 minutes = longitude - (degrees * 100.0f);
fookies 0:8f2e256775d7 101 longitude = degrees + minutes / 60.0f;
QuangAnhLe 1:9a8a3f68b005 102 /*int hour = time/10000;
QuangAnhLe 1:9a8a3f68b005 103 int min = (time % 10000)/100;
QuangAnhLe 1:9a8a3f68b005 104 int sek = (time % 100);*/
fookies 0:8f2e256775d7 105 utc=time;
QuangAnhLe 1:9a8a3f68b005 106 //sat_use=sat;
QuangAnhLe 1:9a8a3f68b005 107 //altitude = altitude;
QuangAnhLe 1:9a8a3f68b005 108 //fix=lock;
fookies 0:8f2e256775d7 109 return 1;
fookies 0:8f2e256775d7 110 }
QuangAnhLe 1:9a8a3f68b005 111 }
QuangAnhLe 1:9a8a3f68b005 112 //getline();
QuangAnhLe 1:9a8a3f68b005 113 /** $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39
QuangAnhLe 1:9a8a3f68b005 114
QuangAnhLe 1:9a8a3f68b005 115 Where:
QuangAnhLe 1:9a8a3f68b005 116 GSA Satellite status
QuangAnhLe 1:9a8a3f68b005 117 A Auto selection of 2D or 3D fix (M = manual)
QuangAnhLe 1:9a8a3f68b005 118 3 3D fix - values include: 1 = no fix
QuangAnhLe 1:9a8a3f68b005 119 2 = 2D fix
QuangAnhLe 1:9a8a3f68b005 120 3 = 3D fix
QuangAnhLe 1:9a8a3f68b005 121 04,05... PRNs of satellites used for fix (space for 12)
QuangAnhLe 1:9a8a3f68b005 122 2.5 PDOP (dilution of precision)
QuangAnhLe 1:9a8a3f68b005 123 1.3 Horizontal dilution of precision (HDOP)
QuangAnhLe 1:9a8a3f68b005 124 2.1 Vertical dilution of precision (VDOP)
QuangAnhLe 1:9a8a3f68b005 125 *39 the checksum data, always begins with **/
QuangAnhLe 1:9a8a3f68b005 126 //getline2();
QuangAnhLe 1:9a8a3f68b005 127 /*if(sscanf(msg, "GPGSA,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%f,%f",&fix_3d,&id1,&id2,&id3,&id4,&id5,&id6,&id7,&id8,&id9,&id10,&id11,&id12,&Pdop,&Hdop)>=1);
QuangAnhLe 1:9a8a3f68b005 128 {
QuangAnhLe 1:9a8a3f68b005 129
QuangAnhLe 1:9a8a3f68b005 130 if (fix_3d==1)
QuangAnhLe 1:9a8a3f68b005 131 {
QuangAnhLe 1:9a8a3f68b005 132 id1=id2=id3=id4=id5=id6=id7=id8=id9=id10=id11=id12=0;
QuangAnhLe 1:9a8a3f68b005 133 pdop=0.0;
QuangAnhLe 1:9a8a3f68b005 134 hdop=0.0;
QuangAnhLe 1:9a8a3f68b005 135 //vdop=0.0;
QuangAnhLe 1:9a8a3f68b005 136
QuangAnhLe 1:9a8a3f68b005 137 return 0;
QuangAnhLe 1:9a8a3f68b005 138 }
QuangAnhLe 1:9a8a3f68b005 139 else
QuangAnhLe 1:9a8a3f68b005 140 {
QuangAnhLe 1:9a8a3f68b005 141 pdop=Pdop;
QuangAnhLe 1:9a8a3f68b005 142 hdop=Hdop;
QuangAnhLe 1:9a8a3f68b005 143 //vhop=Vhop;
QuangAnhLe 1:9a8a3f68b005 144 //aut=Aut;
QuangAnhLe 1:9a8a3f68b005 145 return 1;
QuangAnhLe 1:9a8a3f68b005 146
QuangAnhLe 1:9a8a3f68b005 147 }
QuangAnhLe 1:9a8a3f68b005 148 }*/
QuangAnhLe 2:2b9befe69d07 149 if(sscanf(msg, "GPGSA,%c,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &Aut, &fix_3d,&id1,&id2,&id3,&id4,&id5,&id6,&id7,&id8,&id9,&id10,&id11,&id12,&id13) >= 1)
QuangAnhLe 1:9a8a3f68b005 150 {
QuangAnhLe 1:9a8a3f68b005 151
QuangAnhLe 1:9a8a3f68b005 152 if (fix_3d==1)
QuangAnhLe 1:9a8a3f68b005 153 {
QuangAnhLe 1:9a8a3f68b005 154 id1=id2=id3=id4=id5=id6=id7=id8=id9=id10=id11=id12=0;
QuangAnhLe 2:2b9befe69d07 155 //pdop=0.0;
QuangAnhLe 2:2b9befe69d07 156 //hdop=0.0;
QuangAnhLe 1:9a8a3f68b005 157 //vdop=0.0;
QuangAnhLe 1:9a8a3f68b005 158
QuangAnhLe 1:9a8a3f68b005 159 return 0;
QuangAnhLe 1:9a8a3f68b005 160 }
QuangAnhLe 1:9a8a3f68b005 161 else
QuangAnhLe 1:9a8a3f68b005 162 {
QuangAnhLe 1:9a8a3f68b005 163 //pdop=Pdop;
QuangAnhLe 1:9a8a3f68b005 164 //hdop=Hdop;
QuangAnhLe 1:9a8a3f68b005 165 //vhop=Vhop;
QuangAnhLe 1:9a8a3f68b005 166 aut=Aut;
QuangAnhLe 1:9a8a3f68b005 167 return 1;
QuangAnhLe 1:9a8a3f68b005 168
QuangAnhLe 1:9a8a3f68b005 169 }
QuangAnhLe 1:9a8a3f68b005 170 }
QuangAnhLe 1:9a8a3f68b005 171 if(sscanf(msg, "GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &time2, &ac, &latitude2, &ns2, &longitude2, &ew2, &speed, &date)>=1)
QuangAnhLe 1:9a8a3f68b005 172 {
QuangAnhLe 1:9a8a3f68b005 173 //speed=Speed;
QuangAnhLe 1:9a8a3f68b005 174 //date=Date;
QuangAnhLe 1:9a8a3f68b005 175 //return 1;
QuangAnhLe 1:9a8a3f68b005 176 }
fookies 0:8f2e256775d7 177 return 0;
QuangAnhLe 1:9a8a3f68b005 178 }
fookies 0:8f2e256775d7 179 }
QuangAnhLe 1:9a8a3f68b005 180
QuangAnhLe 1:9a8a3f68b005 181 /**$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A
QuangAnhLe 1:9a8a3f68b005 182 Where:
QuangAnhLe 1:9a8a3f68b005 183 RMC Recommended Minimum sentence C
QuangAnhLe 1:9a8a3f68b005 184 123519 Fix taken at 12:35:19 UTC
QuangAnhLe 1:9a8a3f68b005 185 A Status A=active or V=Void.
QuangAnhLe 1:9a8a3f68b005 186 4807.038,N Latitude 48 deg 07.038' N
QuangAnhLe 1:9a8a3f68b005 187 01131.000,E Longitude 11 deg 31.000' E
QuangAnhLe 1:9a8a3f68b005 188 022.4 Speed over the ground in knots
QuangAnhLe 1:9a8a3f68b005 189 084.4 Track angle in degrees True
QuangAnhLe 1:9a8a3f68b005 190 230394 Date - 23rd of March 1994
QuangAnhLe 1:9a8a3f68b005 191 003.1,W Magnetic Variation
QuangAnhLe 1:9a8a3f68b005 192 *6A The checksum data, always begins with **/
QuangAnhLe 1:9a8a3f68b005 193
QuangAnhLe 1:9a8a3f68b005 194
QuangAnhLe 1:9a8a3f68b005 195 //return 0;
QuangAnhLe 1:9a8a3f68b005 196
fookies 0:8f2e256775d7 197
fookies 0:8f2e256775d7 198 float GPS::trunc(float v) {
fookies 0:8f2e256775d7 199 if (v < 0.0) {
fookies 0:8f2e256775d7 200 v*= -1.0;
fookies 0:8f2e256775d7 201 v = floor(v);
fookies 0:8f2e256775d7 202 v*=-1.0;
fookies 0:8f2e256775d7 203 } else {
fookies 0:8f2e256775d7 204 v = floor(v);
fookies 0:8f2e256775d7 205 }
fookies 0:8f2e256775d7 206 return v;
fookies 0:8f2e256775d7 207 }
fookies 0:8f2e256775d7 208
fookies 0:8f2e256775d7 209 void GPS::getline() {
fookies 0:8f2e256775d7 210 char c;
fookies 0:8f2e256775d7 211 while (c = _gps.getc() != '$'); // wait for the start of a line
fookies 0:8f2e256775d7 212 // printf("entered the getline loop\n\r");
QuangAnhLe 1:9a8a3f68b005 213 for (int i=0; i<280; i++) {
fookies 0:8f2e256775d7 214 msg[i] = _gps.getc();
fookies 0:8f2e256775d7 215 if (msg[i] == '\r') {
fookies 0:8f2e256775d7 216 msg[i] = 0;
fookies 0:8f2e256775d7 217 return;
fookies 0:8f2e256775d7 218 }
fookies 0:8f2e256775d7 219 }
fookies 0:8f2e256775d7 220 error("Overflowed message limit");
fookies 0:8f2e256775d7 221 }
QuangAnhLe 1:9a8a3f68b005 222 /*void GPS::getline2() {
QuangAnhLe 1:9a8a3f68b005 223 char c;
QuangAnhLe 1:9a8a3f68b005 224 while (c = _gps.getc() != '$'); // wait for the start of a line
QuangAnhLe 1:9a8a3f68b005 225 // printf("entered the getline loop\n\r");
QuangAnhLe 1:9a8a3f68b005 226 for (int i=0; i<300; i++) {
QuangAnhLe 1:9a8a3f68b005 227 msg2[i] = _gps.getc();
QuangAnhLe 1:9a8a3f68b005 228 if (msg2[i] == '\r') {
QuangAnhLe 1:9a8a3f68b005 229 msg2[i] = 0;
QuangAnhLe 1:9a8a3f68b005 230 return;
QuangAnhLe 1:9a8a3f68b005 231 }
QuangAnhLe 1:9a8a3f68b005 232 }
QuangAnhLe 1:9a8a3f68b005 233 error("Overflowed message limit");
QuangAnhLe 1:9a8a3f68b005 234 }*/
fookies 0:8f2e256775d7 235
fookies 0:8f2e256775d7 236 /*
fookies 0:8f2e256775d7 237 $GPRMC,000115.039,V,,,,,,,291006,,*2C
fookies 0:8f2e256775d7 238 $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52
fookies 0:8f2e256775d7 239 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
fookies 0:8f2e256775d7 240 $GPGSV,3,1,12,20,00,000,,10,00,000,,31,00,000,,27,00,000,*7C
fookies 0:8f2e256775d7 241 $GPGSV,3,2,12,19,00,000,,07,00,000,,04,00,000,,24,00,000,*76
fookies 0:8f2e256775d7 242 $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78
fookies 0:8f2e256775d7 243 $GPRMC,000116.031,V,,,,,,,291006,,*27
fookies 0:8f2e256775d7 244 $GPGGA,000117.035,,,,,0,00,,,M,0.0,M,,0000*57
fookies 0:8f2e256775d7 245 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
fookies 0:8f2e256775d7 246 $GPRMC,000117.035,V,,,,,,,291006,,*22
fookies 0:8f2e256775d7 247 $GPGGA,000118.039,,,,,0,00,,,M,0.0,M,,0000*54
fookies 0:8f2e256775d7 248 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
fookies 0:8f2e256775d7 249 $GPRMC,000118.039,V,,,,,,,291006,,*21
fookies 0:8f2e256775d7 250 $GPGGA,000119.035,,,,,0,00,,,M,0.0,M,,0000*59
fookies 0:8f2e256775d7 251 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
fookies 0:8f2e256775d7 252 $GPRMC,000119.035,V,,,,,,,291006,,*2C
fookies 0:8f2e256775d7 253 $GPGGA,000120.037,,,,,0,00,,,M,0.0,M,,0000*51
fookies 0:8f2e256775d7 254 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
fookies 0:8f2e256775d7 255 $GPRMC,000120.037,V,,,,,,,291006,,*24
fookies 0:8f2e256775d7 256
fookies 0:8f2e256775d7 257 */