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

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