GPS for mbed

Dependents:   MPOA_GPS

Fork of GPS by Sukkin Pang

Committer:
Happy111
Date:
Sun Jan 15 16:05:12 2017 +0000
Revision:
1:f491b73cbbcf
Parent:
0:0f423a982334
MPOA_GPS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pangsk 0:0f423a982334 1 /* GPS class for mbed Microcontroller
pangsk 0:0f423a982334 2 * Copyright (c) 2008, sford
pangsk 0:0f423a982334 3 */
pangsk 0:0f423a982334 4
pangsk 0:0f423a982334 5 #include "GPS.h"
pangsk 0:0f423a982334 6
pangsk 0:0f423a982334 7 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
pangsk 0:0f423a982334 8 _gps.baud(4800);
pangsk 0:0f423a982334 9 longitude = 0.0;
pangsk 0:0f423a982334 10 latitude = 0.0;
pangsk 0:0f423a982334 11 }
pangsk 0:0f423a982334 12
pangsk 0:0f423a982334 13
pangsk 0:0f423a982334 14 int GPS::sample() {
pangsk 0:0f423a982334 15 float time;
pangsk 0:0f423a982334 16 char ns, ew;
pangsk 0:0f423a982334 17 int lock;
pangsk 0:0f423a982334 18
pangsk 0:0f423a982334 19 while(1) {
pangsk 0:0f423a982334 20 getline();
pangsk 0:0f423a982334 21
pangsk 0:0f423a982334 22 // Check if it is a GPGGA msg (matches both locked and non-locked msg)
Happy111 1:f491b73cbbcf 23 if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d", &time, &latitude, &ns, &longitude, &ew, &lock, &snum) >= 1) {
pangsk 0:0f423a982334 24 if(!lock) {
pangsk 0:0f423a982334 25 longitude = 0.0;
pangsk 0:0f423a982334 26 latitude = 0.0;
pangsk 0:0f423a982334 27 return 0;
pangsk 0:0f423a982334 28 } else {
pangsk 0:0f423a982334 29 if(ns == 'S') { latitude *= -1.0; }
pangsk 0:0f423a982334 30 if(ew == 'W') { longitude *= -1.0; }
pangsk 0:0f423a982334 31 float degrees = trunc(latitude / 100.0f);
pangsk 0:0f423a982334 32 float minutes = latitude - (degrees * 100.0f);
pangsk 0:0f423a982334 33 latitude = degrees + minutes / 60.0f;
pangsk 0:0f423a982334 34 degrees = trunc(longitude / 100.0f * 0.01f);
pangsk 0:0f423a982334 35 minutes = longitude - (degrees * 100.0f);
pangsk 0:0f423a982334 36 longitude = degrees + minutes / 60.0f;
Happy111 1:f491b73cbbcf 37 xns = ns;
Happy111 1:f491b73cbbcf 38 xtime = time;
Happy111 1:f491b73cbbcf 39 xew = ew;
Happy111 1:f491b73cbbcf 40 //return 1;
pangsk 0:0f423a982334 41 }
pangsk 0:0f423a982334 42 }
Happy111 1:f491b73cbbcf 43 /*
Happy111 1:f491b73cbbcf 44 Parsing GSV message
Happy111 1:f491b73cbbcf 45 totmsg - total number of messages in cycle
Happy111 1:f491b73cbbcf 46 actmsg - message number
Happy111 1:f491b73cbbcf 47 siv - number of SVs in view
Happy111 1:f491b73cbbcf 48 prn - SV PRN number
Happy111 1:f491b73cbbcf 49 svele - elevation
Happy111 1:f491b73cbbcf 50 svaz - azimuth
Happy111 1:f491b73cbbcf 51 */
Happy111 1:f491b73cbbcf 52 if(sscanf(msg, "GPGSV,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &totmsg, &actmsg, &siv, &tempprn1, &tempsvele1, &tempsvaz1, &tempprn2, &tempsvele2, &tempsvaz2, &tempprn3, &tempsvele3, &tempsvaz3, &tempprn4, &tempsvele4, &tempsvaz4) >= 1) {
Happy111 1:f491b73cbbcf 53 if(!lock) {
Happy111 1:f491b73cbbcf 54 totmsg = 0;
Happy111 1:f491b73cbbcf 55 actmsg = 0;
Happy111 1:f491b73cbbcf 56 siv = 0;
Happy111 1:f491b73cbbcf 57 tempprn1 = 0;
Happy111 1:f491b73cbbcf 58 tempsvele1 = 0;
Happy111 1:f491b73cbbcf 59 tempsvaz1 = 0;
Happy111 1:f491b73cbbcf 60 tempprn2 = 0;
Happy111 1:f491b73cbbcf 61 tempsvele2 = 0;
Happy111 1:f491b73cbbcf 62 tempsvaz2 = 0;
Happy111 1:f491b73cbbcf 63 tempprn3 = 0;
Happy111 1:f491b73cbbcf 64 tempsvele3 = 0;
Happy111 1:f491b73cbbcf 65 tempsvaz3 = 0;
Happy111 1:f491b73cbbcf 66 tempprn4 = 0;
Happy111 1:f491b73cbbcf 67 tempsvele4 = 0;
Happy111 1:f491b73cbbcf 68 tempsvaz4 = 0;
Happy111 1:f491b73cbbcf 69 return 0;
Happy111 1:f491b73cbbcf 70 } else {
Happy111 1:f491b73cbbcf 71 if(actmsg==1){
Happy111 1:f491b73cbbcf 72 prn1 = tempprn1;
Happy111 1:f491b73cbbcf 73 svele1 = tempsvele1;
Happy111 1:f491b73cbbcf 74 svaz1 = tempsvaz1;
Happy111 1:f491b73cbbcf 75 prn2 = tempprn2;
Happy111 1:f491b73cbbcf 76 svele2 = tempsvele2;
Happy111 1:f491b73cbbcf 77 svaz2 = tempsvaz2;
Happy111 1:f491b73cbbcf 78 prn3 = tempprn3;
Happy111 1:f491b73cbbcf 79 svele3 = tempsvele3;
Happy111 1:f491b73cbbcf 80 svaz3 = tempsvaz3;
Happy111 1:f491b73cbbcf 81 prn4 = tempprn4;
Happy111 1:f491b73cbbcf 82 svele4 = tempsvele4;
Happy111 1:f491b73cbbcf 83 svaz4 = tempsvaz4;
Happy111 1:f491b73cbbcf 84 return 1;
Happy111 1:f491b73cbbcf 85 }
Happy111 1:f491b73cbbcf 86 //naznačené řešení pro příjem druhé čtveřice údají o satelitech
Happy111 1:f491b73cbbcf 87 /*if(actmsg==2){
Happy111 1:f491b73cbbcf 88 prn5 = tempprn1;
Happy111 1:f491b73cbbcf 89 svele5 = tempsvele1;
Happy111 1:f491b73cbbcf 90 svaz5 = tempsvaz1;
Happy111 1:f491b73cbbcf 91 prn6 = tempprn2;
Happy111 1:f491b73cbbcf 92 svele6 = tempsvele2;
Happy111 1:f491b73cbbcf 93 svaz6 = tempsvaz2;
Happy111 1:f491b73cbbcf 94 prn7 = tempprn3;
Happy111 1:f491b73cbbcf 95 svele7 = tempsvele3;
Happy111 1:f491b73cbbcf 96 svaz7 = tempsvaz3;
Happy111 1:f491b73cbbcf 97 prn8 = tempprn4;
Happy111 1:f491b73cbbcf 98 svele8 = tempsvele4;
Happy111 1:f491b73cbbcf 99 svaz8 = tempsvaz4;
Happy111 1:f491b73cbbcf 100 return 1;
Happy111 1:f491b73cbbcf 101 }*/
Happy111 1:f491b73cbbcf 102
Happy111 1:f491b73cbbcf 103 }
Happy111 1:f491b73cbbcf 104 }
Happy111 1:f491b73cbbcf 105
pangsk 0:0f423a982334 106 }
pangsk 0:0f423a982334 107 }
pangsk 0:0f423a982334 108
pangsk 0:0f423a982334 109 float GPS::trunc(float v) {
pangsk 0:0f423a982334 110 if(v < 0.0) {
pangsk 0:0f423a982334 111 v*= -1.0;
pangsk 0:0f423a982334 112 v = floor(v);
pangsk 0:0f423a982334 113 v*=-1.0;
pangsk 0:0f423a982334 114 } else {
pangsk 0:0f423a982334 115 v = floor(v);
pangsk 0:0f423a982334 116 }
pangsk 0:0f423a982334 117 return v;
pangsk 0:0f423a982334 118 }
pangsk 0:0f423a982334 119
pangsk 0:0f423a982334 120 void GPS::getline() {
pangsk 0:0f423a982334 121 while(_gps.getc() != '$'); // wait for the start of a line
pangsk 0:0f423a982334 122 for(int i=0; i<256; i++) {
pangsk 0:0f423a982334 123 msg[i] = _gps.getc();
pangsk 0:0f423a982334 124 if(msg[i] == '\r') {
pangsk 0:0f423a982334 125 msg[i] = 0;
pangsk 0:0f423a982334 126 return;
pangsk 0:0f423a982334 127 }
pangsk 0:0f423a982334 128 }
pangsk 0:0f423a982334 129 error("Overflowed message limit");
pangsk 0:0f423a982334 130 }
pangsk 0:0f423a982334 131