Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
GPS/GPS.cpp@0:feaa05d35ccf, 2012-03-07 (annotated)
- Committer:
- AlexAllen
- Date:
- Wed Mar 07 20:02:35 2012 +0000
- Revision:
- 0:feaa05d35ccf
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| AlexAllen | 0:feaa05d35ccf | 1 | /* mbed EM-406 GPS Module Library |
| AlexAllen | 0:feaa05d35ccf | 2 | * Copyright (c) 2008-2010, sford |
| AlexAllen | 0:feaa05d35ccf | 3 | * |
| AlexAllen | 0:feaa05d35ccf | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| AlexAllen | 0:feaa05d35ccf | 5 | * of this software and associated documentation files (the "Software"), to deal |
| AlexAllen | 0:feaa05d35ccf | 6 | * in the Software without restriction, including without limitation the rights |
| AlexAllen | 0:feaa05d35ccf | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| AlexAllen | 0:feaa05d35ccf | 8 | * copies of the Software, and to permit persons to whom the Software is |
| AlexAllen | 0:feaa05d35ccf | 9 | * furnished to do so, subject to the following conditions: |
| AlexAllen | 0:feaa05d35ccf | 10 | * |
| AlexAllen | 0:feaa05d35ccf | 11 | * The above copyright notice and this permission notice shall be included in |
| AlexAllen | 0:feaa05d35ccf | 12 | * all copies or substantial portions of the Software. |
| AlexAllen | 0:feaa05d35ccf | 13 | * |
| AlexAllen | 0:feaa05d35ccf | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| AlexAllen | 0:feaa05d35ccf | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| AlexAllen | 0:feaa05d35ccf | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| AlexAllen | 0:feaa05d35ccf | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| AlexAllen | 0:feaa05d35ccf | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| AlexAllen | 0:feaa05d35ccf | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| AlexAllen | 0:feaa05d35ccf | 20 | * THE SOFTWARE. |
| AlexAllen | 0:feaa05d35ccf | 21 | */ |
| AlexAllen | 0:feaa05d35ccf | 22 | |
| AlexAllen | 0:feaa05d35ccf | 23 | #include "GPS.h" |
| AlexAllen | 0:feaa05d35ccf | 24 | |
| AlexAllen | 0:feaa05d35ccf | 25 | GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) { |
| AlexAllen | 0:feaa05d35ccf | 26 | _gps.baud(4800); |
| AlexAllen | 0:feaa05d35ccf | 27 | longitude = 0.0; |
| AlexAllen | 0:feaa05d35ccf | 28 | latitude = 0.0; |
| AlexAllen | 0:feaa05d35ccf | 29 | } |
| AlexAllen | 0:feaa05d35ccf | 30 | |
| AlexAllen | 0:feaa05d35ccf | 31 | int GPS::sample() |
| AlexAllen | 0:feaa05d35ccf | 32 | { |
| AlexAllen | 0:feaa05d35ccf | 33 | |
| AlexAllen | 0:feaa05d35ccf | 34 | while(1) { |
| AlexAllen | 0:feaa05d35ccf | 35 | getline(); |
| AlexAllen | 0:feaa05d35ccf | 36 | |
| AlexAllen | 0:feaa05d35ccf | 37 | // Check if it is a GPGGA msg (matches both locked and non-locked msg) |
| AlexAllen | 0:feaa05d35ccf | 38 | if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f", &time, &latitude, &ns, &longitude, &ew, &lock, &satillites, &HDOP, &altitude) >= 1) { |
| AlexAllen | 0:feaa05d35ccf | 39 | if(!lock) |
| AlexAllen | 0:feaa05d35ccf | 40 | { |
| AlexAllen | 0:feaa05d35ccf | 41 | longitude = 0.0; |
| AlexAllen | 0:feaa05d35ccf | 42 | latitude = 0.0; |
| AlexAllen | 0:feaa05d35ccf | 43 | printf("case 1: rx [%s], lock %d, lat %f, long %f\n\r", msg, lock, latitude, longitude); |
| AlexAllen | 0:feaa05d35ccf | 44 | return 0; |
| AlexAllen | 0:feaa05d35ccf | 45 | } |
| AlexAllen | 0:feaa05d35ccf | 46 | else |
| AlexAllen | 0:feaa05d35ccf | 47 | { |
| AlexAllen | 0:feaa05d35ccf | 48 | printf("case 2: rx [%s], time %f, lock %d, lat %f, long %f\n\r", msg, time, lock, latitude, longitude); |
| AlexAllen | 0:feaa05d35ccf | 49 | if(ns == 'S') { latitude *= -1.0; } |
| AlexAllen | 0:feaa05d35ccf | 50 | if(ew == 'W') { longitude *= -1.0; } |
| AlexAllen | 0:feaa05d35ccf | 51 | float degrees = trunc(latitude / 100.0f); |
| AlexAllen | 0:feaa05d35ccf | 52 | float minutes = latitude - (degrees * 100.0f); |
| AlexAllen | 0:feaa05d35ccf | 53 | latitude = degrees + minutes / 60.0f; |
| AlexAllen | 0:feaa05d35ccf | 54 | degrees = trunc(longitude / 100.0f * 0.01f); |
| AlexAllen | 0:feaa05d35ccf | 55 | minutes = longitude - (degrees * 100.0f); |
| AlexAllen | 0:feaa05d35ccf | 56 | longitude = degrees + minutes / 60.0f; |
| AlexAllen | 0:feaa05d35ccf | 57 | return 1; |
| AlexAllen | 0:feaa05d35ccf | 58 | } |
| AlexAllen | 0:feaa05d35ccf | 59 | } |
| AlexAllen | 0:feaa05d35ccf | 60 | } |
| AlexAllen | 0:feaa05d35ccf | 61 | } |
| AlexAllen | 0:feaa05d35ccf | 62 | |
| AlexAllen | 0:feaa05d35ccf | 63 | float GPS::trunc(float v) { |
| AlexAllen | 0:feaa05d35ccf | 64 | if(v < 0.0) { |
| AlexAllen | 0:feaa05d35ccf | 65 | v*= -1.0; |
| AlexAllen | 0:feaa05d35ccf | 66 | v = floor(v); |
| AlexAllen | 0:feaa05d35ccf | 67 | v*=-1.0; |
| AlexAllen | 0:feaa05d35ccf | 68 | } else { |
| AlexAllen | 0:feaa05d35ccf | 69 | v = floor(v); |
| AlexAllen | 0:feaa05d35ccf | 70 | } |
| AlexAllen | 0:feaa05d35ccf | 71 | return v; |
| AlexAllen | 0:feaa05d35ccf | 72 | } |
| AlexAllen | 0:feaa05d35ccf | 73 | |
| AlexAllen | 0:feaa05d35ccf | 74 | void GPS::getline() { |
| AlexAllen | 0:feaa05d35ccf | 75 | while(_gps.getc() != '$'); // wait for the start of a line |
| AlexAllen | 0:feaa05d35ccf | 76 | for(int i=0; i<256; i++) { |
| AlexAllen | 0:feaa05d35ccf | 77 | msg[i] = _gps.getc(); |
| AlexAllen | 0:feaa05d35ccf | 78 | if(msg[i] == '\r') { |
| AlexAllen | 0:feaa05d35ccf | 79 | msg[i] = 0; |
| AlexAllen | 0:feaa05d35ccf | 80 | return; |
| AlexAllen | 0:feaa05d35ccf | 81 | } |
| AlexAllen | 0:feaa05d35ccf | 82 | } |
| AlexAllen | 0:feaa05d35ccf | 83 | error("Overflowed message limit"); |
| AlexAllen | 0:feaa05d35ccf | 84 | } |