Weather Station using an mBed microcontroller and a Windows CE Device
Dependencies: TextLCD mbed HMC6352
GPS.cpp@0:75e6a2e50519, 2011-12-15 (annotated)
- Committer:
- mafischl
- Date:
- Thu Dec 15 13:38:51 2011 +0000
- Revision:
- 0:75e6a2e50519
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mafischl | 0:75e6a2e50519 | 1 | /* mbed EM-406 GPS Module Library |
mafischl | 0:75e6a2e50519 | 2 | * Copyright (c) 2008-2010, sford |
mafischl | 0:75e6a2e50519 | 3 | * |
mafischl | 0:75e6a2e50519 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mafischl | 0:75e6a2e50519 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mafischl | 0:75e6a2e50519 | 6 | * in the Software without restriction, including without limitation the rights |
mafischl | 0:75e6a2e50519 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mafischl | 0:75e6a2e50519 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mafischl | 0:75e6a2e50519 | 9 | * furnished to do so, subject to the following conditions: |
mafischl | 0:75e6a2e50519 | 10 | * |
mafischl | 0:75e6a2e50519 | 11 | * The above copyright notice and this permission notice shall be included in |
mafischl | 0:75e6a2e50519 | 12 | * all copies or substantial portions of the Software. |
mafischl | 0:75e6a2e50519 | 13 | * |
mafischl | 0:75e6a2e50519 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mafischl | 0:75e6a2e50519 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mafischl | 0:75e6a2e50519 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mafischl | 0:75e6a2e50519 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mafischl | 0:75e6a2e50519 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mafischl | 0:75e6a2e50519 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mafischl | 0:75e6a2e50519 | 20 | * THE SOFTWARE. |
mafischl | 0:75e6a2e50519 | 21 | */ |
mafischl | 0:75e6a2e50519 | 22 | |
mafischl | 0:75e6a2e50519 | 23 | #include "GPS.h" |
mafischl | 0:75e6a2e50519 | 24 | |
mafischl | 0:75e6a2e50519 | 25 | |
mafischl | 0:75e6a2e50519 | 26 | GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) { |
mafischl | 0:75e6a2e50519 | 27 | _gps.baud(4800); |
mafischl | 0:75e6a2e50519 | 28 | longitude = 0.0; |
mafischl | 0:75e6a2e50519 | 29 | latitude = 0.0; |
mafischl | 0:75e6a2e50519 | 30 | } |
mafischl | 0:75e6a2e50519 | 31 | |
mafischl | 0:75e6a2e50519 | 32 | int GPS::sample() { |
mafischl | 0:75e6a2e50519 | 33 | float time2, time, date; |
mafischl | 0:75e6a2e50519 | 34 | float latitude2, longitude2, speed, course, magneticVar; |
mafischl | 0:75e6a2e50519 | 35 | char ns, ew, ns2, ew2, mgEW, mode, status; |
mafischl | 0:75e6a2e50519 | 36 | |
mafischl | 0:75e6a2e50519 | 37 | |
mafischl | 0:75e6a2e50519 | 38 | //return 1; //testing by Jigar |
mafischl | 0:75e6a2e50519 | 39 | while (1) { |
mafischl | 0:75e6a2e50519 | 40 | getline(); |
mafischl | 0:75e6a2e50519 | 41 | //printf("\n\rentered the GPS.sample while loop \n\r %s\n\r", msg); |
mafischl | 0:75e6a2e50519 | 42 | |
mafischl | 0:75e6a2e50519 | 43 | |
mafischl | 0:75e6a2e50519 | 44 | // Check if it is a GPGGA msg (matches both locked and non-locked msg) |
mafischl | 0:75e6a2e50519 | 45 | // $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52 |
mafischl | 0:75e6a2e50519 | 46 | /* |
mafischl | 0:75e6a2e50519 | 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 |
mafischl | 0:75e6a2e50519 | 48 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
mafischl | 0:75e6a2e50519 | 49 | 1 = UTC of Position |
mafischl | 0:75e6a2e50519 | 50 | 2 = Latitude |
mafischl | 0:75e6a2e50519 | 51 | 3 = N or S |
mafischl | 0:75e6a2e50519 | 52 | 4 = Longitude |
mafischl | 0:75e6a2e50519 | 53 | 5 = E or W |
mafischl | 0:75e6a2e50519 | 54 | 6 = GPS quality indicator (0=invalid; 1=GPS fix; 2=Diff. GPS fix) |
mafischl | 0:75e6a2e50519 | 55 | 7 = Number of satellites in use [not those in view] |
mafischl | 0:75e6a2e50519 | 56 | 8 = Horizontal dilution of position |
mafischl | 0:75e6a2e50519 | 57 | 9 = Antenna altitude above/below mean sea level (geoid) |
mafischl | 0:75e6a2e50519 | 58 | 10 = Meters (Antenna height unit) |
mafischl | 0:75e6a2e50519 | 59 | 11 = Geoidal separation (Diff. between WGS-84 earth ellipsoid and |
mafischl | 0:75e6a2e50519 | 60 | mean sea level. -=geoid is below WGS-84 ellipsoid) |
mafischl | 0:75e6a2e50519 | 61 | 12 = Meters (Units of geoidal separation) |
mafischl | 0:75e6a2e50519 | 62 | 13 = Age in seconds since last update from diff. reference station |
mafischl | 0:75e6a2e50519 | 63 | 14 = Diff. reference station ID# |
mafischl | 0:75e6a2e50519 | 64 | 15 = Checksum |
mafischl | 0:75e6a2e50519 | 65 | |
mafischl | 0:75e6a2e50519 | 66 | */ |
mafischl | 0:75e6a2e50519 | 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, &numSat, &horDil, &altitude) >= 1) { |
mafischl | 0:75e6a2e50519 | 68 | if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f", &time2, &latitude, &ns, &longitude, &ew, &lock, &numSat, &horDil, &altitude) >= 1) { |
mafischl | 0:75e6a2e50519 | 69 | |
mafischl | 0:75e6a2e50519 | 70 | if (!lock) { |
mafischl | 0:75e6a2e50519 | 71 | longitude = 0.0; |
mafischl | 0:75e6a2e50519 | 72 | latitude = 0.0; |
mafischl | 0:75e6a2e50519 | 73 | return 0; |
mafischl | 0:75e6a2e50519 | 74 | } else { |
mafischl | 0:75e6a2e50519 | 75 | if (ns == 'S') { |
mafischl | 0:75e6a2e50519 | 76 | latitude *= -1.0; |
mafischl | 0:75e6a2e50519 | 77 | } |
mafischl | 0:75e6a2e50519 | 78 | if (ew == 'W') { |
mafischl | 0:75e6a2e50519 | 79 | longitude *= -1.0; |
mafischl | 0:75e6a2e50519 | 80 | } |
mafischl | 0:75e6a2e50519 | 81 | float degrees = trunc(latitude / 100.0f); |
mafischl | 0:75e6a2e50519 | 82 | float minutes = latitude - (degrees * 100.0f); |
mafischl | 0:75e6a2e50519 | 83 | latitude = degrees + minutes / 60.0f; |
mafischl | 0:75e6a2e50519 | 84 | degrees = trunc(longitude / 100.0f ); //degrees = trunc(longitude / 100.0f * 0.01f); |
mafischl | 0:75e6a2e50519 | 85 | minutes = longitude - (degrees * 100.0f); |
mafischl | 0:75e6a2e50519 | 86 | longitude = degrees + minutes / 60.0f; |
mafischl | 0:75e6a2e50519 | 87 | return 1; |
mafischl | 0:75e6a2e50519 | 88 | } |
mafischl | 0:75e6a2e50519 | 89 | } |
mafischl | 0:75e6a2e50519 | 90 | |
mafischl | 0:75e6a2e50519 | 91 | // Check if it is a GPRMC msg (matches both locked and non-locked msg) |
mafischl | 0:75e6a2e50519 | 92 | /* |
mafischl | 0:75e6a2e50519 | 93 | $GPRMC,hhmmss.ss,a,llll.ll,N,llll.ll,E,x.x,x.x,ddmmyy,x.x,E,A*hh |
mafischl | 0:75e6a2e50519 | 94 | 1 2 3 4 5 6 7 8 9 10 11 12 13 |
mafischl | 0:75e6a2e50519 | 95 | 1 = UTC of Position |
mafischl | 0:75e6a2e50519 | 96 | 2 = Status |
mafischl | 0:75e6a2e50519 | 97 | 3 = Latitude |
mafischl | 0:75e6a2e50519 | 98 | 4 = N or S |
mafischl | 0:75e6a2e50519 | 99 | 5 = Longitude |
mafischl | 0:75e6a2e50519 | 100 | 6 = E or W |
mafischl | 0:75e6a2e50519 | 101 | 7 = Speed Over Ground (knots) |
mafischl | 0:75e6a2e50519 | 102 | 8 = Course Over Ground (degrees) |
mafischl | 0:75e6a2e50519 | 103 | 9 = Date (ddmmyy) |
mafischl | 0:75e6a2e50519 | 104 | 10 = Magnetic Variation (degrees) |
mafischl | 0:75e6a2e50519 | 105 | 11 = E or W (for magnetic variation) |
mafischl | 0:75e6a2e50519 | 106 | 12 = Mode (A = Autonomous, D = DGPS, E = DR) |
mafischl | 0:75e6a2e50519 | 107 | 13 = Checksum |
mafischl | 0:75e6a2e50519 | 108 | |
mafischl | 0:75e6a2e50519 | 109 | */ |
mafischl | 0:75e6a2e50519 | 110 | // $GPRMC,161229.487,A,3723.2475,N,12158.3416,W,0.13,309.62,120598, ,*10 |
mafischl | 0:75e6a2e50519 | 111 | if(sscanf(msg, "GPRMC,%f,%c,%f,%c,%f,%c,%f,%f,%f,%f,%c,%c", &time, &status, &latitude2, &ns2, &longitude2, &ew2, &speed, &course, &date, &magneticVar, &mgEW, &mode) >= 1) { |
mafischl | 0:75e6a2e50519 | 112 | |
mafischl | 0:75e6a2e50519 | 113 | // CONVERT TO STRING AND FORMAT TIME AND DATE |
mafischl | 0:75e6a2e50519 | 114 | |
mafischl | 0:75e6a2e50519 | 115 | GPStime = time; |
mafischl | 0:75e6a2e50519 | 116 | GPSdate = date; |
mafischl | 0:75e6a2e50519 | 117 | |
mafischl | 0:75e6a2e50519 | 118 | } |
mafischl | 0:75e6a2e50519 | 119 | |
mafischl | 0:75e6a2e50519 | 120 | |
mafischl | 0:75e6a2e50519 | 121 | } |
mafischl | 0:75e6a2e50519 | 122 | } |
mafischl | 0:75e6a2e50519 | 123 | |
mafischl | 0:75e6a2e50519 | 124 | float GPS::trunc(float v) { |
mafischl | 0:75e6a2e50519 | 125 | if (v < 0.0) { |
mafischl | 0:75e6a2e50519 | 126 | v*= -1.0; |
mafischl | 0:75e6a2e50519 | 127 | v = floor(v); |
mafischl | 0:75e6a2e50519 | 128 | v*=-1.0; |
mafischl | 0:75e6a2e50519 | 129 | } else { |
mafischl | 0:75e6a2e50519 | 130 | v = floor(v); |
mafischl | 0:75e6a2e50519 | 131 | } |
mafischl | 0:75e6a2e50519 | 132 | return v; |
mafischl | 0:75e6a2e50519 | 133 | } |
mafischl | 0:75e6a2e50519 | 134 | |
mafischl | 0:75e6a2e50519 | 135 | void GPS::getline() { |
mafischl | 0:75e6a2e50519 | 136 | |
mafischl | 0:75e6a2e50519 | 137 | while (_gps.getc() != '$'); // wait for the start of a line |
mafischl | 0:75e6a2e50519 | 138 | // printf("entered the getline loop\n\r"); |
mafischl | 0:75e6a2e50519 | 139 | for (int i=0; i<256; i++) { |
mafischl | 0:75e6a2e50519 | 140 | msg[i] = _gps.getc(); |
mafischl | 0:75e6a2e50519 | 141 | if (msg[i] == '\r') { |
mafischl | 0:75e6a2e50519 | 142 | msg[i] = 0; |
mafischl | 0:75e6a2e50519 | 143 | return; |
mafischl | 0:75e6a2e50519 | 144 | } |
mafischl | 0:75e6a2e50519 | 145 | } |
mafischl | 0:75e6a2e50519 | 146 | error("Overflowed message limit"); |
mafischl | 0:75e6a2e50519 | 147 | } |
mafischl | 0:75e6a2e50519 | 148 | |
mafischl | 0:75e6a2e50519 | 149 | /* |
mafischl | 0:75e6a2e50519 | 150 | $GPRMC,000115.039,V,,,,,,,291006,,*2C |
mafischl | 0:75e6a2e50519 | 151 | $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52 |
mafischl | 0:75e6a2e50519 | 152 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
mafischl | 0:75e6a2e50519 | 153 | $GPGSV,3,1,12,20,00,000,,10,00,000,,31,00,000,,27,00,000,*7C |
mafischl | 0:75e6a2e50519 | 154 | $GPGSV,3,2,12,19,00,000,,07,00,000,,04,00,000,,24,00,000,*76 |
mafischl | 0:75e6a2e50519 | 155 | $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78 |
mafischl | 0:75e6a2e50519 | 156 | $GPRMC,000116.031,V,,,,,,,291006,,*27 |
mafischl | 0:75e6a2e50519 | 157 | $GPGGA,000117.035,,,,,0,00,,,M,0.0,M,,0000*57 |
mafischl | 0:75e6a2e50519 | 158 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
mafischl | 0:75e6a2e50519 | 159 | $GPRMC,000117.035,V,,,,,,,291006,,*22 |
mafischl | 0:75e6a2e50519 | 160 | $GPGGA,000118.039,,,,,0,00,,,M,0.0,M,,0000*54 |
mafischl | 0:75e6a2e50519 | 161 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
mafischl | 0:75e6a2e50519 | 162 | $GPRMC,000118.039,V,,,,,,,291006,,*21 |
mafischl | 0:75e6a2e50519 | 163 | $GPGGA,000119.035,,,,,0,00,,,M,0.0,M,,0000*59 |
mafischl | 0:75e6a2e50519 | 164 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
mafischl | 0:75e6a2e50519 | 165 | $GPRMC,000119.035,V,,,,,,,291006,,*2C |
mafischl | 0:75e6a2e50519 | 166 | $GPGGA,000120.037,,,,,0,00,,,M,0.0,M,,0000*51 |
mafischl | 0:75e6a2e50519 | 167 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E |
mafischl | 0:75e6a2e50519 | 168 | $GPRMC,000120.037,V,,,,,,,291006,,*24 |
mafischl | 0:75e6a2e50519 | 169 | |
mafischl | 0:75e6a2e50519 | 170 | */ |