source: ntrip/trunk/BNC/RTCM3/RTCM3Decoder.cpp@ 3090

Last change on this file since 3090 was 3008, checked in by mervart, 14 years ago
File size: 15.3 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"
[511]50#include "bncapp.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*)),
[2585]77 (bncApp*) qApp, SLOT(slotNewGPSEph(gpsephemeris*)));
[939]78 connect(this, SIGNAL(newGlonassEph(glonassephemeris*)),
[2585]79 (bncApp*) qApp, SLOT(slotNewGlonassEph(glonassephemeris*)));
[2770]80 connect(this, SIGNAL(newGalileoEph(galileoephemeris*)),
81 (bncApp*) qApp, 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 // ------------------------------------------
151 bncApp* app = (bncApp*) qApp;
152 app->getGlonassSlotNums(parser.GLOFreq);
153
[880]154 // Remaining part decodes the Observations
155 // ---------------------------------------
[2676]156 if (_mode == unknown || _mode == observations ||
157 _checkMountPoint == _staID || _checkMountPoint == "ALL") {
[1127]158
[2677]159 for (int iByte = 0; iByte < bufLen; iByte++) {
[1127]160
[2677]161 parser.Message[parser.MessageSize++] = buffer[iByte];
162
[2527]163 if (parser.MessageSize >= parser.NeedBytes) {
[1185]164
[2676]165 while (int rr = RTCM3Parser(&parser)) {
[1130]166
[1239]167 // RTCMv3 message types
168 // --------------------
[2527]169 _typeList.push_back(parser.blocktype);
[1185]170
[1239]171 // RTCMv3 antenna descriptor
172 // -------------------------
[2676]173 if (rr == 1007 || rr == 1008 || rr == 1033) {
174 _antType.push_back(parser.antenna);
[1239]175 }
[1185]176
[1239]177 // RTCMv3 antenna XYZ
178 // ------------------
[2676]179 else if (rr == 1005) {
180 _antList.push_back(t_antInfo());
181 _antList.back().type = t_antInfo::ARP;
182 _antList.back().xx = parser.antX * 1e-4;
183 _antList.back().yy = parser.antY * 1e-4;
184 _antList.back().zz = parser.antZ * 1e-4;
185 _antList.back().message = rr;
[1807]186
[2676]187 // Remember station position for 1003 message decoding
188 _antXYZ[0] = parser.antX * 1e-4;
189 _antXYZ[1] = parser.antY * 1e-4;
190 _antXYZ[2] = parser.antZ * 1e-4;
[1239]191 }
[1033]192
[1239]193 // RTCMv3 antenna XYZ-H
194 // --------------------
[2676]195 else if(rr == 1006) {
196 _antList.push_back(t_antInfo());
197 _antList.back().type = t_antInfo::ARP;
198 _antList.back().xx = parser.antX * 1e-4;
199 _antList.back().yy = parser.antY * 1e-4;
200 _antList.back().zz = parser.antZ * 1e-4;
201 _antList.back().height = parser.antH * 1e-4;
202 _antList.back().height_f = true;
203 _antList.back().message = rr;
[1807]204
[2676]205 // Remember station position for 1003 message decoding
206 _antXYZ[0] = parser.antX * 1e-4;
207 _antXYZ[1] = parser.antY * 1e-4;
208 _antXYZ[2] = parser.antZ * 1e-4;
[1239]209 }
210
[1021]211 // GNSS Observations
212 // -----------------
[1239]213 else if (rr == 1 || rr == 2) {
[1127]214 decoded = true;
[1021]215
[2527]216 if (!parser.init) {
217 HandleHeader(&parser);
218 parser.init = 1;
[1021]219 }
220
221 if (rr == 2) {
[2676]222 emit(newMessage( (_staID +
223 ": No valid RINEX! All values are modulo 299792.458!").toAscii(),
224 true));
[1021]225 }
[2683]226
227 gnssdata& gnssData = parser.Data;
[1021]228
[2683]229 for (int iSat = 0; iSat < gnssData.numsats; iSat++) {
[2677]230
[2711]231 t_obs obs;
232 int satID = gnssData.satellites[iSat];
[2674]233
234 // GPS
235 // ---
236 if (satID >= PRN_GPS_START && satID <= PRN_GPS_END) {
[2711]237 obs.satSys = 'G';
238 obs.satNum = satID;
[366]239 }
[2674]240
241 // Glonass
242 // -------
243 else if (satID >= PRN_GLONASS_START && satID <= PRN_GLONASS_END) {
[2711]244 obs.satSys = 'R';
245 obs.satNum = satID - PRN_GLONASS_START + 1;
246 if (obs.satNum <= PRN_GLONASS_NUM &&
247 parser.GLOFreq[obs.satNum-1] != 0) {
248 obs.slotNum = parser.GLOFreq[obs.satNum-1] - 100;
[2669]249 }
250 else {
[2711]251 continue;
[2669]252 }
[1021]253 }
[2674]254
255 // Galileo
256 // -------
257 else if (satID >= PRN_GALILEO_START && satID <= PRN_GALILEO_END) {
[2711]258 obs.satSys = 'E';
259 obs.satNum = satID - PRN_GALILEO_START + 1;
[2676]260 }
[2674]261
262 // WAAS
263 // ----
264 else if (satID >= PRN_WAAS_START && satID <= PRN_WAAS_END) {
[2711]265 obs.satSys = 'S';
266 obs.satNum = satID - PRN_WAAS_START + 20;
[1021]267 }
[2669]268
[2674]269 // Giove A and B
270 // -------------
271 else if (satID >= PRN_GIOVE_START && satID <= PRN_GIOVE_END) {
[2711]272 obs.satSys = 'E';
273 obs.satNum = satID - PRN_GIOVE_START + PRN_GIOVE_OFFSET;
[2676]274 }
[2674]275
276 // Unknown System
277 // --------------
278 else {
[2669]279 continue;
280 }
281
[2711]282 obs.GPSWeek = gnssData.week;
283 obs.GPSWeeks = gnssData.timeofweek / 1000.0;
[1807]284
[2711]285 QString prn = QString("%1%2").arg(obs.satSys)
286 .arg(obs.satNum, 2, 10, QChar('0'));
[2687]287
288 // Handle loss-of-lock flags
289 // -------------------------
290 const int maxSlipCnt = 100;
291 if (!_slip_cnt_L1.contains(prn)) {
292 _slip_cnt_L1[prn] = 0;
293 _slip_cnt_L2[prn] = 0;
294 _slip_cnt_L5[prn] = 0;
295 }
296 if (GNSSDF2_LOCKLOSSL1 & gnssData.dataflags2[iSat]) {
297 if (_slip_cnt_L1[prn] < maxSlipCnt) {
298 ++_slip_cnt_L1[prn];
299 }
300 else {
301 _slip_cnt_L1[prn] = 1;
302 }
[2711]303 obs.slip_cnt_L1 = _slip_cnt_L1[prn];
[2687]304 }
305 if (GNSSDF2_LOCKLOSSL2 & gnssData.dataflags2[iSat]) {
306 if (_slip_cnt_L2[prn] < maxSlipCnt) {
307 ++_slip_cnt_L2[prn];
308 }
309 else {
310 _slip_cnt_L2[prn] = 1;
311 }
[2711]312 obs.slip_cnt_L2 = _slip_cnt_L2[prn];
[2687]313 }
314 if (GNSSDF2_LOCKLOSSL5 & gnssData.dataflags2[iSat]) {
315 if (_slip_cnt_L5[prn] < maxSlipCnt) {
316 ++_slip_cnt_L5[prn];
317 }
318 else {
319 _slip_cnt_L5[prn] = 1;
320 }
[2711]321 obs.slip_cnt_L5 = _slip_cnt_L5[prn];
[2687]322 }
323
[2676]324 // Loop over all data types
325 // ------------------------
[2684]326 for (int iEntry = 0; iEntry < GNSSENTRY_NUMBER; ++iEntry) {
327
328 unsigned df = (1 << iEntry);
[2677]329
[2686]330 if (df & gnssData.dataflags[iSat]) {
331
[2685]332 if (iEntry == GNSSENTRY_C1DATA) {
[2711]333 obs.C1 = gnssData.measdata[iSat][iEntry];
[1021]334 }
[2685]335 else if (iEntry == GNSSENTRY_C2DATA) {
[2711]336 obs.C2 = gnssData.measdata[iSat][iEntry];
[2683]337 }
[2685]338 else if (iEntry == GNSSENTRY_P1DATA) {
[2711]339 obs.P1 = gnssData.measdata[iSat][iEntry];
[2684]340 }
[2685]341 else if (iEntry == GNSSENTRY_P2DATA) {
[2711]342 obs.P2 = gnssData.measdata[iSat][iEntry];
[2684]343 }
[2702]344 else if (iEntry == GNSSENTRY_L1CDATA) {
[2711]345 obs.L1C = gnssData.measdata[iSat][iEntry];
[2684]346 }
[2702]347 else if (iEntry == GNSSENTRY_L1PDATA) {
[2711]348 obs.L1P = gnssData.measdata[iSat][iEntry];
[2684]349 }
[2702]350 else if (iEntry == GNSSENTRY_L2CDATA) {
[2711]351 obs.L2C = gnssData.measdata[iSat][iEntry];
[2684]352 }
[2702]353 else if (iEntry == GNSSENTRY_L2PDATA) {
[2711]354 obs.L2P = gnssData.measdata[iSat][iEntry];
[2684]355 }
[2702]356 else if (iEntry == GNSSENTRY_D1CDATA) {
[2711]357 obs.D1C = gnssData.measdata[iSat][iEntry];
[2702]358 }
359 else if (iEntry == GNSSENTRY_D1PDATA) {
[2711]360 obs.D1P = gnssData.measdata[iSat][iEntry];
[2702]361 }
362 else if (iEntry == GNSSENTRY_S1CDATA) {
[2711]363 obs.S1C = gnssData.measdata[iSat][iEntry];
[2702]364 }
365 else if (iEntry == GNSSENTRY_S1PDATA) {
[2711]366 obs.S1P = gnssData.measdata[iSat][iEntry];
[2702]367 }
368 else if (iEntry == GNSSENTRY_D2CDATA) {
[2711]369 obs.D2C = gnssData.measdata[iSat][iEntry];
[2702]370 }
371 else if (iEntry == GNSSENTRY_D2PDATA) {
[2711]372 obs.D2P = gnssData.measdata[iSat][iEntry];
[2702]373 }
374 else if (iEntry == GNSSENTRY_S2CDATA) {
[2711]375 obs.S2C = gnssData.measdata[iSat][iEntry];
[2702]376 }
377 else if (iEntry == GNSSENTRY_S2PDATA) {
[2711]378 obs.S2P = gnssData.measdata[iSat][iEntry];
[2702]379 }
[2685]380 else if (iEntry == GNSSENTRY_C5DATA) {
[2711]381 obs.C5 = gnssData.measdata[iSat][iEntry];
[2684]382 }
[2685]383 else if (iEntry == GNSSENTRY_L5DATA) {
[2711]384 obs.L5 = gnssData.measdata[iSat][iEntry];
[2684]385 }
[2702]386 else if (iEntry == GNSSENTRY_D5DATA) {
[2711]387 obs.D5 = gnssData.measdata[iSat][iEntry];
[2702]388 }
[2685]389 else if (iEntry == GNSSENTRY_S5DATA) {
[2711]390 obs.S5 = gnssData.measdata[iSat][iEntry];
[2684]391 }
[2683]392 }
[511]393 }
[2711]394 _obsList.push_back(obs);
[366]395 }
[296]396 }
[1021]397
398 // GPS Ephemeris
399 // -------------
400 else if (rr == 1019) {
401 decoded = true;
[2676]402 emit newGPSEph(new gpsephemeris(parser.ephemerisGPS));
[1021]403 }
404
405 // GLONASS Ephemeris
406 // -----------------
407 else if (rr == 1020) {
408 decoded = true;
[2676]409 emit newGlonassEph(new glonassephemeris(parser.ephemerisGLONASS));
[1021]410 }
[2770]411
412 // Galileo Ephemeris
413 // -----------------
414 else if (rr == 1045) {
415 decoded = true;
416 emit newGalileoEph(new galileoephemeris(parser.ephemerisGALILEO));
417 }
[296]418 }
419 }
420 }
[2527]421 if (!_rawFile && _mode == unknown && decoded) {
[2387]422 _mode = observations;
[1021]423 }
[296]424 }
[1021]425
426 if (decoded) {
[2672]427 app->storeGlonassSlotNums(parser.GLOFreq);
[1021]428 return success;
[658]429 }
430 else {
[1021]431 return failure;
[658]432 }
[296]433}
[1807]434
435// Store ephemerides
[2676]436//////////////////////////////////////////////////////////////////////////////
[1807]437bool RTCM3Decoder::storeEph(const gpsephemeris& gpseph) {
438 t_ephGPS eph; eph.set(&gpseph);
439
440 return storeEph(eph);
441}
442
443
444bool RTCM3Decoder::storeEph(const t_ephGPS& gpseph) {
[2463]445 const double secPerWeek = 7.0 * 24.0 * 3600.0;
[1807]446 double weekold = 0.0;
[2463]447 double weeknew = gpseph.GPSweek() + gpseph.GPSweeks() / secPerWeek;
[1807]448 if ( _ephList.find(gpseph.prn()) != _ephList.end() ) {
449 weekold = _ephList.find(gpseph.prn())->second.GPSweek()
[2463]450 + _ephList.find(gpseph.prn())->second.GPSweeks() / secPerWeek;
[1807]451 }
452
[2463]453 if ( weeknew - weekold > 1.0/secPerWeek ) {
[1807]454 _ephList[gpseph.prn()] = gpseph;
455
456 return true;
457 }
458
459 return false;
460}
[3001]461
462// Time of Corrections
463//////////////////////////////////////////////////////////////////////////////
464int RTCM3Decoder::corrGPSEpochTime() const {
465 if (_mode == corrections && _coDecoders.size() > 0) {
466 return _coDecoders.begin().value()->corrGPSEpochTime();
467 }
468 else {
469 return -1;
470 }
471}
Note: See TracBrowser for help on using the repository browser.