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