Rune L / SerialGPS
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialGPS.cpp Source File

SerialGPS.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 "SerialGPS.h"
00025 
00026 SerialGPS::SerialGPS(PinName tx, PinName rx, int baud) 
00027     : 
00028     _gps_p(new BufferedSerial(tx, rx, baud)),
00029     _gps(*_gps_p)
00030 {
00031     this->_gps_p->set_baud(baud);
00032     this->_UTCtime    = 0.0;
00033     this->_lat        = 0.0;
00034     this->_lon        = 0.0;
00035     this->_lock       = 0;
00036     this->_fix        = false;
00037 }
00038 
00039 SerialGPS::SerialGPS(BufferedSerial* serial)
00040     :
00041     _gps_p(serial),
00042     _gps(*_gps_p)
00043 {
00044     this->_UTCtime    = 0.0;
00045     this->_lat        = 0.0;
00046     this->_lon        = 0.0;
00047     this->_lock       = 0;
00048     this->_fix        = false;
00049 }
00050 
00051 bool SerialGPS::fix()
00052 {
00053     return this->_fix;
00054 }
00055 
00056 float SerialGPS::get_lat()
00057 {
00058     return this->_lat;
00059 }
00060 
00061 float SerialGPS::get_lon()
00062 {
00063     return this->_lon;
00064 }
00065 
00066 void SerialGPS::send_command(const char* cmd)
00067 {
00068     this->_gps_p->write(cmd, sizeof(cmd));
00069 }
00070 
00071 void SerialGPS::read() 
00072 {
00073         getline();
00074 
00075         //printf("NMEA sentence: %s\n", this->_nmea);
00076 
00077         // GPRMC sentence: UTC time, status, lat, N/S, lon, E/W, speed, course, date
00078         if(sscanf(this->_nmea, "GPRMC,%f,%c,%f,%c,%f,%c", &this->_UTCtime, &this->_status, &this->_lat, &this->_NS, &this->_lon, &this->_EW) > 0)
00079         {
00080             if(this->_status != 'A')
00081             {
00082                 this->_fix = false;
00083             }
00084             else
00085             {
00086                 set_vals();
00087             }
00088         }
00089         // GPGGA sentence: UTC time, lat, N/S, lon, E/W, fix indicator
00090         else if(sscanf(this->_nmea, "GPGGA,%f,%f,%c,%f,%c,%d", &this->_UTCtime, &this->_lat, &this->_NS, &this->_lon, &this->_EW, &this->_lock) > 0)
00091         {
00092             if(!this->_lock)
00093             {
00094                 this->_fix = false;
00095             }
00096             else 
00097             {
00098                 set_vals();
00099             }
00100         }
00101         // GPGLL sentence: lat, N/S, lo, E/W, UTC time, status
00102         else if(sscanf(this->_nmea, "GPGLL,%f,%c,%f,%c,%f,%c", &this->_lat, &this->_NS, &this->_lon, &this->_EW, &this->_UTCtime, &this->_status) > 0)
00103         {
00104             if(this->_status != 'A')
00105             {
00106                 this->_fix = false;
00107             }
00108             else 
00109             {
00110                 set_vals();
00111             }
00112         }
00113         else
00114         {
00115             this->_fix = false;
00116         }
00117 
00118     // Clear the NMEA buffer
00119     memset(this->_nmea, 0, strlen(this->_nmea));
00120 }
00121 
00122 void SerialGPS::set_vals()
00123 {
00124 
00125     this->_lat = convert(this->_lat);
00126     this->_lon = convert(this->_lon);
00127 
00128     if(this->_NS == 'S') { this->_lat *= -1.0; }
00129     if(this->_EW == 'W') { this->_lon *= -1.0; }
00130 
00131     this->_fix = true;
00132 }
00133 
00134 void SerialGPS::getline() 
00135 {
00136     char ch;
00137     uint8_t idx = 0;
00138 
00139     //MBED_ASSERT(this->_gps->readable());
00140 
00141     // Wait for start of sentence
00142     while(read_char() != '$');
00143 
00144     // Read character until EOL
00145     while((ch = read_char()) != '\r')
00146     {
00147         this->_nmea[idx] = ch;
00148         idx++;
00149     }
00150 }