Driver for Sparkfun GP-20U7 GPS module

Dependents:   GP20U7_HelloWorld gpsmpu60500 BaseStation healt_monitor_ECE4180

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 Serial pc1(USBTX, USBRX);
00026 
00027 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
00028     _gps.baud(9600);
00029       
00030     longitude = 0.0;
00031     latitude = 0.0;
00032     ns = 'N';
00033     ew = 'E';
00034     alt = 0.0;
00035             
00036 }
00037 
00038 int GPS::sample() {
00039     //float time, hori_dilute, alt,geoid;
00040    
00041     int lock;
00042     
00043     while(1) {        
00044         getline();
00045         // Check if it is a GPGGA msg (matches both locked and non-locked msg)
00046         //pc1.printf(msg);
00047         //pc1.printf("\n");
00048         wait(5);
00049         //pc1.printf("display msg\r\n");
00050         //pc1.printf(msg);
00051         //pc1.printf("\n");
00052         if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c,%f,%c", &time, &latitude, &ns, &longitude, &ew, &lock, &num_sat, &hori_dilute, &alt, &hu, &geoid, &gu/*, &age_diff, &diff_ID*/) >= 1) { 
00053             if(!lock) {
00054                 longitude = 0.0;
00055                 latitude = 0.0;  
00056                 ns = 'Z';
00057                 ew = 'Z';
00058                 alt = 0.0;      
00059                 return 0;
00060             } else {
00061                 //if(ns == 'S') {    latitude  *= -1.0; }
00062 //                if(ew == 'W') {    longitude *= -1.0; }
00063 //                float degrees = trunc(latitude / 100.0f);
00064 //                float minutes = latitude - (degrees * 100.0f);
00065 //                latitude = degrees + minutes / 60.0f;    
00066 //                degrees = trunc(longitude / 100.0f * 0.01f);
00067 //                minutes = longitude - (degrees * 100.0f);
00068 //                longitude = degrees + minutes / 60.0f;
00069 //                pc1.printf(msg);
00070                 //pc1.printf("\n\rlongitude is %f\n\r", longitude);
00071                 //pc1.printf("\n\rtime is %f\n\r", time);
00072                 //pc1.printf("ns is %c\n\r", ns);
00073                 //pc1.printf("ew is %c\n\r", ew);
00074                 //pc1.printf("alt is %f\n\r", alt);
00075                 
00076                 latitude /= 100;
00077                 longitude /= 100;
00078                 
00079                 return 1;
00080             }
00081         
00082         }
00083         return 0;
00084     }
00085 }
00086 
00087 float GPS::trunc(float v) {
00088     if(v < 0.0) {
00089         v*= -1.0;
00090         v = floor(v);
00091         v*=-1.0;
00092     } else {
00093         v = floor(v);
00094     }
00095     return v;
00096 }
00097 
00098 void GPS::getline() {
00099     int i;
00100     char a;
00101     int n;
00102     while(1) {
00103     strcpy(msg, '\0');
00104     //pc1.printf("\r\n\n New Message: ");
00105 //    pc1.printf(msg);
00106 //    pc1.printf("\r\n\n");    
00107     i = 0;
00108     a = _gps.getc();
00109     msg[i] = a;
00110     if (a == '$') {
00111         //pc1.printf("%c",a);
00112         a = _gps.getc();
00113         msg[i] = a;
00114         i++;
00115         if (a == 'G') {
00116             //pc1.printf("%c",a);
00117             a = _gps.getc();
00118             msg[i] = a;
00119             i++;
00120             if (a == 'P') {
00121                 //pc1.printf("%c",a);
00122                 a = _gps.getc();
00123                 msg[i] = a;
00124                 i++;
00125                 if (a == 'G') {
00126                     //pc1.printf("%c",a);
00127                     a = _gps.getc();
00128                     msg[i] = a;
00129                     i++;
00130                     if (a == 'G') {
00131                         //pc1.printf("%c",a);
00132                         a = _gps.getc();
00133                         msg[i] = a;
00134                         i++;
00135                         if (a == 'A') {
00136                             //pc1.printf("%c",a);
00137                             //a = _gps.getc();
00138                             //msg[i] = a;
00139                             //pc1.printf(msg);
00140                             pc1.printf("\r\n");
00141         
00142                             for (n = 5; n < 456; n++) {
00143                                msg[n] = _gps.getc();
00144                                pc1.printf("%c", msg[n]);
00145                                if(msg[n] == '\r') {
00146                                  msg[n] = '0';
00147                                  return;
00148                                 }
00149                             }
00150                          }
00151                      }
00152                  }
00153             }
00154        }
00155        }                          
00156 //    while(_gps.getc() != '$') {
00157 //        //char a = _gps.getc();
00158 //        for(i = 0; i < 256; i++) {
00159 //            msg[i] = _gps.getc();
00160 //            pc1.printf("%c", msg[i]);
00161 //            if(msg[i] == '\r') {
00162 //             msg[i] = 0;
00163 //             return;
00164 //                }
00165 //                }
00166 //          
00167 //         
00168 //    }
00169 //    while(_gps.getc() != '$');    // wait for the start of a line
00170 //    for(int i=0; i<256; i++) {
00171 //        msg[i] = _gps.getc();
00172 //        if(msg[i] == '\r') {
00173 //            msg[i] = 0;
00174 //            return;
00175 //        }
00176 //    }
00177 //    error("Overflowed message limit");
00178 }
00179 }