[297] | 1 | // Part of BNC, a utility for retrieving decoding and
|
---|
[464] | 2 | // converting GNSS data streams from NTRIP broadcasters.
|
---|
[297] | 3 | //
|
---|
[464] | 4 | // Copyright (C) 2007
|
---|
[297] | 5 | // German Federal Agency for Cartography and Geodesy (BKG)
|
---|
| 6 | // http://www.bkg.bund.de
|
---|
[464] | 7 | // Czech Technical University Prague, Department of Geodesy
|
---|
[297] | 8 | // http://www.fsv.cvut.cz
|
---|
| 9 | //
|
---|
| 10 | // Email: euref-ip@bkg.bund.de
|
---|
| 11 | //
|
---|
| 12 | // This program is free software; you can redistribute it and/or
|
---|
| 13 | // modify it under the terms of the GNU General Public License
|
---|
| 14 | // as published by the Free Software Foundation, version 2.
|
---|
| 15 | //
|
---|
| 16 | // This program is distributed in the hope that it will be useful,
|
---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | // GNU General Public License for more details.
|
---|
| 20 | //
|
---|
| 21 | // You should have received a copy of the GNU General Public License
|
---|
| 22 | // along with this program; if not, write to the Free Software
|
---|
| 23 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
[296] | 24 |
|
---|
| 25 | /* -------------------------------------------------------------------------
|
---|
| 26 | * BKG NTRIP Client
|
---|
| 27 | * -------------------------------------------------------------------------
|
---|
| 28 | *
|
---|
| 29 | * Class: RTCM3Decoder
|
---|
| 30 | *
|
---|
| 31 | * Purpose: RTCM3 Decoder
|
---|
| 32 | *
|
---|
| 33 | * Author: L. Mervart
|
---|
| 34 | *
|
---|
| 35 | * Created: 24-Aug-2006
|
---|
| 36 | *
|
---|
| 37 | * Changes:
|
---|
| 38 | *
|
---|
| 39 | * -----------------------------------------------------------------------*/
|
---|
| 40 |
|
---|
| 41 | #include <iostream>
|
---|
[1807] | 42 | #include <iomanip>
|
---|
| 43 | #include <sstream>
|
---|
[296] | 44 | #include <math.h>
|
---|
[585] | 45 | #include <string.h>
|
---|
[296] | 46 |
|
---|
| 47 | #include "RTCM3Decoder.h"
|
---|
[1807] | 48 | #include "../RTCM/rtcm_utils.h"
|
---|
[296] | 49 | #include "bncconst.h"
|
---|
[511] | 50 | #include "bncapp.h"
|
---|
[1535] | 51 | #include "bncutils.h"
|
---|
| 52 | #include "bncsettings.h"
|
---|
[296] | 53 |
|
---|
| 54 | using namespace std;
|
---|
| 55 |
|
---|
| 56 | #ifndef isinf
|
---|
| 57 | # define isinf(x) 0
|
---|
| 58 | #endif
|
---|
| 59 |
|
---|
[320] | 60 | // Error Handling
|
---|
| 61 | ////////////////////////////////////////////////////////////////////////////
|
---|
[504] | 62 | void RTCM3Error(const char*, ...) {
|
---|
[505] | 63 | }
|
---|
[320] | 64 |
|
---|
[296] | 65 | // Constructor
|
---|
| 66 | ////////////////////////////////////////////////////////////////////////////
|
---|
[2527] | 67 | RTCM3Decoder::RTCM3Decoder(const QString& staID, bncRawFile* rawFile) :
|
---|
[2387] | 68 | GPSDecoder() {
|
---|
[505] | 69 |
|
---|
[2527] | 70 | _staID = staID;
|
---|
| 71 | _rawFile = rawFile;
|
---|
[2387] | 72 |
|
---|
[1535] | 73 | bncSettings settings;
|
---|
[1580] | 74 | _checkMountPoint = settings.value("miscMount").toString();
|
---|
[1022] | 75 |
|
---|
[939] | 76 | connect(this, SIGNAL(newGPSEph(gpsephemeris*)),
|
---|
| 77 | (bncApp*) qApp, SLOT(slotNewGPSEph(gpsephemeris*)));
|
---|
| 78 | connect(this, SIGNAL(newGlonassEph(glonassephemeris*)),
|
---|
| 79 | (bncApp*) qApp, SLOT(slotNewGlonassEph(glonassephemeris*)));
|
---|
| 80 |
|
---|
[880] | 81 | // Sub-Decoder for Clock and Orbit Corrections
|
---|
| 82 | // -------------------------------------------
|
---|
[970] | 83 | _coDecoder = new RTCM3coDecoder(staID);
|
---|
[1021] | 84 |
|
---|
| 85 | // Mode can be either observations or corrections
|
---|
| 86 | // ----------------------------------------------
|
---|
| 87 | _mode = unknown;
|
---|
[1807] | 88 |
|
---|
| 89 | // Antenna position (used for decoding of message 1003)
|
---|
| 90 | // ----------------------------------------------------
|
---|
| 91 | _antXYZ[0] = _antXYZ[1] = _antXYZ[2] = 0;
|
---|
| 92 |
|
---|
[296] | 93 | }
|
---|
| 94 |
|
---|
| 95 | // Destructor
|
---|
| 96 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 97 | RTCM3Decoder::~RTCM3Decoder() {
|
---|
[880] | 98 | delete _coDecoder;
|
---|
[296] | 99 | }
|
---|
| 100 |
|
---|
| 101 | //
|
---|
| 102 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1218] | 103 | t_irc RTCM3Decoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
|
---|
[508] | 104 |
|
---|
[1218] | 105 | errmsg.clear();
|
---|
| 106 |
|
---|
[913] | 107 | bool decoded = false;
|
---|
| 108 |
|
---|
[880] | 109 | // Try to decode Clock and Orbit Corrections
|
---|
| 110 | // -----------------------------------------
|
---|
[1021] | 111 | if (_mode == unknown || _mode == corrections) {
|
---|
[1218] | 112 | if ( _coDecoder->Decode(buffer, bufLen, errmsg) == success ) {
|
---|
[1021] | 113 | decoded = true;
|
---|
[2527] | 114 | if (!_rawFile && _mode == unknown) {
|
---|
[2387] | 115 | _mode = corrections;
|
---|
[1021] | 116 | }
|
---|
| 117 | }
|
---|
[913] | 118 | }
|
---|
[880] | 119 |
|
---|
[2527] | 120 | // Find the corresponding parser
|
---|
| 121 | // -----------------------------
|
---|
| 122 | QByteArray staID("default");
|
---|
| 123 | if (_rawFile) {
|
---|
| 124 | staID = _rawFile->staID();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | bool newParser = !_parsers.contains(staID);
|
---|
| 128 |
|
---|
| 129 | RTCM3ParserData& parser = _parsers[staID];
|
---|
| 130 |
|
---|
| 131 | // Initialize a new parser
|
---|
| 132 | // -----------------------
|
---|
| 133 | if (newParser) {
|
---|
| 134 | parser.rinex3 = 0;
|
---|
| 135 | memset(&parser, 0, sizeof(parser));
|
---|
| 136 | double secGPS;
|
---|
| 137 | currentGPSWeeks(parser.GPSWeek, secGPS);
|
---|
| 138 | parser.GPSTOW = int(secGPS);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[880] | 141 | // Remaining part decodes the Observations
|
---|
| 142 | // ---------------------------------------
|
---|
[1033] | 143 | if (_mode == unknown || _mode == observations || _checkMountPoint == _staID || _checkMountPoint == "ALL") {
|
---|
[1127] | 144 |
|
---|
[1021] | 145 | for (int ii = 0; ii < bufLen; ii++) {
|
---|
[2527] | 146 | parser.Message[parser.MessageSize++] = buffer[ii];
|
---|
[1127] | 147 |
|
---|
[2527] | 148 | if (parser.MessageSize >= parser.NeedBytes) {
|
---|
[1185] | 149 |
|
---|
[2527] | 150 | while(int rr = RTCM3Parser(&parser)) {
|
---|
[1130] | 151 |
|
---|
[1239] | 152 | // RTCMv3 message types
|
---|
| 153 | // --------------------
|
---|
[2527] | 154 | _typeList.push_back(parser.blocktype);
|
---|
[1185] | 155 |
|
---|
[1239] | 156 | // RTCMv3 antenna descriptor
|
---|
| 157 | // -------------------------
|
---|
| 158 | if(rr == 1007 || rr == 1008 || rr == 1033)
|
---|
| 159 | {
|
---|
[2527] | 160 | _antType.push_back(parser.antenna); /* correct ? */
|
---|
[1239] | 161 | }
|
---|
[1185] | 162 |
|
---|
[1239] | 163 | // RTCMv3 antenna XYZ
|
---|
| 164 | // ------------------
|
---|
| 165 | else if(rr == 1005)
|
---|
| 166 | {
|
---|
[1268] | 167 | _antList.push_back(t_antInfo());
|
---|
| 168 | _antList.back().type = t_antInfo::ARP;
|
---|
[2527] | 169 | _antList.back().xx = parser.antX * 1e-4;
|
---|
| 170 | _antList.back().yy = parser.antY * 1e-4;
|
---|
| 171 | _antList.back().zz = parser.antZ * 1e-4;
|
---|
[1268] | 172 | _antList.back().message = rr;
|
---|
[1807] | 173 |
|
---|
| 174 | // Remember station position for 1003 message decoding
|
---|
[2527] | 175 | _antXYZ[0] = parser.antX * 1e-4;
|
---|
| 176 | _antXYZ[1] = parser.antY * 1e-4;
|
---|
| 177 | _antXYZ[2] = parser.antZ * 1e-4;
|
---|
[1239] | 178 | }
|
---|
[1033] | 179 |
|
---|
[1239] | 180 | // RTCMv3 antenna XYZ-H
|
---|
| 181 | // --------------------
|
---|
| 182 | else if(rr == 1006)
|
---|
| 183 | {
|
---|
[1268] | 184 | _antList.push_back(t_antInfo());
|
---|
| 185 | _antList.back().type = t_antInfo::ARP;
|
---|
[2527] | 186 | _antList.back().xx = parser.antX * 1e-4;
|
---|
| 187 | _antList.back().yy = parser.antY * 1e-4;
|
---|
| 188 | _antList.back().zz = parser.antZ * 1e-4;
|
---|
| 189 | _antList.back().height = parser.antH * 1e-4;
|
---|
[1268] | 190 | _antList.back().height_f = true;
|
---|
| 191 | _antList.back().message = rr;
|
---|
[1807] | 192 |
|
---|
| 193 | // Remember station position for 1003 message decoding
|
---|
[2527] | 194 | _antXYZ[0] = parser.antX * 1e-4;
|
---|
| 195 | _antXYZ[1] = parser.antY * 1e-4;
|
---|
| 196 | _antXYZ[2] = parser.antZ * 1e-4;
|
---|
[1239] | 197 | }
|
---|
| 198 |
|
---|
[1021] | 199 | // GNSS Observations
|
---|
| 200 | // -----------------
|
---|
[1239] | 201 | else if (rr == 1 || rr == 2) {
|
---|
[1127] | 202 | decoded = true;
|
---|
[1021] | 203 |
|
---|
[2527] | 204 | if (!parser.init) {
|
---|
| 205 | HandleHeader(&parser);
|
---|
| 206 | parser.init = 1;
|
---|
[1021] | 207 | }
|
---|
| 208 |
|
---|
[1807] | 209 | // apply "GPS Integer L1 Pseudorange Modulus Ambiguity"
|
---|
| 210 | bool applyModulusAmb = false;
|
---|
| 211 | ///if (rr == 2) {
|
---|
| 212 | /// applyModulusAmb = true;
|
---|
| 213 | ///}
|
---|
| 214 |
|
---|
[1021] | 215 | if (rr == 2) {
|
---|
[1299] | 216 | emit(newMessage( (_staID + ": No valid RINEX! All values are modulo 299792.458!").toAscii(), true));
|
---|
[1021] | 217 | }
|
---|
| 218 |
|
---|
[2527] | 219 | for (int ii = 0; ii < parser.Data.numsats; ii++) {
|
---|
[1021] | 220 | p_obs obs = new t_obs();
|
---|
| 221 | _obsList.push_back(obs);
|
---|
[2527] | 222 | if (parser.Data.satellites[ii] <= PRN_GPS_END) {
|
---|
[1021] | 223 | obs->_o.satSys = 'G';
|
---|
[2527] | 224 | obs->_o.satNum = parser.Data.satellites[ii];
|
---|
[366] | 225 | }
|
---|
[2527] | 226 | else if (parser.Data.satellites[ii] <= PRN_GLONASS_END) {
|
---|
[1021] | 227 | obs->_o.satSys = 'R';
|
---|
[2527] | 228 | obs->_o.satNum = parser.Data.satellites[ii] - PRN_GLONASS_START + 1;
|
---|
| 229 | obs->_o.slot = parser.Data.channels[ii];
|
---|
[1021] | 230 | }
|
---|
[511] | 231 | else {
|
---|
[1021] | 232 | obs->_o.satSys = 'S';
|
---|
[2527] | 233 | obs->_o.satNum = parser.Data.satellites[ii] - PRN_WAAS_START + 20;
|
---|
[1021] | 234 | }
|
---|
[2527] | 235 | obs->_o.GPSWeek = parser.Data.week;
|
---|
| 236 | obs->_o.GPSWeeks = parser.Data.timeofweek / 1000.0;
|
---|
[1807] | 237 |
|
---|
| 238 | // Estimate "GPS Integer L1 Pseudorange Modulus Ambiguity"
|
---|
| 239 | // -------------------------------------------------------
|
---|
| 240 | double modulusAmb = 0;
|
---|
| 241 | if (applyModulusAmb) {
|
---|
| 242 | // Missing antenna coordinates: skip all data
|
---|
| 243 | if ( !_antXYZ[0] && !_antXYZ[1] && !_antXYZ[2] ) {
|
---|
| 244 | continue;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | ostringstream prns;
|
---|
| 248 | prns << obs->_o.satSys << setfill('0') << setw(2) << obs->_o.satNum;
|
---|
| 249 |
|
---|
| 250 | string prn = prns.str();
|
---|
| 251 |
|
---|
| 252 | // Missing ephemerides, skip satellite
|
---|
| 253 | if (_ephList.find(prn) == _ephList.end()) {
|
---|
| 254 | continue;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | const t_eph* eph = &(_ephList.find(prn)->second);
|
---|
| 258 |
|
---|
| 259 | double rho, xSat, ySat, zSat, clkSat, GPSWeeks_tot;
|
---|
| 260 | int GPSWeek_tot;
|
---|
| 261 | cmpRho(eph, _antXYZ[0], _antXYZ[1], _antXYZ[2],
|
---|
| 262 | obs->_o.GPSWeek, obs->_o.GPSWeeks,
|
---|
| 263 | rho, GPSWeek_tot, GPSWeeks_tot,
|
---|
| 264 | xSat, ySat, zSat, clkSat);
|
---|
| 265 |
|
---|
| 266 | const double CC = 299792458.0;
|
---|
| 267 |
|
---|
| 268 | int nn = static_cast<int>(rho / (CC * 0.001));
|
---|
| 269 |
|
---|
| 270 | modulusAmb = nn * CC * 0.001;
|
---|
| 271 | }
|
---|
[1021] | 272 |
|
---|
[1807] | 273 | // Loop over all data types
|
---|
| 274 | // ------------------------
|
---|
[2527] | 275 | for (int jj = 0; jj < parser.numdatatypesGPS; jj++) {
|
---|
[1021] | 276 | int v = 0;
|
---|
| 277 | // sepearated declaration and initalization of df and pos. Perlt
|
---|
| 278 | int df;
|
---|
| 279 | int pos;
|
---|
[2527] | 280 | df = parser.dataflag[jj];
|
---|
| 281 | pos = parser.datapos[jj];
|
---|
| 282 | if ( (parser.Data.dataflags[ii] & df)
|
---|
| 283 | && !isnan(parser.Data.measdata[ii][pos])
|
---|
| 284 | && !isinf(parser.Data.measdata[ii][pos])) {
|
---|
[1021] | 285 | v = 1;
|
---|
[511] | 286 | }
|
---|
[1021] | 287 | else {
|
---|
[2527] | 288 | df = parser.dataflagGPS[jj];
|
---|
| 289 | pos = parser.dataposGPS[jj];
|
---|
| 290 | if ( (parser.Data.dataflags[ii] & df)
|
---|
| 291 | && !isnan(parser.Data.measdata[ii][pos])
|
---|
| 292 | && !isinf(parser.Data.measdata[ii][pos])) {
|
---|
[1126] | 293 | v = 1;
|
---|
[1021] | 294 | }
|
---|
[627] | 295 | }
|
---|
[1021] | 296 | if (!v) {
|
---|
| 297 | continue;
|
---|
[627] | 298 | }
|
---|
[1021] | 299 | else
|
---|
| 300 | {
|
---|
[2527] | 301 | int isat = (parser.Data.satellites[ii] < 120
|
---|
| 302 | ? parser.Data.satellites[ii]
|
---|
| 303 | : parser.Data.satellites[ii] - 80);
|
---|
[1126] | 304 |
|
---|
[1021] | 305 | // variables df and pos are used consequently. Perlt
|
---|
| 306 | if (df & GNSSDF_C1DATA) {
|
---|
[2527] | 307 | obs->_o.C1 = parser.Data.measdata[ii][pos] + modulusAmb;
|
---|
[1021] | 308 | }
|
---|
| 309 | else if (df & GNSSDF_C2DATA) {
|
---|
[2527] | 310 | obs->_o.C2 = parser.Data.measdata[ii][pos] + modulusAmb;
|
---|
[1021] | 311 | }
|
---|
| 312 | else if (df & GNSSDF_P1DATA) {
|
---|
[2527] | 313 | obs->_o.P1 = parser.Data.measdata[ii][pos] + modulusAmb;
|
---|
[1021] | 314 | }
|
---|
| 315 | else if (df & GNSSDF_P2DATA) {
|
---|
[2527] | 316 | obs->_o.P2 = parser.Data.measdata[ii][pos] + modulusAmb;
|
---|
[1021] | 317 | }
|
---|
| 318 | else if (df & (GNSSDF_L1CDATA|GNSSDF_L1PDATA)) {
|
---|
[2527] | 319 | obs->_o.L1 = parser.Data.measdata[ii][pos] + modulusAmb;
|
---|
| 320 | obs->_o.SNR1 = parser.Data.snrL1[ii];
|
---|
| 321 | obs->_o.lock_timei_L1 = parser.lastlockGPSl1[isat];
|
---|
[1021] | 322 | }
|
---|
| 323 | else if (df & (GNSSDF_L2CDATA|GNSSDF_L2PDATA)) {
|
---|
[2527] | 324 | obs->_o.L2 = parser.Data.measdata[ii][pos] + modulusAmb;
|
---|
| 325 | obs->_o.SNR2 = parser.Data.snrL2[ii];
|
---|
| 326 | obs->_o.lock_timei_L2 = parser.lastlockGPSl2[isat];
|
---|
[1021] | 327 | }
|
---|
| 328 | else if (df & (GNSSDF_S1CDATA|GNSSDF_S1PDATA)) {
|
---|
[2527] | 329 | obs->_o.S1 = parser.Data.measdata[ii][pos];
|
---|
[1021] | 330 | }
|
---|
| 331 | else if (df & (GNSSDF_S2CDATA|GNSSDF_S2PDATA)) {
|
---|
[2527] | 332 | obs->_o.S2 = parser.Data.measdata[ii][pos];
|
---|
[1021] | 333 | }
|
---|
[627] | 334 | }
|
---|
[511] | 335 | }
|
---|
[366] | 336 | }
|
---|
[296] | 337 | }
|
---|
[1021] | 338 |
|
---|
| 339 | // GPS Ephemeris
|
---|
| 340 | // -------------
|
---|
| 341 | else if (rr == 1019) {
|
---|
| 342 | decoded = true;
|
---|
[2527] | 343 | gpsephemeris* ep = new gpsephemeris(parser.ephemerisGPS);
|
---|
[1021] | 344 | emit newGPSEph(ep);
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | // GLONASS Ephemeris
|
---|
| 348 | // -----------------
|
---|
| 349 | else if (rr == 1020) {
|
---|
| 350 | decoded = true;
|
---|
[2527] | 351 | glonassephemeris* ep = new glonassephemeris(parser.ephemerisGLONASS);
|
---|
[1021] | 352 | emit newGlonassEph(ep);
|
---|
| 353 | }
|
---|
[296] | 354 | }
|
---|
| 355 | }
|
---|
| 356 | }
|
---|
[2527] | 357 | if (!_rawFile && _mode == unknown && decoded) {
|
---|
[2387] | 358 | _mode = observations;
|
---|
[1021] | 359 | }
|
---|
[296] | 360 | }
|
---|
[1021] | 361 |
|
---|
| 362 | if (decoded) {
|
---|
| 363 | return success;
|
---|
[658] | 364 | }
|
---|
| 365 | else {
|
---|
[1021] | 366 | return failure;
|
---|
[658] | 367 | }
|
---|
[296] | 368 | }
|
---|
[1807] | 369 |
|
---|
| 370 | // Store ephemerides
|
---|
| 371 | ////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 372 | bool RTCM3Decoder::storeEph(const gpsephemeris& gpseph) {
|
---|
| 373 | t_ephGPS eph; eph.set(&gpseph);
|
---|
| 374 |
|
---|
| 375 | return storeEph(eph);
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 |
|
---|
| 379 | bool RTCM3Decoder::storeEph(const t_ephGPS& gpseph) {
|
---|
[2463] | 380 | const double secPerWeek = 7.0 * 24.0 * 3600.0;
|
---|
[1807] | 381 | double weekold = 0.0;
|
---|
[2463] | 382 | double weeknew = gpseph.GPSweek() + gpseph.GPSweeks() / secPerWeek;
|
---|
[1807] | 383 | if ( _ephList.find(gpseph.prn()) != _ephList.end() ) {
|
---|
| 384 | weekold = _ephList.find(gpseph.prn())->second.GPSweek()
|
---|
[2463] | 385 | + _ephList.find(gpseph.prn())->second.GPSweeks() / secPerWeek;
|
---|
[1807] | 386 | }
|
---|
| 387 |
|
---|
[2463] | 388 | if ( weeknew - weekold > 1.0/secPerWeek ) {
|
---|
[1807] | 389 | _ephList[gpseph.prn()] = gpseph;
|
---|
| 390 |
|
---|
| 391 | return true;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | return false;
|
---|
| 395 | }
|
---|