GPS library

Committer:
wschon
Date:
Wed Mar 16 19:18:00 2016 +0000
Revision:
7:120983998da2
Parent:
6:0a167594f765
Child:
8:720966cb4824
made gps variables viewable to caller

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