simple gps lib. no changes made to this yet.
Fork of GPS by
GPS.cpp@1:71e46131743f, 2013-07-19 (annotated)
- Committer:
- nherriot
- Date:
- Fri Jul 19 22:26:45 2013 +0000
- Revision:
- 1:71e46131743f
- Parent:
- 0:0f423a982334
first commit
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) { |
nherriot | 1:71e46131743f | 8 | _gps.baud(9600); |
nherriot | 1:71e46131743f | 9 | //_gps.baud(4800); |
pangsk | 0:0f423a982334 | 10 | longitude = 0.0; |
pangsk | 0:0f423a982334 | 11 | latitude = 0.0; |
pangsk | 0:0f423a982334 | 12 | } |
pangsk | 0:0f423a982334 | 13 | |
pangsk | 0:0f423a982334 | 14 | |
pangsk | 0:0f423a982334 | 15 | int GPS::sample() { |
pangsk | 0:0f423a982334 | 16 | float time; |
pangsk | 0:0f423a982334 | 17 | char ns, ew; |
pangsk | 0:0f423a982334 | 18 | int lock; |
pangsk | 0:0f423a982334 | 19 | |
pangsk | 0:0f423a982334 | 20 | while(1) { |
pangsk | 0:0f423a982334 | 21 | getline(); |
pangsk | 0:0f423a982334 | 22 | |
pangsk | 0:0f423a982334 | 23 | // Check if it is a GPGGA msg (matches both locked and non-locked msg) |
pangsk | 0:0f423a982334 | 24 | if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) { |
pangsk | 0:0f423a982334 | 25 | if(!lock) { |
pangsk | 0:0f423a982334 | 26 | longitude = 0.0; |
pangsk | 0:0f423a982334 | 27 | latitude = 0.0; |
pangsk | 0:0f423a982334 | 28 | return 0; |
pangsk | 0:0f423a982334 | 29 | } else { |
pangsk | 0:0f423a982334 | 30 | if(ns == 'S') { latitude *= -1.0; } |
pangsk | 0:0f423a982334 | 31 | if(ew == 'W') { longitude *= -1.0; } |
pangsk | 0:0f423a982334 | 32 | float degrees = trunc(latitude / 100.0f); |
pangsk | 0:0f423a982334 | 33 | float minutes = latitude - (degrees * 100.0f); |
pangsk | 0:0f423a982334 | 34 | latitude = degrees + minutes / 60.0f; |
pangsk | 0:0f423a982334 | 35 | degrees = trunc(longitude / 100.0f * 0.01f); |
pangsk | 0:0f423a982334 | 36 | minutes = longitude - (degrees * 100.0f); |
pangsk | 0:0f423a982334 | 37 | longitude = degrees + minutes / 60.0f; |
pangsk | 0:0f423a982334 | 38 | return 1; |
pangsk | 0:0f423a982334 | 39 | } |
pangsk | 0:0f423a982334 | 40 | } |
pangsk | 0:0f423a982334 | 41 | } |
pangsk | 0:0f423a982334 | 42 | } |
pangsk | 0:0f423a982334 | 43 | |
pangsk | 0:0f423a982334 | 44 | float GPS::trunc(float v) { |
pangsk | 0:0f423a982334 | 45 | if(v < 0.0) { |
pangsk | 0:0f423a982334 | 46 | v*= -1.0; |
pangsk | 0:0f423a982334 | 47 | v = floor(v); |
pangsk | 0:0f423a982334 | 48 | v*=-1.0; |
pangsk | 0:0f423a982334 | 49 | } else { |
pangsk | 0:0f423a982334 | 50 | v = floor(v); |
pangsk | 0:0f423a982334 | 51 | } |
pangsk | 0:0f423a982334 | 52 | return v; |
pangsk | 0:0f423a982334 | 53 | } |
pangsk | 0:0f423a982334 | 54 | |
pangsk | 0:0f423a982334 | 55 | void GPS::getline() { |
pangsk | 0:0f423a982334 | 56 | while(_gps.getc() != '$'); // wait for the start of a line |
pangsk | 0:0f423a982334 | 57 | for(int i=0; i<256; i++) { |
pangsk | 0:0f423a982334 | 58 | msg[i] = _gps.getc(); |
pangsk | 0:0f423a982334 | 59 | if(msg[i] == '\r') { |
pangsk | 0:0f423a982334 | 60 | msg[i] = 0; |
pangsk | 0:0f423a982334 | 61 | return; |
pangsk | 0:0f423a982334 | 62 | } |
pangsk | 0:0f423a982334 | 63 | } |
pangsk | 0:0f423a982334 | 64 | error("Overflowed message limit"); |
nherriot | 1:71e46131743f | 65 | error("Maybe you need higher baud rate?"); |
pangsk | 0:0f423a982334 | 66 | } |
pangsk | 0:0f423a982334 | 67 |