Library for ublox neo 6m
GPS.cpp@1:380b615d1237, 2019-11-29 (annotated)
- Committer:
- JorgeGtz
- Date:
- Fri Nov 29 19:59:45 2019 +0000
- Revision:
- 1:380b615d1237
- Parent:
- 0:8f2e256775d7
Satelite
Who changed what in which revision?
User | Revision | Line number | New 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; |
fookies | 0:8f2e256775d7 | 29 | } |
fookies | 0:8f2e256775d7 | 30 | |
fookies | 0:8f2e256775d7 | 31 | int GPS::sample() { |
fookies | 0:8f2e256775d7 | 32 | float time; |
fookies | 0:8f2e256775d7 | 33 | char ns, ew; |
fookies | 0:8f2e256775d7 | 34 | int lock; |
fookies | 0:8f2e256775d7 | 35 | |
fookies | 0:8f2e256775d7 | 36 | //return 1; //testing by Jigar |
fookies | 0:8f2e256775d7 | 37 | while (1) { |
fookies | 0:8f2e256775d7 | 38 | getline(); |
fookies | 0:8f2e256775d7 | 39 | //printf("\n\rentered the GPS.sample while loop \n\r %s\n\r", msg); |
fookies | 0:8f2e256775d7 | 40 | |
fookies | 0:8f2e256775d7 | 41 | |
fookies | 0:8f2e256775d7 | 42 | // Check if it is a GPGGA msg (matches both locked and non-locked msg) |
fookies | 0:8f2e256775d7 | 43 | // $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52 |
fookies | 0:8f2e256775d7 | 44 | /* |
fookies | 0:8f2e256775d7 | 45 | eg3. $GPGGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh |
fookies | 0:8f2e256775d7 | 46 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fookies | 0:8f2e256775d7 | 47 | 1 = UTC of Position |
fookies | 0:8f2e256775d7 | 48 | 2 = Latitude |
fookies | 0:8f2e256775d7 | 49 | 3 = N or S |
fookies | 0:8f2e256775d7 | 50 | 4 = Longitude |
fookies | 0:8f2e256775d7 | 51 | 5 = E or W |
fookies | 0:8f2e256775d7 | 52 | 6 = GPS quality indicator (0=invalid; 1=GPS fix; 2=Diff. GPS fix) |
fookies | 0:8f2e256775d7 | 53 | 7 = Number of satellites in use [not those in view] |
fookies | 0:8f2e256775d7 | 54 | 8 = Horizontal dilution of position |
fookies | 0:8f2e256775d7 | 55 | 9 = Antenna altitude above/below mean sea level (geoid) |
fookies | 0:8f2e256775d7 | 56 | 10 = Meters (Antenna height unit) |
fookies | 0:8f2e256775d7 | 57 | 11 = Geoidal separation (Diff. between WGS-84 earth ellipsoid and |
fookies | 0:8f2e256775d7 | 58 | mean sea level. -=geoid is below WGS-84 ellipsoid) |
fookies | 0:8f2e256775d7 | 59 | 12 = Meters (Units of geoidal separation) |
fookies | 0:8f2e256775d7 | 60 | 13 = Age in seconds since last update from diff. reference station |
fookies | 0:8f2e256775d7 | 61 | 14 = Diff. reference station ID# |
fookies | 0:8f2e256775d7 | 62 | 15 = Checksum |
fookies | 0:8f2e256775d7 | 63 | |
fookies | 0:8f2e256775d7 | 64 | */ |
fookies | 0:8f2e256775d7 | 65 | // 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) { |
JorgeGtz | 1:380b615d1237 | 66 | if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c,%f", &time, &latitude, &ns, &longitude, &ew, &lock, &sats, &hdop, &alt, &unit, &geoid) >= 1) { |
JorgeGtz | 1:380b615d1237 | 67 | // if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) { |
fookies | 0:8f2e256775d7 | 68 | |
fookies | 0:8f2e256775d7 | 69 | if (!lock) |
fookies | 0:8f2e256775d7 | 70 | { |
fookies | 0:8f2e256775d7 | 71 | longitude = 0.0; |
fookies | 0:8f2e256775d7 | 72 | latitude = 0.0; |
fookies | 0:8f2e256775d7 | 73 | utc=0.0; |
fookies | 0:8f2e256775d7 | 74 | return 0; |
fookies | 0:8f2e256775d7 | 75 | } |
fookies | 0:8f2e256775d7 | 76 | else |
fookies | 0:8f2e256775d7 | 77 | { |
fookies | 0:8f2e256775d7 | 78 | if (ns == 'S') { |
fookies | 0:8f2e256775d7 | 79 | latitude *= -1.0; |
fookies | 0:8f2e256775d7 | 80 | } |
fookies | 0:8f2e256775d7 | 81 | if (ew == 'W') { |
fookies | 0:8f2e256775d7 | 82 | longitude *= -1.0; |
fookies | 0:8f2e256775d7 | 83 | } |
fookies | 0:8f2e256775d7 | 84 | float degrees = trunc(latitude / 100.0f); |
fookies | 0:8f2e256775d7 | 85 | float minutes = latitude - (degrees * 100.0f); |
fookies | 0:8f2e256775d7 | 86 | latitude = degrees + minutes / 60.0f; |
fookies | 0:8f2e256775d7 | 87 | degrees = trunc(longitude / 100.0f ); //degrees = trunc(longitude / 100.0f * 0.01f); |
fookies | 0:8f2e256775d7 | 88 | minutes = longitude - (degrees * 100.0f); |
fookies | 0:8f2e256775d7 | 89 | longitude = degrees + minutes / 60.0f; |
fookies | 0:8f2e256775d7 | 90 | utc=time; |
fookies | 0:8f2e256775d7 | 91 | return 1; |
fookies | 0:8f2e256775d7 | 92 | } |
fookies | 0:8f2e256775d7 | 93 | } |
fookies | 0:8f2e256775d7 | 94 | return 0; |
fookies | 0:8f2e256775d7 | 95 | } |
fookies | 0:8f2e256775d7 | 96 | } |
fookies | 0:8f2e256775d7 | 97 | |
fookies | 0:8f2e256775d7 | 98 | float GPS::trunc(float v) { |
fookies | 0:8f2e256775d7 | 99 | if (v < 0.0) { |
fookies | 0:8f2e256775d7 | 100 | v*= -1.0; |
fookies | 0:8f2e256775d7 | 101 | v = floor(v); |
fookies | 0:8f2e256775d7 | 102 | v*=-1.0; |
fookies | 0:8f2e256775d7 | 103 | } else { |
fookies | 0:8f2e256775d7 | 104 | v = floor(v); |
fookies | 0:8f2e256775d7 | 105 | } |
fookies | 0:8f2e256775d7 | 106 | return v; |
fookies | 0:8f2e256775d7 | 107 | } |
fookies | 0:8f2e256775d7 | 108 | |
fookies | 0:8f2e256775d7 | 109 | void GPS::getline() { |
fookies | 0:8f2e256775d7 | 110 | char c; |
fookies | 0:8f2e256775d7 | 111 | while (c = _gps.getc() != '$'); // wait for the start of a line |
fookies | 0:8f2e256775d7 | 112 | // printf("entered the getline loop\n\r"); |
fookies | 0:8f2e256775d7 | 113 | for (int i=0; i<256; i++) { |
fookies | 0:8f2e256775d7 | 114 | msg[i] = _gps.getc(); |
fookies | 0:8f2e256775d7 | 115 | if (msg[i] == '\r') { |
fookies | 0:8f2e256775d7 | 116 | msg[i] = 0; |
fookies | 0:8f2e256775d7 | 117 | return; |
fookies | 0:8f2e256775d7 | 118 | } |
fookies | 0:8f2e256775d7 | 119 | } |
fookies | 0:8f2e256775d7 | 120 | error("Overflowed message limit"); |
fookies | 0:8f2e256775d7 | 121 | } |
fookies | 0:8f2e256775d7 | 122 | |
fookies | 0:8f2e256775d7 | 123 | /* |
fookies | 0:8f2e256775d7 | 124 | $GPRMC,000115.039,V,,,,,,,291006,,*2C |
fookies | 0:8f2e256775d7 | 125 | $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52 |
fookies | 0:8f2e256775d7 | 126 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
fookies | 0:8f2e256775d7 | 127 | $GPGSV,3,1,12,20,00,000,,10,00,000,,31,00,000,,27,00,000,*7C |
fookies | 0:8f2e256775d7 | 128 | $GPGSV,3,2,12,19,00,000,,07,00,000,,04,00,000,,24,00,000,*76 |
fookies | 0:8f2e256775d7 | 129 | $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78 |
fookies | 0:8f2e256775d7 | 130 | $GPRMC,000116.031,V,,,,,,,291006,,*27 |
fookies | 0:8f2e256775d7 | 131 | $GPGGA,000117.035,,,,,0,00,,,M,0.0,M,,0000*57 |
fookies | 0:8f2e256775d7 | 132 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
fookies | 0:8f2e256775d7 | 133 | $GPRMC,000117.035,V,,,,,,,291006,,*22 |
fookies | 0:8f2e256775d7 | 134 | $GPGGA,000118.039,,,,,0,00,,,M,0.0,M,,0000*54 |
fookies | 0:8f2e256775d7 | 135 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
fookies | 0:8f2e256775d7 | 136 | $GPRMC,000118.039,V,,,,,,,291006,,*21 |
fookies | 0:8f2e256775d7 | 137 | $GPGGA,000119.035,,,,,0,00,,,M,0.0,M,,0000*59 |
fookies | 0:8f2e256775d7 | 138 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
fookies | 0:8f2e256775d7 | 139 | $GPRMC,000119.035,V,,,,,,,291006,,*2C |
fookies | 0:8f2e256775d7 | 140 | $GPGGA,000120.037,,,,,0,00,,,M,0.0,M,,0000*51 |
fookies | 0:8f2e256775d7 | 141 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
fookies | 0:8f2e256775d7 | 142 | $GPRMC,000120.037,V,,,,,,,291006,,*24 |
fookies | 0:8f2e256775d7 | 143 | |
fookies | 0:8f2e256775d7 | 144 | */ |