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

Last change on this file since 5539 was 5531, checked in by mervart, 11 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 *
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
[4428]79 connect(this, SIGNAL(newMessage(QByteArray,bool)),
[5068]80 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
[4428]81
[1828]82 memset(&_co, 0, sizeof(_co));
[3077]83 memset(&_bias, 0, sizeof(_bias));
[866]84}
85
86// Destructor
87////////////////////////////////////////////////////////////////////////////
88RTCM3coDecoder::~RTCM3coDecoder() {
[935]89 delete _out;
[866]90}
91
[934]92// Reopen Output File
93////////////////////////////////////////////////////////////////////////
[3035]94void RTCM3coDecoder::reopen(const QString& fileNameSkl, QString& fileName,
95 ofstream*& out) {
[934]96
[3035]97 if (!fileNameSkl.isEmpty()) {
[934]98
[1535]99 bncSettings settings;
[934]100
[1154]101 QDateTime datTim = currentDateAndTimeGPS();
[934]102
103 QString hlpStr = bncRinex::nextEpochStr(datTim,
104 settings.value("corrIntr").toString());
105
[3035]106 QString fileNameHlp = fileNameSkl
[934]107 + QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'))
[938]108 + hlpStr + datTim.toString(".yyC");
[934]109
[3035]110 if (fileName == fileNameHlp) {
[934]111 return;
112 }
113 else {
[3035]114 fileName = fileNameHlp;
[934]115 }
116
[3035]117 delete out;
[1727]118 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
[3035]119 out = new ofstream( fileName.toAscii().data(),
[1727]120 ios_base::out | ios_base::app );
121 }
122 else {
[3035]123 out = new ofstream( fileName.toAscii().data() );
[1727]124 }
[934]125 }
126}
127
[866]128//
129////////////////////////////////////////////////////////////////////////////
[1227]130t_irc RTCM3coDecoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
[868]131
[1218]132 errmsg.clear();
133
[1227]134 _buffer.append(QByteArray(buffer,bufLen));
[868]135
[1023]136 t_irc retCode = failure;
137
[1832]138 while(_buffer.size()) {
139
[879]140 int bytesused = 0;
[1832]141 struct ClockOrbit co_sav;
142 memcpy(&co_sav, &_co, sizeof(co_sav)); // save state
[1829]143
144 GCOB_RETURN irc = GetClockOrbitBias(&_co, &_bias, _buffer.data(),
[879]145 _buffer.size(), &bytesused);
146
[1833]147 if (irc <= -30) { // not enough data - restore state and exit loop
[1832]148 memcpy(&_co, &co_sav, sizeof(co_sav));
[1833]149 break;
[869]150 }
[1832]151
[1833]152 else if (irc < 0) { // error - skip 1 byte and retry
153 memset(&_co, 0, sizeof(_co));
[1842]154 memset(&_bias, 0, sizeof(_bias));
155 _buffer = _buffer.mid(bytesused ? bytesused : 1);
[1833]156 }
157
158 else { // OK or MESSAGEFOLLOWS
[1828]159 _buffer = _buffer.mid(bytesused);
160
[4900]161 if ( (irc == GCOBR_OK || irc == GCOBR_MESSAGEFOLLOWS ) &&
[3427]162 (_co.NumberOfGPSSat > 0 || _co.NumberOfGLONASSSat > 0 ||
163 _bias.NumberOfGPSSat > 0 || _bias.NumberOfGLONASSSat > 0) ) {
[2435]164
[3035]165 reopen(_fileNameSkl, _fileName, _out);
[1835]166
167 // Guess GPS week and sec using system time
168 // ----------------------------------------
[1829]169 int GPSweek;
[1835]170 double GPSweeksHlp;
171 currentGPSWeeks(GPSweek, GPSweeksHlp);
172
173 // Correction Epoch from GPSEpochTime
174 // ----------------------------------
[3427]175 if (_co.NumberOfGPSSat > 0 || _bias.NumberOfGPSSat > 0) {
176 int GPSEpochTime = (_co.NumberOfGPSSat > 0) ?
[3428]177 _co.GPSEpochTime : _bias.GPSEpochTime;
[3427]178 if (GPSweeksHlp > GPSEpochTime + 86400.0) {
[919]179 GPSweek += 1;
180 }
[3427]181 else if (GPSweeksHlp < GPSEpochTime - 86400.0) {
[919]182 GPSweek -= 1;
183 }
[3427]184 _GPSweeks = GPSEpochTime;
[919]185 }
[1835]186
187 // Correction Epoch from Glonass Epoch
188 // -----------------------------------
[3427]189 else if (_co.NumberOfGLONASSSat > 0 || _bias.NumberOfGLONASSSat > 0){
190 int GLONASSEpochTime = (_co.NumberOfGLONASSSat > 0) ?
[3428]191 _co.GLONASSEpochTime : _bias.GLONASSEpochTime;
[1834]192
193 // Second of day (GPS time) from Glonass Epoch
194 // -------------------------------------------
[1835]195 QDate date = dateAndTimeFromGPSweek(GPSweek, GPSweeksHlp).date();
[1834]196 int leapSecond = gnumleap(date.year(), date.month(), date.day());
[3427]197 int GPSDaySec = GLONASSEpochTime - 3 * 3600 + leapSecond;
[1834]198
[1835]199 int weekDay = int(GPSweeksHlp/86400.0);
200 int GPSDaySecHlp = int(GPSweeksHlp) - weekDay * 86400;
[1834]201
202 // Handle the difference between system clock and correction epoch
203 // ---------------------------------------------------------------
204 if (GPSDaySec < GPSDaySecHlp - 3600) {
[1829]205 weekDay += 1;
[1835]206 if (weekDay > 6) {
207 weekDay = 0;
208 GPSweek += 1;
209 }
[1829]210 }
[1834]211 else if (GPSDaySec > GPSDaySecHlp + 3600) {
[1829]212 weekDay -= 1;
[1835]213 if (weekDay < 0) {
214 weekDay = 6;
215 GPSweek -= 1;
216 }
[1834]217 }
218
219 _GPSweeks = weekDay * 86400.0 + GPSDaySec;
[1829]220 }
[918]221
[3022]222 QStringList asciiLines = corrsToASCIIlines(GPSweek, _GPSweeks,
[3024]223 _co, &_bias);
[3022]224
225 QStringListIterator it(asciiLines);
226 while (it.hasNext()) {
227 QString line = it.next();
[4428]228 printLine(line, GPSweek, _GPSweeks);
[1852]229 }
230
[1829]231 retCode = success;
232 memset(&_co, 0, sizeof(_co));
[1842]233 memset(&_bias, 0, sizeof(_bias));
[869]234 }
[1829]235 }
[1833]236 }
[1832]237
[1833]238 if (retCode != success) {
239 _GPSweeks = -1.0;
[868]240 }
[1829]241 return retCode;
[866]242}
[934]243
244//
245////////////////////////////////////////////////////////////////////////////
[4428]246void RTCM3coDecoder::printLine(const QString& line, int GPSweek,
247 double GPSweeks) {
[934]248 if (_out) {
[971]249 *_out << line.toAscii().data() << endl;
[934]250 _out->flush();
251 }
252
[4428]253 int currWeek;
254 double currSec;
255 currentGPSWeeks(currWeek, currSec);
256 bncTime currTime(currWeek, currSec);
257
[5120]258 bncTime coTime(GPSweek, GPSweeks);
[4428]259
[5120]260 double dt = currTime - coTime;
[4428]261 const double MAXDT = 10 * 60.0;
262 if (fabs(dt) > MAXDT) {
[4429]263 emit newMessage("suspicious correction: " + _staID.toAscii() + " "
264 + line.toAscii(), false);
[4428]265 }
266 else {
267 emit newCorrLine(line, _staID, coTime);
268 }
[934]269}
[3022]270
271//
272////////////////////////////////////////////////////////////////////////////
273QStringList RTCM3coDecoder::corrsToASCIIlines(int GPSweek, double GPSweeks,
274 const ClockOrbit& co,
[3024]275 const Bias* bias) {
[3022]276
277 QStringList retLines;
278
279 // Loop over all satellites (GPS and Glonass)
280 // ------------------------------------------
281 if (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) {
282 QString line1;
283 line1.sprintf("! Orbits/Clocks: %d GPS %d Glonass",
284 co.NumberOfGPSSat, co.NumberOfGLONASSSat);
285 retLines << line1;
286 }
287 for (int ii = 0; ii < CLOCKORBIT_NUMGPS+co.NumberOfGLONASSSat; ii++) {
288 char sysCh = ' ';
289 if (ii < co.NumberOfGPSSat) {
290 sysCh = 'G';
291 }
292 else if (ii >= CLOCKORBIT_NUMGPS) {
293 sysCh = 'R';
294 }
[3024]295
[3022]296 if (sysCh != ' ') {
297
298 QString linePart;
299 linePart.sprintf("%d %d %d %.1f %c%2.2d",
300 co.messageType, co.UpdateInterval, GPSweek, GPSweeks,
301 sysCh, co.Sat[ii].ID);
302
303 // Combined message (orbit and clock)
304 // ----------------------------------
305 if ( co.messageType == COTYPE_GPSCOMBINED ||
306 co.messageType == COTYPE_GLONASSCOMBINED ) {
307 QString line;
308 line.sprintf(" %3d"
309 " %8.3f %8.3f %8.3f %8.3f"
310 " %10.5f %10.5f %10.5f %10.5f"
311 " %10.5f",
312 co.Sat[ii].IOD,
313 co.Sat[ii].Clock.DeltaA0,
314 co.Sat[ii].Orbit.DeltaRadial,
315 co.Sat[ii].Orbit.DeltaAlongTrack,
316 co.Sat[ii].Orbit.DeltaCrossTrack,
317 co.Sat[ii].Clock.DeltaA1,
318 co.Sat[ii].Orbit.DotDeltaRadial,
319 co.Sat[ii].Orbit.DotDeltaAlongTrack,
320 co.Sat[ii].Orbit.DotDeltaCrossTrack,
321 co.Sat[ii].Clock.DeltaA2);
322 retLines << linePart+line;
323 }
324
325 // Orbits only
326 // -----------
327 else if ( co.messageType == COTYPE_GPSORBIT ||
328 co.messageType == COTYPE_GLONASSORBIT ) {
329 QString line;
330 line.sprintf(" %3d"
331 " %8.3f %8.3f %8.3f"
332 " %10.5f %10.5f %10.5f",
333 co.Sat[ii].IOD,
334 co.Sat[ii].Orbit.DeltaRadial,
335 co.Sat[ii].Orbit.DeltaAlongTrack,
336 co.Sat[ii].Orbit.DeltaCrossTrack,
337 co.Sat[ii].Orbit.DotDeltaRadial,
338 co.Sat[ii].Orbit.DotDeltaAlongTrack,
339 co.Sat[ii].Orbit.DotDeltaCrossTrack);
340 retLines << linePart+line;
341 }
342
343 // Clocks only
344 // -----------
345 else if ( co.messageType == COTYPE_GPSCLOCK ||
346 co.messageType == COTYPE_GLONASSCLOCK ) {
347 QString line;
[5531]348 line.sprintf(" %8.3f %10.5f %10.5f",
[3022]349 co.Sat[ii].Clock.DeltaA0,
350 co.Sat[ii].Clock.DeltaA1,
351 co.Sat[ii].Clock.DeltaA2);
352 retLines << linePart+line;
353 }
354
355 // User Range Accuracy
356 // -------------------
357 else if ( co.messageType == COTYPE_GPSURA ||
358 co.messageType == COTYPE_GLONASSURA ) {
359 QString line;
360 line.sprintf(" %3d %f",
361 co.Sat[ii].IOD, co.Sat[ii].UserRangeAccuracy);
362 retLines << linePart+line;
363 }
364
365 // High-Resolution Clocks
366 // ----------------------
367 else if ( co.messageType == COTYPE_GPSHR ||
368 co.messageType == COTYPE_GLONASSHR ) {
369 QString line;
370 line.sprintf(" %3d %8.3f",
371 co.Sat[ii].IOD, co.Sat[ii].hrclock);
372 retLines << linePart+line;
373 }
374 }
375 }
376
377 // Loop over all satellites (GPS and Glonass)
378 // ------------------------------------------
[3024]379 if (bias) {
380 if (bias->NumberOfGPSSat > 0 || bias->NumberOfGLONASSSat > 0) {
381 QString line1;
382 line1.sprintf("! Biases: %d GPS %d Glonass",
383 bias->NumberOfGPSSat, bias->NumberOfGLONASSSat);
384 retLines << line1;
[3022]385 }
[3024]386 for (int ii = 0; ii < CLOCKORBIT_NUMGPS + bias->NumberOfGLONASSSat; ii++) {
387 char sysCh = ' ';
388 int messageType;
389 if (ii < bias->NumberOfGPSSat) {
390 sysCh = 'G';
391 messageType = BTYPE_GPS;
[3022]392 }
[3024]393 else if (ii >= CLOCKORBIT_NUMGPS) {
394 sysCh = 'R';
395 messageType = BTYPE_GLONASS;
396 }
397 if (sysCh != ' ') {
398 QString line;
399 line.sprintf("%d %d %d %.1f %c%2.2d %d",
400 messageType, bias->UpdateInterval, GPSweek, GPSweeks,
401 sysCh, bias->Sat[ii].ID,
402 bias->Sat[ii].NumberOfCodeBiases);
403 for (int jj = 0; jj < bias->Sat[ii].NumberOfCodeBiases; jj++) {
404 QString hlp;
405 hlp.sprintf(" %d %8.3f", bias->Sat[ii].Biases[jj].Type,
406 bias->Sat[ii].Biases[jj].Bias);
407 line += hlp;
408 }
409 retLines << line;
410 }
[3022]411 }
412 }
413
414 return retLines;
415}
Note: See TracBrowser for help on using the repository browser.