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

Last change on this file since 2674 was 2674, checked in by mervart, 13 years ago
File size: 14.3 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 parser.rinex3 = 0;
156 memset(&parser, 0, sizeof(parser));
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 || _checkMountPoint == _staID || _checkMountPoint == "ALL") {
165
166 for (int ii = 0; ii < bufLen; ii++) {
167 parser.Message[parser.MessageSize++] = buffer[ii];
168
169 if (parser.MessageSize >= parser.NeedBytes) {
170
171 while(int rr = RTCM3Parser(&parser)) {
172
173 // RTCMv3 message types
174 // --------------------
175 _typeList.push_back(parser.blocktype);
176
177 // RTCMv3 antenna descriptor
178 // -------------------------
179 if(rr == 1007 || rr == 1008 || rr == 1033)
180 {
181 _antType.push_back(parser.antenna); /* correct ? */
182 }
183
184 // RTCMv3 antenna XYZ
185 // ------------------
186 else if(rr == 1005)
187 {
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 {
205 _antList.push_back(t_antInfo());
206 _antList.back().type = t_antInfo::ARP;
207 _antList.back().xx = parser.antX * 1e-4;
208 _antList.back().yy = parser.antY * 1e-4;
209 _antList.back().zz = parser.antZ * 1e-4;
210 _antList.back().height = parser.antH * 1e-4;
211 _antList.back().height_f = true;
212 _antList.back().message = rr;
213
214 // Remember station position for 1003 message decoding
215 _antXYZ[0] = parser.antX * 1e-4;
216 _antXYZ[1] = parser.antY * 1e-4;
217 _antXYZ[2] = parser.antZ * 1e-4;
218 }
219
220 // GNSS Observations
221 // -----------------
222 else if (rr == 1 || rr == 2) {
223 decoded = true;
224
225 if (!parser.init) {
226 HandleHeader(&parser);
227 parser.init = 1;
228 }
229
230 // apply "GPS Integer L1 Pseudorange Modulus Ambiguity"
231 bool applyModulusAmb = false;
232 ///if (rr == 2) {
233 /// applyModulusAmb = true;
234 ///}
235
236 if (rr == 2) {
237 emit(newMessage( (_staID + ": No valid RINEX! All values are modulo 299792.458!").toAscii(), true));
238 }
239
240 for (int ii = 0; ii < parser.Data.numsats; ii++) {
241 p_obs obs = new t_obs();
242 int satID = parser.Data.satellites[ii];
243
244 // GPS
245 // ---
246 if (satID >= PRN_GPS_START && satID <= PRN_GPS_END) {
247 obs->_o.satSys = 'G';
248 obs->_o.satNum = satID;
249 }
250
251 // Glonass
252 // -------
253 else if (satID >= PRN_GLONASS_START && satID <= PRN_GLONASS_END) {
254 obs->_o.satSys = 'R';
255 obs->_o.satNum = satID - PRN_GLONASS_START + 1;
256 if (obs->_o.satNum <= PRN_GLONASS_NUM &&
257 parser.GLOFreq[obs->_o.satNum-1] != 0) {
258 obs->_o.slotNum = parser.GLOFreq[obs->_o.satNum-1] - 100;
259 }
260 else {
261 delete obs;
262 obs = 0;
263 }
264 }
265
266 // Galileo
267 // -------
268 else if (satID >= PRN_GALILEO_START && satID <= PRN_GALILEO_END) {
269 obs->_o.satSys = 'E';
270 obs->_o.satNum = satID - PRN_GALILEO_START + 1;
271 }
272
273 // WAAS
274 // ----
275 else if (satID >= PRN_WAAS_START && satID <= PRN_WAAS_END) {
276 obs->_o.satSys = 'S';
277 obs->_o.satNum = satID - PRN_WAAS_START + 20;
278 }
279
280 // Giove A and B
281 // -------------
282 else if (satID >= PRN_GIOVE_START && satID <= PRN_GIOVE_END) {
283 obs->_o.satSys = 'E';
284 obs->_o.satNum = satID - PRN_GIOVE_START + PRN_GIOVE_OFFSET;
285 }
286
287 // Unknown System
288 // --------------
289 else {
290 delete obs;
291 obs = 0;
292 }
293
294 if (obs) {
295 _obsList.push_back(obs);
296 }
297 else {
298 continue;
299 }
300
301 obs->_o.GPSWeek = parser.Data.week;
302 obs->_o.GPSWeeks = parser.Data.timeofweek / 1000.0;
303
304 // Estimate "GPS Integer L1 Pseudorange Modulus Ambiguity"
305 // -------------------------------------------------------
306 double modulusAmb = 0;
307 if (applyModulusAmb) {
308 // Missing antenna coordinates: skip all data
309 if ( !_antXYZ[0] && !_antXYZ[1] && !_antXYZ[2] ) {
310 continue;
311 }
312
313 ostringstream prns;
314 prns << obs->_o.satSys << setfill('0') << setw(2) << obs->_o.satNum;
315
316 string prn = prns.str();
317
318 // Missing ephemerides, skip satellite
319 if (_ephList.find(prn) == _ephList.end()) {
320 continue;
321 }
322
323 const t_eph* eph = &(_ephList.find(prn)->second);
324
325 double rho, xSat, ySat, zSat, clkSat, GPSWeeks_tot;
326 int GPSWeek_tot;
327 cmpRho(eph, _antXYZ[0], _antXYZ[1], _antXYZ[2],
328 obs->_o.GPSWeek, obs->_o.GPSWeeks,
329 rho, GPSWeek_tot, GPSWeeks_tot,
330 xSat, ySat, zSat, clkSat);
331
332 const double CC = 299792458.0;
333
334 int nn = static_cast<int>(rho / (CC * 0.001));
335
336 modulusAmb = nn * CC * 0.001;
337 }
338
339 // Loop over all data types
340 // ------------------------
341 for (int jj = 0; jj < parser.numdatatypesGPS; jj++) {
342 int v = 0;
343 // sepearated declaration and initalization of df and pos. Perlt
344 int df;
345 int pos;
346 df = parser.dataflag[jj];
347 pos = parser.datapos[jj];
348 if ( (parser.Data.dataflags[ii] & df)
349 && !isnan(parser.Data.measdata[ii][pos])
350 && !isinf(parser.Data.measdata[ii][pos])) {
351 v = 1;
352 }
353 else {
354 df = parser.dataflagGPS[jj];
355 pos = parser.dataposGPS[jj];
356 if ( (parser.Data.dataflags[ii] & df)
357 && !isnan(parser.Data.measdata[ii][pos])
358 && !isinf(parser.Data.measdata[ii][pos])) {
359 v = 1;
360 }
361 }
362 if (!v) {
363 continue;
364 }
365 else
366 {
367 int isat = (parser.Data.satellites[ii] < 120
368 ? parser.Data.satellites[ii]
369 : parser.Data.satellites[ii] - 80);
370
371 // variables df and pos are used consequently. Perlt
372 if (df & GNSSDF_C1DATA) {
373 obs->_o.C1 = parser.Data.measdata[ii][pos] + modulusAmb;
374 }
375 else if (df & GNSSDF_C2DATA) {
376 obs->_o.C2 = parser.Data.measdata[ii][pos] + modulusAmb;
377 }
378 else if (df & GNSSDF_P1DATA) {
379 obs->_o.P1 = parser.Data.measdata[ii][pos] + modulusAmb;
380 }
381 else if (df & GNSSDF_P2DATA) {
382 obs->_o.P2 = parser.Data.measdata[ii][pos] + modulusAmb;
383 }
384 else if (df & (GNSSDF_L1CDATA|GNSSDF_L1PDATA)) {
385 obs->_o.L1 = parser.Data.measdata[ii][pos] + modulusAmb;
386 obs->_o.SNR1 = parser.Data.snrL1[ii];
387 obs->_o.lock_timei_L1 = parser.lastlockGPSl1[isat];
388 }
389 else if (df & (GNSSDF_L2CDATA|GNSSDF_L2PDATA)) {
390 obs->_o.L2 = parser.Data.measdata[ii][pos] + modulusAmb;
391 obs->_o.SNR2 = parser.Data.snrL2[ii];
392 obs->_o.lock_timei_L2 = parser.lastlockGPSl2[isat];
393 }
394 else if (df & (GNSSDF_S1CDATA|GNSSDF_S1PDATA)) {
395 obs->_o.S1 = parser.Data.measdata[ii][pos];
396 }
397 else if (df & (GNSSDF_S2CDATA|GNSSDF_S2PDATA)) {
398 obs->_o.S2 = parser.Data.measdata[ii][pos];
399 }
400 }
401 }
402 }
403 }
404
405 // GPS Ephemeris
406 // -------------
407 else if (rr == 1019) {
408 decoded = true;
409 gpsephemeris* ep = new gpsephemeris(parser.ephemerisGPS);
410 emit newGPSEph(ep);
411 }
412
413 // GLONASS Ephemeris
414 // -----------------
415 else if (rr == 1020) {
416 decoded = true;
417 glonassephemeris* ep = new glonassephemeris(parser.ephemerisGLONASS);
418 emit newGlonassEph(ep);
419 }
420 }
421 }
422 }
423 if (!_rawFile && _mode == unknown && decoded) {
424 _mode = observations;
425 }
426 }
427
428 if (decoded) {
429 app->storeGlonassSlotNums(parser.GLOFreq);
430 return success;
431 }
432 else {
433 return failure;
434 }
435}
436
437// Store ephemerides
438////////////////////////////////////////////////////////////////////////////////////////
439bool RTCM3Decoder::storeEph(const gpsephemeris& gpseph) {
440 t_ephGPS eph; eph.set(&gpseph);
441
442 return storeEph(eph);
443}
444
445
446bool RTCM3Decoder::storeEph(const t_ephGPS& gpseph) {
447 const double secPerWeek = 7.0 * 24.0 * 3600.0;
448 double weekold = 0.0;
449 double weeknew = gpseph.GPSweek() + gpseph.GPSweeks() / secPerWeek;
450 if ( _ephList.find(gpseph.prn()) != _ephList.end() ) {
451 weekold = _ephList.find(gpseph.prn())->second.GPSweek()
452 + _ephList.find(gpseph.prn())->second.GPSweeks() / secPerWeek;
453 }
454
455 if ( weeknew - weekold > 1.0/secPerWeek ) {
456 _ephList[gpseph.prn()] = gpseph;
457
458 return true;
459 }
460
461 return false;
462}
Note: See TracBrowser for help on using the repository browser.