Forked para SNOCC

Dependents:   RA8875

Fork of GPS by SNOCC

GPS.cpp

Committer:
gstedile
Date:
2017-04-04
Revision:
4:a0760d64f0f8
Parent:
3:f9327dc1cc11

File content as of revision 4:a0760d64f0f8:

/* mbed EM-406 GPS Module Library
 * Copyright (c) 2008-2010, sford
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
 
#include "GPS.h"

GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {   // En PinNames.h (https://developer.mbed.org/users/screamer/code/mbed/file/667d61c9177b/PinNames.h) se define: typedef enum PinName PinName;
    _gps.baud(9600);                                // Cambio baudrate a 9600
    longitude = 0.0;
    latitude = 0.0; 
       
}


int GPS::sample() {
    //float time;
    char ns, ew, signal;  // Agrego signal para parsear el campo NMEA que indica es estado de señal.
    int lock;
    

    while(1) {        
        getline();
        
        

        /*
        GGA - essential fix data which provide 3D location and accuracy data.

 $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47

Where:
     GGA          Global Positioning System Fix Data
     123519       Fix taken at 12:35:19 UTC
     4807.038,N   Latitude 48 deg 07.038' N
     01131.000,E  Longitude 11 deg 31.000' E
     1            Fix quality: 0 = invalid
                               1 = GPS fix (SPS)
                               2 = DGPS fix
                               3 = PPS fix
                   4 = Real Time Kinematic
                   5 = Float RTK
                               6 = estimated (dead reckoning) (2.3 feature)
                   7 = Manual input mode
                   8 = Simulation mode
     08           Number of satellites being tracked
     0.9          Horizontal dilution of position
     545.4,M      Altitude, Meters, above mean sea level
     46.9,M       Height of geoid (mean sea level) above WGS84
                      ellipsoid
     (empty field) time in seconds since last DGPS update
     (empty field) DGPS station ID number
     *47          the checksum data, always begins with *
     */
        
       
     /* 
       sscanf(msg, "GN %s",mensaje1);
       sscanf(msg, "GP,%s", mensaje2);
       sscanf(msg, "GSV,%s", mensaje3);
       sscanf(msg, "G,%s", mensaje4);
       */
       //              $GPRMC,194530.000,A,3051.8007,N,10035.9989,W,1.49,111.67,310714,,,A*74      Esta es la trama GP. En nuestro caso es GN:

       if(sscanf(msg, "GNRMC, %f,%c,%f,%c,%f,%c,%f", &time, &signal, &latitude, &ns, &longitude, &ew, &speed) >=1){
     // if(sscanf(msg, "GNRMC, %f,%c,%f,%c,%f,%c,%f,%f,%f", &time, &signal, &latitude, &ns, &longitude, &ew, &speed, &nosequees,&fecha) >=1){  // SE AGREGA CAMPO NOSEQUEES y FECHA; Declarar variable!!!
       // Agrego las siguientes lineas porque originalmente se analizaba el mensaje GGA y se evaluaba "lock" para saber si habia datos validos
       // y ahora evaluo si la señal (signal) está Available (A) o Void (V). Lo dejo asi por si se retorna a GGA.
           
            if(signal == 'A'){
                lock = 1;
                }
            else {
                lock = 0;
                }
              
     // if(sscanf(msg, "GNGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) { 
        
           
            
             if(!lock) { 
               
                longitude = 0.0;
                latitude = 0.0;    
                speed = 0.0;    // 
                time=0.0;
                return 0;
            } 
          
            else {
                if(ns == 'S') {    latitude  *= -1.0; }
                if(ew == 'W') {    longitude *= -1.0; }
                float degrees = trunc(latitude / 100.0f);      // El formato del campo es GGMM.MMMM: Divido por 100 y tomo parte entera para los grados.
                float minutes = latitude - (degrees * 100.0f); // Resto los grados y me quedo con los minutos
                latitude = degrees + minutes / 60.0f;          // Convierto a decimal sumando grados y dividiendo entre 60 los minutos 
                //degrees = trunc(longitude / 100.0f * 0.01f); // Corrijo esta linea porque es como dividir por 10000.
                degrees = trunc(longitude / 100.0f);           // Repito para la latitud.
                minutes = longitude - (degrees * 100.0f);
                longitude = degrees + minutes / 60.0f;
                speed *= 1.852000f;                            // Convierto nudos  a km/h
  
                return 1;
                
            }
        }
         
    }
}

float GPS::trunc(float v) {
    if(v < 0.0) {
        v*= -1.0;
        v = floor(v);
        v*=-1.0;
    } else {
        v = floor(v);
    }
    return v;
}

void GPS::getline() {
    while(_gps.getc() != '$');    // wait for the start of a line
    for(int i=0; i<256; i++) {
        msg[i] = _gps.getc();
        if(msg[i] == '\r') {
            msg[i] = 0;
            return;
        }
    }
    error("Overflowed message limit");
} 

/*void GPS::getline() {
    if(_gps.getc() != '$') return;    // exit if there isn't start of a line
    for(int i=0; i<256; i++) {
        msg[i] = _gps.getc();
        if(msg[i] == '\r') {
            msg[i] = 0;
            return;
        }
    }
    error("Overflowed message limit");
}*/