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