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

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