Gustavo Ramirez / GPS_G

Dependents:   GoogleMapa

Fork of GPS_G by joshema 216

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(9600);    
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     int degla;
00036     float minla;
00037     int deglo;
00038     float minlo;
00039     
00040     while(1) {        
00041         getline();
00042         //$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
00043         // Check if it is a GPGGA msg (matches both locked and non-locked msg)
00044         if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) { 
00045             if(!lock) {   //si lock es 1  hay lecturas ok
00046                 longitude = 0.0;
00047                 latitude = 0.0;        
00048                 return 0;
00049             } 
00050             else {
00051                 if(ns == 'S') {    latitude  *= -1.0; }
00052                 if(ew == 'W') {    longitude *= -1.0; }
00053                 latitude = (latitude / 100.0f);//ok asi se hace
00054                 degla = (int)latitude;
00055                 minla = latitude - (degla);
00056                 latitude = degla + minla/60.0f*100.0f;
00057                 //float minutes = latitude - (degrees * 100.0f);
00058                 //latitude = degrees + minutes / 60.0f;    
00059                 longitude = (longitude / 100.0f);//ok asi se hace
00060                 deglo = (int)longitude;
00061                 minlo = longitude - (deglo);
00062                 longitude = deglo + minlo/60.0f*100.0f;
00063                 //minutes = longitude - (degrees * 100.0f);
00064                 //longitude = degrees + minutes / 60.0f;
00065                 
00066                 return 1;
00067             }
00068         }
00069     }
00070 }
00071 
00072 /*
00073 float GPS::trunc(float v) {
00074     if(v < 0.0) {
00075         v*= -1.0;
00076         v = floor(v);
00077         v*=-1.0;
00078     } else {
00079         v = floor(v);
00080     }
00081     return v;
00082 }
00083 */
00084 void GPS::getline() {
00085     while(_gps.getc() != '$');    // wait for the start of a line
00086     for(int i=0; i<256; i++) {
00087         msg[i] = _gps.getc();
00088         if(msg[i] == '\r') {
00089             msg[i] = 0;
00090             return;
00091         }
00092     }
00093     error("Overflowed message limit");
00094 }