Gps
Dependencies: mbed
Fork of GPS_System_with_Google_Maps by
GPS.cpp@6:8cc48b05d20e, 2017-01-04 (annotated)
- Committer:
- fadi_lad
- Date:
- Wed Jan 04 15:52:24 2017 +0000
- Revision:
- 6:8cc48b05d20e
- Parent:
- 3:3c7906d60f89
gpsSerial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ifrah | 3:3c7906d60f89 | 1 | /* mbed EM-406 GPS Module Library |
Ifrah | 3:3c7906d60f89 | 2 | * Copyright (c) 2008-2010, sford |
Ifrah | 3:3c7906d60f89 | 3 | * |
Ifrah | 3:3c7906d60f89 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Ifrah | 3:3c7906d60f89 | 5 | * of this software and associated documentation files (the "Software"), to deal |
Ifrah | 3:3c7906d60f89 | 6 | * in the Software without restriction, including without limitation the rights |
Ifrah | 3:3c7906d60f89 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Ifrah | 3:3c7906d60f89 | 8 | * copies of the Software, and to permit persons to whom the Software is |
Ifrah | 3:3c7906d60f89 | 9 | * furnished to do so, subject to the following conditions: |
Ifrah | 3:3c7906d60f89 | 10 | * |
Ifrah | 3:3c7906d60f89 | 11 | * The above copyright notice and this permission notice shall be included in |
Ifrah | 3:3c7906d60f89 | 12 | * all copies or substantial portions of the Software. |
Ifrah | 3:3c7906d60f89 | 13 | * |
Ifrah | 3:3c7906d60f89 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Ifrah | 3:3c7906d60f89 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Ifrah | 3:3c7906d60f89 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Ifrah | 3:3c7906d60f89 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Ifrah | 3:3c7906d60f89 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Ifrah | 3:3c7906d60f89 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Ifrah | 3:3c7906d60f89 | 20 | * THE SOFTWARE. |
Ifrah | 3:3c7906d60f89 | 21 | */ |
Ifrah | 3:3c7906d60f89 | 22 | |
Ifrah | 3:3c7906d60f89 | 23 | #include "GPS.h" |
Ifrah | 3:3c7906d60f89 | 24 | |
fadi_lad | 6:8cc48b05d20e | 25 | |
Ifrah | 3:3c7906d60f89 | 26 | GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) { |
fadi_lad | 6:8cc48b05d20e | 27 | _gps.baud(9600); |
Ifrah | 3:3c7906d60f89 | 28 | longitude = 0.0; |
Ifrah | 3:3c7906d60f89 | 29 | latitude = 0.0; |
Ifrah | 3:3c7906d60f89 | 30 | } |
Ifrah | 3:3c7906d60f89 | 31 | |
Ifrah | 3:3c7906d60f89 | 32 | int GPS::sample() { |
Ifrah | 3:3c7906d60f89 | 33 | float time; |
Ifrah | 3:3c7906d60f89 | 34 | char ns, ew; |
Ifrah | 3:3c7906d60f89 | 35 | int lock; |
Ifrah | 3:3c7906d60f89 | 36 | |
Ifrah | 3:3c7906d60f89 | 37 | //return 1; //testing by Jigar |
Ifrah | 3:3c7906d60f89 | 38 | while (1) { |
fadi_lad | 6:8cc48b05d20e | 39 | // pc.printf("\n\rentered the GPS.sample while loop\n\r"); |
Ifrah | 3:3c7906d60f89 | 40 | getline(); |
fadi_lad | 6:8cc48b05d20e | 41 | // pc.printf("\n\rentered the GPS.sample while loop \n\r %s\n\r", msg); |
Ifrah | 3:3c7906d60f89 | 42 | |
Ifrah | 3:3c7906d60f89 | 43 | |
Ifrah | 3:3c7906d60f89 | 44 | // Check if it is a GPGGA msg (matches both locked and non-locked msg) |
Ifrah | 3:3c7906d60f89 | 45 | // $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52 |
Ifrah | 3:3c7906d60f89 | 46 | /* |
Ifrah | 3:3c7906d60f89 | 47 | eg3. $GPGGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh |
Ifrah | 3:3c7906d60f89 | 48 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Ifrah | 3:3c7906d60f89 | 49 | 1 = UTC of Position |
Ifrah | 3:3c7906d60f89 | 50 | 2 = Latitude |
Ifrah | 3:3c7906d60f89 | 51 | 3 = N or S |
Ifrah | 3:3c7906d60f89 | 52 | 4 = Longitude |
Ifrah | 3:3c7906d60f89 | 53 | 5 = E or W |
Ifrah | 3:3c7906d60f89 | 54 | 6 = GPS quality indicator (0=invalid; 1=GPS fix; 2=Diff. GPS fix) |
Ifrah | 3:3c7906d60f89 | 55 | 7 = Number of satellites in use [not those in view] |
Ifrah | 3:3c7906d60f89 | 56 | 8 = Horizontal dilution of position |
Ifrah | 3:3c7906d60f89 | 57 | 9 = Antenna altitude above/below mean sea level (geoid) |
Ifrah | 3:3c7906d60f89 | 58 | 10 = Meters (Antenna height unit) |
Ifrah | 3:3c7906d60f89 | 59 | 11 = Geoidal separation (Diff. between WGS-84 earth ellipsoid and |
Ifrah | 3:3c7906d60f89 | 60 | mean sea level. -=geoid is below WGS-84 ellipsoid) |
Ifrah | 3:3c7906d60f89 | 61 | 12 = Meters (Units of geoidal separation) |
Ifrah | 3:3c7906d60f89 | 62 | 13 = Age in seconds since last update from diff. reference station |
Ifrah | 3:3c7906d60f89 | 63 | 14 = Diff. reference station ID# |
Ifrah | 3:3c7906d60f89 | 64 | 15 = Checksum |
Ifrah | 3:3c7906d60f89 | 65 | |
Ifrah | 3:3c7906d60f89 | 66 | */ |
Ifrah | 3:3c7906d60f89 | 67 | // 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) { |
Ifrah | 3:3c7906d60f89 | 68 | if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) { |
Ifrah | 3:3c7906d60f89 | 69 | |
Ifrah | 3:3c7906d60f89 | 70 | if (!lock) { |
Ifrah | 3:3c7906d60f89 | 71 | longitude = 0.0; |
Ifrah | 3:3c7906d60f89 | 72 | latitude = 0.0; |
Ifrah | 3:3c7906d60f89 | 73 | return 0; |
Ifrah | 3:3c7906d60f89 | 74 | } else { |
Ifrah | 3:3c7906d60f89 | 75 | if (ns == 'S') { |
Ifrah | 3:3c7906d60f89 | 76 | latitude *= -1.0; |
Ifrah | 3:3c7906d60f89 | 77 | } |
Ifrah | 3:3c7906d60f89 | 78 | if (ew == 'W') { |
Ifrah | 3:3c7906d60f89 | 79 | longitude *= -1.0; |
Ifrah | 3:3c7906d60f89 | 80 | } |
Ifrah | 3:3c7906d60f89 | 81 | float degrees = trunc(latitude / 100.0f); |
Ifrah | 3:3c7906d60f89 | 82 | float minutes = latitude - (degrees * 100.0f); |
Ifrah | 3:3c7906d60f89 | 83 | latitude = degrees + minutes / 60.0f; |
fadi_lad | 6:8cc48b05d20e | 84 | degrees = trunc(longitude / 100.0f ); //degrees = trunc(longitude / 100.0f * 0.01f); |
Ifrah | 3:3c7906d60f89 | 85 | minutes = longitude - (degrees * 100.0f); |
Ifrah | 3:3c7906d60f89 | 86 | longitude = degrees + minutes / 60.0f; |
Ifrah | 3:3c7906d60f89 | 87 | return 1; |
Ifrah | 3:3c7906d60f89 | 88 | } |
Ifrah | 3:3c7906d60f89 | 89 | } |
Ifrah | 3:3c7906d60f89 | 90 | } |
Ifrah | 3:3c7906d60f89 | 91 | } |
Ifrah | 3:3c7906d60f89 | 92 | |
Ifrah | 3:3c7906d60f89 | 93 | float GPS::trunc(float v) { |
Ifrah | 3:3c7906d60f89 | 94 | if (v < 0.0) { |
Ifrah | 3:3c7906d60f89 | 95 | v*= -1.0; |
Ifrah | 3:3c7906d60f89 | 96 | v = floor(v); |
Ifrah | 3:3c7906d60f89 | 97 | v*=-1.0; |
Ifrah | 3:3c7906d60f89 | 98 | } else { |
Ifrah | 3:3c7906d60f89 | 99 | v = floor(v); |
Ifrah | 3:3c7906d60f89 | 100 | } |
Ifrah | 3:3c7906d60f89 | 101 | return v; |
Ifrah | 3:3c7906d60f89 | 102 | } |
Ifrah | 3:3c7906d60f89 | 103 | |
Ifrah | 3:3c7906d60f89 | 104 | void GPS::getline() { |
fadi_lad | 6:8cc48b05d20e | 105 | |
fadi_lad | 6:8cc48b05d20e | 106 | //pc.printf("entered the getline loop\n\r"); |
Ifrah | 3:3c7906d60f89 | 107 | while (_gps.getc() != '$'); // wait for the start of a line |
fadi_lad | 6:8cc48b05d20e | 108 | // pc.printf("entered the getline loop\n\r"); |
Ifrah | 3:3c7906d60f89 | 109 | for (int i=0; i<256; i++) { |
Ifrah | 3:3c7906d60f89 | 110 | msg[i] = _gps.getc(); |
Ifrah | 3:3c7906d60f89 | 111 | if (msg[i] == '\r') { |
Ifrah | 3:3c7906d60f89 | 112 | msg[i] = 0; |
Ifrah | 3:3c7906d60f89 | 113 | return; |
Ifrah | 3:3c7906d60f89 | 114 | } |
Ifrah | 3:3c7906d60f89 | 115 | } |
Ifrah | 3:3c7906d60f89 | 116 | error("Overflowed message limit"); |
Ifrah | 3:3c7906d60f89 | 117 | } |
Ifrah | 3:3c7906d60f89 | 118 | |
Ifrah | 3:3c7906d60f89 | 119 | /* |
Ifrah | 3:3c7906d60f89 | 120 | $GPRMC,000115.039,V,,,,,,,291006,,*2C |
Ifrah | 3:3c7906d60f89 | 121 | $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52 |
Ifrah | 3:3c7906d60f89 | 122 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
Ifrah | 3:3c7906d60f89 | 123 | $GPGSV,3,1,12,20,00,000,,10,00,000,,31,00,000,,27,00,000,*7C |
Ifrah | 3:3c7906d60f89 | 124 | $GPGSV,3,2,12,19,00,000,,07,00,000,,04,00,000,,24,00,000,*76 |
Ifrah | 3:3c7906d60f89 | 125 | $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78 |
Ifrah | 3:3c7906d60f89 | 126 | $GPRMC,000116.031,V,,,,,,,291006,,*27 |
Ifrah | 3:3c7906d60f89 | 127 | $GPGGA,000117.035,,,,,0,00,,,M,0.0,M,,0000*57 |
Ifrah | 3:3c7906d60f89 | 128 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
Ifrah | 3:3c7906d60f89 | 129 | $GPRMC,000117.035,V,,,,,,,291006,,*22 |
Ifrah | 3:3c7906d60f89 | 130 | $GPGGA,000118.039,,,,,0,00,,,M,0.0,M,,0000*54 |
Ifrah | 3:3c7906d60f89 | 131 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
Ifrah | 3:3c7906d60f89 | 132 | $GPRMC,000118.039,V,,,,,,,291006,,*21 |
Ifrah | 3:3c7906d60f89 | 133 | $GPGGA,000119.035,,,,,0,00,,,M,0.0,M,,0000*59 |
Ifrah | 3:3c7906d60f89 | 134 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
Ifrah | 3:3c7906d60f89 | 135 | $GPRMC,000119.035,V,,,,,,,291006,,*2C |
Ifrah | 3:3c7906d60f89 | 136 | $GPGGA,000120.037,,,,,0,00,,,M,0.0,M,,0000*51 |
Ifrah | 3:3c7906d60f89 | 137 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
Ifrah | 3:3c7906d60f89 | 138 | $GPRMC,000120.037,V,,,,,,,291006,,*24 |
Ifrah | 3:3c7906d60f89 | 139 | |
Ifrah | 3:3c7906d60f89 | 140 | */ |