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