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