source: ntrip/trunk/BNC/src/PPP/pppClient.cpp@ 5788

Last change on this file since 5788 was 5788, checked in by mervart, 10 years ago
File size: 15.5 KB
RevLine 
[5683]1
[5740]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_pppClient
31 *
32 * Purpose: PPP Client processing starts here
33 *
34 * Author: L. Mervart
35 *
36 * Created: 29-Jul-2014
37 *
38 * Changes:
39 *
40 * -----------------------------------------------------------------------*/
41
[5758]42#include <QThreadStorage>
[5740]43
[5683]44#include <iostream>
45#include <iomanip>
46#include <stdlib.h>
47#include <string.h>
48#include <stdexcept>
49
[5734]50#include "pppClient.h"
[5683]51#include "ephpool.h"
52#include "obspool.h"
53#include "satbias.h"
[5740]54#include "bncconst.h"
55#include "bncutils.h"
[5683]56#include "station.h"
[5740]57#include "bnctides.h"
58#include "bncantex.h"
[5683]59#include "filter.h"
60
61using namespace BNC;
62using namespace std;
63
[5758]64// Global variable holding thread-specific pointers
[5683]65//////////////////////////////////////////////////////////////////////////////
[5758]66QThreadStorage<t_pppClient*> CLIENTS;
[5683]67
[5758]68// Static function returning thread-specific pointer
69//////////////////////////////////////////////////////////////////////////////
70t_pppClient* t_pppClient::instance() {
71 return CLIENTS.localData();
72}
73
[5683]74// Constructor
75//////////////////////////////////////////////////////////////////////////////
[5761]76t_pppClient::t_pppClient(const t_options* opt) {
[5683]77 _output = 0;
[5761]78 _opt = new t_options(*opt);
[5683]79 _log = new ostringstream();
80 _ephPool = new t_ephPool();
81 _obsPool = new t_obsPool();
[5740]82 _staRover = new t_station();
[5683]83 _filter = new t_filter();
[5761]84
85 if (!_opt->_antexFile.empty()) {
86 _antex = new bncAntex(_opt->_antexFile.c_str());
87 }
88 else {
89 _antex = 0;
90 }
91
[5759]92 CLIENTS.setLocalData(this); // CLIENTS takes ownership over "this"
[5683]93}
94
95// Destructor
96//////////////////////////////////////////////////////////////////////////////
[5734]97t_pppClient::~t_pppClient() {
[5683]98 delete _log;
99 delete _opt;
100 delete _ephPool;
101 delete _obsPool;
102 delete _staRover;
103 delete _antex;
104 delete _filter;
105 clearObs();
106}
107
108//
109//////////////////////////////////////////////////////////////////////////////
[5772]110void t_pppClient::putEphemeris(const t_eph* eph) {
111 const t_ephGPS* ephGPS = dynamic_cast<const t_ephGPS*>(eph);
112 const t_ephGlo* ephGlo = dynamic_cast<const t_ephGlo*>(eph);
113 const t_ephGal* ephGal = dynamic_cast<const t_ephGal*>(eph);
114 if (ephGPS) {
115 _ephPool->putEphemeris(new t_ephGPS(*ephGPS));
116 }
117 else if (ephGlo) {
118 _ephPool->putEphemeris(new t_ephGlo(*ephGlo));
119 }
120 else if (ephGal) {
121 _ephPool->putEphemeris(new t_ephGal(*ephGal));
122 }
[5683]123}
124
125//
126//////////////////////////////////////////////////////////////////////////////
[5779]127void t_pppClient::putOrbCorrections(const vector<t_orbCorr*>& corr) {
[5772]128 for (unsigned ii = 0; ii < corr.size(); ii++) {
[5779]129 _ephPool->putOrbCorrection(new t_orbCorr(*corr[ii]));
[5683]130 }
131}
132
133//
134//////////////////////////////////////////////////////////////////////////////
[5779]135void t_pppClient::putClkCorrections(const vector<t_clkCorr*>& corr) {
[5772]136 for (unsigned ii = 0; ii < corr.size(); ii++) {
[5779]137 _ephPool->putClkCorrection(new t_clkCorr(*corr[ii]));
[5683]138 }
139}
140
141//
142//////////////////////////////////////////////////////////////////////////////
[5779]143void t_pppClient::putBiases(const vector<t_satBiases*>& biases) {
[5772]144 for (unsigned ii = 0; ii < biases.size(); ii++) {
[5779]145 _obsPool->putBiases(new t_satBias(*biases[ii]));
[5683]146 }
147}
148
149//
150//////////////////////////////////////////////////////////////////////////////
[5778]151t_irc t_pppClient::prepareObs(const vector<t_pppSatObs*>& pppSatObs,
[5772]152 vector<t_satObs*>& obsVector, bncTime& epoTime) {
[5683]153 // Default
154 // -------
155 epoTime.reset();
156
157 // Create vector of valid observations
158 // -----------------------------------
159 int numValidGPS = 0;
[5772]160 for (unsigned ii = 0; ii < pppSatObs.size(); ii++) {
[5778]161 char system = pppSatObs[ii]->_prn.system();
[5683]162 if (system == 'G' || (system == 'R' && OPT->useGlonass())) {
[5778]163 t_satObs* satObs = new t_satObs(*pppSatObs[ii]);
[5683]164 if (satObs->isValid()) {
165 obsVector.push_back(satObs);
166 if (satObs->prn().system() == 'G') {
167 ++numValidGPS;
168 }
169 }
170 else {
171 delete satObs;
172 }
173 }
174 }
175
176 // Check whether data are synchronized, compute epoTime
177 // ----------------------------------------------------
178 const double MAXSYNC = 0.05; // synchronization limit
179 double meanDt = 0.0;
180 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
181 const t_satObs* satObs = obsVector.at(ii);
182 if (epoTime.undef()) {
183 epoTime = satObs->time();
184 }
185 else {
186 double dt = satObs->time() - epoTime;
187 if (fabs(dt) > MAXSYNC) {
[5734]188 LOG << "t_pppClient::prepareObs asynchronous observations" << endl;
[5740]189 return failure;
[5683]190 }
191 meanDt += dt;
192 }
193 }
194
[5787]195 if (obsVector.size() > 0) {
196 epoTime += meanDt / obsVector.size();
197 }
198
[5740]199 return success;
[5683]200}
201
202// Compute the Bancroft position, check for blunders
203//////////////////////////////////////////////////////////////////////////////
[5740]204t_irc t_pppClient::cmpBancroft(const bncTime& epoTime,
[5683]205 vector<t_satObs*>& obsVector,
206 ColumnVector& xyzc, bool print) {
207
208 t_lc::type tLC = (OPT->dualFreqRequired() ? t_lc::cIF : t_lc::c1);
209
210 while (true) {
211 Matrix BB(obsVector.size(), 4);
212 int iObs = -1;
213 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
214 const t_satObs* satObs = obsVector.at(ii);
215 if ( satObs->isValid() && satObs->prn().system() == 'G' &&
[5740]216 (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle) ) {
[5683]217 ++iObs;
218 BB[iObs][0] = satObs->xc()[0];
219 BB[iObs][1] = satObs->xc()[1];
220 BB[iObs][2] = satObs->xc()[2];
221 BB[iObs][3] = satObs->obsValue(tLC) - satObs->cmpValueForBanc(tLC);
222 }
223 }
[5740]224 if (iObs + 1 < OPT->_minObs) {
[5734]225 LOG << "t_pppClient::cmpBancroft not enough observations" << endl;
[5740]226 return failure;
[5683]227 }
228 BB = BB.Rows(1,iObs+1);
229 bancroft(BB, xyzc);
230
[5740]231 xyzc[3] /= t_CST::c;
[5683]232
233 // Check Blunders
234 // --------------
235 const double BLUNDER = 100.0;
236 double maxRes = 0.0;
237 unsigned maxResIndex = 0;
238 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
239 const t_satObs* satObs = obsVector.at(ii);
240 if ( satObs->isValid() && satObs->prn().system() == 'G' &&
[5740]241 (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle) ) {
[5683]242 ColumnVector rr = satObs->xc().Rows(1,3) - xyzc.Rows(1,3);
243 double res = rr.norm_Frobenius() - satObs->obsValue(tLC)
[5740]244 - (satObs->xc()[3] - xyzc[3]) * t_CST::c;
[5683]245 if (fabs(res) > maxRes) {
246 maxRes = fabs(res);
247 maxResIndex = ii;
248 }
249 }
250 }
251 if (maxRes < BLUNDER) {
252 if (print) {
253 LOG.setf(ios::fixed);
254 LOG << string(epoTime) << " BANCROFT:" << ' '
255 << setw(14) << setprecision(3) << xyzc[0] << ' '
256 << setw(14) << setprecision(3) << xyzc[1] << ' '
257 << setw(14) << setprecision(3) << xyzc[2] << ' '
[5740]258 << setw(14) << setprecision(3) << xyzc[3] * t_CST::c << endl << endl;
[5683]259 }
260 break;
261 }
262 else {
263 t_satObs* satObs = obsVector.at(maxResIndex);
[5734]264 LOG << "t_pppClient::cmpBancroft outlier " << satObs->prn().toString()
[5683]265 << " " << maxRes << endl;
266 delete satObs;
267 obsVector.erase(obsVector.begin() + maxResIndex);
268 }
269 }
270
[5740]271 return success;
[5683]272}
273
274// Compute A Priori GPS-Glonass Offset
275//////////////////////////////////////////////////////////////////////////////
[5734]276double t_pppClient::cmpOffGG(vector<t_satObs*>& obsVector) {
[5683]277
278 t_lc::type tLC = (OPT->dualFreqRequired() ? t_lc::cIF : t_lc::c1);
279 double offGG = 0.0;
280
281 if (OPT->useGlonass()) {
282 while (true) {
283 offGG = 0.0;
284 bool outlierFound = false;
285 unsigned nObs = 0;
286 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
287 t_satObs* satObs = obsVector.at(ii);
288 if ( !satObs->outlier() && satObs->isValid() && satObs->prn().system() == 'R' &&
[5740]289 (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle) ) {
[5683]290 ++nObs;
291 double ll = satObs->obsValue(tLC) - satObs->cmpValue(tLC);
292 if (fabs(ll) > 1000.0) {
293 satObs->setOutlier();
294 outlierFound = true;
[5734]295 LOG << "t_pppClient::cmpOffGG outlier " << satObs->prn().toString()
[5683]296 << " " << ll << endl;
297 }
298 offGG += ll;
299 }
300 }
301 if (nObs > 0) {
302 offGG = offGG / nObs;
303 }
304 else {
305 offGG = 0.0;
306 }
307 if (!outlierFound) {
308 break;
309 }
310 }
311 }
312
313 return offGG;
314}
315
316//
317//////////////////////////////////////////////////////////////////////////////
[5744]318void t_pppClient::initOutput(t_output* output) {
[5683]319 _output = output;
[5740]320 _output->_numSat = 0;
321 _output->_pDop = 0.0;
322 _output->_error = false;
[5683]323}
324
325//
326//////////////////////////////////////////////////////////////////////////////
[5734]327void t_pppClient::clearObs() {
[5683]328 for (unsigned ii = 0; ii < _obsRover.size(); ii++) {
329 delete _obsRover.at(ii);
330 }
331 _obsRover.clear();
332}
333
334//
335//////////////////////////////////////////////////////////////////////////////
[5740]336void t_pppClient::finish(t_irc irc) {
[5683]337
338 clearObs();
339
[5740]340 _output->_epoTime = _epoTimeRover;
[5683]341
[5740]342 if (irc == success) {
[5683]343 _output->_xyzRover[0] = _staRover->xyzApr()[0] + _filter->x()[0];
344 _output->_xyzRover[1] = _staRover->xyzApr()[1] + _filter->x()[1];
345 _output->_xyzRover[2] = _staRover->xyzApr()[2] + _filter->x()[2];
346 copy(&_filter->Q().data()[0], &_filter->Q().data()[6], _output->_covMatrix);
347 _output->_numSat = _filter->numSat();
348 _output->_pDop = _filter->PDOP();
349 _output->_error = false;
350 }
351 else {
352 _output->_error = true;
353 }
[5740]354 _output->_log = _log->str();
[5683]355 delete _log; _log = new ostringstream();
356}
357
358//
359//////////////////////////////////////////////////////////////////////////////
[5740]360t_irc t_pppClient::cmpModel(t_station* station, const ColumnVector& xyzc,
[5683]361 vector<t_satObs*>& obsVector) {
362
[5740]363 bncTime time;
364 time = _epoTimeRover;
365 station->setName(OPT->_roverName);
366 station->setAntName(OPT->_antNameRover);
367 if (OPT->xyzAprRoverSet()) {
368 station->setXyzApr(OPT->_xyzAprRover);
[5683]369 }
370 else {
[5740]371 station->setXyzApr(xyzc.Rows(1,3));
[5683]372 }
[5740]373 station->setNeuEcc(OPT->_neuEccRover);
[5683]374
375 // Receiver Clock
376 // --------------
377 station->setDClk(xyzc[3]);
378
379 // Tides
380 // -----
[5740]381 ColumnVector hlp = station->xyzApr();
382 tides(time, hlp);
383 station->setTideDspl(hlp - station->xyzApr());
[5683]384
385 // Observation model
386 // -----------------
387 vector<t_satObs*>::iterator it = obsVector.begin();
388 while (it != obsVector.end()) {
389 t_satObs* satObs = *it;
390 satObs->cmpModel(station);
[5740]391 if (satObs->isValid() && satObs->eleSat() >= OPT->_minEle) {
[5683]392 ++it;
393 }
394 else {
395 delete satObs;
396 it = obsVector.erase(it);
397 }
398 }
399
[5740]400 return success;
[5683]401}
402
403//
404//////////////////////////////////////////////////////////////////////////////
[5778]405void t_pppClient::processEpoch(const vector<t_pppSatObs*>& pppSatObs, t_output* output) {
[5683]406
407 try {
408 initOutput(output);
409
410 // Prepare Observations of the Rover
411 // ---------------------------------
[5772]412 if (prepareObs(pppSatObs, _obsRover, _epoTimeRover) != success) {
[5740]413 return finish(failure);
[5683]414 }
415
416 LOG << "\nResults of Epoch ";
417 if (!_epoTimeRover.undef()) LOG << string(_epoTimeRover);
418 LOG << "\n--------------------------------------\n";
419
420 for (int iter = 1; iter <= 2; iter++) {
421 ColumnVector xyzc(4); xyzc = 0.0;
422 bool print = (iter == 2);
[5740]423 if (cmpBancroft(_epoTimeRover, _obsRover, xyzc, print) != success) {
424 return finish(failure);
[5683]425 }
[5740]426 if (cmpModel(_staRover, xyzc, _obsRover) != success) {
427 return finish(failure);
[5683]428 }
429 }
430
431 _offGG = cmpOffGG(_obsRover);
432
433 // Store last epoch of data
434 // ------------------------
435 _obsPool->putEpoch(_epoTimeRover, _obsRover);
436
437 // Process Epoch in Filter
438 // -----------------------
[5740]439 if (_filter->processEpoch(_obsPool) != success) {
440 return finish(failure);
[5683]441 }
442 }
443 catch (Exception& exc) {
444 LOG << exc.what() << endl;
[5740]445 return finish(failure);
[5683]446 }
[5763]447 catch (pppExcept msg) {
448 LOG << msg.what() << endl;
[5740]449 return finish(failure);
[5683]450 }
[5786]451 catch (const char* msg) {
452 LOG << msg << endl;
453 return finish(failure);
454 }
[5683]455 catch (...) {
456 LOG << "unknown exception" << endl;
[5740]457 return finish(failure);
[5683]458 }
459
[5740]460 return finish(success);
[5683]461}
[5740]462
463//
464////////////////////////////////////////////////////////////////////////////
465double lorentz(const ColumnVector& aa, const ColumnVector& bb) {
466 return aa[0]*bb[0] + aa[1]*bb[1] + aa[2]*bb[2] - aa[3]*bb[3];
467}
468
469//
470////////////////////////////////////////////////////////////////////////////
471void t_pppClient::bancroft(const Matrix& BBpass, ColumnVector& pos) {
472
473 if (pos.Nrows() != 4) {
474 pos.ReSize(4);
475 }
476 pos = 0.0;
477
478 for (int iter = 1; iter <= 2; iter++) {
479 Matrix BB = BBpass;
480 int mm = BB.Nrows();
481 for (int ii = 1; ii <= mm; ii++) {
482 double xx = BB(ii,1);
483 double yy = BB(ii,2);
484 double traveltime = 0.072;
485 if (iter > 1) {
486 double zz = BB(ii,3);
487 double rho = sqrt( (xx-pos(1)) * (xx-pos(1)) +
488 (yy-pos(2)) * (yy-pos(2)) +
489 (zz-pos(3)) * (zz-pos(3)) );
490 traveltime = rho / t_CST::c;
491 }
492 double angle = traveltime * t_CST::omega;
493 double cosa = cos(angle);
494 double sina = sin(angle);
495 BB(ii,1) = cosa * xx + sina * yy;
496 BB(ii,2) = -sina * xx + cosa * yy;
497 }
498
499 Matrix BBB;
500 if (mm > 4) {
501 SymmetricMatrix hlp; hlp << BB.t() * BB;
502 BBB = hlp.i() * BB.t();
503 }
504 else {
505 BBB = BB.i();
506 }
507 ColumnVector ee(mm); ee = 1.0;
508 ColumnVector alpha(mm); alpha = 0.0;
509 for (int ii = 1; ii <= mm; ii++) {
510 alpha(ii) = lorentz(BB.Row(ii).t(),BB.Row(ii).t())/2.0;
511 }
512 ColumnVector BBBe = BBB * ee;
513 ColumnVector BBBalpha = BBB * alpha;
514 double aa = lorentz(BBBe, BBBe);
515 double bb = lorentz(BBBe, BBBalpha)-1;
516 double cc = lorentz(BBBalpha, BBBalpha);
517 double root = sqrt(bb*bb-aa*cc);
518
519 Matrix hlpPos(4,2);
520 hlpPos.Column(1) = (-bb-root)/aa * BBBe + BBBalpha;
521 hlpPos.Column(2) = (-bb+root)/aa * BBBe + BBBalpha;
522
523 ColumnVector omc(2);
524 for (int pp = 1; pp <= 2; pp++) {
525 hlpPos(4,pp) = -hlpPos(4,pp);
526 omc(pp) = BB(1,4) -
527 sqrt( (BB(1,1)-hlpPos(1,pp)) * (BB(1,1)-hlpPos(1,pp)) +
528 (BB(1,2)-hlpPos(2,pp)) * (BB(1,2)-hlpPos(2,pp)) +
529 (BB(1,3)-hlpPos(3,pp)) * (BB(1,3)-hlpPos(3,pp)) ) -
530 hlpPos(4,pp);
531 }
532 if ( fabs(omc(1)) > fabs(omc(2)) ) {
533 pos = hlpPos.Column(2);
534 }
535 else {
536 pos = hlpPos.Column(1);
537 }
538 }
539}
540
Note: See TracBrowser for help on using the repository browser.