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

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