source: ntrip/trunk/BNC/src/PPP/pppRun.cpp@ 6004

Last change on this file since 6004 was 6001, checked in by mervart, 10 years ago
File size: 17.9 KB
RevLine 
[5860]1
2// Part of BNC, a utility for retrieving decoding and
3// converting GNSS data streams from NTRIP broadcasters.
4//
5// Copyright (C) 2007
6// German Federal Agency for Cartography and Geodesy (BKG)
7// http://www.bkg.bund.de
8// Czech Technical University Prague, Department of Geodesy
9// http://www.fsv.cvut.cz
10//
11// Email: euref-ip@bkg.bund.de
12//
13// This program is free software; you can redistribute it and/or
14// modify it under the terms of the GNU General Public License
15// as published by the Free Software Foundation, version 2.
16//
17// This program is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with this program; if not, write to the Free Software
24// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26/* -------------------------------------------------------------------------
27 * BKG NTRIP Client
28 * -------------------------------------------------------------------------
29 *
[5883]30 * Class: t_pppRun
[5860]31 *
32 * Purpose: Single Real-Time PPP Client
33 *
34 * Author: L. Mervart
35 *
36 * Created: 29-Jul-2014
37 *
38 * Changes:
39 *
40 * -----------------------------------------------------------------------*/
41
42
43#include <iostream>
44#include <iomanip>
[5980]45#include <sstream>
[5860]46#include <string.h>
47#include <map>
48
[5883]49#include "pppRun.h"
[5891]50#include "pppThread.h"
[5860]51#include "bnccore.h"
52#include "bncephuser.h"
[5953]53#include "bncsettings.h"
[5984]54#include "bncoutf.h"
[5883]55#include "rinex/rnxobsfile.h"
56#include "rinex/rnxnavfile.h"
57#include "rinex/corrfile.h"
[5860]58
59using namespace BNC_PPP;
60using namespace std;
61
62// Constructor
63////////////////////////////////////////////////////////////////////////////
[5883]64t_pppRun::t_pppRun(const t_pppOptions* opt) {
65
[5860]66 _opt = opt;
[5883]67
[5860]68 connect(this, SIGNAL(newMessage(QByteArray,bool)),
69 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
[5883]70
[5991]71 connect(this, SIGNAL(newPosition(QByteArray, bncTime, QVector<double>)),
[5954]72 BNC_CORE, SIGNAL(newPosition(QByteArray, bncTime, QVector<double>)));
[5949]73
[5991]74 connect(this, SIGNAL(newNMEAstr(QByteArray, QByteArray)),
75 BNC_CORE, SIGNAL(newNMEAstr(QByteArray, QByteArray)));
76
[5883]77 for (unsigned iPrn = 0; iPrn <= t_prn::MAXPRN; iPrn++) {
78 _lastOrbCorrIOD[iPrn] = -1;
79 _lastClkCorrValue[iPrn] = 0.0;
80 }
81
82 _pppClient = new t_pppClient(_opt);
83
[5984]84 bncSettings settings;
85
[5860]86 if (_opt->_realTime) {
[5945]87 Qt::ConnectionType conType = Qt::AutoConnection;
88 if (BNC_CORE->mode() == t_bncCore::batchPostProcessing) {
89 conType = Qt::BlockingQueuedConnection;
90 }
91
[5860]92 connect(BNC_CORE->caster(), SIGNAL(newObs(QByteArray, QList<t_obs>)),
[5945]93 this, SLOT(slotNewObs(QByteArray, QList<t_obs>)),conType);
[5860]94
95 connect(BNC_CORE, SIGNAL(newEphGPS(gpsephemeris)),
[5945]96 this, SLOT(slotNewEphGPS(gpsephemeris)),conType);
[5860]97
98 connect(BNC_CORE, SIGNAL(newEphGlonass(glonassephemeris)),
[5945]99 this, SLOT(slotNewEphGlonass(glonassephemeris)),conType);
[5860]100
101 connect(BNC_CORE, SIGNAL(newEphGalileo(galileoephemeris)),
[5945]102 this, SLOT(slotNewEphGalileo(galileoephemeris)),conType);
[5860]103
104 connect(BNC_CORE, SIGNAL(newCorrections(QStringList)),
[5945]105 this, SLOT(slotNewCorrections(QStringList)),conType);
[5860]106 }
107 else {
[5889]108 _rnxObsFile = 0;
109 _rnxNavFile = 0;
110 _corrFile = 0;
[5953]111 _speed = settings.value("PPP/mapSpeedSlider").toInt();
[5941]112 connect(this, SIGNAL(progressRnxPPP(int)), BNC_CORE, SIGNAL(progressRnxPPP(int)));
113 connect(this, SIGNAL(finishedRnxPPP()), BNC_CORE, SIGNAL(finishedRnxPPP()));
[5950]114 connect(BNC_CORE, SIGNAL(mapSpeedSliderChanged(int)),
115 this, SLOT(slotSetSpeed(int)));
[5973]116 connect(BNC_CORE, SIGNAL(stopRinexPPP()), this, SLOT(slotSetStopFlag()));
[5860]117 }
[5973]118
119 _stopFlag = false;
[5984]120
[5988]121 QString roverName(_opt->_roverName.c_str());
122
[5984]123 QString logFileSkl = settings.value("PPP/logFile").toString();
[5988]124 if (logFileSkl.isEmpty()) {
125 _logFile = 0;
[5987]126 }
127 else {
[5988]128 if (logFileSkl.indexOf("${STATION}") == -1) {
129 logFileSkl = roverName + "_" + logFileSkl;
130 }
131 else {
132 logFileSkl.replace("${STATION}", roverName);
133 }
134 _logFile = new bncoutf(logFileSkl, "1 day", 0);
[5987]135 }
[5988]136
137 QString nmeaFileSkl = settings.value("PPP/nmeaFile").toString();
138 if (nmeaFileSkl.isEmpty()) {
139 _nmeaFile = 0;
140 }
141 else {
142 if (nmeaFileSkl.indexOf("${STATION}") == -1) {
143 nmeaFileSkl = roverName + "_" + nmeaFileSkl;
144 }
145 else {
146 nmeaFileSkl.replace("${STATION}", roverName);
147 }
148 _nmeaFile = new bncoutf(nmeaFileSkl, "1 day", 0);
149 }
[5860]150}
151
152// Destructor
153////////////////////////////////////////////////////////////////////////////
[5883]154t_pppRun::~t_pppRun() {
[5984]155 delete _logFile;
[5988]156 delete _nmeaFile;
[5860]157}
158
159//
160////////////////////////////////////////////////////////////////////////////
[5883]161void t_pppRun::slotNewEphGPS(gpsephemeris gpseph) {
[5860]162 QMutexLocker locker(&_mutex);
163 t_ephGPS eph;
164 eph.set(&gpseph);
165 _pppClient->putEphemeris(&eph);
166}
167
168//
169////////////////////////////////////////////////////////////////////////////
[5883]170void t_pppRun::slotNewEphGlonass(glonassephemeris gloeph) {
[5860]171 QMutexLocker locker(&_mutex);
172 t_ephGlo eph;
173 eph.set(&gloeph);
174 _pppClient->putEphemeris(&eph);
175}
176
177//
178////////////////////////////////////////////////////////////////////////////
[5883]179void t_pppRun::slotNewEphGalileo(galileoephemeris galeph) {
[5860]180 QMutexLocker locker(&_mutex);
181 t_ephGal eph;
182 eph.set(&galeph);
183 _pppClient->putEphemeris(&eph);
184}
185
186//
187////////////////////////////////////////////////////////////////////////////
[5883]188void t_pppRun::slotNewObs(QByteArray staID, QList<t_obs> obsList) {
[5860]189 QMutexLocker locker(&_mutex);
190
191 if (string(staID.data()) != _opt->_roverName) {
192 return;
193 }
194
195 // Loop over all obsevations (possible different epochs)
196 // -----------------------------------------------------
197 QListIterator<t_obs> it(obsList);
198 while (it.hasNext()) {
199 const t_obs& oldObs = it.next();
200 t_satObs* newObs = new t_satObs;
201
202 newObs->_prn.set(oldObs.satSys, oldObs.satNum);
203 newObs->_time.set(oldObs.GPSWeek, oldObs.GPSWeeks);
204
205 // Find the corresponding data epoch or create a new one
206 // -----------------------------------------------------
207 t_epoData* epoch = 0;
208 deque<t_epoData*>::const_iterator it;
209 for (it = _epoData.begin(); it != _epoData.end(); it++) {
210 if (newObs->_time == (*it)->_time) {
211 epoch = *it;
212 break;
213 }
214 }
215 if (epoch == 0) {
216 if (_epoData.empty() || newObs->_time > _epoData.back()->_time) {
217 epoch = new t_epoData;
218 epoch->_time = newObs->_time;
219 _epoData.push_back(epoch);
220 }
221 }
222
223 // Fill the new observation and add it to the corresponding epoch
224 // --------------------------------------------------------------
225 if (epoch != 0) {
226 epoch->_satObs.push_back(newObs);
227 map<string, t_frqObs*> frqObsMap;
228 for (unsigned iEntry = 0; iEntry < GNSSENTRY_NUMBER; iEntry++) {
[5936]229 string hlp = oldObs.rnxStr(iEntry).toAscii().data();
[5933]230 if (hlp.length() >= 2 && oldObs._measdata[iEntry] != 0.0) {
[5860]231 char obsType = hlp[0];
232 string rnxType2ch = hlp.substr(1);
233 if (obsType == 'C' || obsType == 'L') {
234 t_frqObs* frqObs = 0;
235 if (frqObsMap.find(rnxType2ch) == frqObsMap.end()) {
236 frqObs = new t_frqObs();
237 frqObsMap[rnxType2ch] = frqObs;
238 frqObs->_rnxType2ch = rnxType2ch;
239 newObs->_obs.push_back(frqObs);
240 }
241 else {
242 frqObs = frqObsMap[rnxType2ch];
243 }
244 if (obsType == 'C') {
245 frqObs->_code = oldObs._measdata[iEntry];
246 frqObs->_codeValid = true;
247 }
248 else if (obsType == 'L') {
249 frqObs->_phase = oldObs._measdata[iEntry];
250 frqObs->_phaseValid = true;
251 }
252 }
253 }
254 }
255 }
256 }
257
258 // Process the oldest epochs
259 // ------------------------
[5999]260 while (_epoData.size() && !waitForCorr(_epoData.front()->_time)) {
[5860]261
262 const vector<t_satObs*>& satObs = _epoData.front()->_satObs;
263
264 t_output output;
265 _pppClient->processEpoch(satObs, &output);
266
[5953]267 if (!output._error) {
[5879]268 QVector<double> xx(6);
269 xx.data()[0] = output._xyzRover[0];
270 xx.data()[1] = output._xyzRover[1];
271 xx.data()[2] = output._xyzRover[2];
272 xx.data()[3] = output._neu[0];
273 xx.data()[4] = output._neu[1];
274 xx.data()[5] = output._neu[2];
[5954]275 emit newPosition(staID, output._epoTime, xx);
[5873]276 }
277
[5860]278 delete _epoData.front(); _epoData.pop_front();
279
[5980]280 ostringstream log;
281 if (output._error) {
282 log << output._log;
283 }
284 else {
285 log.setf(ios::fixed);
[5982]286 log << string(output._epoTime) << ' ' << staID.data()
287 << " X = " << setprecision(4) << output._xyzRover[0]
288 << " Y = " << setprecision(4) << output._xyzRover[1]
289 << " Z = " << setprecision(4) << output._xyzRover[2]
290 << " NEU: " << setprecision(4) << output._neu[0]
291 << " " << setprecision(4) << output._neu[1]
292 << " " << setprecision(4) << output._neu[2];
[5980]293 }
294
[5988]295 if (_logFile && output._epoTime.valid()) {
[5986]296 _logFile->write(output._epoTime.gpsw(), output._epoTime.gpssec(),
297 QString(output._log.c_str()));
298 }
[5984]299
[5989]300 if (!output._error) {
[5990]301 QString rmcStr = nmeaString('R', output);
302 QString ggaStr = nmeaString('G', output);
[5989]303 if (_nmeaFile) {
304 _nmeaFile->write(output._epoTime.gpsw(), output._epoTime.gpssec(), rmcStr);
305 _nmeaFile->write(output._epoTime.gpsw(), output._epoTime.gpssec(), ggaStr);
306 }
307 emit newNMEAstr(staID, rmcStr.toAscii());
308 emit newNMEAstr(staID, ggaStr.toAscii());
309 }
310
[5980]311 emit newMessage(QByteArray(log.str().c_str()), true);
[5860]312 }
313}
314
315//
316////////////////////////////////////////////////////////////////////////////
[5883]317void t_pppRun::slotNewCorrections(QStringList corrList) {
[5860]318 QMutexLocker locker(&_mutex);
319
320 // Check the Mountpoint (source of corrections)
321 // --------------------------------------------
[5937]322 if (_opt->_realTime) {
323 if (_opt->_corrMount.empty()) {
324 return;
325 }
326 QMutableListIterator<QString> itm(corrList);
327 while (itm.hasNext()) {
328 QStringList hlp = itm.next().split(" ");
329 if (hlp.size() > 0) {
330 QString mountpoint = hlp[hlp.size()-1];
331 if (mountpoint != QString(_opt->_corrMount.c_str())) {
332 itm.remove();
333 }
[5860]334 }
335 }
336 }
337
338 if (corrList.size() == 0) {
339 return;
340 }
341
342 vector<t_orbCorr*> orbCorr;
343 vector<t_clkCorr*> clkCorr;
344 vector<t_satBias*> satBias;
345
346 QListIterator<QString> it(corrList);
347 while (it.hasNext()) {
348 QString line = it.next();
349
350 QTextStream in(&line);
351 int messageType;
352 int updateInterval;
353 int GPSweek;
354 double GPSweeks;
355 QString prn;
356 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
357
358 if ( t_corr::relevantMessageType(messageType) ) {
359 t_corr corr;
360 corr.readLine(line);
361 if (messageType == COTYPE_GPSCOMBINED || messageType == COTYPE_GLONASSCOMBINED ||
362 messageType == COTYPE_GPSORBIT || messageType == COTYPE_GLONASSORBIT ) {
363 t_orbCorr* cc = new t_orbCorr();
364 cc->_prn.set(corr.prn.toAscii().data());
365 cc->_iod = corr.iod;
366 cc->_time = corr.tRao;
367 cc->_system = 'R';
368 cc->_xr[0] = corr.rao[0];
369 cc->_xr[1] = corr.rao[1];
370 cc->_xr[2] = corr.rao[2];
371 cc->_dotXr[0] = corr.dotRao[0];
372 cc->_dotXr[0] = corr.dotRao[1];
373 cc->_dotXr[0] = corr.dotRao[2];
374 orbCorr.push_back(cc);
375
376 _lastOrbCorrIOD[cc->_prn.toInt()] = cc->_iod;
377 }
378 else if (messageType == COTYPE_GPSCOMBINED || messageType == COTYPE_GLONASSCOMBINED ||
379 messageType == COTYPE_GPSCLOCK || messageType == COTYPE_GLONASSCLOCK ) {
380 t_clkCorr* cc = new t_clkCorr();
381 cc->_prn.set(corr.prn.toAscii().data());
382 cc->_iod = corr.iod;
383 cc->_time = corr.tClk;
384 cc->_dClk = corr.dClk;
385 cc->_dotDClk = corr.dotDClk;
386 cc->_dotDotDClk = corr.dotDotDClk;
387 cc->_clkPartial = 0.0;
388 if (messageType == COTYPE_GPSCLOCK || messageType == COTYPE_GLONASSCLOCK) {
389 int lastIOD = _lastOrbCorrIOD[cc->_prn.toInt()];
390 if (lastIOD != -1) {
391 cc->_iod = lastIOD;
392 }
393 else {
394 delete cc;
395 cc = 0;
396 }
397 }
398 if (cc) {
[5998]399 clkCorr.push_back(cc);
400 _lastClkCorrValue[cc->_prn.toInt()] = cc->_dClk;
[5860]401 if (_lastClkCorrTime.undef() || cc->_time > _lastClkCorrTime) {
402 _lastClkCorrTime = cc->_time;
403 }
404 }
405 }
406 }
407 else if ( messageType == BTYPE_GPS || messageType == BTYPE_GLONASS ) {
408 t_bias bias;
409 bias.readLine(line);
410 }
411 }
412
413 _pppClient->putOrbCorrections(orbCorr);
414 _pppClient->putClkCorrections(clkCorr);
415 _pppClient->putBiases(satBias);
416
417 for (unsigned ii = 0; ii < orbCorr.size(); ii++) {
418 delete orbCorr[ii];
419 }
420 for (unsigned ii = 0; ii < clkCorr.size(); ii++) {
421 delete clkCorr[ii];
422 }
423 for (unsigned ii = 0; ii < satBias.size(); ii++) {
424 delete satBias[ii];
425 }
426}
[5883]427
428
429//
430////////////////////////////////////////////////////////////////////////////
431void t_pppRun::processFiles() {
432
433 try {
[5889]434 _rnxObsFile = new t_rnxObsFile(QString(_opt->_rinexObs.c_str()), t_rnxObsFile::input);
[5883]435 }
436 catch (...) {
437 delete _rnxObsFile; _rnxObsFile = 0;
[5940]438 emit finishedRnxPPP();
[5883]439 return;
440 }
441
[5889]442 _rnxNavFile = new t_rnxNavFile(QString(_opt->_rinexNav.c_str()), t_rnxNavFile::input);
[5883]443
[5889]444 if (!_opt->_corrFile.empty()) {
445 _corrFile = new t_corrFile(QString(_opt->_corrFile.c_str()));
[5883]446 connect(_corrFile, SIGNAL(newCorrections(QStringList)),
[5889]447 this, SLOT(slotNewCorrections(QStringList)));
[5883]448 }
449
450 // Read/Process Observations
451 // -------------------------
452 int nEpo = 0;
453 const t_rnxObsFile::t_rnxEpo* epo = 0;
[5973]454 while ( !_stopFlag && (epo = _rnxObsFile->nextEpoch()) != 0 ) {
[5883]455 ++nEpo;
456
[5951]457 if (_speed < 100) {
458 double sleepTime = 2.0 / _speed;
459 t_pppThread::msleep(sleepTime*1.e3);
[5883]460 }
461
462 // Get Corrections
463 // ---------------
464 if (_corrFile) {
465 _corrFile->syncRead(epo->tt);
466 }
467
468 // Get Ephemerides
469 // ----------------
470 t_eph* eph = 0;
471 const QMap<QString, int>* corrIODs = _corrFile ? &_corrFile->corrIODs() : 0;
472 while ( (eph = _rnxNavFile->getNextEph(epo->tt, corrIODs)) != 0 ) {
[5889]473 _pppClient->putEphemeris(eph);
474 delete eph; eph = 0;
[5883]475 }
476
[5890]477 // Create list of observations and start epoch processing
478 // ------------------------------------------------------
479 QList<t_obs> obsList;
[5883]480 for (unsigned iObs = 0; iObs < epo->rnxSat.size(); iObs++) {
481 const t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iObs];
482
483 t_obs obs;
484 t_rnxObsFile::setObsFromRnx(_rnxObsFile, epo, rnxSat, obs);
[5890]485 obsList << obs;
486 }
487 slotNewObs(QByteArray(_opt->_roverName.c_str()), obsList);
[5883]488
[5890]489
[5883]490 if (nEpo % 10 == 0) {
[5940]491 emit progressRnxPPP(nEpo);
[5883]492 }
[5950]493
494 QCoreApplication::processEvents();
[5883]495 }
496
[5940]497 emit finishedRnxPPP();
[5927]498
[5883]499 if (BNC_CORE->mode() != t_bncCore::interactive) {
500 qApp->exit(0);
501 }
[5944]502 else {
503 BNC_CORE->stopPPP();
504 }
[5883]505}
506
507//
508////////////////////////////////////////////////////////////////////////////
509void t_pppRun::slotSetSpeed(int speed) {
510 QMutexLocker locker(&_mutex);
511 _speed = speed;
512}
[5973]513
514//
515////////////////////////////////////////////////////////////////////////////
516void t_pppRun::slotSetStopFlag() {
517 QMutexLocker locker(&_mutex);
518 _stopFlag = true;
519}
[5989]520
521//
522////////////////////////////////////////////////////////////////////////////
[5990]523QString t_pppRun::nmeaString(char strType, const t_output& output) {
[5989]524
525 double ell[3];
526 xyz2ell(output._xyzRover, ell);
527 double phiDeg = ell[0] * 180 / M_PI;
528 double lamDeg = ell[1] * 180 / M_PI;
529
530 char phiCh = 'N';
531 if (phiDeg < 0) {
532 phiDeg = -phiDeg;
533 phiCh = 'S';
534 }
535 char lamCh = 'E';
536 if (lamDeg < 0) {
537 lamDeg = -lamDeg;
538 lamCh = 'W';
539 }
540
[5990]541 ostringstream out;
542 out.setf(ios::fixed);
[5989]543
[5990]544 if (strType == 'R') {
545 string datestr = output._epoTime.datestr(0); // yyyymmdd
546 out << "GPRMC,"
547 << output._epoTime.timestr(0,0) << ",A,"
548 << setw(2) << setfill('0') << int(phiDeg)
549 << setw(6) << setprecision(3) << setfill('0')
550 << fmod(60*phiDeg,60) << ',' << phiCh << ','
551 << setw(3) << setfill('0') << int(lamDeg)
552 << setw(6) << setprecision(3) << setfill('0')
553 << fmod(60*lamDeg,60) << ',' << lamCh << ",,,"
554 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
555 << datestr[2] << datestr[3] << ",,";
556 }
557 else if (strType == 'G') {
558 out << "GPGGA,"
559 << output._epoTime.timestr(0,0) << ','
560 << setw(2) << setfill('0') << int(phiDeg)
561 << setw(10) << setprecision(7) << setfill('0')
562 << fmod(60*phiDeg,60) << ',' << phiCh << ','
563 << setw(3) << setfill('0') << int(lamDeg)
564 << setw(10) << setprecision(7) << setfill('0')
565 << fmod(60*lamDeg,60) << ',' << lamCh
566 << ",1," << setw(2) << setfill('0') << output._numSat << ','
567 << setw(3) << setprecision(1) << output._pDop << ','
568 << setprecision(3) << ell[2] << ",M,0.0,M,,";
569 }
570 else {
571 return "";
572 }
[5989]573
[5990]574 QString nmStr(out.str().c_str());
[5989]575 unsigned char XOR = 0;
576 for (int ii = 0; ii < nmStr.length(); ii++) {
577 XOR ^= (unsigned char) nmStr[ii].toAscii();
578 }
579
580 return '$' + nmStr + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
581}
582
[5999]583//
584////////////////////////////////////////////////////////////////////////////
585bool t_pppRun::waitForCorr(const bncTime& epoTime) const {
586
[6001]587 if (!_opt->_realTime || _opt->_corrMount.empty()) {
[5999]588 return false;
589 }
[6001]590 else if (!_lastClkCorrTime.valid()) {
591 return true;
592 }
593 else {
594 double dt = epoTime - _lastClkCorrTime;
595 if (dt > 1.0 && dt < _opt->_corrWaitTime) {
596 return true;
597 }
598 else {
599 return false;
600 }
601 }
[5999]602 return false;
603}
Note: See TracBrowser for help on using the repository browser.