GPS library

Committer:
wschon
Date:
Wed Mar 16 04:28:03 2016 +0000
Revision:
3:5cfc893bfd25
Parent:
2:c17f5ba1c3f0
Child:
4:3aa177df0bcb
Made it work, so it now grabs correct GPGGA messages, with all components.  We still need to reformat some of those components.

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