programme GPS pour ERS4

Dependencies:   mbed

Fork of GPS_IUT_Nice by tito tito

Committer:
dreday20
Date:
Fri Nov 23 19:10:01 2012 +0000
Revision:
0:2c0686a07ab8
Child:
1:f24af888e699
1.0;

Who changed what in which revision?

UserRevisionLine numberNew 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();
dreday20 0:2c0686a07ab8 38
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;
dreday20 0:2c0686a07ab8 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;
dreday20 0:2c0686a07ab8 65 } else {
dreday20 0:2c0686a07ab8 66 v = floor(v);
dreday20 0:2c0686a07ab8 67 }
dreday20 0:2c0686a07ab8 68 return v;
dreday20 0:2c0686a07ab8 69 }
dreday20 0:2c0686a07ab8 70
dreday20 0:2c0686a07ab8 71 void GPS::getline() {
dreday20 0:2c0686a07ab8 72 while(_gps.getc() != '$'); // wait for the start of a line
dreday20 0:2c0686a07ab8 73 for(int i=0; i<256; i++) {
dreday20 0:2c0686a07ab8 74 msg[i] = _gps.getc();
dreday20 0:2c0686a07ab8 75 if(msg[i] == '\r') {
dreday20 0:2c0686a07ab8 76 msg[i] = 0;
dreday20 0:2c0686a07ab8 77 return;
dreday20 0:2c0686a07ab8 78 }
dreday20 0:2c0686a07ab8 79 }
dreday20 0:2c0686a07ab8 80 error("Overflowed message limit");
dreday20 0:2c0686a07ab8 81 }