source: ntrip/branches/BNC_2.11.0/src/RTCM3/RTCM3coDecoder.cpp@ 6611

Last change on this file since 6611 was 6611, checked in by stuerze, 9 years ago

minor changes to get a correct time stamp for GLONASS corrections at the end of a week

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;
[6611]205 if (GPSDaySec < 0) {
206 GPSDaySec += 86400;
207 }
[1835]208 int weekDay = int(GPSweeksHlp/86400.0);
209 int GPSDaySecHlp = int(GPSweeksHlp) - weekDay * 86400;
[1834]210
211 // Handle the difference between system clock and correction epoch
212 // ---------------------------------------------------------------
213 if (GPSDaySec < GPSDaySecHlp - 3600) {
[1829]214 weekDay += 1;
[1835]215 if (weekDay > 6) {
216 weekDay = 0;
217 GPSweek += 1;
218 }
[1829]219 }
[1834]220 else if (GPSDaySec > GPSDaySecHlp + 3600) {
[1829]221 weekDay -= 1;
[1835]222 if (weekDay < 0) {
223 weekDay = 6;
224 GPSweek -= 1;
225 }
[1834]226 }
227
228 _GPSweeks = weekDay * 86400.0 + GPSDaySec;
[1829]229 }
[918]230
[5576]231 checkProviderID();
232
[3022]233 QStringList asciiLines = corrsToASCIIlines(GPSweek, _GPSweeks,
[3024]234 _co, &_bias);
[3022]235
236 QStringListIterator it(asciiLines);
237 while (it.hasNext()) {
238 QString line = it.next();
[4428]239 printLine(line, GPSweek, _GPSweeks);
[1852]240 }
241
[1829]242 retCode = success;
243 memset(&_co, 0, sizeof(_co));
[1842]244 memset(&_bias, 0, sizeof(_bias));
[869]245 }
[1829]246 }
[1833]247 }
[1832]248
[1833]249 if (retCode != success) {
250 _GPSweeks = -1.0;
[868]251 }
[1829]252 return retCode;
[866]253}
[934]254
255//
256////////////////////////////////////////////////////////////////////////////
[4428]257void RTCM3coDecoder::printLine(const QString& line, int GPSweek,
258 double GPSweeks) {
[934]259 if (_out) {
[971]260 *_out << line.toAscii().data() << endl;
[934]261 _out->flush();
262 }
263
[4428]264 int currWeek;
265 double currSec;
266 currentGPSWeeks(currWeek, currSec);
267 bncTime currTime(currWeek, currSec);
268
[5120]269 bncTime coTime(GPSweek, GPSweeks);
[4428]270
[5120]271 double dt = currTime - coTime;
[4428]272 const double MAXDT = 10 * 60.0;
273 if (fabs(dt) > MAXDT) {
[4429]274 emit newMessage("suspicious correction: " + _staID.toAscii() + " "
275 + line.toAscii(), false);
[4428]276 }
277 else {
278 emit newCorrLine(line, _staID, coTime);
279 }
[934]280}
[3022]281
282//
283////////////////////////////////////////////////////////////////////////////
284QStringList RTCM3coDecoder::corrsToASCIIlines(int GPSweek, double GPSweeks,
285 const ClockOrbit& co,
[5665]286 const CodeBias* bias) {
[3022]287
288 QStringList retLines;
289
290 // Loop over all satellites (GPS and Glonass)
291 // ------------------------------------------
[5665]292 if (co.NumberOfSat[CLOCKORBIT_SATGPS] > 0 || co.NumberOfSat[CLOCKORBIT_SATGLONASS] > 0) {
[3022]293 QString line1;
294 line1.sprintf("! Orbits/Clocks: %d GPS %d Glonass",
[5665]295 co.NumberOfSat[CLOCKORBIT_SATGPS], co.NumberOfSat[CLOCKORBIT_SATGLONASS]);
[3022]296 retLines << line1;
297 }
[5665]298 for (int ii = 0; ii < CLOCKORBIT_NUMGPS+co.NumberOfSat[CLOCKORBIT_SATGLONASS]; ii++) {
[3022]299 char sysCh = ' ';
[5665]300 if (ii < co.NumberOfSat[CLOCKORBIT_SATGPS]) {
[3022]301 sysCh = 'G';
302 }
303 else if (ii >= CLOCKORBIT_NUMGPS) {
304 sysCh = 'R';
305 }
[3024]306
[3022]307 if (sysCh != ' ') {
308
309 QString linePart;
310 linePart.sprintf("%d %d %d %.1f %c%2.2d",
311 co.messageType, co.UpdateInterval, GPSweek, GPSweeks,
312 sysCh, co.Sat[ii].ID);
313
314 // Combined message (orbit and clock)
315 // ----------------------------------
316 if ( co.messageType == COTYPE_GPSCOMBINED ||
317 co.messageType == COTYPE_GLONASSCOMBINED ) {
318 QString line;
319 line.sprintf(" %3d"
320 " %8.3f %8.3f %8.3f %8.3f"
321 " %10.5f %10.5f %10.5f %10.5f"
[5576]322 " %10.5f",
[3022]323 co.Sat[ii].IOD,
324 co.Sat[ii].Clock.DeltaA0,
325 co.Sat[ii].Orbit.DeltaRadial,
326 co.Sat[ii].Orbit.DeltaAlongTrack,
327 co.Sat[ii].Orbit.DeltaCrossTrack,
328 co.Sat[ii].Clock.DeltaA1,
329 co.Sat[ii].Orbit.DotDeltaRadial,
330 co.Sat[ii].Orbit.DotDeltaAlongTrack,
331 co.Sat[ii].Orbit.DotDeltaCrossTrack,
[5576]332 co.Sat[ii].Clock.DeltaA2);
[3022]333 retLines << linePart+line;
334 }
335
336 // Orbits only
337 // -----------
338 else if ( co.messageType == COTYPE_GPSORBIT ||
339 co.messageType == COTYPE_GLONASSORBIT ) {
340 QString line;
341 line.sprintf(" %3d"
342 " %8.3f %8.3f %8.3f"
[5576]343 " %10.5f %10.5f %10.5f",
[3022]344 co.Sat[ii].IOD,
345 co.Sat[ii].Orbit.DeltaRadial,
346 co.Sat[ii].Orbit.DeltaAlongTrack,
347 co.Sat[ii].Orbit.DeltaCrossTrack,
348 co.Sat[ii].Orbit.DotDeltaRadial,
349 co.Sat[ii].Orbit.DotDeltaAlongTrack,
[5576]350 co.Sat[ii].Orbit.DotDeltaCrossTrack);
[3022]351 retLines << linePart+line;
352 }
353
354 // Clocks only
355 // -----------
356 else if ( co.messageType == COTYPE_GPSCLOCK ||
357 co.messageType == COTYPE_GLONASSCLOCK ) {
358 QString line;
[5531]359 line.sprintf(" %8.3f %10.5f %10.5f",
[3022]360 co.Sat[ii].Clock.DeltaA0,
361 co.Sat[ii].Clock.DeltaA1,
362 co.Sat[ii].Clock.DeltaA2);
363 retLines << linePart+line;
364 }
365
366 // User Range Accuracy
367 // -------------------
368 else if ( co.messageType == COTYPE_GPSURA ||
369 co.messageType == COTYPE_GLONASSURA ) {
370 QString line;
[5543]371 line.sprintf(" %f", co.Sat[ii].UserRangeAccuracy);
[3022]372 retLines << linePart+line;
373 }
374
375 // High-Resolution Clocks
376 // ----------------------
377 else if ( co.messageType == COTYPE_GPSHR ||
378 co.messageType == COTYPE_GLONASSHR ) {
379 QString line;
[5543]380 line.sprintf(" %8.3f", co.Sat[ii].hrclock);
[3022]381 retLines << linePart+line;
382 }
383 }
384 }
385
386 // Loop over all satellites (GPS and Glonass)
387 // ------------------------------------------
[3024]388 if (bias) {
[5665]389 if (bias->NumberOfSat[CLOCKORBIT_SATGPS] > 0 || bias->NumberOfSat[CLOCKORBIT_SATGLONASS] > 0) {
[3024]390 QString line1;
391 line1.sprintf("! Biases: %d GPS %d Glonass",
[5665]392 bias->NumberOfSat[CLOCKORBIT_SATGPS], bias->NumberOfSat[CLOCKORBIT_SATGLONASS]);
[3024]393 retLines << line1;
[3022]394 }
[5665]395 for (int ii = 0; ii < CLOCKORBIT_NUMGPS + bias->NumberOfSat[CLOCKORBIT_SATGLONASS]; ii++) {
[3024]396 char sysCh = ' ';
397 int messageType;
[5665]398 if (ii < bias->NumberOfSat[CLOCKORBIT_SATGPS]) {
[3024]399 sysCh = 'G';
400 messageType = BTYPE_GPS;
[3022]401 }
[3024]402 else if (ii >= CLOCKORBIT_NUMGPS) {
403 sysCh = 'R';
404 messageType = BTYPE_GLONASS;
405 }
406 if (sysCh != ' ') {
407 QString line;
408 line.sprintf("%d %d %d %.1f %c%2.2d %d",
409 messageType, bias->UpdateInterval, GPSweek, GPSweeks,
410 sysCh, bias->Sat[ii].ID,
411 bias->Sat[ii].NumberOfCodeBiases);
412 for (int jj = 0; jj < bias->Sat[ii].NumberOfCodeBiases; jj++) {
413 QString hlp;
414 hlp.sprintf(" %d %8.3f", bias->Sat[ii].Biases[jj].Type,
415 bias->Sat[ii].Biases[jj].Bias);
416 line += hlp;
417 }
418 retLines << line;
419 }
[3022]420 }
421 }
422
423 return retLines;
424}
[5576]425
426//
427////////////////////////////////////////////////////////////////////////////
428void RTCM3coDecoder::checkProviderID() {
429
430 if (_co.SSRProviderID == 0 && _co.SSRSolutionID == 0 && _co.SSRIOD == 0) {
431 return;
432 }
433
434 int newProviderID[3];
435 newProviderID[0] = _co.SSRProviderID;
436 newProviderID[1] = _co.SSRSolutionID;
437 newProviderID[2] = _co.SSRIOD;
438
439 bool alreadySet = false;
440 bool different = false;
441
442 for (unsigned ii = 0; ii < 3; ii++) {
443 if (_providerID[ii] != -1) {
444 alreadySet = true;
445 }
446 if (_providerID[ii] != newProviderID[ii]) {
447 different = true;
448 }
449 _providerID[ii] = newProviderID[ii];
450 }
451
452 if (alreadySet && different) {
[5580]453 emit newMessage("RTCM3coDecoder: Provider Changed " + _staID.toAscii() + "\n", true);
[5577]454 emit providerIDChanged(_staID);
[5576]455 }
456}
Note: See TracBrowser for help on using the repository browser.