GPS library

Committer:
wschon
Date:
Wed Mar 16 04:37:56 2016 +0000
Revision:
4:3aa177df0bcb
Parent:
3:5cfc893bfd25
Child:
5:42f6779a9584
minor fix

Who changed what in which revision?

UserRevisionLine numberNew 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 2:c17f5ba1c3f0 28 _gps.baud(9600);
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() {
wschon 3:5cfc893bfd25 35 float time, hori_dilute, alt,geoid;
wschon 3:5cfc893bfd25 36 char ns, ew, gu, hu;
wschon 3:5cfc893bfd25 37 int lock, num_sat;
wschon 3:5cfc893bfd25 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)
wschon 3:5cfc893bfd25 42 //pc1.printf(msg);
wschon 3:5cfc893bfd25 43 pc1.printf("\n");
wschon 3:5cfc893bfd25 44 wait(5);
wschon 3:5cfc893bfd25 45 pc1.printf("display msg\r\n");
nliu96 1:55ded12c9c89 46 pc1.printf(msg);
nliu96 1:55ded12c9c89 47 pc1.printf("\n");
wschon 3:5cfc893bfd25 48 if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c,%f,%c", &time, &latitude, &ns, &longitude, &ew, &lock, &num_sat, &hori_dilute, &alt, &hu, &geoid, &gu/*, &age_diff, &diff_ID*/) >= 1) {
simon 0:15611c7938a3 49 if(!lock) {
simon 0:15611c7938a3 50 longitude = 0.0;
simon 0:15611c7938a3 51 latitude = 0.0;
simon 0:15611c7938a3 52 return 0;
simon 0:15611c7938a3 53 } else {
wschon 4:3aa177df0bcb 54 //if(ns == 'S') { latitude *= -1.0; }
wschon 4:3aa177df0bcb 55 // if(ew == 'W') { longitude *= -1.0; }
wschon 4:3aa177df0bcb 56 // float degrees = trunc(latitude / 100.0f);
wschon 4:3aa177df0bcb 57 // float minutes = latitude - (degrees * 100.0f);
wschon 4:3aa177df0bcb 58 // latitude = degrees + minutes / 60.0f;
wschon 4:3aa177df0bcb 59 // degrees = trunc(longitude / 100.0f * 0.01f);
wschon 4:3aa177df0bcb 60 // minutes = longitude - (degrees * 100.0f);
wschon 4:3aa177df0bcb 61 // longitude = degrees + minutes / 60.0f;
wschon 4:3aa177df0bcb 62 // pc1.printf(msg);
wschon 4:3aa177df0bcb 63 latitude /= 100;
wschon 4:3aa177df0bcb 64 longitude /= 100;
wschon 4:3aa177df0bcb 65
simon 0:15611c7938a3 66 return 1;
simon 0:15611c7938a3 67 }
wschon 3:5cfc893bfd25 68
simon 0:15611c7938a3 69 }
wschon 3:5cfc893bfd25 70 return 0;
simon 0:15611c7938a3 71 }
simon 0:15611c7938a3 72 }
simon 0:15611c7938a3 73
simon 0:15611c7938a3 74 float GPS::trunc(float v) {
simon 0:15611c7938a3 75 if(v < 0.0) {
simon 0:15611c7938a3 76 v*= -1.0;
simon 0:15611c7938a3 77 v = floor(v);
simon 0:15611c7938a3 78 v*=-1.0;
simon 0:15611c7938a3 79 } else {
simon 0:15611c7938a3 80 v = floor(v);
simon 0:15611c7938a3 81 }
simon 0:15611c7938a3 82 return v;
simon 0:15611c7938a3 83 }
simon 0:15611c7938a3 84
simon 0:15611c7938a3 85 void GPS::getline() {
wschon 3:5cfc893bfd25 86 int i;
wschon 3:5cfc893bfd25 87 char a;
wschon 3:5cfc893bfd25 88 int n;
nliu96 1:55ded12c9c89 89 while(1) {
wschon 3:5cfc893bfd25 90 strcpy(msg, '\0');
wschon 3:5cfc893bfd25 91 //pc1.printf("\r\n\n New Message: ");
wschon 3:5cfc893bfd25 92 // pc1.printf(msg);
wschon 3:5cfc893bfd25 93 // pc1.printf("\r\n\n");
wschon 3:5cfc893bfd25 94 i = 0;
wschon 3:5cfc893bfd25 95 a = _gps.getc();
wschon 3:5cfc893bfd25 96 msg[i] = a;
wschon 3:5cfc893bfd25 97 if (a == '$') {
wschon 3:5cfc893bfd25 98 //pc1.printf("%c",a);
wschon 3:5cfc893bfd25 99 a = _gps.getc();
wschon 3:5cfc893bfd25 100 msg[i] = a;
wschon 3:5cfc893bfd25 101 i++;
wschon 3:5cfc893bfd25 102 if (a == 'G') {
wschon 3:5cfc893bfd25 103 //pc1.printf("%c",a);
wschon 3:5cfc893bfd25 104 a = _gps.getc();
wschon 3:5cfc893bfd25 105 msg[i] = a;
wschon 3:5cfc893bfd25 106 i++;
wschon 3:5cfc893bfd25 107 if (a == 'P') {
wschon 3:5cfc893bfd25 108 //pc1.printf("%c",a);
wschon 3:5cfc893bfd25 109 a = _gps.getc();
wschon 3:5cfc893bfd25 110 msg[i] = a;
wschon 3:5cfc893bfd25 111 i++;
wschon 3:5cfc893bfd25 112 if (a == 'G') {
wschon 3:5cfc893bfd25 113 //pc1.printf("%c",a);
wschon 3:5cfc893bfd25 114 a = _gps.getc();
wschon 3:5cfc893bfd25 115 msg[i] = a;
wschon 3:5cfc893bfd25 116 i++;
wschon 3:5cfc893bfd25 117 if (a == 'G') {
wschon 3:5cfc893bfd25 118 //pc1.printf("%c",a);
wschon 3:5cfc893bfd25 119 a = _gps.getc();
wschon 3:5cfc893bfd25 120 msg[i] = a;
wschon 3:5cfc893bfd25 121 i++;
wschon 3:5cfc893bfd25 122 if (a == 'A') {
wschon 3:5cfc893bfd25 123 //pc1.printf("%c",a);
wschon 3:5cfc893bfd25 124 //a = _gps.getc();
wschon 3:5cfc893bfd25 125 //msg[i] = a;
wschon 3:5cfc893bfd25 126 //pc1.printf(msg);
wschon 3:5cfc893bfd25 127 pc1.printf("\r\n");
wschon 3:5cfc893bfd25 128
wschon 3:5cfc893bfd25 129 for (n = 5; n < 456; n++) {
wschon 3:5cfc893bfd25 130 msg[n] = _gps.getc();
wschon 3:5cfc893bfd25 131 pc1.printf("%c", msg[n]);
wschon 3:5cfc893bfd25 132 if(msg[n] == '\r') {
wschon 3:5cfc893bfd25 133 msg[n] = '0';
wschon 3:5cfc893bfd25 134 return;
wschon 3:5cfc893bfd25 135 }
wschon 3:5cfc893bfd25 136 }
wschon 3:5cfc893bfd25 137 }
wschon 3:5cfc893bfd25 138 }
wschon 3:5cfc893bfd25 139 }
wschon 3:5cfc893bfd25 140 }
wschon 3:5cfc893bfd25 141 }
wschon 3:5cfc893bfd25 142 }
wschon 3:5cfc893bfd25 143 // while(_gps.getc() != '$') {
wschon 3:5cfc893bfd25 144 // //char a = _gps.getc();
wschon 3:5cfc893bfd25 145 // for(i = 0; i < 256; i++) {
wschon 3:5cfc893bfd25 146 // msg[i] = _gps.getc();
wschon 3:5cfc893bfd25 147 // pc1.printf("%c", msg[i]);
wschon 3:5cfc893bfd25 148 // if(msg[i] == '\r') {
wschon 3:5cfc893bfd25 149 // msg[i] = 0;
wschon 3:5cfc893bfd25 150 // return;
wschon 3:5cfc893bfd25 151 // }
wschon 3:5cfc893bfd25 152 // }
wschon 3:5cfc893bfd25 153 //
wschon 3:5cfc893bfd25 154 //
wschon 3:5cfc893bfd25 155 // }
nliu96 1:55ded12c9c89 156 // while(_gps.getc() != '$'); // wait for the start of a line
nliu96 1:55ded12c9c89 157 // for(int i=0; i<256; i++) {
nliu96 1:55ded12c9c89 158 // msg[i] = _gps.getc();
nliu96 1:55ded12c9c89 159 // if(msg[i] == '\r') {
nliu96 1:55ded12c9c89 160 // msg[i] = 0;
nliu96 1:55ded12c9c89 161 // return;
nliu96 1:55ded12c9c89 162 // }
nliu96 1:55ded12c9c89 163 // }
nliu96 1:55ded12c9c89 164 // error("Overflowed message limit");
simon 0:15611c7938a3 165 }
wschon 3:5cfc893bfd25 166 }