source: ntrip/trunk/BNC/src/pppRun.cpp@ 7288

Last change on this file since 7288 was 7288, checked in by stuerze, 9 years ago

phase biases added

File size: 18.2 KB
Line 
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 *
30 * Class: t_pppRun
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>
45#include <sstream>
46#include <string.h>
47#include <map>
48
49#include "pppRun.h"
50#include "pppThread.h"
51#include "bnccore.h"
52#include "bncephuser.h"
53#include "bncsettings.h"
54#include "bncoutf.h"
55#include "bncsinextro.h"
56#include "rinex/rnxobsfile.h"
57#include "rinex/rnxnavfile.h"
58#include "rinex/corrfile.h"
59
60using namespace BNC_PPP;
61using namespace std;
62
63// Constructor
64////////////////////////////////////////////////////////////////////////////
65t_pppRun::t_pppRun(const t_pppOptions* opt) {
66
67 _opt = opt;
68
69 connect(this, SIGNAL(newMessage(QByteArray,bool)),
70 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
71
72 connect(this, SIGNAL(newPosition(QByteArray, bncTime, QVector<double>)),
73 BNC_CORE, SIGNAL(newPosition(QByteArray, bncTime, QVector<double>)));
74
75 connect(this, SIGNAL(newNMEAstr(QByteArray, QByteArray)),
76 BNC_CORE, SIGNAL(newNMEAstr(QByteArray, QByteArray)));
77
78 _pppClient = new t_pppClient(_opt);
79
80 bncSettings settings;
81
82 if (_opt->_realTime) {
83 Qt::ConnectionType conType = Qt::AutoConnection;
84 if (BNC_CORE->mode() == t_bncCore::batchPostProcessing) {
85 conType = Qt::BlockingQueuedConnection;
86 }
87
88 connect(BNC_CORE->caster(), SIGNAL(newObs(QByteArray, QList<t_satObs>)),
89 this, SLOT(slotNewObs(QByteArray, QList<t_satObs>)),conType);
90
91 connect(BNC_CORE, SIGNAL(newGPSEph(t_ephGPS)),
92 this, SLOT(slotNewGPSEph(t_ephGPS)),conType);
93
94 connect(BNC_CORE, SIGNAL(newGlonassEph(t_ephGlo)),
95 this, SLOT(slotNewGlonassEph(t_ephGlo)),conType);
96
97 connect(BNC_CORE, SIGNAL(newGalileoEph(t_ephGal)),
98 this, SLOT(slotNewGalileoEph(t_ephGal)),conType);
99
100 connect(BNC_CORE, SIGNAL(newBDSEph(t_ephBDS)),
101 this, SLOT(slotNewBDSEph(t_ephBDS)),conType);
102
103 connect(BNC_CORE, SIGNAL(newTec(t_vTec)),
104 this, SLOT(slotNewTec(t_vTec)),conType);
105
106 connect(BNC_CORE, SIGNAL(newOrbCorrections(QList<t_orbCorr>)),
107 this, SLOT(slotNewOrbCorrections(QList<t_orbCorr>)),conType);
108
109 connect(BNC_CORE, SIGNAL(newClkCorrections(QList<t_clkCorr>)),
110 this, SLOT(slotNewClkCorrections(QList<t_clkCorr>)),conType);
111
112 connect(BNC_CORE, SIGNAL(newCodeBiases(QList<t_satCodeBias>)),
113 this, SLOT(slotNewCodeBiases(QList<t_satCodeBias>)),conType);
114
115 connect(BNC_CORE, SIGNAL(newPhaseBiases(QList<t_satPhaseBias>)),
116 this, SLOT(slotNewPhaseBiases(QList<t_satPhaseBias>)),conType);
117 }
118 else {
119 _rnxObsFile = 0;
120 _rnxNavFile = 0;
121 _corrFile = 0;
122 _speed = settings.value("PPP/mapSpeedSlider").toInt();
123 connect(this, SIGNAL(progressRnxPPP(int)), BNC_CORE, SIGNAL(progressRnxPPP(int)));
124 connect(this, SIGNAL(finishedRnxPPP()), BNC_CORE, SIGNAL(finishedRnxPPP()));
125 connect(BNC_CORE, SIGNAL(mapSpeedSliderChanged(int)),
126 this, SLOT(slotSetSpeed(int)));
127 connect(BNC_CORE, SIGNAL(stopRinexPPP()), this, SLOT(slotSetStopFlag()));
128 }
129
130 _stopFlag = false;
131
132 QString roverName(_opt->_roverName.c_str());
133
134 QString logFileSkl = settings.value("PPP/logFilePPP").toString();
135 if (logFileSkl.isEmpty()) {
136 _logFile = 0;
137 }
138 else {
139 if (logFileSkl.indexOf("${STATION}") == -1) {
140 logFileSkl = logFileSkl + "_" + roverName;
141 }
142 else {
143 logFileSkl.replace("${STATION}", roverName);
144 }
145 _logFile = new bncoutf(logFileSkl, "1 day", 0);
146 }
147
148 QString nmeaFileSkl = settings.value("PPP/nmeaFile").toString();
149 if (nmeaFileSkl.isEmpty()) {
150 _nmeaFile = 0;
151 }
152 else {
153 if (nmeaFileSkl.indexOf("${STATION}") == -1) {
154 nmeaFileSkl = roverName + "_" + nmeaFileSkl;
155 }
156 else {
157 nmeaFileSkl.replace("${STATION}", roverName);
158 }
159 _nmeaFile = new bncoutf(nmeaFileSkl, "1 day", 0);
160 }
161
162 QString snxtroFileSkl = settings.value("PPP/snxtroFile").toString();
163 if (snxtroFileSkl.isEmpty()) {
164 _snxtroFile = 0;
165 }
166 else {
167 if (snxtroFileSkl.indexOf("${STATION}") == -1) {
168 snxtroFileSkl = snxtroFileSkl + "_" + roverName;
169 }
170 else {
171 snxtroFileSkl.replace("${STATION}", roverName.left(4));
172 }
173 int samplSnxTro = settings.value("PPP/snxtroSampl").toInt();
174 _snxtroFile = new bncSinexTro(_opt, snxtroFileSkl, "1 day", samplSnxTro);
175 }
176}
177
178
179
180// Destructor
181////////////////////////////////////////////////////////////////////////////
182t_pppRun::~t_pppRun() {
183 delete _logFile;
184 delete _nmeaFile;
185 delete _snxtroFile;
186}
187
188//
189////////////////////////////////////////////////////////////////////////////
190void t_pppRun::slotNewGPSEph(t_ephGPS eph) {
191 QMutexLocker locker(&_mutex);
192 _pppClient->putEphemeris(&eph);
193}
194
195//
196////////////////////////////////////////////////////////////////////////////
197void t_pppRun::slotNewGlonassEph(t_ephGlo eph) {
198 QMutexLocker locker(&_mutex);
199 _pppClient->putEphemeris(&eph);
200}
201
202//
203////////////////////////////////////////////////////////////////////////////
204void t_pppRun::slotNewGalileoEph(t_ephGal eph) {
205 QMutexLocker locker(&_mutex);
206 _pppClient->putEphemeris(&eph);
207}
208
209//
210////////////////////////////////////////////////////////////////////////////
211void t_pppRun::slotNewBDSEph(t_ephBDS eph) {
212 QMutexLocker locker(&_mutex);
213 _pppClient->putEphemeris(&eph);
214}
215
216//
217////////////////////////////////////////////////////////////////////////////
218void t_pppRun::slotNewObs(QByteArray staID, QList<t_satObs> obsList) {
219 QMutexLocker locker(&_mutex);
220
221 if (string(staID.data()) != _opt->_roverName) {
222 return;
223 }
224
225 // Loop over all observations (possible different epochs)
226 // -----------------------------------------------------
227 QListIterator<t_satObs> it(obsList);
228 while (it.hasNext()) {
229 const t_satObs& oldObs = it.next();
230 t_satObs* newObs = new t_satObs(oldObs);
231
232 // Find the corresponding data epoch or create a new one
233 // -----------------------------------------------------
234 t_epoData* epoch = 0;
235 deque<t_epoData*>::const_iterator it;
236 for (it = _epoData.begin(); it != _epoData.end(); it++) {
237 if (newObs->_time == (*it)->_time) {
238 epoch = *it;
239 break;
240 }
241 }
242 if (epoch == 0) {
243 if (_epoData.empty() || newObs->_time > _epoData.back()->_time) {
244 epoch = new t_epoData;
245 epoch->_time = newObs->_time;
246 _epoData.push_back(epoch);
247 }
248 }
249
250 // Put data into the epoch
251 // -----------------------
252 if (epoch != 0) {
253 epoch->_satObs.push_back(newObs);
254 }
255 else {
256 delete newObs;
257 }
258 }
259
260 // Process the oldest epochs
261 // ------------------------
262 while (_epoData.size() && !waitForCorr(_epoData.front()->_time)) {
263
264 const vector<t_satObs*>& satObs = _epoData.front()->_satObs;
265
266 t_output output;
267 _pppClient->processEpoch(satObs, &output);
268
269 if (!output._error) {
270 QVector<double> xx(6);
271 xx.data()[0] = output._xyzRover[0];
272 xx.data()[1] = output._xyzRover[1];
273 xx.data()[2] = output._xyzRover[2];
274 xx.data()[3] = output._neu[0];
275 xx.data()[4] = output._neu[1];
276 xx.data()[5] = output._neu[2];
277 emit newPosition(staID, output._epoTime, xx);
278 }
279
280 delete _epoData.front(); _epoData.pop_front();
281
282 ostringstream log;
283 if (output._error) {
284 log << output._log;
285 }
286 else {
287 log.setf(ios::fixed);
288 log << string(output._epoTime) << ' ' << staID.data()
289 << " X = " << setprecision(4) << output._xyzRover[0]
290 << " Y = " << setprecision(4) << output._xyzRover[1]
291 << " Z = " << setprecision(4) << output._xyzRover[2]
292 << " NEU: " << showpos << setw(8) << setprecision(4) << output._neu[0]
293 << " " << showpos << setw(8) << setprecision(4) << output._neu[1]
294 << " " << showpos << setw(8) << setprecision(4) << output._neu[2]
295 << " TRP: " << showpos << setw(8) << setprecision(4) << output._trp0
296 << " " << showpos << setw(8) << setprecision(4) << output._trp;
297 }
298
299 if (_logFile && output._epoTime.valid()) {
300 _logFile->write(output._epoTime.gpsw(), output._epoTime.gpssec(),
301 QString(output._log.c_str()));
302 }
303
304 if (!output._error) {
305 QString rmcStr = nmeaString('R', output);
306 QString ggaStr = nmeaString('G', output);
307 if (_nmeaFile) {
308 _nmeaFile->write(output._epoTime.gpsw(), output._epoTime.gpssec(), rmcStr);
309 _nmeaFile->write(output._epoTime.gpsw(), output._epoTime.gpssec(), ggaStr);
310 }
311 emit newNMEAstr(staID, rmcStr.toAscii());
312 emit newNMEAstr(staID, ggaStr.toAscii());
313 }
314
315 if (_snxtroFile && output._epoTime.valid()) {
316 _snxtroFile->write(staID, int(output._epoTime.gpsw()), output._epoTime.gpssec(),
317 output._trp0 + output._trp, output._trpStdev);
318 }
319
320 emit newMessage(QByteArray(log.str().c_str()), true);
321 }
322}
323
324//
325////////////////////////////////////////////////////////////////////////////
326void t_pppRun::slotNewTec(t_vTec vTec) {
327 if (vTec._layers.size() == 0) {
328 return;
329 }
330
331 if (_opt->_realTime) {
332 if (_opt->_corrMount.empty() || _opt->_corrMount != vTec._staID) {
333 return;
334 }
335 }
336
337 _pppClient->putTec(&vTec);
338}
339
340//
341////////////////////////////////////////////////////////////////////////////
342void t_pppRun::slotNewOrbCorrections(QList<t_orbCorr> orbCorr) {
343 if (orbCorr.size() == 0) {
344 return;
345 }
346
347 if (_opt->_realTime) {
348 if (_opt->_corrMount.empty() || _opt->_corrMount != orbCorr[0]._staID) {
349 return;
350 }
351 }
352 vector<t_orbCorr*> corrections;
353 for (int ii = 0; ii < orbCorr.size(); ii++) {
354 corrections.push_back(new t_orbCorr(orbCorr[ii]));
355 }
356
357 _pppClient->putOrbCorrections(corrections);
358
359 for (unsigned ii = 0; ii < corrections.size(); ii++) {
360 delete corrections[ii];
361 }
362}
363
364//
365////////////////////////////////////////////////////////////////////////////
366void t_pppRun::slotNewClkCorrections(QList<t_clkCorr> clkCorr) {
367 if (clkCorr.size() == 0) {
368 return;
369 }
370
371 if (_opt->_realTime) {
372 if (_opt->_corrMount.empty() || _opt->_corrMount != clkCorr[0]._staID) {
373 return;
374 }
375 }
376 vector<t_clkCorr*> corrections;
377 for (int ii = 0; ii < clkCorr.size(); ii++) {
378 corrections.push_back(new t_clkCorr(clkCorr[ii]));
379 _lastClkCorrTime = clkCorr[ii]._time;
380 }
381 _pppClient->putClkCorrections(corrections);
382
383 for (unsigned ii = 0; ii < corrections.size(); ii++) {
384 delete corrections[ii];
385 }
386}
387
388//
389////////////////////////////////////////////////////////////////////////////
390void t_pppRun::slotNewCodeBiases(QList<t_satCodeBias> codeBiases) {
391 if (codeBiases.size() == 0) {
392 return;
393 }
394
395 if (_opt->_realTime) {
396 if (_opt->_corrMount.empty() || _opt->_corrMount != codeBiases[0]._staID) {
397 return;
398 }
399 }
400 vector<t_satCodeBias*> biases;
401 for (int ii = 0; ii < codeBiases.size(); ii++) {
402 biases.push_back(new t_satCodeBias(codeBiases[ii]));
403 }
404
405 _pppClient->putCodeBiases(biases);
406
407 for (unsigned ii = 0; ii < biases.size(); ii++) {
408 delete biases[ii];
409 }
410}
411
412//
413////////////////////////////////////////////////////////////////////////////
414void t_pppRun::slotNewPhaseBiases(QList<t_satPhaseBias> phaseBiases) {
415 if (phaseBiases.size() == 0) {
416 return;
417 }
418
419 if (_opt->_realTime) {
420 if (_opt->_corrMount.empty() || _opt->_corrMount != phaseBiases[0]._staID) {
421 return;
422 }
423 }
424 vector<t_satPhaseBias*> biases;
425 for (int ii = 0; ii < phaseBiases.size(); ii++) {
426 biases.push_back(new t_satPhaseBias(phaseBiases[ii]));
427 }
428
429 _pppClient->putPhaseBiases(biases);
430
431 for (unsigned ii = 0; ii < biases.size(); ii++) {
432 delete biases[ii];
433 }
434}
435
436//
437////////////////////////////////////////////////////////////////////////////
438void t_pppRun::processFiles() {
439
440 try {
441 _rnxObsFile = new t_rnxObsFile(QString(_opt->_rinexObs.c_str()), t_rnxObsFile::input);
442 }
443 catch (...) {
444 delete _rnxObsFile; _rnxObsFile = 0;
445 emit finishedRnxPPP();
446 return;
447 }
448
449 _rnxNavFile = new t_rnxNavFile(QString(_opt->_rinexNav.c_str()), t_rnxNavFile::input);
450
451 if (!_opt->_corrFile.empty()) {
452 _corrFile = new t_corrFile(QString(_opt->_corrFile.c_str()));
453 connect(_corrFile, SIGNAL(newTec(t_vTec)),
454 this, SLOT(slotNewTec(t_vTec)));
455 connect(_corrFile, SIGNAL(newOrbCorrections(QList<t_orbCorr>)),
456 this, SLOT(slotNewOrbCorrections(QList<t_orbCorr>)));
457 connect(_corrFile, SIGNAL(newClkCorrections(QList<t_clkCorr>)),
458 this, SLOT(slotNewClkCorrections(QList<t_clkCorr>)));
459 connect(_corrFile, SIGNAL(newCodeBiases(QList<t_satCodeBias>)),
460 this, SLOT(slotNewCodeBiases(QList<t_satCodeBias>)));
461 connect(_corrFile, SIGNAL(newPhaseBiases(QList<t_satPhaseBias>)),
462 this, SLOT(slotNewPhaseBiases(QList<t_satPhaseBias>)));
463 }
464
465 // Read/Process Observations
466 // -------------------------
467 int nEpo = 0;
468 const t_rnxObsFile::t_rnxEpo* epo = 0;
469 while ( !_stopFlag && (epo = _rnxObsFile->nextEpoch()) != 0 ) {
470 ++nEpo;
471
472 if (_speed < 100) {
473 double sleepTime = 2.0 / _speed;
474 t_pppThread::msleep(int(sleepTime*1.e3));
475 }
476
477 // Get Corrections
478 // ---------------
479 if (_corrFile) {
480 try {
481 _corrFile->syncRead(epo->tt);
482 }
483 catch (const char* msg) {
484 emit newMessage(QByteArray(msg), true);
485 break;
486 }
487 catch (const string& msg) {
488 emit newMessage(QByteArray(msg.c_str()), true);
489 break;
490 }
491 catch (...) {
492 emit newMessage("unknown exceptions in corrFile", true);
493 break;
494 }
495 }
496
497 // Get Ephemerides
498 // ----------------
499 t_eph* eph = 0;
500 const QMap<QString, unsigned int>* corrIODs = _corrFile ? &_corrFile->corrIODs() : 0;
501 while ( (eph = _rnxNavFile->getNextEph(epo->tt, corrIODs)) != 0 ) {
502 _pppClient->putEphemeris(eph);
503 delete eph; eph = 0;
504 }
505
506 // Create list of observations and start epoch processing
507 // ------------------------------------------------------
508 QList<t_satObs> obsList;
509 for (unsigned iObs = 0; iObs < epo->rnxSat.size(); iObs++) {
510 const t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iObs];
511
512 t_satObs obs;
513 t_rnxObsFile::setObsFromRnx(_rnxObsFile, epo, rnxSat, obs);
514 obsList << obs;
515 }
516 slotNewObs(QByteArray(_opt->_roverName.c_str()), obsList);
517
518
519 if (nEpo % 10 == 0) {
520 emit progressRnxPPP(nEpo);
521 }
522
523 QCoreApplication::processEvents();
524 }
525
526 emit finishedRnxPPP();
527
528 if (BNC_CORE->mode() != t_bncCore::interactive) {
529 qApp->exit(0);
530 }
531 else {
532 BNC_CORE->stopPPP();
533 }
534}
535
536//
537////////////////////////////////////////////////////////////////////////////
538void t_pppRun::slotSetSpeed(int speed) {
539 QMutexLocker locker(&_mutex);
540 _speed = speed;
541}
542
543//
544////////////////////////////////////////////////////////////////////////////
545void t_pppRun::slotSetStopFlag() {
546 QMutexLocker locker(&_mutex);
547 _stopFlag = true;
548}
549
550//
551////////////////////////////////////////////////////////////////////////////
552QString t_pppRun::nmeaString(char strType, const t_output& output) {
553
554 double ell[3];
555 xyz2ell(output._xyzRover, ell);
556 double phiDeg = ell[0] * 180 / M_PI;
557 double lamDeg = ell[1] * 180 / M_PI;
558
559 char phiCh = 'N';
560 if (phiDeg < 0) {
561 phiDeg = -phiDeg;
562 phiCh = 'S';
563 }
564 char lamCh = 'E';
565 if (lamDeg < 0) {
566 lamDeg = -lamDeg;
567 lamCh = 'W';
568 }
569
570 ostringstream out;
571 out.setf(ios::fixed);
572
573 if (strType == 'R') {
574 string datestr = output._epoTime.datestr(0); // yyyymmdd
575 out << "GPRMC,"
576 << output._epoTime.timestr(0,0) << ",A,"
577 << setw(2) << setfill('0') << int(phiDeg)
578 << setw(6) << setprecision(3) << setfill('0')
579 << fmod(60*phiDeg,60) << ',' << phiCh << ','
580 << setw(3) << setfill('0') << int(lamDeg)
581 << setw(6) << setprecision(3) << setfill('0')
582 << fmod(60*lamDeg,60) << ',' << lamCh << ",,,"
583 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
584 << datestr[2] << datestr[3] << ",,";
585 }
586 else if (strType == 'G') {
587 out << "GPGGA,"
588 << output._epoTime.timestr(0,0) << ','
589 << setw(2) << setfill('0') << int(phiDeg)
590 << setw(10) << setprecision(7) << setfill('0')
591 << fmod(60*phiDeg,60) << ',' << phiCh << ','
592 << setw(3) << setfill('0') << int(lamDeg)
593 << setw(10) << setprecision(7) << setfill('0')
594 << fmod(60*lamDeg,60) << ',' << lamCh
595 << ",1," << setw(2) << setfill('0') << output._numSat << ','
596 << setw(3) << setprecision(1) << output._pDop << ','
597 << setprecision(3) << ell[2] << ",M,0.0,M,,";
598 }
599 else {
600 return "";
601 }
602
603 QString nmStr(out.str().c_str());
604 unsigned char XOR = 0;
605 for (int ii = 0; ii < nmStr.length(); ii++) {
606 XOR ^= (unsigned char) nmStr[ii].toAscii();
607 }
608
609 return '$' + nmStr + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
610}
611
612//
613////////////////////////////////////////////////////////////////////////////
614bool t_pppRun::waitForCorr(const bncTime& epoTime) const {
615
616 if (!_opt->_realTime || _opt->_corrMount.empty()) {
617 return false;
618 }
619 else if (!_lastClkCorrTime.valid()) {
620 return true;
621 }
622 else {
623 double dt = epoTime - _lastClkCorrTime;
624 if (dt > 1.0 && dt < _opt->_corrWaitTime) {
625 return true;
626 }
627 else {
628 return false;
629 }
630 }
631 return false;
632}
Note: See TracBrowser for help on using the repository browser.