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

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