Dependents:   Lab3_Surveillance 6adcSerial

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GPS.cpp Source File

GPS.cpp

00001 #include "GPS.h"
00002 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
00003     _gps.baud(4800);
00004     longitude = 0.0;
00005     latitude = 0.0;
00006 }
00007 int GPS::sample() {
00008     float time;
00009     char ns,ew;
00010     int lock;
00011     
00012     while(1){
00013         getline();
00014         if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d",&time, &latitude, &ns, &longitude, &ew, &lock) >=1) {
00015             if(!lock) {
00016                 longitude = 0.0;
00017                 latitude = 0.0;
00018                 return 0;
00019             } else {
00020                 if(ns =='S') {  latitude   *= -1.0; }
00021                 if(ew =='W') {  longitude  *= -1.0; }
00022                 float degrees = trunc(latitude / 100.0f);
00023                 float minutes = latitude - (degrees * 100.0f);
00024                 latitude = degrees + minutes / 60.0f;
00025                 degrees = trunc(longitude / 100.0f * 0.01f);
00026                 minutes = longitude - (degrees * 100.0f);
00027                 longitude = degrees + minutes /60.0f;
00028                 return 1;
00029             }
00030          }
00031     }
00032 }
00033 float GPS::trunc(float v) {
00034     if(v < 0.0) {
00035         v*= -1.0;
00036         v = floor(v);
00037         v*=-1.0;
00038     } else {
00039         v = floor(v);
00040     }
00041     return v;
00042 }
00043 void GPS::getline() {
00044     while(_gps.getc() != '$');
00045     for(int i=0; i<256; i++) {
00046         msg[i] = _gps.getc();
00047         if(msg[i] == '\r') {
00048             msg[i] = 0;
00049             return;
00050         }
00051     }
00052     error("overflowed message limit");
00053 }
00054