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