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

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