GPS and Compass update every1 sec

Dependencies:   HMC6352 mbed

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 }
00030 
00031 int GPS::sample() {
00032     float time;
00033     char ns, ew;
00034     int lock;
00035 
00036     //return 1; //testing by Jigar
00037     while (1) {
00038         getline();
00039         //printf("\n\rentered the GPS.sample while loop \n\r %s\n\r", msg);
00040         
00041         
00042         // Check if it is a GPGGA msg (matches both locked and non-locked msg)
00043         // $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52
00044         /*
00045         eg3. $GPGGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh
00046                         1       2     3  4       5 6  7  8   9  10 11 12 13  14   15
00047             1    = UTC of Position
00048             2    = Latitude
00049             3    = N or S
00050             4    = Longitude
00051             5    = E or W
00052             6    = GPS quality indicator (0=invalid; 1=GPS fix; 2=Diff. GPS fix)
00053             7    = Number of satellites in use [not those in view]
00054             8    = Horizontal dilution of position
00055             9    = Antenna altitude above/below mean sea level (geoid)
00056             10   = Meters  (Antenna height unit)
00057             11   = Geoidal separation (Diff. between WGS-84 earth ellipsoid and
00058                    mean sea level.  -=geoid is below WGS-84 ellipsoid)
00059             12   = Meters  (Units of geoidal separation)
00060             13   = Age in seconds since last update from diff. reference station
00061             14   = Diff. reference station ID#
00062             15   = Checksum
00063 
00064         */
00065        // 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) {
00066         if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) { 
00067 
00068             if (!lock) {
00069                 longitude = 0.0;
00070                 latitude = 0.0;
00071                 return 0;
00072             } else {
00073                 if (ns == 'S') {
00074                     latitude  *= -1.0;
00075                 }
00076                 if (ew == 'W') {
00077                     longitude *= -1.0;
00078                 }
00079                 float degrees = trunc(latitude / 100.0f);
00080                 float minutes = latitude - (degrees * 100.0f);
00081                 latitude = degrees + minutes / 60.0f;
00082                  degrees = trunc(longitude / 100.0f ); //degrees = trunc(longitude / 100.0f * 0.01f);
00083                 minutes = longitude - (degrees * 100.0f);
00084                 longitude = degrees + minutes / 60.0f;
00085                 return 1;
00086             }
00087         }  
00088     }
00089 }
00090 
00091 float GPS::trunc(float v) {
00092     if (v < 0.0) {
00093         v*= -1.0;
00094         v = floor(v);
00095         v*=-1.0;
00096     } else {
00097         v = floor(v);
00098     }
00099     return v;
00100 }
00101 
00102 void GPS::getline() {
00103     
00104     while (_gps.getc() != '$');   // wait for the start of a line
00105     // printf("entered the getline loop\n\r");
00106     for (int i=0; i<256; i++) {
00107         msg[i] = _gps.getc();
00108         if (msg[i] == '\r') {
00109             msg[i] = 0;
00110             return;
00111         }
00112     }
00113     error("Overflowed message limit");
00114 }
00115 
00116 /*
00117 $GPRMC,000115.039,V,,,,,,,291006,,*2C
00118 $GPGGA,000116.031,,,,,0,00,,,M,0.0,M,,0000*52
00119 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00120 $GPGSV,3,1,12,20,00,000,,10,00,000,,31,00,000,,27,00,000,*7C
00121 $GPGSV,3,2,12,19,00,000,,07,00,000,,04,00,000,,24,00,000,*76
00122 $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78
00123 $GPRMC,000116.031,V,,,,,,,291006,,*27
00124 $GPGGA,000117.035,,,,,0,00,,,M,0.0,M,,0000*57
00125 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00126 $GPRMC,000117.035,V,,,,,,,291006,,*22
00127 $GPGGA,000118.039,,,,,0,00,,,M,0.0,M,,0000*54
00128 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00129 $GPRMC,000118.039,V,,,,,,,291006,,*21
00130 $GPGGA,000119.035,,,,,0,00,,,M,0.0,M,,0000*59
00131 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00132 $GPRMC,000119.035,V,,,,,,,291006,,*2C
00133 $GPGGA,000120.037,,,,,0,00,,,M,0.0,M,,0000*51
00134 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
00135 $GPRMC,000120.037,V,,,,,,,291006,,*24
00136 
00137 */