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

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