Dependents: Lab3_Surveillance 6adcSerial
Diff: GPS.cpp
- Revision:
- 0:14fd31ae13bb
- Child:
- 1:b4de63a99f18
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GPS.cpp Fri Jan 21 13:26:20 2011 +0000 @@ -0,0 +1,54 @@ +#include "GPS.h" +GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) { + _gp.baud(4800); + longitude = 0.0; + latitude = 0.0; +} +int GPS::sample() { + float time; + char ns,ew; + int lock; + + while(1){ + getline(); + if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d",&time, &latitude, &ns, &longitude, &ew, &lock) >=1) { + if(!lock) { + longitude = 0.0; + latitude = 0.0; + return 0; + } else { + if(ns =='s') { latitude *= -1.0; } + if(ew =='w') { longitude *= -1.0; } + float degrees = trunc(latitude / 100.0f); + float minutes = latitude - (degrees * 100.0f); + latitude = degrees + minutes / 60.0f; + degrees = trunc(longitude / 100.0f * 0.01f); + minutes = longitude - (degrees * 100.0f); + longitude = degrees + minute /60.0f; + return 1; + } + } + } +} +float GPS::trunc(float v) { + if(v<0.0) { + v*= -1.0; + v = floor(v); + v*=-1.0; + } else { + v = floor(v); + } + return; +} +void GPS::getline() { + while(_gps.get() != '$'); + for(int i=0; i<256; i++) { + msg[i] = _gps.getc(); + if(msg[i] == '\r') { + msg[i] = 0; + return; + } + } + error("overflowed message limit"); +} + \ No newline at end of file