source: ntrip/trunk/BNC/src/RTCM3/RTCM3coDecoder.cpp@ 5665

Last change on this file since 5665 was 5665, checked in by stoecker, 10 years ago

update due to RTCM-SSR fixes

File size: 14.4 KB
RevLine 
[866]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: RTCM3coDecoder
30 *
31 * Purpose: RTCM3 Clock Orbit Decoder
32 *
33 * Author: L. Mervart
34 *
35 * Created: 05-May-2008
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
[868]41#include <stdio.h>
[920]42#include <math.h>
[868]43
[866]44#include "RTCM3coDecoder.h"
[918]45#include "bncutils.h"
[934]46#include "bncrinex.h"
[5070]47#include "bnccore.h"
[1535]48#include "bncsettings.h"
[1834]49#include "rtcm3torinex.h"
[4428]50#include "bnctime.h"
[866]51
52using namespace std;
53
54// Constructor
55////////////////////////////////////////////////////////////////////////////
[970]56RTCM3coDecoder::RTCM3coDecoder(const QString& staID) {
[934]57
[970]58 _staID = staID;
59
[934]60 // File Output
61 // -----------
[1535]62 bncSettings settings;
[934]63 QString path = settings.value("corrPath").toString();
64 if (!path.isEmpty()) {
65 expandEnvVar(path);
66 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
67 path += QDir::separator();
68 }
[970]69 _fileNameSkl = path + staID;
[934]70 }
[1582]71 _out = 0;
72 _GPSweeks = -1.0;
[934]73
[5124]74 qRegisterMetaType<bncTime>("bncTime");
75
[5120]76 connect(this, SIGNAL(newCorrLine(QString, QString, bncTime)),
77 BNC_CORE, SLOT(slotNewCorrLine(QString, QString, bncTime)));
[1828]78
[5577]79 connect(this, SIGNAL(providerIDChanged(QString)),
80 BNC_CORE, SIGNAL(providerIDChanged(QString)));
81
[4428]82 connect(this, SIGNAL(newMessage(QByteArray,bool)),
[5068]83 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
[4428]84
[1828]85 memset(&_co, 0, sizeof(_co));
[3077]86 memset(&_bias, 0, sizeof(_bias));
[5576]87
88 _providerID[0] = -1;
89 _providerID[1] = -1;
90 _providerID[2] = -1;
[866]91}
92
93// Destructor
94////////////////////////////////////////////////////////////////////////////
95RTCM3coDecoder::~RTCM3coDecoder() {
[935]96 delete _out;
[866]97}
98
[934]99// Reopen Output File
100////////////////////////////////////////////////////////////////////////
[3035]101void RTCM3coDecoder::reopen(const QString& fileNameSkl, QString& fileName,
102 ofstream*& out) {
[934]103
[3035]104 if (!fileNameSkl.isEmpty()) {
[934]105
[1535]106 bncSettings settings;
[934]107
[1154]108 QDateTime datTim = currentDateAndTimeGPS();
[934]109
110 QString hlpStr = bncRinex::nextEpochStr(datTim,
111 settings.value("corrIntr").toString());
112
[3035]113 QString fileNameHlp = fileNameSkl
[934]114 + QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'))
[938]115 + hlpStr + datTim.toString(".yyC");
[934]116
[3035]117 if (fileName == fileNameHlp) {
[934]118 return;
119 }
120 else {
[3035]121 fileName = fileNameHlp;
[934]122 }
123
[3035]124 delete out;
[1727]125 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
[3035]126 out = new ofstream( fileName.toAscii().data(),
[1727]127 ios_base::out | ios_base::app );
128 }
129 else {
[3035]130 out = new ofstream( fileName.toAscii().data() );
[1727]131 }
[934]132 }
133}
134
[866]135//
136////////////////////////////////////////////////////////////////////////////
[1227]137t_irc RTCM3coDecoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
[868]138
[1218]139 errmsg.clear();
140
[1227]141 _buffer.append(QByteArray(buffer,bufLen));
[868]142
[1023]143 t_irc retCode = failure;
144
[1832]145 while(_buffer.size()) {
146
[879]147 int bytesused = 0;
[1832]148 struct ClockOrbit co_sav;
149 memcpy(&co_sav, &_co, sizeof(co_sav)); // save state
[1829]150
[5665]151 GCOB_RETURN irc = GetSSR(&_co, &_bias, 0, 0, _buffer.data(),
[879]152 _buffer.size(), &bytesused);
153
[1833]154 if (irc <= -30) { // not enough data - restore state and exit loop
[1832]155 memcpy(&_co, &co_sav, sizeof(co_sav));
[1833]156 break;
[869]157 }
[1832]158
[1833]159 else if (irc < 0) { // error - skip 1 byte and retry
160 memset(&_co, 0, sizeof(_co));
[1842]161 memset(&_bias, 0, sizeof(_bias));
162 _buffer = _buffer.mid(bytesused ? bytesused : 1);
[1833]163 }
164
165 else { // OK or MESSAGEFOLLOWS
[1828]166 _buffer = _buffer.mid(bytesused);
167
[4900]168 if ( (irc == GCOBR_OK || irc == GCOBR_MESSAGEFOLLOWS ) &&
[5665]169 (_co.NumberOfSat[CLOCKORBIT_SATGPS] > 0 || _co.NumberOfSat[CLOCKORBIT_SATGLONASS] > 0 ||
170 _bias.NumberOfSat[CLOCKORBIT_SATGPS] > 0 || _bias.NumberOfSat[CLOCKORBIT_SATGLONASS] > 0) ) {
[2435]171
[3035]172 reopen(_fileNameSkl, _fileName, _out);
[1835]173
174 // Guess GPS week and sec using system time
175 // ----------------------------------------
[1829]176 int GPSweek;
[1835]177 double GPSweeksHlp;
178 currentGPSWeeks(GPSweek, GPSweeksHlp);
179
180 // Correction Epoch from GPSEpochTime
181 // ----------------------------------
[5665]182 if (_co.NumberOfSat[CLOCKORBIT_SATGPS] > 0 || _bias.NumberOfSat[CLOCKORBIT_SATGPS] > 0) {
183 int GPSEpochTime = (_co.NumberOfSat[CLOCKORBIT_SATGPS] > 0) ?
184 _co.EpochTime[CLOCKORBIT_SATGPS] : _bias.EpochTime[CLOCKORBIT_SATGPS];
[3427]185 if (GPSweeksHlp > GPSEpochTime + 86400.0) {
[919]186 GPSweek += 1;
187 }
[3427]188 else if (GPSweeksHlp < GPSEpochTime - 86400.0) {
[919]189 GPSweek -= 1;
190 }
[3427]191 _GPSweeks = GPSEpochTime;
[919]192 }
[1835]193
194 // Correction Epoch from Glonass Epoch
195 // -----------------------------------
[5665]196 else if (_co.NumberOfSat[CLOCKORBIT_SATGLONASS] > 0 || _bias.NumberOfSat[CLOCKORBIT_SATGLONASS] > 0){
197 int GLONASSEpochTime = (_co.NumberOfSat[CLOCKORBIT_SATGLONASS] > 0) ?
198 _co.EpochTime[CLOCKORBIT_SATGLONASS] : _bias.EpochTime[CLOCKORBIT_SATGLONASS];
[1834]199
200 // Second of day (GPS time) from Glonass Epoch
201 // -------------------------------------------
[1835]202 QDate date = dateAndTimeFromGPSweek(GPSweek, GPSweeksHlp).date();
[1834]203 int leapSecond = gnumleap(date.year(), date.month(), date.day());
[3427]204 int GPSDaySec = GLONASSEpochTime - 3 * 3600 + leapSecond;
[1834]205
[1835]206 int weekDay = int(GPSweeksHlp/86400.0);
207 int GPSDaySecHlp = int(GPSweeksHlp) - weekDay * 86400;
[1834]208
209 // Handle the difference between system clock and correction epoch
210 // ---------------------------------------------------------------
211 if (GPSDaySec < GPSDaySecHlp - 3600) {
[1829]212 weekDay += 1;
[1835]213 if (weekDay > 6) {
214 weekDay = 0;
215 GPSweek += 1;
216 }
[1829]217 }
[1834]218 else if (GPSDaySec > GPSDaySecHlp + 3600) {
[1829]219 weekDay -= 1;
[1835]220 if (weekDay < 0) {
221 weekDay = 6;
222 GPSweek -= 1;
223 }
[1834]224 }
225
226 _GPSweeks = weekDay * 86400.0 + GPSDaySec;
[1829]227 }
[918]228
[5576]229 checkProviderID();
230
[3022]231 QStringList asciiLines = corrsToASCIIlines(GPSweek, _GPSweeks,
[3024]232 _co, &_bias);
[3022]233
234 QStringListIterator it(asciiLines);
235 while (it.hasNext()) {
236 QString line = it.next();
[4428]237 printLine(line, GPSweek, _GPSweeks);
[1852]238 }
239
[1829]240 retCode = success;
241 memset(&_co, 0, sizeof(_co));
[1842]242 memset(&_bias, 0, sizeof(_bias));
[869]243 }
[1829]244 }
[1833]245 }
[1832]246
[1833]247 if (retCode != success) {
248 _GPSweeks = -1.0;
[868]249 }
[1829]250 return retCode;
[866]251}
[934]252
253//
254////////////////////////////////////////////////////////////////////////////
[4428]255void RTCM3coDecoder::printLine(const QString& line, int GPSweek,
256 double GPSweeks) {
[934]257 if (_out) {
[971]258 *_out << line.toAscii().data() << endl;
[934]259 _out->flush();
260 }
261
[4428]262 int currWeek;
263 double currSec;
264 currentGPSWeeks(currWeek, currSec);
265 bncTime currTime(currWeek, currSec);
266
[5120]267 bncTime coTime(GPSweek, GPSweeks);
[4428]268
[5120]269 double dt = currTime - coTime;
[4428]270 const double MAXDT = 10 * 60.0;
271 if (fabs(dt) > MAXDT) {
[4429]272 emit newMessage("suspicious correction: " + _staID.toAscii() + " "
273 + line.toAscii(), false);
[4428]274 }
275 else {
276 emit newCorrLine(line, _staID, coTime);
277 }
[934]278}
[3022]279
280//
281////////////////////////////////////////////////////////////////////////////
282QStringList RTCM3coDecoder::corrsToASCIIlines(int GPSweek, double GPSweeks,
283 const ClockOrbit& co,
[5665]284 const CodeBias* bias) {
[3022]285
286 QStringList retLines;
287
288 // Loop over all satellites (GPS and Glonass)
289 // ------------------------------------------
[5665]290 if (co.NumberOfSat[CLOCKORBIT_SATGPS] > 0 || co.NumberOfSat[CLOCKORBIT_SATGLONASS] > 0) {
[3022]291 QString line1;
292 line1.sprintf("! Orbits/Clocks: %d GPS %d Glonass",
[5665]293 co.NumberOfSat[CLOCKORBIT_SATGPS], co.NumberOfSat[CLOCKORBIT_SATGLONASS]);
[3022]294 retLines << line1;
295 }
[5665]296 for (int ii = 0; ii < CLOCKORBIT_NUMGPS+co.NumberOfSat[CLOCKORBIT_SATGLONASS]; ii++) {
[3022]297 char sysCh = ' ';
[5665]298 if (ii < co.NumberOfSat[CLOCKORBIT_SATGPS]) {
[3022]299 sysCh = 'G';
300 }
301 else if (ii >= CLOCKORBIT_NUMGPS) {
302 sysCh = 'R';
303 }
[3024]304
[3022]305 if (sysCh != ' ') {
306
307 QString linePart;
308 linePart.sprintf("%d %d %d %.1f %c%2.2d",
309 co.messageType, co.UpdateInterval, GPSweek, GPSweeks,
310 sysCh, co.Sat[ii].ID);
311
312 // Combined message (orbit and clock)
313 // ----------------------------------
314 if ( co.messageType == COTYPE_GPSCOMBINED ||
315 co.messageType == COTYPE_GLONASSCOMBINED ) {
316 QString line;
317 line.sprintf(" %3d"
318 " %8.3f %8.3f %8.3f %8.3f"
319 " %10.5f %10.5f %10.5f %10.5f"
[5576]320 " %10.5f",
[3022]321 co.Sat[ii].IOD,
322 co.Sat[ii].Clock.DeltaA0,
323 co.Sat[ii].Orbit.DeltaRadial,
324 co.Sat[ii].Orbit.DeltaAlongTrack,
325 co.Sat[ii].Orbit.DeltaCrossTrack,
326 co.Sat[ii].Clock.DeltaA1,
327 co.Sat[ii].Orbit.DotDeltaRadial,
328 co.Sat[ii].Orbit.DotDeltaAlongTrack,
329 co.Sat[ii].Orbit.DotDeltaCrossTrack,
[5576]330 co.Sat[ii].Clock.DeltaA2);
[3022]331 retLines << linePart+line;
332 }
333
334 // Orbits only
335 // -----------
336 else if ( co.messageType == COTYPE_GPSORBIT ||
337 co.messageType == COTYPE_GLONASSORBIT ) {
338 QString line;
339 line.sprintf(" %3d"
340 " %8.3f %8.3f %8.3f"
[5576]341 " %10.5f %10.5f %10.5f",
[3022]342 co.Sat[ii].IOD,
343 co.Sat[ii].Orbit.DeltaRadial,
344 co.Sat[ii].Orbit.DeltaAlongTrack,
345 co.Sat[ii].Orbit.DeltaCrossTrack,
346 co.Sat[ii].Orbit.DotDeltaRadial,
347 co.Sat[ii].Orbit.DotDeltaAlongTrack,
[5576]348 co.Sat[ii].Orbit.DotDeltaCrossTrack);
[3022]349 retLines << linePart+line;
350 }
351
352 // Clocks only
353 // -----------
354 else if ( co.messageType == COTYPE_GPSCLOCK ||
355 co.messageType == COTYPE_GLONASSCLOCK ) {
356 QString line;
[5531]357 line.sprintf(" %8.3f %10.5f %10.5f",
[3022]358 co.Sat[ii].Clock.DeltaA0,
359 co.Sat[ii].Clock.DeltaA1,
360 co.Sat[ii].Clock.DeltaA2);
361 retLines << linePart+line;
362 }
363
364 // User Range Accuracy
365 // -------------------
366 else if ( co.messageType == COTYPE_GPSURA ||
367 co.messageType == COTYPE_GLONASSURA ) {
368 QString line;
[5543]369 line.sprintf(" %f", co.Sat[ii].UserRangeAccuracy);
[3022]370 retLines << linePart+line;
371 }
372
373 // High-Resolution Clocks
374 // ----------------------
375 else if ( co.messageType == COTYPE_GPSHR ||
376 co.messageType == COTYPE_GLONASSHR ) {
377 QString line;
[5543]378 line.sprintf(" %8.3f", co.Sat[ii].hrclock);
[3022]379 retLines << linePart+line;
380 }
381 }
382 }
383
384 // Loop over all satellites (GPS and Glonass)
385 // ------------------------------------------
[3024]386 if (bias) {
[5665]387 if (bias->NumberOfSat[CLOCKORBIT_SATGPS] > 0 || bias->NumberOfSat[CLOCKORBIT_SATGLONASS] > 0) {
[3024]388 QString line1;
389 line1.sprintf("! Biases: %d GPS %d Glonass",
[5665]390 bias->NumberOfSat[CLOCKORBIT_SATGPS], bias->NumberOfSat[CLOCKORBIT_SATGLONASS]);
[3024]391 retLines << line1;
[3022]392 }
[5665]393 for (int ii = 0; ii < CLOCKORBIT_NUMGPS + bias->NumberOfSat[CLOCKORBIT_SATGLONASS]; ii++) {
[3024]394 char sysCh = ' ';
395 int messageType;
[5665]396 if (ii < bias->NumberOfSat[CLOCKORBIT_SATGPS]) {
[3024]397 sysCh = 'G';
398 messageType = BTYPE_GPS;
[3022]399 }
[3024]400 else if (ii >= CLOCKORBIT_NUMGPS) {
401 sysCh = 'R';
402 messageType = BTYPE_GLONASS;
403 }
404 if (sysCh != ' ') {
405 QString line;
406 line.sprintf("%d %d %d %.1f %c%2.2d %d",
407 messageType, bias->UpdateInterval, GPSweek, GPSweeks,
408 sysCh, bias->Sat[ii].ID,
409 bias->Sat[ii].NumberOfCodeBiases);
410 for (int jj = 0; jj < bias->Sat[ii].NumberOfCodeBiases; jj++) {
411 QString hlp;
412 hlp.sprintf(" %d %8.3f", bias->Sat[ii].Biases[jj].Type,
413 bias->Sat[ii].Biases[jj].Bias);
414 line += hlp;
415 }
416 retLines << line;
417 }
[3022]418 }
419 }
420
421 return retLines;
422}
[5576]423
424//
425////////////////////////////////////////////////////////////////////////////
426void RTCM3coDecoder::checkProviderID() {
427
428 if (_co.SSRProviderID == 0 && _co.SSRSolutionID == 0 && _co.SSRIOD == 0) {
429 return;
430 }
431
432 int newProviderID[3];
433 newProviderID[0] = _co.SSRProviderID;
434 newProviderID[1] = _co.SSRSolutionID;
435 newProviderID[2] = _co.SSRIOD;
436
437 bool alreadySet = false;
438 bool different = false;
439
440 for (unsigned ii = 0; ii < 3; ii++) {
441 if (_providerID[ii] != -1) {
442 alreadySet = true;
443 }
444 if (_providerID[ii] != newProviderID[ii]) {
445 different = true;
446 }
447 _providerID[ii] = newProviderID[ii];
448 }
449
450 if (alreadySet && different) {
[5580]451 emit newMessage("RTCM3coDecoder: Provider Changed " + _staID.toAscii() + "\n", true);
[5577]452 emit providerIDChanged(_staID);
[5576]453 }
454}
Note: See TracBrowser for help on using the repository browser.