Library for ublox neo 6m

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GPS.cpp Source File

GPS.cpp

00001 /* mbed GPS Module Library
00002  * Copyright (c) 2008-2010, sford
00003  * Copyright (c) 2013, B.Adryan
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  */
00023 
00024 #include "GPS.h"
00025 
00026 GPS::GPS(PinName tx, PinName rx, int Baud) : _gps(tx, rx)
00027 {
00028     _gps.baud(Baud);
00029     longitude = 0.0;
00030     latitude = 0.0;
00031 }
00032 
00033 int GPS::sample()
00034 {
00035 
00036     int lock;
00037 
00038     while(1) {
00039         getline();
00040 
00041         // Check if it is a GPGGA msg (matches both locked and non-locked msg)
00042         if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c,%f", &time, &latitude, &ns, &longitude, &ew, &lock, &sats, &hdop, &alt, &unit, &geoid) >= 1) {
00043             if(!lock) {
00044                 time = 0.0;
00045                 longitude = 0.0;
00046                 latitude = 0.0;
00047                 sats = 0;
00048                 hdop = 0.0;
00049                 alt = 0.0;
00050                 geoid = 0.0;
00051                 return 0;
00052             } else {
00053                 //GPGGA format according http://aprs.gids.nl/nmea/#gga
00054                 // time (float), lat (f), (N/S) (c), long (f), (E/W) (c), fix (d), sats (d),
00055                 // hdop (float), altitude (float), M, geoid (float), M, , ,
00056                 //GPGGA,092010.000,5210.9546,N,00008.8913,E,1,07,1.3,9.7,M,47.0,M,,0000*5D
00057 
00058                 //format utc time to beijing time,add 8 time zone
00059                 time = time + 80000.00f;
00060                 hour = int(time) / 10000;
00061                 minute = (int(time) % 10000) / 100;
00062                 seconed = int(time) % 100;
00063             
00064                 return 1;
00065             }
00066         }
00067     }
00068 }
00069 
00070 float GPS::trunc(float v)
00071 {
00072     if(v < 0.0) {
00073         v*= -1.0;
00074         v = floor(v);
00075         v*=-1.0;
00076     } else {
00077         v = floor(v);
00078     }
00079     return v;
00080 }
00081 
00082 void GPS::getline()
00083 {
00084     while(_gps.getc() != '$');    // wait for the start of a line
00085     for(int i=0; i<256; i++) {
00086         msg[i] = _gps.getc();
00087         if(msg[i] == '\r') {
00088             msg[i] = 0;
00089             return;
00090         }
00091     }
00092     error("Overflowed message limit");
00093 }
00094