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