Library for ublox neo 6m

Dependents:   GPS_classroom wifigpslcd GPS_classroom GPS_classroom2 ... more

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 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
00026     //_gps.baud(4800);
00027     longitude = 0.0;
00028     latitude = 0.0;
00029 }
00030 
00031 int GPS::sample() {
00032     float time;
00033     char ns, ew;
00034     int lock;
00035 
00036     //return 1; //testing by Jigar
00037     while (1) {
00038         getline();
00039         //printf("\n\rentered the GPS.sample while loop \n\r %s\n\r", msg);
00040         
00041         
00042         // Check if it is a GPGGA msg (matches both locked and non-locked msg)
00043         // $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52
00044         /*
00045         eg3. $GPGGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh
00046                         1       2     3  4       5 6  7  8   9  10 11 12 13  14   15
00047             1    = UTC of Position
00048             2    = Latitude
00049             3    = N or S
00050             4    = Longitude
00051             5    = E or W
00052             6    = GPS quality indicator (0=invalid; 1=GPS fix; 2=Diff. GPS fix)
00053             7    = Number of satellites in use [not those in view]
00054             8    = Horizontal dilution of position
00055             9    = Antenna altitude above/below mean sea level (geoid)
00056             10   = Meters  (Antenna height unit)
00057             11   = Geoidal separation (Diff. between WGS-84 earth ellipsoid and
00058                    mean sea level.  -=geoid is below WGS-84 ellipsoid)
00059             12   = Meters  (Units of geoidal separation)
00060             13   = Age in seconds since last update from diff. reference station
00061             14   = Diff. reference station ID#
00062             15   = Checksum
00063 
00064         */
00065        // 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) {
00066         if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) { 
00067 
00068             if (!lock) 
00069             {
00070                 longitude = 0.0;
00071                 latitude = 0.0;
00072                 utc=0.0;
00073                 return 0;
00074             } 
00075             else 
00076             {
00077                 if (ns == 'S') {
00078                     latitude  *= -1.0;
00079                 }
00080                 if (ew == 'W') {
00081                     longitude *= -1.0;
00082                 }
00083                 float degrees = trunc(latitude / 100.0f);
00084                 float minutes = latitude - (degrees * 100.0f);
00085                 latitude = degrees + minutes / 60.0f;
00086                  degrees = trunc(longitude / 100.0f ); //degrees = trunc(longitude / 100.0f * 0.01f);
00087                 minutes = longitude - (degrees * 100.0f);
00088                 longitude = degrees + minutes / 60.0f;
00089                 utc=time;
00090                 return 1;
00091             }
00092         }  
00093         return 0;
00094     }
00095 }
00096 
00097 float GPS::trunc(float v) {
00098     if (v < 0.0) {
00099         v*= -1.0;
00100         v = floor(v);
00101         v*=-1.0;
00102     } else {
00103         v = floor(v);
00104     }
00105     return v;
00106 }
00107 
00108 void GPS::getline() {
00109     char c;
00110     while (c = _gps.getc() != '$');   // wait for the start of a line
00111     // printf("entered the getline loop\n\r");
00112     for (int i=0; i<256; i++) {
00113         msg[i] = _gps.getc();
00114         if (msg[i] == '\r') {
00115             msg[i] = 0;
00116             return;
00117         }
00118     }
00119     error("Overflowed message limit");
00120 }
00121 
00122 /*
00123 $GPRMC,000115.039,V,,,,,,,291006,,*2C
00124 $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52
00125 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00126 $GPGSV,3,1,12,20,00,000,,10,00,000,,31,00,000,,27,00,000,*7C
00127 $GPGSV,3,2,12,19,00,000,,07,00,000,,04,00,000,,24,00,000,*76
00128 $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78
00129 $GPRMC,000116.031,V,,,,,,,291006,,*27
00130 $GPGGA,000117.035,,,,,0,00,,,M,0.0,M,,0000*57
00131 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00132 $GPRMC,000117.035,V,,,,,,,291006,,*22
00133 $GPGGA,000118.039,,,,,0,00,,,M,0.0,M,,0000*54
00134 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00135 $GPRMC,000118.039,V,,,,,,,291006,,*21
00136 $GPGGA,000119.035,,,,,0,00,,,M,0.0,M,,0000*59
00137 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00138 $GPRMC,000119.035,V,,,,,,,291006,,*2C
00139 $GPGGA,000120.037,,,,,0,00,,,M,0.0,M,,0000*51
00140 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00141 $GPRMC,000120.037,V,,,,,,,291006,,*24
00142 
00143 */