Weather Station using an mBed microcontroller and a Windows CE Device

Dependencies:   TextLCD mbed HMC6352

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GPS.cpp Source File

GPS.cpp

00001 /* mbed EM-406 GPS Module Library
00002  * Copyright (c) 2008-2010, sford
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #include "GPS.h"
00024 
00025 
00026 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
00027     _gps.baud(4800);
00028     longitude = 0.0;
00029     latitude = 0.0;
00030 }
00031 
00032 int GPS::sample() {
00033     float time2, time, date;
00034     float latitude2, longitude2, speed, course, magneticVar;
00035     char ns, ew, ns2, ew2, mgEW, mode, status;
00036     
00037 
00038     //return 1; //testing by Jigar
00039     while (1) {
00040         getline();
00041         //printf("\n\rentered the GPS.sample while loop \n\r %s\n\r", msg);
00042         
00043         
00044         // Check if it is a GPGGA msg (matches both locked and non-locked msg)
00045         // $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52
00046         /*
00047         eg3. $GPGGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh
00048                         1       2     3  4       5 6  7  8   9  10 11 12 13  14   15
00049             1    = UTC of Position
00050             2    = Latitude
00051             3    = N or S
00052             4    = Longitude
00053             5    = E or W
00054             6    = GPS quality indicator (0=invalid; 1=GPS fix; 2=Diff. GPS fix)
00055             7    = Number of satellites in use [not those in view]
00056             8    = Horizontal dilution of position
00057             9    = Antenna altitude above/below mean sea level (geoid)
00058             10   = Meters  (Antenna height unit)
00059             11   = Geoidal separation (Diff. between WGS-84 earth ellipsoid and
00060                    mean sea level.  -=geoid is below WGS-84 ellipsoid)
00061             12   = Meters  (Units of geoidal separation)
00062             13   = Age in seconds since last update from diff. reference station
00063             14   = Diff. reference station ID#
00064             15   = Checksum
00065 
00066         */
00067        // 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) {
00068         if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f", &time2, &latitude, &ns, &longitude, &ew, &lock, &numSat, &horDil, &altitude) >= 1) { 
00069 
00070             if (!lock) {
00071                 longitude = 0.0;
00072                 latitude = 0.0;
00073                 return 0;
00074             } else {
00075                 if (ns == 'S') {
00076                     latitude  *= -1.0;
00077                 }
00078                 if (ew == 'W') {
00079                     longitude *= -1.0;
00080                 }
00081                 float degrees = trunc(latitude / 100.0f);
00082                 float minutes = latitude - (degrees * 100.0f);
00083                 latitude = degrees + minutes / 60.0f;
00084                 degrees = trunc(longitude / 100.0f ); //degrees = trunc(longitude / 100.0f * 0.01f);
00085                 minutes = longitude - (degrees * 100.0f);
00086                 longitude = degrees + minutes / 60.0f;
00087                 return 1;
00088             }
00089         }  
00090         
00091         // Check if it is a GPRMC msg (matches both locked and non-locked msg)
00092         /*
00093         $GPRMC,hhmmss.ss,a,llll.ll,N,llll.ll,E,x.x,x.x,ddmmyy,x.x,E,A*hh
00094                    1     2    3    4    5    6  7   8    9    10 11 12 13
00095             1    = UTC of Position
00096             2    = Status
00097             3    = Latitude
00098             4    = N or S
00099             5    = Longitude
00100             6    = E or W
00101             7    = Speed Over Ground (knots)
00102             8    = Course Over Ground (degrees)
00103             9    = Date (ddmmyy)
00104             10   = Magnetic Variation (degrees)
00105             11   = E or W (for magnetic variation)
00106             12   = Mode (A = Autonomous, D = DGPS, E = DR)
00107             13   = Checksum
00108 
00109         */
00110         // $GPRMC,161229.487,A,3723.2475,N,12158.3416,W,0.13,309.62,120598, ,*10
00111         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) { 
00112         
00113         // CONVERT TO STRING AND FORMAT TIME AND DATE
00114         
00115         GPStime = time;
00116         GPSdate = date;
00117   
00118         }
00119         
00120         
00121     }
00122 }
00123 
00124 float GPS::trunc(float v) {
00125     if (v < 0.0) {
00126         v*= -1.0;
00127         v = floor(v);
00128         v*=-1.0;
00129     } else {
00130         v = floor(v);
00131     }
00132     return v;
00133 }
00134 
00135 void GPS::getline() {
00136     
00137     while (_gps.getc() != '$');   // wait for the start of a line
00138     // printf("entered the getline loop\n\r");
00139     for (int i=0; i<256; i++) {
00140         msg[i] = _gps.getc();
00141         if (msg[i] == '\r') {
00142             msg[i] = 0;
00143             return;
00144         }
00145     }
00146     error("Overflowed message limit");
00147 }
00148 
00149 /*
00150 $GPRMC,000115.039,V,,,,,,,291006,,*2C
00151 $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52
00152 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00153 $GPGSV,3,1,12,20,00,000,,10,00,000,,31,00,000,,27,00,000,*7C
00154 $GPGSV,3,2,12,19,00,000,,07,00,000,,04,00,000,,24,00,000,*76
00155 $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78
00156 $GPRMC,000116.031,V,,,,,,,291006,,*27
00157 $GPGGA,000117.035,,,,,0,00,,,M,0.0,M,,0000*57
00158 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00159 $GPRMC,000117.035,V,,,,,,,291006,,*22
00160 $GPGGA,000118.039,,,,,0,00,,,M,0.0,M,,0000*54
00161 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00162 $GPRMC,000118.039,V,,,,,,,291006,,*21
00163 $GPGGA,000119.035,,,,,0,00,,,M,0.0,M,,0000*59
00164 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00165 $GPRMC,000119.035,V,,,,,,,291006,,*2C
00166 $GPGGA,000120.037,,,,,0,00,,,M,0.0,M,,0000*51
00167 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00168 $GPRMC,000120.037,V,,,,,,,291006,,*24
00169 
00170 */