Quang Anh Le / GPS

Dependents:   GPS-Tracking-Velo

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(4800);
00027     longitude = 0.0;
00028     latitude = 0.0;
00029     altitude = 0.0;
00030     
00031 }
00032 
00033 int GPS::sample() {
00034     float time,horizontal;
00035     float time2,longitude2,latitude2,Speed,track;
00036     char  ns,ew,aut,ac,ns2,ew2;
00037     //int data;
00038     //float Hdop, Pdop;
00039     char Aut;
00040     //char star;
00041     //int check_data;
00042     //char  Pdop;
00043     //int   fix_3d;
00044     //int   Id1,Id2,Id3,Id4,Id5,Id6,Id7,Id8,Id9,Id10,Id11,Id12;
00045 
00046     //return 1; //testing by Jigar
00047     while (1) {
00048         getline();
00049         //printf("\n\rentered the GPS.sample while loop \n\r %s\n\r", msg);
00050         
00051         
00052         /* $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
00053 
00054      Where:
00055      GGA          Global Positioning System Fix Data
00056      123519       Fix taken at 12:35:19 UTC
00057      4807.038,N   Latitude 48 deg 07.038' N
00058      01131.000,E  Longitude 11 deg 31.000' E
00059      1            Fix quality: 0 = invalid
00060                                1 = GPS fix (SPS)
00061                                2 = DGPS fix
00062                                3 = PPS fix
00063                    4 = Real Time Kinematic
00064                    5 = Float RTK
00065                                6 = estimated (dead reckoning) (2.3 feature)
00066                    7 = Manual input mode
00067                    8 = Simulation mode
00068      08           Number of satellites being tracked
00069      0.9          Horizontal dilution of position
00070      545.4,M      Altitude, Meters, above mean sea level
00071      46.9,M       Height of geoid (mean sea level) above WGS84
00072                       ellipsoid
00073      (empty field) time in seconds since last DGPS update
00074      (empty field) DGPS station ID number
00075      *47          the checksum data, always begins with **/
00076        // if (sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%*d,%*d, %*f,%*f,%*f,%*f,%*f,%*f,%d ", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) {
00077         if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f", &time, &latitude, &ns, &longitude, &ew, &fix,& sat_tracked,&horizontal,&altitude) >= 1) { 
00078 
00079             if (!fix) 
00080             {
00081                 longitude = 0.0;
00082                 latitude = 0.0;
00083                 altitude = 0.0;
00084                 utc=0.0;
00085                 sat_tracked=0;
00086                 return 0;
00087             } 
00088             else 
00089             {
00090                 if (ns == 'S') {
00091                     latitude  *= -1.0;
00092                 }
00093                 if (ew == 'W') {
00094                     longitude *= -1.0;
00095                 }
00096                 float degrees = trunc(latitude / 100.0f);
00097                 float minutes = latitude - (degrees * 100.0f);
00098                 latitude = degrees + minutes / 60.0f;
00099                 degrees = trunc(longitude / 100.0f ); //degrees = trunc(longitude / 100.0f * 0.01f);
00100                 minutes = longitude - (degrees * 100.0f);
00101                 longitude = degrees + minutes / 60.0f;
00102                 /*int hour = time/10000;
00103                 int min = (time % 10000)/100;
00104                 int sek = (time % 100);*/
00105                 utc=time;
00106                 //sat_use=sat;
00107                 //altitude = altitude;
00108                 //fix=lock;
00109                 return 1;
00110             }
00111         } 
00112        //getline();
00113        /** $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39
00114 
00115      Where:
00116      GSA      Satellite status
00117      A        Auto selection of 2D or 3D fix (M = manual) 
00118      3        3D fix - values include: 1 = no fix
00119                                        2 = 2D fix
00120                                        3 = 3D fix
00121      04,05... PRNs of satellites used for fix (space for 12) 
00122      2.5      PDOP (dilution of precision) 
00123      1.3      Horizontal dilution of precision (HDOP) 
00124      2.1      Vertical dilution of precision (VDOP)
00125      *39      the checksum data, always begins with **/
00126      //getline2();
00127        /*if(sscanf(msg, "GPGSA,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%f,%f",&fix_3d,&id1,&id2,&id3,&id4,&id5,&id6,&id7,&id8,&id9,&id10,&id11,&id12,&Pdop,&Hdop)>=1);
00128        {
00129            
00130             if (fix_3d==1) 
00131             {
00132                 id1=id2=id3=id4=id5=id6=id7=id8=id9=id10=id11=id12=0;
00133                 pdop=0.0;
00134                 hdop=0.0;
00135                 //vdop=0.0;
00136                 
00137                 return 0;
00138             } 
00139             else
00140             {
00141                 pdop=Pdop;
00142                 hdop=Hdop;
00143                 //vhop=Vhop;
00144                 //aut=Aut;
00145                 return 1;
00146                 
00147             }
00148        }*/
00149         if(sscanf(msg, "GPGSA,%c,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &Aut, &fix_3d,&id1,&id2,&id3,&id4,&id5,&id6,&id7,&id8,&id9,&id10,&id11,&id12,&id13) >= 1) 
00150        {
00151            
00152             if (fix_3d==1) 
00153             {
00154                 id1=id2=id3=id4=id5=id6=id7=id8=id9=id10=id11=id12=0;
00155                 //pdop=0.0;
00156                 //hdop=0.0;
00157                 //vdop=0.0;
00158                 
00159                 return 0;
00160             } 
00161             else
00162             {
00163                 //pdop=Pdop;
00164                 //hdop=Hdop;
00165                 //vhop=Vhop;
00166                 aut=Aut;
00167                 return 1;
00168                 
00169             }
00170        }
00171           if(sscanf(msg, "GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &time2, &ac, &latitude2, &ns2, &longitude2, &ew2, &speed, &date)>=1)
00172         {
00173             //speed=Speed;
00174             //date=Date;
00175             //return 1;
00176         }   
00177         return 0;
00178         }
00179     }
00180 
00181        /**$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A
00182         Where:
00183         RMC          Recommended Minimum sentence C
00184         123519       Fix taken at 12:35:19 UTC
00185         A            Status A=active or V=Void.
00186         4807.038,N   Latitude 48 deg 07.038' N
00187         01131.000,E  Longitude 11 deg 31.000' E
00188         022.4        Speed over the ground in knots
00189         084.4        Track angle in degrees True
00190         230394       Date - 23rd of March 1994
00191         003.1,W      Magnetic Variation
00192         *6A          The checksum data, always begins with **/  
00193      
00194         
00195         //return 0;
00196 
00197 
00198 float GPS::trunc(float v) {
00199     if (v < 0.0) {
00200         v*= -1.0;
00201         v = floor(v);
00202         v*=-1.0;
00203     } else {
00204         v = floor(v);
00205     }
00206     return v;
00207 }
00208 
00209 void GPS::getline() {
00210     char c;
00211     while (c = _gps.getc() != '$');   // wait for the start of a line
00212     // printf("entered the getline loop\n\r");
00213     for (int i=0; i<280; i++) {
00214         msg[i] = _gps.getc();
00215         if (msg[i] == '\r') {
00216             msg[i] = 0;
00217             return;
00218         }
00219     }
00220     error("Overflowed message limit");
00221 }
00222 /*void GPS::getline2() {
00223     char c;
00224     while (c = _gps.getc() != '$');   // wait for the start of a line
00225     // printf("entered the getline loop\n\r");
00226     for (int i=0; i<300; i++) {
00227         msg2[i] = _gps.getc();
00228         if (msg2[i] == '\r') {
00229             msg2[i] = 0;
00230             return;
00231         }
00232     }
00233     error("Overflowed message limit");
00234 }*/
00235 
00236 /*
00237 $GPRMC,000115.039,V,,,,,,,291006,,*2C
00238 $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52
00239 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00240 $GPGSV,3,1,12,20,00,000,,10,00,000,,31,00,000,,27,00,000,*7C
00241 $GPGSV,3,2,12,19,00,000,,07,00,000,,04,00,000,,24,00,000,*76
00242 $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78
00243 $GPRMC,000116.031,V,,,,,,,291006,,*27
00244 $GPGGA,000117.035,,,,,0,00,,,M,0.0,M,,0000*57
00245 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00246 $GPRMC,000117.035,V,,,,,,,291006,,*22
00247 $GPGGA,000118.039,,,,,0,00,,,M,0.0,M,,0000*54
00248 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00249 $GPRMC,000118.039,V,,,,,,,291006,,*21
00250 $GPGGA,000119.035,,,,,0,00,,,M,0.0,M,,0000*59
00251 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00252 $GPRMC,000119.035,V,,,,,,,291006,,*2C
00253 $GPGGA,000120.037,,,,,0,00,,,M,0.0,M,,0000*51
00254 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00255 $GPRMC,000120.037,V,,,,,,,291006,,*24
00256 
00257 */