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