Calcula bien la latitud y la longitud teniendo en cuenta los conversión de grados, minutos y segundos a grados decimales.

Dependents:   TAREA_4_GPS_GSM Tarea6_GSM Tarea6_GPSGSM TareaGSM ... more

Fork of GPS7 by Gustavo Ramirez

Committer:
joshema216
Date:
Fri Nov 18 21:25:45 2016 +0000
Revision:
3:7f621626d7e9
Parent:
2:8d7c7165ffe2
Version 2.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:15611c7938a3 1 /* mbed EM-406 GPS Module Library
simon 0:15611c7938a3 2 * Copyright (c) 2008-2010, sford
simon 0:15611c7938a3 3 *
simon 0:15611c7938a3 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:15611c7938a3 5 * of this software and associated documentation files (the "Software"), to deal
simon 0:15611c7938a3 6 * in the Software without restriction, including without limitation the rights
simon 0:15611c7938a3 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:15611c7938a3 8 * copies of the Software, and to permit persons to whom the Software is
simon 0:15611c7938a3 9 * furnished to do so, subject to the following conditions:
simon 0:15611c7938a3 10 *
simon 0:15611c7938a3 11 * The above copyright notice and this permission notice shall be included in
simon 0:15611c7938a3 12 * all copies or substantial portions of the Software.
simon 0:15611c7938a3 13 *
simon 0:15611c7938a3 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:15611c7938a3 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:15611c7938a3 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:15611c7938a3 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:15611c7938a3 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:15611c7938a3 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:15611c7938a3 20 * THE SOFTWARE.
simon 0:15611c7938a3 21 */
simon 0:15611c7938a3 22
simon 0:15611c7938a3 23 #include "GPS.h"
simon 0:15611c7938a3 24
simon 0:15611c7938a3 25 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
tony63 1:df8295aa2b2b 26 _gps.baud(9600);
simon 0:15611c7938a3 27 longitude = 0.0;
simon 0:15611c7938a3 28 latitude = 0.0;
simon 0:15611c7938a3 29 }
simon 0:15611c7938a3 30
simon 0:15611c7938a3 31 int GPS::sample() {
simon 0:15611c7938a3 32 float time;
simon 0:15611c7938a3 33 char ns, ew;
simon 0:15611c7938a3 34 int lock;
joshema216 3:7f621626d7e9 35 int degla;
joshema216 3:7f621626d7e9 36 float minla;
joshema216 3:7f621626d7e9 37 int deglo;
joshema216 3:7f621626d7e9 38 float minlo;
joshema216 3:7f621626d7e9 39
simon 0:15611c7938a3 40 while(1) {
simon 0:15611c7938a3 41 getline();
joshema216 3:7f621626d7e9 42 //$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
simon 0:15611c7938a3 43 // Check if it is a GPGGA msg (matches both locked and non-locked msg)
simon 0:15611c7938a3 44 if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) {
tony63 2:8d7c7165ffe2 45 if(!lock) { //si lock es 1 hay lecturas ok
simon 0:15611c7938a3 46 longitude = 0.0;
simon 0:15611c7938a3 47 latitude = 0.0;
simon 0:15611c7938a3 48 return 0;
tony63 2:8d7c7165ffe2 49 }
joshema216 3:7f621626d7e9 50 else {
simon 0:15611c7938a3 51 if(ns == 'S') { latitude *= -1.0; }
simon 0:15611c7938a3 52 if(ew == 'W') { longitude *= -1.0; }
tony63 2:8d7c7165ffe2 53 latitude = (latitude / 100.0f);
joshema216 3:7f621626d7e9 54 degla = (int)latitude;
joshema216 3:7f621626d7e9 55 minla = latitude - (degla);
joshema216 3:7f621626d7e9 56 latitude = degla + minla/60.0f*100.0f;
tony63 2:8d7c7165ffe2 57 //float minutes = latitude - (degrees * 100.0f);
tony63 2:8d7c7165ffe2 58 //latitude = degrees + minutes / 60.0f;
tony63 2:8d7c7165ffe2 59 longitude = (longitude / 100.0f);
joshema216 3:7f621626d7e9 60 deglo = (int)longitude;
joshema216 3:7f621626d7e9 61 minlo = longitude - (deglo);
joshema216 3:7f621626d7e9 62 longitude = deglo + minlo/60.0f*100.0f;
tony63 2:8d7c7165ffe2 63 //minutes = longitude - (degrees * 100.0f);
tony63 2:8d7c7165ffe2 64 //longitude = degrees + minutes / 60.0f;
tony63 2:8d7c7165ffe2 65
simon 0:15611c7938a3 66 return 1;
simon 0:15611c7938a3 67 }
simon 0:15611c7938a3 68 }
simon 0:15611c7938a3 69 }
simon 0:15611c7938a3 70 }
simon 0:15611c7938a3 71
tony63 2:8d7c7165ffe2 72 /*
simon 0:15611c7938a3 73 float GPS::trunc(float v) {
simon 0:15611c7938a3 74 if(v < 0.0) {
simon 0:15611c7938a3 75 v*= -1.0;
simon 0:15611c7938a3 76 v = floor(v);
simon 0:15611c7938a3 77 v*=-1.0;
simon 0:15611c7938a3 78 } else {
simon 0:15611c7938a3 79 v = floor(v);
simon 0:15611c7938a3 80 }
simon 0:15611c7938a3 81 return v;
simon 0:15611c7938a3 82 }
tony63 2:8d7c7165ffe2 83 */
simon 0:15611c7938a3 84 void GPS::getline() {
simon 0:15611c7938a3 85 while(_gps.getc() != '$'); // wait for the start of a line
simon 0:15611c7938a3 86 for(int i=0; i<256; i++) {
simon 0:15611c7938a3 87 msg[i] = _gps.getc();
simon 0:15611c7938a3 88 if(msg[i] == '\r') {
simon 0:15611c7938a3 89 msg[i] = 0;
simon 0:15611c7938a3 90 return;
simon 0:15611c7938a3 91 }
simon 0:15611c7938a3 92 }
simon 0:15611c7938a3 93 error("Overflowed message limit");
simon 0:15611c7938a3 94 }