simple gps lib. no changes made to this yet.
Fork of GPS by
GPS.cpp@0:0f423a982334, 2012-07-27 (annotated)
- Committer:
- pangsk
- Date:
- Fri Jul 27 15:07:00 2012 +0000
- Revision:
- 0:0f423a982334
- Child:
- 1:71e46131743f
[mbed] converted /ecu_reader/GPS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pangsk | 0:0f423a982334 | 1 | /* GPS class for mbed Microcontroller |
pangsk | 0:0f423a982334 | 2 | * Copyright (c) 2008, sford |
pangsk | 0:0f423a982334 | 3 | */ |
pangsk | 0:0f423a982334 | 4 | |
pangsk | 0:0f423a982334 | 5 | #include "GPS.h" |
pangsk | 0:0f423a982334 | 6 | |
pangsk | 0:0f423a982334 | 7 | GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) { |
pangsk | 0:0f423a982334 | 8 | _gps.baud(4800); |
pangsk | 0:0f423a982334 | 9 | longitude = 0.0; |
pangsk | 0:0f423a982334 | 10 | latitude = 0.0; |
pangsk | 0:0f423a982334 | 11 | } |
pangsk | 0:0f423a982334 | 12 | |
pangsk | 0:0f423a982334 | 13 | |
pangsk | 0:0f423a982334 | 14 | int GPS::sample() { |
pangsk | 0:0f423a982334 | 15 | float time; |
pangsk | 0:0f423a982334 | 16 | char ns, ew; |
pangsk | 0:0f423a982334 | 17 | int lock; |
pangsk | 0:0f423a982334 | 18 | |
pangsk | 0:0f423a982334 | 19 | while(1) { |
pangsk | 0:0f423a982334 | 20 | getline(); |
pangsk | 0:0f423a982334 | 21 | |
pangsk | 0:0f423a982334 | 22 | // Check if it is a GPGGA msg (matches both locked and non-locked msg) |
pangsk | 0:0f423a982334 | 23 | if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) { |
pangsk | 0:0f423a982334 | 24 | if(!lock) { |
pangsk | 0:0f423a982334 | 25 | longitude = 0.0; |
pangsk | 0:0f423a982334 | 26 | latitude = 0.0; |
pangsk | 0:0f423a982334 | 27 | return 0; |
pangsk | 0:0f423a982334 | 28 | } else { |
pangsk | 0:0f423a982334 | 29 | if(ns == 'S') { latitude *= -1.0; } |
pangsk | 0:0f423a982334 | 30 | if(ew == 'W') { longitude *= -1.0; } |
pangsk | 0:0f423a982334 | 31 | float degrees = trunc(latitude / 100.0f); |
pangsk | 0:0f423a982334 | 32 | float minutes = latitude - (degrees * 100.0f); |
pangsk | 0:0f423a982334 | 33 | latitude = degrees + minutes / 60.0f; |
pangsk | 0:0f423a982334 | 34 | degrees = trunc(longitude / 100.0f * 0.01f); |
pangsk | 0:0f423a982334 | 35 | minutes = longitude - (degrees * 100.0f); |
pangsk | 0:0f423a982334 | 36 | longitude = degrees + minutes / 60.0f; |
pangsk | 0:0f423a982334 | 37 | return 1; |
pangsk | 0:0f423a982334 | 38 | } |
pangsk | 0:0f423a982334 | 39 | } |
pangsk | 0:0f423a982334 | 40 | } |
pangsk | 0:0f423a982334 | 41 | } |
pangsk | 0:0f423a982334 | 42 | |
pangsk | 0:0f423a982334 | 43 | float GPS::trunc(float v) { |
pangsk | 0:0f423a982334 | 44 | if(v < 0.0) { |
pangsk | 0:0f423a982334 | 45 | v*= -1.0; |
pangsk | 0:0f423a982334 | 46 | v = floor(v); |
pangsk | 0:0f423a982334 | 47 | v*=-1.0; |
pangsk | 0:0f423a982334 | 48 | } else { |
pangsk | 0:0f423a982334 | 49 | v = floor(v); |
pangsk | 0:0f423a982334 | 50 | } |
pangsk | 0:0f423a982334 | 51 | return v; |
pangsk | 0:0f423a982334 | 52 | } |
pangsk | 0:0f423a982334 | 53 | |
pangsk | 0:0f423a982334 | 54 | void GPS::getline() { |
pangsk | 0:0f423a982334 | 55 | while(_gps.getc() != '$'); // wait for the start of a line |
pangsk | 0:0f423a982334 | 56 | for(int i=0; i<256; i++) { |
pangsk | 0:0f423a982334 | 57 | msg[i] = _gps.getc(); |
pangsk | 0:0f423a982334 | 58 | if(msg[i] == '\r') { |
pangsk | 0:0f423a982334 | 59 | msg[i] = 0; |
pangsk | 0:0f423a982334 | 60 | return; |
pangsk | 0:0f423a982334 | 61 | } |
pangsk | 0:0f423a982334 | 62 | } |
pangsk | 0:0f423a982334 | 63 | error("Overflowed message limit"); |
pangsk | 0:0f423a982334 | 64 | } |
pangsk | 0:0f423a982334 | 65 |