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

Last change on this file since 1079 was 1079, checked in by weber, 16 years ago

* empty log message *

File size: 12.5 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 <math.h>
43#include <string.h>
44
45#include "RTCM3Decoder.h"
46#include "RTCM3coDecoder.h"
47#include "bncconst.h"
48#include "bncapp.h"
49#include "bncutils.h" /* Weber, for latencies */
50
51using namespace std;
52
53#ifndef isinf
54# define isinf(x) 0
55#endif
56
57// Error Handling
58////////////////////////////////////////////////////////////////////////////
59void RTCM3Error(const char*, ...) {
60}
61
62// Constructor
63////////////////////////////////////////////////////////////////////////////
64RTCM3Decoder::RTCM3Decoder(const QString& staID) : GPSDecoder() {
65
66 const int LEAPSECONDS = 14; /* only needed for approx. time */
67
68 QSettings settings;
69 _checkMountPoint = settings.value("messTypes").toString();
70 _staID = staID;
71
72 // Latency
73 _numLat = 0;
74 _minLat = 1000.;
75 _maxLat = -1000.;
76 _sumLat = 0.;
77 _sumLatQ = 0.;
78 _followSec = false;
79 _meanDiff = 0.;
80 _diffSecGPS= 0.;
81 _numGaps = 0;
82 _oldSecGPS = 0.;
83 _newSecGPS = 0.;
84 _curLat = 0.;
85 _perfIntr = 86400;
86 if ( settings.value("perfIntr").toString().isEmpty() ) { _perfIntr = 0; }
87 if ( settings.value("perfIntr").toString().indexOf("1 min") != -1 ) { _perfIntr = 60; }
88 if ( settings.value("perfIntr").toString().indexOf("5 min") != -1 ) { _perfIntr = 300; }
89 if ( settings.value("perfIntr").toString().indexOf("15 min") != -1 ) { _perfIntr = 900; }
90 if ( settings.value("perfIntr").toString().indexOf("1 hour") != -1 ) { _perfIntr = 3600; }
91 if ( settings.value("perfIntr").toString().indexOf("6 hours") != -1 ) { _perfIntr = 21600; }
92 if ( settings.value("perfIntr").toString().indexOf("1 day") != -1 ) { _perfIntr = 86400; }
93
94 // Ensure, that the Decoder uses the "old" convention for the data structure for Rinex2. Perlt
95 _Parser.rinex3 = 0;
96
97 time_t tim;
98 tim = time(0) - ((10*365+2+5)*24*60*60 + LEAPSECONDS);
99
100 memset(&_Parser, 0, sizeof(_Parser));
101 _Parser.GPSWeek = tim/(7*24*60*60);
102 _Parser.GPSTOW = tim%(7*24*60*60);
103
104 connect(this, SIGNAL(newGPSEph(gpsephemeris*)),
105 (bncApp*) qApp, SLOT(slotNewGPSEph(gpsephemeris*)));
106 connect(this, SIGNAL(newGlonassEph(glonassephemeris*)),
107 (bncApp*) qApp, SLOT(slotNewGlonassEph(glonassephemeris*)));
108
109 // Sub-Decoder for Clock and Orbit Corrections
110 // -------------------------------------------
111 _coDecoder = new RTCM3coDecoder(staID);
112
113 // Mode can be either observations or corrections
114 // ----------------------------------------------
115 _mode = unknown;
116}
117
118// Destructor
119////////////////////////////////////////////////////////////////////////////
120RTCM3Decoder::~RTCM3Decoder() {
121 delete _coDecoder;
122}
123
124//
125////////////////////////////////////////////////////////////////////////////
126t_irc RTCM3Decoder::Decode(char* buffer, int bufLen) {
127
128 bool decoded = false;
129
130 // Try to decode Clock and Orbit Corrections
131 // -----------------------------------------
132 if (_mode == unknown || _mode == corrections) {
133 if ( _coDecoder->Decode(buffer, bufLen) == success ) {
134 decoded = true;
135
136 // Latency
137 // -------
138 if (_perfIntr>0) {
139 if (0<_coDecoder->_epochList.size()) {
140 for (int ii=0;ii<_coDecoder->_epochList.size();ii++) {
141 int week;
142 double sec;
143 _newSecGPS = _coDecoder->_epochList[ii];
144 leapsecGPSWeeks(week, sec);
145 double dt = fabs(sec - _newSecGPS);
146 const double secPerWeek = 7.0 * 24.0 * 3600.0;
147 if (dt > 0.5 * secPerWeek) {
148 if (sec > _newSecGPS) {
149 sec -= secPerWeek;
150 } else {
151 sec += secPerWeek;
152 }
153 }
154 if (_newSecGPS != _oldSecGPS) {
155 if (int(_newSecGPS) % _perfIntr < int(_oldSecGPS) % _perfIntr) {
156 if (_numLat>0) {
157 QString late;
158 if (_meanDiff>0.) {
159 late = QString(": Mean latency %1 sec, min %2, max %3, rms %4, %5 epochs, %6 gaps")
160 .arg(int(_sumLat/_numLat*100)/100.)
161 .arg(int(_minLat*100)/100.)
162 .arg(int(_maxLat*100)/100.)
163 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
164 .arg(_numLat)
165 .arg(_numGaps);
166 emit(newMessage(QString(_staID + late ).toAscii() ) );
167 } else {
168 late = QString(": Mean latency %1 sec, min %2, max %3, rms %4, %5 epochs")
169 .arg(int(_sumLat/_numLat*100)/100.)
170 .arg(int(_minLat*100)/100.)
171 .arg(int(_maxLat*100)/100.)
172 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
173 .arg(_numLat);
174 emit(newMessage(QString(_staID + late ).toAscii() ) );
175 }
176 }
177 _meanDiff = int(_diffSecGPS)/_numLat;
178 _diffSecGPS = 0.;
179 _numGaps = 0;
180 _sumLat = 0.;
181 _sumLatQ = 0.;
182 _numLat = 0;
183 _minLat = 1000.;
184 _maxLat = -1000.;
185 }
186 if (_followSec) {
187 _diffSecGPS += _newSecGPS - _oldSecGPS;
188 if (_meanDiff>0.) {
189 if (_newSecGPS - _oldSecGPS > 1.5 * _meanDiff) {
190 _numGaps += 1;
191 }
192 }
193 }
194 _curLat = sec - _newSecGPS;
195 _sumLat += _curLat;
196 _sumLatQ += _curLat * _curLat;
197 if (_curLat < _minLat) {_minLat = _curLat;}
198 if (_curLat >= _maxLat) {_maxLat = _curLat;}
199 _numLat += 1;
200 _oldSecGPS = _newSecGPS;
201 _followSec = true;
202 }
203 }
204 }
205 }
206 _coDecoder->_epochList.clear();
207
208 if (_mode == unknown) {
209 _mode = corrections;
210// emit(newMessage( (_staID + " : mode set to corrections").toAscii() ));
211 }
212 }
213 }
214
215 // Remaining part decodes the Observations
216 // ---------------------------------------
217 if (_mode == unknown || _mode == observations || _checkMountPoint == _staID || _checkMountPoint == "ALL") {
218 for (int ii = 0; ii < bufLen; ii++) {
219
220 _Parser.Message[_Parser.MessageSize++] = buffer[ii];
221 if (_Parser.MessageSize >= _Parser.NeedBytes) {
222
223 // RTCM message types
224 // ------------------
225 for (int kk = 0; kk < _Parser.typeSize; kk++) {
226 _typeList.push_back(_Parser.typeList[kk]);
227 }
228 _Parser.typeSize = 0;
229
230 while(int rr = RTCM3Parser(&_Parser)) {
231
232 // GNSS Observations
233 // -----------------
234 if (rr == 1 || rr == 2) {
235 decoded = true;
236
237 if (!_Parser.init) {
238 HandleHeader(&_Parser);
239 _Parser.init = 1;
240 }
241
242 if (rr == 2) {
243// std::cerr << "No valid RINEX! All values are modulo 299792.458!\n";
244 emit(newMessage( (_staID + ": No valid RINEX! All values are modulo 299792.458!").toAscii() ));
245 }
246
247 for (int ii = 0; ii < _Parser.Data.numsats; ii++) {
248 p_obs obs = new t_obs();
249 _obsList.push_back(obs);
250 if (_Parser.Data.satellites[ii] <= PRN_GPS_END) {
251 obs->_o.satSys = 'G';
252 obs->_o.satNum = _Parser.Data.satellites[ii];
253 }
254 else if (_Parser.Data.satellites[ii] <= PRN_GLONASS_END) {
255 obs->_o.satSys = 'R';
256 obs->_o.satNum = _Parser.Data.satellites[ii] - PRN_GLONASS_START + 1;
257 }
258 else {
259 obs->_o.satSys = 'S';
260 obs->_o.satNum = _Parser.Data.satellites[ii] - PRN_WAAS_START + 20;
261 }
262 obs->_o.GPSWeek = _Parser.Data.week;
263 obs->_o.GPSWeeks = _Parser.Data.timeofweek / 1000.0;
264
265 for (int jj = 0; jj < _Parser.numdatatypesGPS; jj++) {
266 int v = 0;
267 // sepearated declaration and initalization of df and pos. Perlt
268 int df;
269 int pos;
270 df = _Parser.dataflag[jj];
271 pos = _Parser.datapos[jj];
272 if ( (_Parser.Data.dataflags[ii] & df)
273 && !isnan(_Parser.Data.measdata[ii][pos])
274 && !isinf(_Parser.Data.measdata[ii][pos])) {
275 v = 1;
276 }
277 else {
278 df = _Parser.dataflagGPS[jj];
279 pos = _Parser.dataposGPS[jj];
280 if ( (_Parser.Data.dataflags[ii] & df)
281 && !isnan(_Parser.Data.measdata[ii][pos])
282 && !isinf(_Parser.Data.measdata[ii][pos])) {
283 v = 1;
284 }
285 }
286 if (!v) {
287 continue;
288 }
289 else
290 {
291 int isat = (_Parser.Data.satellites[ii] < 120
292 ? _Parser.Data.satellites[ii]
293 : _Parser.Data.satellites[ii] - 80);
294
295 // variables df and pos are used consequently. Perlt
296 if (df & GNSSDF_C1DATA) {
297 obs->_o.C1 = _Parser.Data.measdata[ii][pos];
298 }
299 else if (df & GNSSDF_C2DATA) {
300 obs->_o.C2 = _Parser.Data.measdata[ii][pos];
301 }
302 else if (df & GNSSDF_P1DATA) {
303 obs->_o.P1 = _Parser.Data.measdata[ii][pos];
304 }
305 else if (df & GNSSDF_P2DATA) {
306 obs->_o.P2 = _Parser.Data.measdata[ii][pos];
307 }
308 else if (df & (GNSSDF_L1CDATA|GNSSDF_L1PDATA)) {
309 obs->_o.L1 = _Parser.Data.measdata[ii][pos];
310 obs->_o.SNR1 = _Parser.Data.snrL1[ii];
311 obs->_o.lock_timei_L1 = _Parser.lastlockl1[isat];
312 }
313 else if (df & (GNSSDF_L2CDATA|GNSSDF_L2PDATA)) {
314 obs->_o.L2 = _Parser.Data.measdata[ii][pos];
315 obs->_o.SNR2 = _Parser.Data.snrL2[ii];
316 obs->_o.lock_timei_L2 = _Parser.lastlockl2[isat];
317 }
318 else if (df & (GNSSDF_S1CDATA|GNSSDF_S1PDATA)) {
319 obs->_o.S1 = _Parser.Data.measdata[ii][pos];
320 }
321 else if (df & (GNSSDF_S2CDATA|GNSSDF_S2PDATA)) {
322 obs->_o.S2 = _Parser.Data.measdata[ii][pos];
323 }
324 }
325 }
326 }
327 }
328
329 // GPS Ephemeris
330 // -------------
331 else if (rr == 1019) {
332 decoded = true;
333 gpsephemeris* ep = new gpsephemeris(_Parser.ephemerisGPS);
334 emit newGPSEph(ep);
335 }
336
337 // GLONASS Ephemeris
338 // -----------------
339 else if (rr == 1020) {
340 decoded = true;
341 glonassephemeris* ep = new glonassephemeris(_Parser.ephemerisGLONASS);
342 emit newGlonassEph(ep);
343 }
344 }
345 }
346 }
347 if (_mode == unknown && decoded) {
348 _mode = observations;
349// emit(newMessage( (_staID + " : mode set to observations").toAscii() ));
350 }
351 }
352
353 if (decoded) {
354 return success;
355 }
356 else {
357 return failure;
358 }
359}
Note: See TracBrowser for help on using the repository browser.