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

Last change on this file since 5990 was 5990, checked in by mervart, 10 years ago
File size: 17.3 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 for (unsigned iPrn = 0; iPrn <= t_prn::MAXPRN; iPrn++) {
75 _lastOrbCorrIOD[iPrn] = -1;
76 _lastClkCorrValue[iPrn] = 0.0;
77 }
78
79 _pppClient = new t_pppClient(_opt);
80
81 bncSettings settings;
82
83 if (_opt->_realTime) {
84 Qt::ConnectionType conType = Qt::AutoConnection;
85 if (BNC_CORE->mode() == t_bncCore::batchPostProcessing) {
86 conType = Qt::BlockingQueuedConnection;
87 }
88
89 connect(BNC_CORE->caster(), SIGNAL(newObs(QByteArray, QList<t_obs>)),
90 this, SLOT(slotNewObs(QByteArray, QList<t_obs>)),conType);
91
92 connect(BNC_CORE, SIGNAL(newEphGPS(gpsephemeris)),
93 this, SLOT(slotNewEphGPS(gpsephemeris)),conType);
94
95 connect(BNC_CORE, SIGNAL(newEphGlonass(glonassephemeris)),
96 this, SLOT(slotNewEphGlonass(glonassephemeris)),conType);
97
98 connect(BNC_CORE, SIGNAL(newEphGalileo(galileoephemeris)),
99 this, SLOT(slotNewEphGalileo(galileoephemeris)),conType);
100
101 connect(BNC_CORE, SIGNAL(newCorrections(QStringList)),
102 this, SLOT(slotNewCorrections(QStringList)),conType);
103 }
104 else {
105 _rnxObsFile = 0;
106 _rnxNavFile = 0;
107 _corrFile = 0;
108 _speed = settings.value("PPP/mapSpeedSlider").toInt();
109 connect(this, SIGNAL(progressRnxPPP(int)), BNC_CORE, SIGNAL(progressRnxPPP(int)));
110 connect(this, SIGNAL(finishedRnxPPP()), BNC_CORE, SIGNAL(finishedRnxPPP()));
111 connect(BNC_CORE, SIGNAL(mapSpeedSliderChanged(int)),
112 this, SLOT(slotSetSpeed(int)));
113 connect(BNC_CORE, SIGNAL(stopRinexPPP()), this, SLOT(slotSetStopFlag()));
114 }
115
116 _stopFlag = false;
117
118 QString roverName(_opt->_roverName.c_str());
119
120 QString logFileSkl = settings.value("PPP/logFile").toString();
121 if (logFileSkl.isEmpty()) {
122 _logFile = 0;
123 }
124 else {
125 if (logFileSkl.indexOf("${STATION}") == -1) {
126 logFileSkl = roverName + "_" + logFileSkl;
127 }
128 else {
129 logFileSkl.replace("${STATION}", roverName);
130 }
131 _logFile = new bncoutf(logFileSkl, "1 day", 0);
132 }
133
134 QString nmeaFileSkl = settings.value("PPP/nmeaFile").toString();
135 if (nmeaFileSkl.isEmpty()) {
136 _nmeaFile = 0;
137 }
138 else {
139 if (nmeaFileSkl.indexOf("${STATION}") == -1) {
140 nmeaFileSkl = roverName + "_" + nmeaFileSkl;
141 }
142 else {
143 nmeaFileSkl.replace("${STATION}", roverName);
144 }
145 _nmeaFile = new bncoutf(nmeaFileSkl, "1 day", 0);
146 }
147}
148
149// Destructor
150////////////////////////////////////////////////////////////////////////////
151t_pppRun::~t_pppRun() {
152 delete _logFile;
153 delete _nmeaFile;
154}
155
156//
157////////////////////////////////////////////////////////////////////////////
158void t_pppRun::slotNewEphGPS(gpsephemeris gpseph) {
159 QMutexLocker locker(&_mutex);
160 t_ephGPS eph;
161 eph.set(&gpseph);
162 _pppClient->putEphemeris(&eph);
163}
164
165//
166////////////////////////////////////////////////////////////////////////////
167void t_pppRun::slotNewEphGlonass(glonassephemeris gloeph) {
168 QMutexLocker locker(&_mutex);
169 t_ephGlo eph;
170 eph.set(&gloeph);
171 _pppClient->putEphemeris(&eph);
172}
173
174//
175////////////////////////////////////////////////////////////////////////////
176void t_pppRun::slotNewEphGalileo(galileoephemeris galeph) {
177 QMutexLocker locker(&_mutex);
178 t_ephGal eph;
179 eph.set(&galeph);
180 _pppClient->putEphemeris(&eph);
181}
182
183//
184////////////////////////////////////////////////////////////////////////////
185void t_pppRun::slotNewObs(QByteArray staID, QList<t_obs> obsList) {
186 QMutexLocker locker(&_mutex);
187
188 if (string(staID.data()) != _opt->_roverName) {
189 return;
190 }
191
192 // Loop over all obsevations (possible different epochs)
193 // -----------------------------------------------------
194 QListIterator<t_obs> it(obsList);
195 while (it.hasNext()) {
196 const t_obs& oldObs = it.next();
197 t_satObs* newObs = new t_satObs;
198
199 newObs->_prn.set(oldObs.satSys, oldObs.satNum);
200 newObs->_time.set(oldObs.GPSWeek, oldObs.GPSWeeks);
201
202 // Find the corresponding data epoch or create a new one
203 // -----------------------------------------------------
204 t_epoData* epoch = 0;
205 deque<t_epoData*>::const_iterator it;
206 for (it = _epoData.begin(); it != _epoData.end(); it++) {
207 if (newObs->_time == (*it)->_time) {
208 epoch = *it;
209 break;
210 }
211 }
212 if (epoch == 0) {
213 if (_epoData.empty() || newObs->_time > _epoData.back()->_time) {
214 epoch = new t_epoData;
215 epoch->_time = newObs->_time;
216 _epoData.push_back(epoch);
217 }
218 }
219
220 // Fill the new observation and add it to the corresponding epoch
221 // --------------------------------------------------------------
222 if (epoch != 0) {
223 epoch->_satObs.push_back(newObs);
224 map<string, t_frqObs*> frqObsMap;
225 for (unsigned iEntry = 0; iEntry < GNSSENTRY_NUMBER; iEntry++) {
226 string hlp = oldObs.rnxStr(iEntry).toAscii().data();
227 if (hlp.length() >= 2 && oldObs._measdata[iEntry] != 0.0) {
228 char obsType = hlp[0];
229 string rnxType2ch = hlp.substr(1);
230 if (obsType == 'C' || obsType == 'L') {
231 t_frqObs* frqObs = 0;
232 if (frqObsMap.find(rnxType2ch) == frqObsMap.end()) {
233 frqObs = new t_frqObs();
234 frqObsMap[rnxType2ch] = frqObs;
235 frqObs->_rnxType2ch = rnxType2ch;
236 newObs->_obs.push_back(frqObs);
237 }
238 else {
239 frqObs = frqObsMap[rnxType2ch];
240 }
241 if (obsType == 'C') {
242 frqObs->_code = oldObs._measdata[iEntry];
243 frqObs->_codeValid = true;
244 }
245 else if (obsType == 'L') {
246 frqObs->_phase = oldObs._measdata[iEntry];
247 frqObs->_phaseValid = true;
248 }
249 }
250 }
251 }
252 }
253 }
254
255 // Process the oldest epochs
256 // ------------------------
257 while (_epoData.size() &&
258 (!_opt->_realTime || _epoData.front()->_time < _lastClkCorrTime + 10.0)) {
259
260 const vector<t_satObs*>& satObs = _epoData.front()->_satObs;
261
262 t_output output;
263 _pppClient->processEpoch(satObs, &output);
264
265 if (!output._error) {
266 QVector<double> xx(6);
267 xx.data()[0] = output._xyzRover[0];
268 xx.data()[1] = output._xyzRover[1];
269 xx.data()[2] = output._xyzRover[2];
270 xx.data()[3] = output._neu[0];
271 xx.data()[4] = output._neu[1];
272 xx.data()[5] = output._neu[2];
273 emit newPosition(staID, output._epoTime, xx);
274 }
275
276 delete _epoData.front(); _epoData.pop_front();
277
278 ostringstream log;
279 if (output._error) {
280 log << output._log;
281 }
282 else {
283 log.setf(ios::fixed);
284 log << string(output._epoTime) << ' ' << staID.data()
285 << " X = " << setprecision(4) << output._xyzRover[0]
286 << " Y = " << setprecision(4) << output._xyzRover[1]
287 << " Z = " << setprecision(4) << output._xyzRover[2]
288 << " NEU: " << setprecision(4) << output._neu[0]
289 << " " << setprecision(4) << output._neu[1]
290 << " " << setprecision(4) << output._neu[2];
291 }
292
293 if (_logFile && output._epoTime.valid()) {
294 _logFile->write(output._epoTime.gpsw(), output._epoTime.gpssec(),
295 QString(output._log.c_str()));
296 }
297
298 if (!output._error) {
299 QString rmcStr = nmeaString('R', output);
300 QString ggaStr = nmeaString('G', output);
301 if (_nmeaFile) {
302 _nmeaFile->write(output._epoTime.gpsw(), output._epoTime.gpssec(), rmcStr);
303 _nmeaFile->write(output._epoTime.gpsw(), output._epoTime.gpssec(), ggaStr);
304 }
305 emit newNMEAstr(staID, rmcStr.toAscii());
306 emit newNMEAstr(staID, ggaStr.toAscii());
307 }
308
309 emit newMessage(QByteArray(log.str().c_str()), true);
310 }
311}
312
313//
314////////////////////////////////////////////////////////////////////////////
315void t_pppRun::slotNewCorrections(QStringList corrList) {
316 QMutexLocker locker(&_mutex);
317
318 // Check the Mountpoint (source of corrections)
319 // --------------------------------------------
320 if (_opt->_realTime) {
321 if (_opt->_corrMount.empty()) {
322 return;
323 }
324 QMutableListIterator<QString> itm(corrList);
325 while (itm.hasNext()) {
326 QStringList hlp = itm.next().split(" ");
327 if (hlp.size() > 0) {
328 QString mountpoint = hlp[hlp.size()-1];
329 if (mountpoint != QString(_opt->_corrMount.c_str())) {
330 itm.remove();
331 }
332 }
333 }
334 }
335
336 if (corrList.size() == 0) {
337 return;
338 }
339
340 vector<t_orbCorr*> orbCorr;
341 vector<t_clkCorr*> clkCorr;
342 vector<t_satBias*> satBias;
343
344 QListIterator<QString> it(corrList);
345 while (it.hasNext()) {
346 QString line = it.next();
347
348 QTextStream in(&line);
349 int messageType;
350 int updateInterval;
351 int GPSweek;
352 double GPSweeks;
353 QString prn;
354 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
355
356 if ( t_corr::relevantMessageType(messageType) ) {
357 t_corr corr;
358 corr.readLine(line);
359 if (messageType == COTYPE_GPSCOMBINED || messageType == COTYPE_GLONASSCOMBINED ||
360 messageType == COTYPE_GPSORBIT || messageType == COTYPE_GLONASSORBIT ) {
361 t_orbCorr* cc = new t_orbCorr();
362 cc->_prn.set(corr.prn.toAscii().data());
363 cc->_iod = corr.iod;
364 cc->_time = corr.tRao;
365 cc->_system = 'R';
366 cc->_xr[0] = corr.rao[0];
367 cc->_xr[1] = corr.rao[1];
368 cc->_xr[2] = corr.rao[2];
369 cc->_dotXr[0] = corr.dotRao[0];
370 cc->_dotXr[0] = corr.dotRao[1];
371 cc->_dotXr[0] = corr.dotRao[2];
372 orbCorr.push_back(cc);
373
374 _lastOrbCorrIOD[cc->_prn.toInt()] = cc->_iod;
375 }
376 else if (messageType == COTYPE_GPSCOMBINED || messageType == COTYPE_GLONASSCOMBINED ||
377 messageType == COTYPE_GPSCLOCK || messageType == COTYPE_GLONASSCLOCK ) {
378 t_clkCorr* cc = new t_clkCorr();
379 cc->_prn.set(corr.prn.toAscii().data());
380 cc->_iod = corr.iod;
381 cc->_time = corr.tClk;
382 cc->_dClk = corr.dClk;
383 cc->_dotDClk = corr.dotDClk;
384 cc->_dotDotDClk = corr.dotDotDClk;
385 cc->_clkPartial = 0.0;
386 if (messageType == COTYPE_GPSCLOCK || messageType == COTYPE_GLONASSCLOCK) {
387 int lastIOD = _lastOrbCorrIOD[cc->_prn.toInt()];
388 if (lastIOD != -1) {
389 cc->_iod = lastIOD;
390 }
391 else {
392 delete cc;
393 cc = 0;
394 }
395 }
396 if (cc) {
397 clkCorr.push_back(cc);
398 if (_lastClkCorrTime.undef() || cc->_time > _lastClkCorrTime) {
399 _lastClkCorrTime = cc->_time;
400 }
401 }
402 }
403 }
404 else if ( messageType == BTYPE_GPS || messageType == BTYPE_GLONASS ) {
405 t_bias bias;
406 bias.readLine(line);
407 }
408 }
409
410 _pppClient->putOrbCorrections(orbCorr);
411 _pppClient->putClkCorrections(clkCorr);
412 _pppClient->putBiases(satBias);
413
414 for (unsigned ii = 0; ii < orbCorr.size(); ii++) {
415 delete orbCorr[ii];
416 }
417 for (unsigned ii = 0; ii < clkCorr.size(); ii++) {
418 delete clkCorr[ii];
419 }
420 for (unsigned ii = 0; ii < satBias.size(); ii++) {
421 delete satBias[ii];
422 }
423}
424
425
426//
427////////////////////////////////////////////////////////////////////////////
428void t_pppRun::processFiles() {
429
430 try {
431 _rnxObsFile = new t_rnxObsFile(QString(_opt->_rinexObs.c_str()), t_rnxObsFile::input);
432 }
433 catch (...) {
434 delete _rnxObsFile; _rnxObsFile = 0;
435 emit finishedRnxPPP();
436 return;
437 }
438
439 _rnxNavFile = new t_rnxNavFile(QString(_opt->_rinexNav.c_str()), t_rnxNavFile::input);
440
441 if (!_opt->_corrFile.empty()) {
442 _corrFile = new t_corrFile(QString(_opt->_corrFile.c_str()));
443 connect(_corrFile, SIGNAL(newCorrections(QStringList)),
444 this, SLOT(slotNewCorrections(QStringList)));
445 }
446
447 // Read/Process Observations
448 // -------------------------
449 int nEpo = 0;
450 const t_rnxObsFile::t_rnxEpo* epo = 0;
451 while ( !_stopFlag && (epo = _rnxObsFile->nextEpoch()) != 0 ) {
452 ++nEpo;
453
454 if (_speed < 100) {
455 double sleepTime = 2.0 / _speed;
456 t_pppThread::msleep(sleepTime*1.e3);
457 }
458
459 // Get Corrections
460 // ---------------
461 if (_corrFile) {
462 _corrFile->syncRead(epo->tt);
463 }
464
465 // Get Ephemerides
466 // ----------------
467 t_eph* eph = 0;
468 const QMap<QString, int>* corrIODs = _corrFile ? &_corrFile->corrIODs() : 0;
469 while ( (eph = _rnxNavFile->getNextEph(epo->tt, corrIODs)) != 0 ) {
470 _pppClient->putEphemeris(eph);
471 delete eph; eph = 0;
472 }
473
474 // Create list of observations and start epoch processing
475 // ------------------------------------------------------
476 QList<t_obs> obsList;
477 for (unsigned iObs = 0; iObs < epo->rnxSat.size(); iObs++) {
478 const t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iObs];
479
480 t_obs obs;
481 t_rnxObsFile::setObsFromRnx(_rnxObsFile, epo, rnxSat, obs);
482 obsList << obs;
483 }
484 slotNewObs(QByteArray(_opt->_roverName.c_str()), obsList);
485
486
487 if (nEpo % 10 == 0) {
488 emit progressRnxPPP(nEpo);
489 }
490
491 QCoreApplication::processEvents();
492 }
493
494 emit finishedRnxPPP();
495
496 if (BNC_CORE->mode() != t_bncCore::interactive) {
497 qApp->exit(0);
498 }
499 else {
500 BNC_CORE->stopPPP();
501 }
502}
503
504//
505////////////////////////////////////////////////////////////////////////////
506void t_pppRun::slotSetSpeed(int speed) {
507 QMutexLocker locker(&_mutex);
508 _speed = speed;
509}
510
511//
512////////////////////////////////////////////////////////////////////////////
513void t_pppRun::slotSetStopFlag() {
514 QMutexLocker locker(&_mutex);
515 _stopFlag = true;
516}
517
518//
519////////////////////////////////////////////////////////////////////////////
520QString t_pppRun::nmeaString(char strType, const t_output& output) {
521
522 double ell[3];
523 xyz2ell(output._xyzRover, ell);
524 double phiDeg = ell[0] * 180 / M_PI;
525 double lamDeg = ell[1] * 180 / M_PI;
526
527 char phiCh = 'N';
528 if (phiDeg < 0) {
529 phiDeg = -phiDeg;
530 phiCh = 'S';
531 }
532 char lamCh = 'E';
533 if (lamDeg < 0) {
534 lamDeg = -lamDeg;
535 lamCh = 'W';
536 }
537
538 ostringstream out;
539 out.setf(ios::fixed);
540
541 if (strType == 'R') {
542 string datestr = output._epoTime.datestr(0); // yyyymmdd
543 out << "GPRMC,"
544 << output._epoTime.timestr(0,0) << ",A,"
545 << setw(2) << setfill('0') << int(phiDeg)
546 << setw(6) << setprecision(3) << setfill('0')
547 << fmod(60*phiDeg,60) << ',' << phiCh << ','
548 << setw(3) << setfill('0') << int(lamDeg)
549 << setw(6) << setprecision(3) << setfill('0')
550 << fmod(60*lamDeg,60) << ',' << lamCh << ",,,"
551 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
552 << datestr[2] << datestr[3] << ",,";
553 }
554 else if (strType == 'G') {
555 out << "GPGGA,"
556 << output._epoTime.timestr(0,0) << ','
557 << setw(2) << setfill('0') << int(phiDeg)
558 << setw(10) << setprecision(7) << setfill('0')
559 << fmod(60*phiDeg,60) << ',' << phiCh << ','
560 << setw(3) << setfill('0') << int(lamDeg)
561 << setw(10) << setprecision(7) << setfill('0')
562 << fmod(60*lamDeg,60) << ',' << lamCh
563 << ",1," << setw(2) << setfill('0') << output._numSat << ','
564 << setw(3) << setprecision(1) << output._pDop << ','
565 << setprecision(3) << ell[2] << ",M,0.0,M,,";
566 }
567 else {
568 return "";
569 }
570
571 QString nmStr(out.str().c_str());
572 unsigned char XOR = 0;
573 for (int ii = 0; ii < nmStr.length(); ii++) {
574 XOR ^= (unsigned char) nmStr[ii].toAscii();
575 }
576
577 return '$' + nmStr + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
578}
579
Note: See TracBrowser for help on using the repository browser.