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

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