[342] | 1 | #include <string.h>
|
---|
| 2 | #include <stdio.h>
|
---|
| 3 |
|
---|
| 4 | #include "gpsparser.h"
|
---|
| 5 |
|
---|
| 6 | GPSParser::GPSParser() {
|
---|
| 7 | *gpsString = 0;
|
---|
| 8 | stringOk = false;
|
---|
| 9 | gpsutc = 0;
|
---|
| 10 | gpslat = 0;
|
---|
| 11 | gpslong = 0;
|
---|
| 12 | quality = 0;
|
---|
| 13 | nsat = 0;
|
---|
| 14 | hdop = 0;
|
---|
| 15 | alt = 0;
|
---|
| 16 | gsep = 0;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | GPSParser::~GPSParser() {
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | bool GPSParser::hasTime() {
|
---|
| 23 | return (stringOk && gpsutc>0);
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | bool GPSParser::hasGPSTime() {
|
---|
| 27 | return (stringOk && gpsutc>0 && quality>0 && nsat>=3);
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | bool GPSParser::hasPosition() {
|
---|
| 31 | return (stringOk && quality>0 && nsat>=3);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | bool GPSParser::hasAltitude() {
|
---|
| 35 | return (stringOk && quality>0 && nsat>=4);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | double GPSParser::getUTC() {
|
---|
| 39 | if (!stringOk) return -99999;
|
---|
| 40 | return gpsutc;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | double GPSParser::getLatitude() {
|
---|
| 44 | if (!stringOk) return -99999;
|
---|
| 45 | if (quality == 0 || nsat<3) return -99999;
|
---|
| 46 | return gpslat;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | double GPSParser::getLongitude() {
|
---|
| 50 | if (!stringOk) return -99999;
|
---|
| 51 | if (quality == 0 || nsat<3) return -99999;
|
---|
| 52 | return gpslong;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | double GPSParser::getAltitude() {
|
---|
| 56 | if (!stringOk) return -99999;
|
---|
| 57 | if (quality == 0 || nsat<4) return -99999;
|
---|
| 58 | return alt;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | // GPGGA string :
|
---|
| 62 | // $hhmmss.ss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,,
|
---|
| 63 | // $104020,3740.712,N,00400.781,W,0,00,01.0,0003,M,-050,M,,
|
---|
| 64 |
|
---|
[346] | 65 | //#include <fstream.h>
|
---|
| 66 | //#include <iomanip.h>
|
---|
| 67 | //ofstream gpsdump("gpsdump");
|
---|
| 68 | //ofstream gpsdata("gpsdata");
|
---|
| 69 | //double outc = 0; double jr = 0;
|
---|
[342] | 70 |
|
---|
| 71 | void GPSParser::ProcessBlock(block_type_gps* blk) {
|
---|
| 72 | strncpy(gpsString, blk->gps, 80);
|
---|
| 73 | char* p = gpsString;
|
---|
[346] | 74 | // gpsdump << p ; if (p[strlen(p)-1] != '\n') gpsdump << '\n';
|
---|
[342] | 75 | char* fence = p+80;
|
---|
| 76 | stringOk = false;
|
---|
| 77 | if (*p!='$') return;
|
---|
| 78 | p++;
|
---|
| 79 |
|
---|
| 80 | // UTC
|
---|
| 81 | double t;
|
---|
| 82 | sscanf(p, "%lg", &t);
|
---|
| 83 | int h = int(t/10000); t -= h*10000;
|
---|
| 84 | int m = int(t/100); t -= m*100;
|
---|
| 85 | gpsutc = h*3600. + m*60. + t;
|
---|
| 86 | p += 6;
|
---|
| 87 | if (*p != ',') return;
|
---|
| 88 | p++;
|
---|
| 89 | if (p>=fence) return;
|
---|
| 90 |
|
---|
| 91 | // LATITUDE
|
---|
| 92 | sscanf(p, "%lg", &t);
|
---|
| 93 | int d = int(t/100); t -= d*100;
|
---|
| 94 | t = d + t/60;
|
---|
| 95 | p += 8;
|
---|
| 96 | if (*p != ',') return;
|
---|
| 97 | p++;
|
---|
| 98 | if (*p == 'S') t = -t;
|
---|
| 99 | gpslat = t;
|
---|
| 100 | p++;
|
---|
| 101 | if (*p != ',') return;
|
---|
| 102 | p++;
|
---|
| 103 | if (p>=fence) return;
|
---|
| 104 |
|
---|
| 105 | // LONGITUDE
|
---|
| 106 | sscanf(p, "%lg", &t);
|
---|
| 107 | d = int(t/100); t -= d*100;
|
---|
| 108 | t = d + t/60;
|
---|
| 109 | p += 9;
|
---|
| 110 | if (*p != ',') return;
|
---|
| 111 | p++;
|
---|
| 112 | if (*p == 'W') t = -t;
|
---|
| 113 | gpslong = t;
|
---|
| 114 | p++;
|
---|
| 115 | if (*p != ',') return;
|
---|
| 116 | p++;
|
---|
| 117 | if (p>=fence) return;
|
---|
| 118 |
|
---|
| 119 | // QUALITY
|
---|
| 120 | quality = *p - '0';
|
---|
| 121 | if (quality < 0 || quality > 9) return;
|
---|
| 122 | p++;
|
---|
| 123 | if (*p != ',') return;
|
---|
| 124 | p++;
|
---|
| 125 | if (p>=fence) return;
|
---|
| 126 |
|
---|
| 127 | // NSAT
|
---|
| 128 | sscanf(p, "%d", &nsat);
|
---|
| 129 | p += 2;
|
---|
| 130 | if (*p != ',') return;
|
---|
| 131 | p++;
|
---|
| 132 |
|
---|
| 133 | // HDOP
|
---|
| 134 | sscanf(p, "%lg", &hdop);
|
---|
| 135 | while (*p != ',' && p<fence) p++;
|
---|
| 136 | if (*p != ',') return;
|
---|
| 137 | p++;
|
---|
| 138 |
|
---|
| 139 | // Altitude
|
---|
| 140 | sscanf(p, "%lg", &alt);
|
---|
| 141 | while (*p != ',' && p<fence) p++;
|
---|
| 142 | if (*p != ',') return;
|
---|
| 143 | p++;
|
---|
| 144 | if (*p != 'M') return;
|
---|
| 145 | p++;
|
---|
| 146 | if (*p != ',') return;
|
---|
| 147 | p++;
|
---|
| 148 |
|
---|
| 149 | // Geoid separation
|
---|
| 150 | sscanf(p, "%lg", &gsep);
|
---|
| 151 | while (*p != ',' && p<fence) p++;
|
---|
| 152 | if (*p != ',') return;
|
---|
| 153 | p++;
|
---|
| 154 | if (*p != 'M') return;
|
---|
| 155 |
|
---|
[346] | 156 | //if (gpsutc < outc) jr += 86400;
|
---|
| 157 | //outc = gpsutc;
|
---|
| 158 | //gpsdata << setprecision(10) << gpsutc + jr << " " << gpslat << " " << gpslong << " " << quality << " " << nsat << '\n';
|
---|
[342] | 159 |
|
---|
| 160 | stringOk = true;
|
---|
| 161 | }
|
---|