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

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