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

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