source: ntrip/trunk/BNC/src/RTCM3/RTCM3Decoder.cpp@ 6383

Last change on this file since 6383 was 6383, checked in by mervart, 9 years ago
File size: 13.9 KB
RevLine 
[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"
[5070]50#include "bnccore.h"
[1535]51#include "bncutils.h"
52#include "bncsettings.h"
[296]53
54using namespace std;
55
56#ifndef isinf
57# define isinf(x) 0
58#endif
59
[320]60// Error Handling
61////////////////////////////////////////////////////////////////////////////
[504]62void RTCM3Error(const char*, ...) {
[505]63}
[320]64
[296]65// Constructor
66////////////////////////////////////////////////////////////////////////////
[2527]67RTCM3Decoder::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*)),
[5068]77 BNC_CORE, SLOT(slotNewGPSEph(gpsephemeris*)));
[5206]78 connect(this, SIGNAL(newGlonassEph(glonassephemeris*, const QString&)),
79 BNC_CORE, SLOT(slotNewGlonassEph(glonassephemeris*, const QString&)));
[2770]80 connect(this, SIGNAL(newGalileoEph(galileoephemeris*)),
[5068]81 BNC_CORE, SLOT(slotNewGalileoEph(galileoephemeris*)));
[6383]82 connect(this, SIGNAL(newSBASEph(sbasephemeris*)),
83 BNC_CORE, SLOT(slotNewSBASEph(sbasephemeris*)));
[939]84
[1021]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////////////////////////////////////////////////////////////////////////////
97RTCM3Decoder::~RTCM3Decoder() {
[3001]98 QMapIterator<QByteArray, RTCM3coDecoder*> it(_coDecoders);
99 while (it.hasNext()) {
[3008]100 it.next();
101 delete it.value();
[3001]102 }
[296]103}
104
105//
106////////////////////////////////////////////////////////////////////////////
[1218]107t_irc RTCM3Decoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
[508]108
[1218]109 errmsg.clear();
110
[913]111 bool decoded = false;
112
[3001]113 // If read from file, mode is always uknown
114 // ----------------------------------------
115 if (_rawFile) {
116 _mode = unknown;
117 _staID = _rawFile->staID();
[2551]118 }
119
[880]120 // Try to decode Clock and Orbit Corrections
121 // -----------------------------------------
[1021]122 if (_mode == unknown || _mode == corrections) {
[3002]123
124 // Find the corresponding coDecoder
125 // --------------------------------
126 if (!_coDecoders.contains(_staID.toAscii())) {
127 _coDecoders[_staID.toAscii()] = new RTCM3coDecoder(_staID);
128 }
129 RTCM3coDecoder* coDecoder = _coDecoders[_staID.toAscii()];
130
[3001]131 if ( coDecoder->Decode(buffer, bufLen, errmsg) == success ) {
[1021]132 decoded = true;
[3001]133 if (!_rawFile && _mode == unknown) {
134 _mode = corrections;
[1021]135 }
136 }
[913]137 }
[880]138
[3001]139 // Find the corresponding parser, initialize a new parser if necessary
140 // -------------------------------------------------------------------
141 bool newParser = !_parsers.contains(_staID.toAscii());
142 RTCM3ParserData& parser = _parsers[_staID.toAscii()];
143 if (newParser) {
144 memset(&parser, 0, sizeof(parser));
145 parser.rinex3 = 0;
146 double secGPS;
147 currentGPSWeeks(parser.GPSWeek, secGPS);
148 parser.GPSTOW = int(secGPS);
[2527]149 }
150
[880]151 // Remaining part decodes the Observations
152 // ---------------------------------------
[2676]153 if (_mode == unknown || _mode == observations ||
154 _checkMountPoint == _staID || _checkMountPoint == "ALL") {
[1127]155
[2677]156 for (int iByte = 0; iByte < bufLen; iByte++) {
[1127]157
[2677]158 parser.Message[parser.MessageSize++] = buffer[iByte];
159
[2527]160 if (parser.MessageSize >= parser.NeedBytes) {
[1185]161
[2676]162 while (int rr = RTCM3Parser(&parser)) {
[1130]163
[1239]164 // RTCMv3 message types
165 // --------------------
[6376]166 _typeList.push_back(rr);
[1185]167
[1239]168 // RTCMv3 antenna descriptor
169 // -------------------------
[2676]170 if (rr == 1007 || rr == 1008 || rr == 1033) {
171 _antType.push_back(parser.antenna);
[1239]172 }
[1185]173
[1239]174 // RTCMv3 antenna XYZ
175 // ------------------
[2676]176 else if (rr == 1005) {
177 _antList.push_back(t_antInfo());
178 _antList.back().type = t_antInfo::ARP;
179 _antList.back().xx = parser.antX * 1e-4;
180 _antList.back().yy = parser.antY * 1e-4;
181 _antList.back().zz = parser.antZ * 1e-4;
182 _antList.back().message = rr;
[1807]183
[2676]184 // Remember station position for 1003 message decoding
185 _antXYZ[0] = parser.antX * 1e-4;
186 _antXYZ[1] = parser.antY * 1e-4;
187 _antXYZ[2] = parser.antZ * 1e-4;
[1239]188 }
[1033]189
[1239]190 // RTCMv3 antenna XYZ-H
191 // --------------------
[2676]192 else if(rr == 1006) {
193 _antList.push_back(t_antInfo());
194 _antList.back().type = t_antInfo::ARP;
195 _antList.back().xx = parser.antX * 1e-4;
196 _antList.back().yy = parser.antY * 1e-4;
197 _antList.back().zz = parser.antZ * 1e-4;
198 _antList.back().height = parser.antH * 1e-4;
199 _antList.back().height_f = true;
200 _antList.back().message = rr;
[1807]201
[2676]202 // Remember station position for 1003 message decoding
203 _antXYZ[0] = parser.antX * 1e-4;
204 _antXYZ[1] = parser.antY * 1e-4;
205 _antXYZ[2] = parser.antZ * 1e-4;
[1239]206 }
207
[1021]208 // GNSS Observations
209 // -----------------
[1239]210 else if (rr == 1 || rr == 2) {
[1127]211 decoded = true;
[1021]212
[2527]213 if (!parser.init) {
214 HandleHeader(&parser);
215 parser.init = 1;
[1021]216 }
217
218 if (rr == 2) {
[2676]219 emit(newMessage( (_staID +
220 ": No valid RINEX! All values are modulo 299792.458!").toAscii(),
221 true));
[1021]222 }
[2683]223
224 gnssdata& gnssData = parser.Data;
[1021]225
[2683]226 for (int iSat = 0; iSat < gnssData.numsats; iSat++) {
[2677]227
[6137]228 t_satObs obs;
[2711]229 int satID = gnssData.satellites[iSat];
[2674]230
231 // GPS
232 // ---
233 if (satID >= PRN_GPS_START && satID <= PRN_GPS_END) {
[6137]234 obs._prn.set('G', satID);
[366]235 }
[2674]236
237 // Glonass
238 // -------
239 else if (satID >= PRN_GLONASS_START && satID <= PRN_GLONASS_END) {
[6137]240 obs._prn.set('R', satID - PRN_GLONASS_START + 1);
[1021]241 }
[2674]242
243 // Galileo
244 // -------
245 else if (satID >= PRN_GALILEO_START && satID <= PRN_GALILEO_END) {
[6137]246 obs._prn.set('E', satID - PRN_GALILEO_START + 1);
[2676]247 }
[2674]248
[4368]249 // SBAS
[2674]250 // ----
[4368]251 else if (satID >= PRN_SBAS_START && satID <= PRN_SBAS_END) {
[6137]252 obs._prn.set('S', satID - PRN_SBAS_START + 20);
[1021]253 }
[2669]254
[2674]255 // Giove A and B
256 // -------------
257 else if (satID >= PRN_GIOVE_START && satID <= PRN_GIOVE_END) {
[6137]258 obs._prn.set('E', satID - PRN_GIOVE_START + PRN_GIOVE_OFFSET);
[2676]259 }
[2674]260
[4368]261 // QZSS
262 // -------------
263 else if (satID >= PRN_QZSS_START && satID <= PRN_QZSS_END) {
[6137]264 obs._prn.set('J', satID - PRN_QZSS_START + 1);
[4368]265 }
266
267 // COMPASS
268 // -------------
269 else if (satID >= PRN_COMPASS_START && satID <= PRN_COMPASS_END) {
[6137]270 obs._prn.set('C', satID - PRN_COMPASS_START + 1);
[4368]271 }
272
[2674]273 // Unknown System
274 // --------------
275 else {
[2669]276 continue;
277 }
278
[6137]279 obs._time.set(gnssData.week, gnssData.timeofweek / 1000.0);
[1807]280
[6137]281 QString prn(obs._prn.toString().c_str());
[2687]282
[6137]283 int obs_slip_cnt_L1 = 0;
284 int obs_slip_cnt_L2 = 0;
285 int obs_slip_cnt_L5 = 0;
286
[2687]287 // Handle loss-of-lock flags
288 // -------------------------
289 const int maxSlipCnt = 100;
290 if (!_slip_cnt_L1.contains(prn)) {
291 _slip_cnt_L1[prn] = 0;
292 _slip_cnt_L2[prn] = 0;
293 _slip_cnt_L5[prn] = 0;
294 }
295 if (GNSSDF2_LOCKLOSSL1 & gnssData.dataflags2[iSat]) {
296 if (_slip_cnt_L1[prn] < maxSlipCnt) {
297 ++_slip_cnt_L1[prn];
298 }
299 else {
300 _slip_cnt_L1[prn] = 1;
301 }
[6137]302 obs_slip_cnt_L1 = _slip_cnt_L1[prn];
[2687]303 }
304 if (GNSSDF2_LOCKLOSSL2 & gnssData.dataflags2[iSat]) {
305 if (_slip_cnt_L2[prn] < maxSlipCnt) {
306 ++_slip_cnt_L2[prn];
307 }
308 else {
309 _slip_cnt_L2[prn] = 1;
310 }
[6137]311 obs_slip_cnt_L2 = _slip_cnt_L2[prn];
[2687]312 }
313 if (GNSSDF2_LOCKLOSSL5 & gnssData.dataflags2[iSat]) {
314 if (_slip_cnt_L5[prn] < maxSlipCnt) {
315 ++_slip_cnt_L5[prn];
316 }
317 else {
318 _slip_cnt_L5[prn] = 1;
319 }
[6137]320 obs_slip_cnt_L5 = _slip_cnt_L5[prn];
[2687]321 }
322
[2676]323 // Loop over all data types
324 // ------------------------
[2684]325 for (int iEntry = 0; iEntry < GNSSENTRY_NUMBER; ++iEntry) {
[6137]326 if (gnssData.codetype[iSat][iEntry] == 0) {
327 continue;
328 }
329 string rnxType(gnssData.codetype[iSat][iEntry]);
330
331 t_frqObs* frqObs = 0;
332 for (unsigned iFrq = 0; iFrq < obs._obs.size(); iFrq++) {
333 if (obs._obs[iFrq]->_rnxType2ch == rnxType) {
334 frqObs = obs._obs[iFrq];
335 break;
336 }
337 }
338 if (frqObs == 0) {
339 frqObs = new t_frqObs;
340 frqObs->_rnxType2ch = rnxType;
341 obs._obs.push_back(frqObs);
342 }
343
344 switch(iEntry & 3) {
345 case GNSSENTRY_CODE:
346 frqObs->_codeValid = true;
347 frqObs->_code = gnssData.measdata[iSat][iEntry];
348 break;
349 case GNSSENTRY_PHASE:
350 frqObs->_phaseValid = true;
351 frqObs->_phase = gnssData.measdata[iSat][iEntry];
352 if (rnxType[0] == '1') {
353 frqObs->_slipCounter = obs_slip_cnt_L1;
354 }
355 else if (rnxType[0] == '2') {
356 frqObs->_slipCounter = obs_slip_cnt_L2;
357 }
358 else if (rnxType[0] == '5') {
359 frqObs->_slipCounter = obs_slip_cnt_L5;
360 }
361 break;
362 case GNSSENTRY_DOPPLER:
363 frqObs->_dopplerValid = true;
364 frqObs->_doppler = gnssData.measdata[iSat][iEntry];
365 break;
366 case GNSSENTRY_SNR:
367 frqObs->_snrValid = true;
368 frqObs->_snr = gnssData.measdata[iSat][iEntry];
369 break;
370 }
[511]371 }
[2711]372 _obsList.push_back(obs);
[366]373 }
[296]374 }
[1021]375
376 // GPS Ephemeris
377 // -------------
378 else if (rr == 1019) {
379 decoded = true;
[2676]380 emit newGPSEph(new gpsephemeris(parser.ephemerisGPS));
[1021]381 }
382
383 // GLONASS Ephemeris
384 // -----------------
[6383]385 else if (rr == 1020 && parser.ephemerisGLONASS.almanac_number >= 1 &&
386 parser.ephemerisGLONASS.almanac_number <= PRN_GLONASS_NUM) {
[1021]387 decoded = true;
[5206]388 emit newGlonassEph(new glonassephemeris(parser.ephemerisGLONASS), _staID);
[1021]389 }
[2770]390
391 // Galileo Ephemeris
392 // -----------------
[5545]393 else if (rr == 1045 || rr == 1046) {
[2770]394 decoded = true;
395 emit newGalileoEph(new galileoephemeris(parser.ephemerisGALILEO));
396 }
[6372]397
398 // QZSS Ephemeris
399 // --------------
400 else if (rr == 1044) {
401 decoded = true;
402 emit newGPSEph(new gpsephemeris(parser.ephemerisGPS));
403 }
[6383]404
405 // SBAS Ephemeris
406 // --------------
407 else if (rr == 1043) {
408 decoded = true;
409 emit newSBASEph(new sbasephemeris(parser.ephemerisSBAS));
410 }
[296]411 }
412 }
413 }
[2527]414 if (!_rawFile && _mode == unknown && decoded) {
[2387]415 _mode = observations;
[1021]416 }
[296]417 }
[1021]418
419 if (decoded) {
420 return success;
[658]421 }
422 else {
[1021]423 return failure;
[658]424 }
[296]425}
[1807]426
[3001]427// Time of Corrections
428//////////////////////////////////////////////////////////////////////////////
429int RTCM3Decoder::corrGPSEpochTime() const {
430 if (_mode == corrections && _coDecoders.size() > 0) {
431 return _coDecoders.begin().value()->corrGPSEpochTime();
432 }
433 else {
434 return -1;
435 }
436}
Note: See TracBrowser for help on using the repository browser.