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

Last change on this file since 6470 was 6470, checked in by mervart, 9 years ago
File size: 12.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 *
[6215]37 * Changes:
[866]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 }
[6141]71 _out = 0;
[934]72
[5124]73 qRegisterMetaType<bncTime>("bncTime");
[6215]74 qRegisterMetaType< QList<t_orbCorr> >("QList<t_orbCorr>");
75 qRegisterMetaType< QList<t_clkCorr> >("QList<t_clkCorr>");
[5124]76
[6215]77 connect(this, SIGNAL(newOrbCorrections(QList<t_orbCorr>)),
[6141]78 BNC_CORE, SLOT(slotNewOrbCorrections(QList<t_orbCorr>)));
[1828]79
[6215]80 connect(this, SIGNAL(newClkCorrections(QList<t_clkCorr>)),
[6141]81 BNC_CORE, SLOT(slotNewClkCorrections(QList<t_clkCorr>)));
82
[6215]83 connect(this, SIGNAL(providerIDChanged(QString)),
[5577]84 BNC_CORE, SIGNAL(providerIDChanged(QString)));
85
[6215]86 connect(this, SIGNAL(newMessage(QByteArray,bool)),
[5068]87 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
[4428]88
[6467]89 reset();
[5576]90
91 _providerID[0] = -1;
92 _providerID[1] = -1;
93 _providerID[2] = -1;
[866]94}
95
96// Destructor
97////////////////////////////////////////////////////////////////////////////
98RTCM3coDecoder::~RTCM3coDecoder() {
[935]99 delete _out;
[866]100}
101
[6467]102//
103////////////////////////////////////////////////////////////////////////////
104void RTCM3coDecoder::reset() {
105 memset(&_clkOrb, 0, sizeof(_clkOrb));
106 memset(&_codeBias, 0, sizeof(_codeBias));
107 memset(&_phaseBias, 0, sizeof(_phaseBias));
108 memset(&_vTEC, 0, sizeof(_vTEC));
109}
110
[934]111// Reopen Output File
[6215]112////////////////////////////////////////////////////////////////////////
[6141]113void RTCM3coDecoder::reopen() {
[934]114
[6141]115 if (!_fileNameSkl.isEmpty()) {
[934]116
[1535]117 bncSettings settings;
[934]118
[1154]119 QDateTime datTim = currentDateAndTimeGPS();
[934]120
121 QString hlpStr = bncRinex::nextEpochStr(datTim,
122 settings.value("corrIntr").toString());
123
[6215]124 QString fileNameHlp = _fileNameSkl
[934]125 + QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'))
[938]126 + hlpStr + datTim.toString(".yyC");
[934]127
[6141]128 if (_fileName == fileNameHlp) {
[934]129 return;
130 }
131 else {
[6141]132 _fileName = fileNameHlp;
[934]133 }
134
[6141]135 delete _out;
[1727]136 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
[6141]137 _out = new ofstream( _fileName.toAscii().data(), ios_base::out | ios_base::app );
[1727]138 }
139 else {
[6141]140 _out = new ofstream( _fileName.toAscii().data() );
[1727]141 }
[934]142 }
143}
144
[6215]145//
[866]146////////////////////////////////////////////////////////////////////////////
[1227]147t_irc RTCM3coDecoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
[868]148
[1218]149 errmsg.clear();
150
[1227]151 _buffer.append(QByteArray(buffer,bufLen));
[868]152
[1023]153 t_irc retCode = failure;
154
[1832]155 while(_buffer.size()) {
156
[6454]157 struct ClockOrbit clkOrbSav;
158 struct CodeBias codeBiasSav;
159 struct PhaseBias phaseBiasSav;
160 struct VTEC vTECSav;
161 memcpy(&clkOrbSav, &_clkOrb, sizeof(clkOrbSav)); // save state
162 memcpy(&codeBiasSav, &_codeBias, sizeof(codeBiasSav));
163 memcpy(&phaseBiasSav, &_phaseBias, sizeof(phaseBiasSav));
164 memcpy(&vTECSav, &_vTEC, sizeof(vTECSav));
165
[879]166 int bytesused = 0;
[6454]167 GCOB_RETURN irc = GetSSR(&_clkOrb, &_codeBias, &_vTEC, &_phaseBias,
168 _buffer.data(), _buffer.size(), &bytesused);
[1829]169
[1833]170 if (irc <= -30) { // not enough data - restore state and exit loop
[6454]171 memcpy(&_clkOrb, &clkOrbSav, sizeof(clkOrbSav));
172 memcpy(&_codeBias, &codeBiasSav, sizeof(codeBiasSav));
173 memcpy(&_phaseBias, &phaseBiasSav, sizeof(phaseBiasSav));
174 memcpy(&_vTEC, &vTECSav, sizeof(vTECSav));
[1833]175 break;
[869]176 }
[1832]177
[1833]178 else if (irc < 0) { // error - skip 1 byte and retry
[6467]179 reset();
[1842]180 _buffer = _buffer.mid(bytesused ? bytesused : 1);
[1833]181 }
182
183 else { // OK or MESSAGEFOLLOWS
[1828]184 _buffer = _buffer.mid(bytesused);
185
[6467]186 if (irc == GCOBR_OK || irc == GCOBR_MESSAGEFOLLOWS ) {
[2435]187
[6467]188 setEpochTime(); // sets _lastTime
189
190 if (_lastTime.valid()) {
191 reopen();
192 checkProviderID();
193 sendResults();
194 retCode = success;
[919]195 }
[6467]196 else {
197 retCode = failure;
[1829]198 }
[918]199
[6467]200 reset();
[869]201 }
[1829]202 }
[1833]203 }
[1832]204
[1829]205 return retCode;
[866]206}
[934]207
[6215]208//
[934]209////////////////////////////////////////////////////////////////////////////
[6141]210void RTCM3coDecoder::sendResults() {
[934]211
[6455]212 QList<t_orbCorr>& orbCorrections = _orbCorrections[_lastTime];
213 QList<t_clkCorr>& clkCorrections = _clkCorrections[_lastTime];
[4428]214
[6455]215 // Orbit and clock corrections of all satellites
216 // ---------------------------------------------
[6454]217 for (unsigned ii = 0; ii < CLOCKORBIT_NUMGPS + _clkOrb.NumberOfSat[CLOCKORBIT_SATGLONASS]; ii++) {
[3022]218 char sysCh = ' ';
[6454]219 if (ii < _clkOrb.NumberOfSat[CLOCKORBIT_SATGPS]) {
[3022]220 sysCh = 'G';
221 }
222 else if (ii >= CLOCKORBIT_NUMGPS) {
223 sysCh = 'R';
224 }
[6141]225 else {
226 continue;
227 }
[3024]228
[6141]229 // Orbit correction
230 // ----------------
[6454]231 if ( _clkOrb.messageType == COTYPE_GPSCOMBINED ||
232 _clkOrb.messageType == COTYPE_GLONASSCOMBINED ||
233 _clkOrb.messageType == COTYPE_GPSORBIT ||
234 _clkOrb.messageType == COTYPE_GLONASSORBIT ) {
[3022]235
[6141]236 t_orbCorr orbCorr;
[6454]237 orbCorr._prn.set(sysCh, _clkOrb.Sat[ii].ID);
[6141]238 orbCorr._staID = _staID.toAscii().data();
[6454]239 orbCorr._iod = _clkOrb.Sat[ii].IOD;
[6141]240 orbCorr._time = _lastTime;
241 orbCorr._system = 'R';
[6454]242 orbCorr._xr[0] = _clkOrb.Sat[ii].Orbit.DeltaRadial;
243 orbCorr._xr[1] = _clkOrb.Sat[ii].Orbit.DeltaAlongTrack;
244 orbCorr._xr[2] = _clkOrb.Sat[ii].Orbit.DeltaCrossTrack;
245 orbCorr._dotXr[0] = _clkOrb.Sat[ii].Orbit.DotDeltaRadial;
246 orbCorr._dotXr[1] = _clkOrb.Sat[ii].Orbit.DotDeltaAlongTrack;
247 orbCorr._dotXr[2] = _clkOrb.Sat[ii].Orbit.DotDeltaCrossTrack;
[3022]248
[6141]249 orbCorrections.push_back(orbCorr);
[3022]250
[6454]251 _IODs[orbCorr._prn.toString()] = _clkOrb.Sat[ii].IOD;
[6141]252 }
[3022]253
[6455]254 // Clock Corrections
255 // -----------------
[6454]256 if ( _clkOrb.messageType == COTYPE_GPSCOMBINED ||
257 _clkOrb.messageType == COTYPE_GLONASSCOMBINED ||
258 _clkOrb.messageType == COTYPE_GPSCLOCK ||
259 _clkOrb.messageType == COTYPE_GLONASSCLOCK ) {
[3022]260
[6141]261 t_clkCorr clkCorr;
[6454]262 clkCorr._prn.set(sysCh, _clkOrb.Sat[ii].ID);
[6141]263 clkCorr._staID = _staID.toAscii().data();
264 clkCorr._time = _lastTime;
[6454]265 clkCorr._dClk = _clkOrb.Sat[ii].Clock.DeltaA0 / t_CST::c;
266 clkCorr._dotDClk = _clkOrb.Sat[ii].Clock.DeltaA1 / t_CST::c;
267 clkCorr._dotDotDClk = _clkOrb.Sat[ii].Clock.DeltaA2 / t_CST::c;
[3022]268
[6141]269 if (_IODs.contains(clkCorr._prn.toString())) {
270 clkCorr._iod = _IODs[clkCorr._prn.toString()];
271 clkCorrections.push_back(clkCorr);
[3022]272 }
273 }
[6141]274
275 // High-Resolution Clocks
276 // ----------------------
[6454]277 if ( _clkOrb.messageType == COTYPE_GPSHR ||
278 _clkOrb.messageType == COTYPE_GLONASSHR ) {
[6141]279 }
[3022]280 }
281
[6455]282 // Code Biases
283 // -----------
[6463]284 QList<t_satCodeBias> satCodeBiases;
[6454]285 for (unsigned ii = 0; ii < CLOCKORBIT_NUMGPS + _codeBias.NumberOfSat[CLOCKORBIT_SATGLONASS]; ii++) {
[6141]286 char sysCh = ' ';
[6454]287 if (ii < _codeBias.NumberOfSat[CLOCKORBIT_SATGPS]) {
[6141]288 sysCh = 'G';
[3022]289 }
[6141]290 else if (ii >= CLOCKORBIT_NUMGPS) {
291 sysCh = 'R';
[3022]292 }
[6141]293 else {
294 continue;
295 }
[6463]296 t_satCodeBias satCodeBias;
297 satCodeBias._prn.set(sysCh, _codeBias.Sat[ii].ID);
298 satCodeBias._time = _lastTime;
[6454]299 for (unsigned jj = 0; jj < _codeBias.Sat[ii].NumberOfCodeBiases; jj++) {
[6141]300 }
[3022]301 }
302
[6455]303 // Dump all older epochs
304 // ---------------------
305 QMutableMapIterator<bncTime, QList<t_orbCorr> > itOrb(_orbCorrections);
306 while (itOrb.hasNext()) {
307 itOrb.next();
308 if (itOrb.key() < _lastTime) {
309 emit newOrbCorrections(itOrb.value());
[6456]310 t_orbCorr::writeEpoch(_out, itOrb.value());
[6455]311 itOrb.remove();
312 }
[6141]313 }
[6455]314 QMutableMapIterator<bncTime, QList<t_clkCorr> > itClk(_clkCorrections);
315 while (itClk.hasNext()) {
316 itClk.next();
317 if (itClk.key() < _lastTime) {
318 emit newClkCorrections(itClk.value());
[6456]319 t_clkCorr::writeEpoch(_out, itClk.value());
[6455]320 itClk.remove();
321 }
[6141]322 }
[3022]323}
[5576]324
[6215]325//
[5576]326////////////////////////////////////////////////////////////////////////////
327void RTCM3coDecoder::checkProviderID() {
328
[6454]329 if (_clkOrb.SSRProviderID == 0 && _clkOrb.SSRSolutionID == 0 && _clkOrb.SSRIOD == 0) {
[5576]330 return;
331 }
332
333 int newProviderID[3];
[6454]334 newProviderID[0] = _clkOrb.SSRProviderID;
335 newProviderID[1] = _clkOrb.SSRSolutionID;
336 newProviderID[2] = _clkOrb.SSRIOD;
[5576]337
338 bool alreadySet = false;
339 bool different = false;
340
341 for (unsigned ii = 0; ii < 3; ii++) {
342 if (_providerID[ii] != -1) {
343 alreadySet = true;
344 }
345 if (_providerID[ii] != newProviderID[ii]) {
346 different = true;
347 }
348 _providerID[ii] = newProviderID[ii];
349 }
[6215]350
[5576]351 if (alreadySet && different) {
[5580]352 emit newMessage("RTCM3coDecoder: Provider Changed " + _staID.toAscii() + "\n", true);
[5577]353 emit providerIDChanged(_staID);
[5576]354 }
355}
[6467]356
357
358//
359////////////////////////////////////////////////////////////////////////////
360void RTCM3coDecoder::setEpochTime() {
361
362 _lastTime.reset();
363
364 int epoSecGPS = -1;
365 int epoSecGlo = -1;
366 if (_clkOrb.NumberOfSat[CLOCKORBIT_SATGPS] > 0) {
[6470]367 epoSecGPS = _clkOrb.EpochTime[CLOCKORBIT_SATGPS]; // 0 .. 604799 s
[6467]368 }
369 else if (_codeBias.NumberOfSat[CLOCKORBIT_SATGPS] > 0) {
[6470]370 epoSecGPS = _codeBias.EpochTime[CLOCKORBIT_SATGPS]; // 0 .. 604799 s
[6467]371 }
[6470]372 else if (_phaseBias.NumberOfSat[CLOCKORBIT_SATGPS] > 0) {
373 epoSecGPS = _phaseBias.EpochTime[CLOCKORBIT_SATGPS]; // 0 .. 604799 s
374 }
375 else if (_vTEC.NumLayers > 0) {
376 epoSecGPS = _vTEC.EpochTime; // 0 .. 604799 s
377 }
[6467]378 else if (_clkOrb.NumberOfSat[CLOCKORBIT_SATGLONASS] > 0) {
[6470]379 epoSecGlo = _clkOrb.EpochTime[CLOCKORBIT_SATGLONASS]; // 0 .. 86399 s
[6467]380 }
381 else if (_codeBias.NumberOfSat[CLOCKORBIT_SATGLONASS] > 0) {
[6470]382 epoSecGlo = _codeBias.EpochTime[CLOCKORBIT_SATGLONASS]; // 0 .. 86399 s
[6467]383 }
[6470]384 else if (_phaseBias.NumberOfSat[CLOCKORBIT_SATGLONASS] > 0) {
385 epoSecGlo = _phaseBias.EpochTime[CLOCKORBIT_SATGLONASS]; // 0 .. 86399 s
386 }
[6467]387
388 // Retrieve current time
389 // ---------------------
390 int currentWeek = 0;
391 double currentSec = 0.0;
392 currentGPSWeeks(currentWeek, currentSec);
393 bncTime currentTime(currentWeek, currentSec);
394
395 // Set _lastTime close to currentTime
396 // ----------------------------------
397 if (epoSecGPS != -1) {
398 _lastTime.set(currentWeek, epoSecGPS);
399 }
400 else if (epoSecGlo != -1) {
401 QDate date = dateAndTimeFromGPSweek(currentTime.gpsw(), currentTime.gpssec()).date();
402 epoSecGlo = epoSecGlo - 3 * 3600 + gnumleap(date.year(), date.month(), date.day());
403 _lastTime.set(currentWeek, epoSecGlo);
[6468]404 }
405
406 if (_lastTime.valid()) {
[6469]407 double maxDiff = 12 * 3600.0;
408 while (_lastTime < currentTime - maxDiff) {
409 _lastTime = _lastTime + maxDiff;
[6467]410 }
[6469]411 while (_lastTime > currentTime + maxDiff) {
412 _lastTime = _lastTime - maxDiff;
[6467]413 }
414 }
415}
Note: See TracBrowser for help on using the repository browser.