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

Last change on this file since 6020 was 6020, checked in by mervart, 10 years ago
File size: 16.0 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 int numValidGPS = 0;
160 for (unsigned ii = 0; ii < satObs.size(); ii++) {
161 char system = satObs[ii]->_prn.system();
162 if (system == 'G' || (system == 'R' && OPT->useGlonass())) {
163 t_pppSatObs* pppSatObs = new t_pppSatObs(*satObs[ii]);
164 if (pppSatObs->isValid()) {
165 obsVector.push_back(pppSatObs);
166 if (pppSatObs->prn().system() == 'G') {
167 ++numValidGPS;
168 }
169 }
170 else {
171 delete pppSatObs;
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_pppSatObs* 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) {
188 LOG << "t_pppClient::prepareObs asynchronous observations" << endl;
189 return failure;
190 }
191 meanDt += dt;
192 }
193 }
194
195 if (obsVector.size() > 0) {
196 epoTime += meanDt / obsVector.size();
197 }
198
199 return success;
200}
201
202// Compute the Bancroft position, check for blunders
203//////////////////////////////////////////////////////////////////////////////
204t_irc t_pppClient::cmpBancroft(const bncTime& epoTime,
205 vector<t_pppSatObs*>& obsVector,
206 ColumnVector& xyzc, bool print) {
207
208 t_lc::type tLC = t_lc::dummy;
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_pppSatObs* satObs = obsVector.at(ii);
215 if (satObs->prn().system() == 'G') {
216 if (tLC == t_lc::dummy) {
217 tLC = satObs->isValid(t_lc::cIF) ? t_lc::cIF : t_lc::c1;
218 }
219 if ( satObs->isValid(tLC) && (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle) ) {
220 ++iObs;
221 BB[iObs][0] = satObs->xc()[0];
222 BB[iObs][1] = satObs->xc()[1];
223 BB[iObs][2] = satObs->xc()[2];
224 BB[iObs][3] = satObs->obsValue(tLC) - satObs->cmpValueForBanc(tLC);
225 }
226 }
227 }
228 if (iObs + 1 < OPT->_minObs) {
229 LOG << "t_pppClient::cmpBancroft not enough observations" << endl;
230 return failure;
231 }
232 BB = BB.Rows(1,iObs+1);
233 bancroft(BB, xyzc);
234
235 xyzc[3] /= t_CST::c;
236
237 // Check Blunders
238 // --------------
239 const double BLUNDER = 100.0;
240 double maxRes = 0.0;
241 unsigned maxResIndex = 0;
242 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
243 const t_pppSatObs* satObs = obsVector.at(ii);
244 if ( satObs->isValid() && satObs->prn().system() == 'G' &&
245 (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle) ) {
246 ColumnVector rr = satObs->xc().Rows(1,3) - xyzc.Rows(1,3);
247 double res = rr.norm_Frobenius() - satObs->obsValue(tLC)
248 - (satObs->xc()[3] - xyzc[3]) * t_CST::c;
249 if (fabs(res) > maxRes) {
250 maxRes = fabs(res);
251 maxResIndex = ii;
252 }
253 }
254 }
255 if (maxRes < BLUNDER) {
256 if (print) {
257 LOG.setf(ios::fixed);
258 LOG << string(epoTime) << " BANCROFT:" << ' '
259 << setw(14) << setprecision(3) << xyzc[0] << ' '
260 << setw(14) << setprecision(3) << xyzc[1] << ' '
261 << setw(14) << setprecision(3) << xyzc[2] << ' '
262 << setw(14) << setprecision(3) << xyzc[3] * t_CST::c << endl << endl;
263 }
264 break;
265 }
266 else {
267 t_pppSatObs* satObs = obsVector.at(maxResIndex);
268 LOG << "t_pppClient::cmpBancroft outlier " << satObs->prn().toString()
269 << " " << maxRes << endl;
270 delete satObs;
271 obsVector.erase(obsVector.begin() + maxResIndex);
272 }
273 }
274
275 return success;
276}
277
278// Compute A Priori GPS-Glonass Offset
279//////////////////////////////////////////////////////////////////////////////
280double t_pppClient::cmpOffGG(vector<t_pppSatObs*>& obsVector) {
281
282 t_lc::type tLC = t_lc::dummy;
283 double offGG = 0.0;
284
285 if (OPT->useGlonass()) {
286
287 while (obsVector.size() > 0) {
288 offGG = 0.0;
289 double maxRes = 0.0;
290 int maxResIndex = -1;
291 t_prn maxResPrn;
292 unsigned nObs = 0;
293 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
294 t_pppSatObs* satObs = obsVector.at(ii);
295 if (satObs->prn().system() == 'R') {
296 if (tLC == t_lc::dummy) {
297 tLC = satObs->isValid(t_lc::cIF) ? t_lc::cIF : t_lc::c1;
298 }
299 if (satObs->isValid(tLC) && (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle)) {
300 double ll = satObs->obsValue(tLC) - satObs->cmpValue(tLC);
301 ++nObs;
302 offGG += ll;
303 if (fabs(ll) > fabs(maxRes)) {
304 maxRes = ll;
305 maxResIndex = ii;
306 maxResPrn = satObs->prn();
307 }
308 }
309 }
310 }
311
312 if (nObs > 0) {
313 offGG = offGG / nObs;
314 }
315 else {
316 offGG = 0.0;
317 }
318
319 if (fabs(maxRes) > 1000.0) {
320 LOG << "t_pppClient::cmpOffGG outlier " << maxResPrn.toString() << " " << maxRes << endl;
321 obsVector.erase(obsVector.begin() + maxResIndex);
322 }
323 else {
324 break;
325 }
326 }
327 }
328
329 return offGG;
330}
331
332//
333//////////////////////////////////////////////////////////////////////////////
334void t_pppClient::initOutput(t_output* output) {
335 _output = output;
336 _output->_numSat = 0;
337 _output->_pDop = 0.0;
338 _output->_error = false;
339}
340
341//
342//////////////////////////////////////////////////////////////////////////////
343void t_pppClient::clearObs() {
344 for (unsigned ii = 0; ii < _obsRover.size(); ii++) {
345 delete _obsRover.at(ii);
346 }
347 _obsRover.clear();
348}
349
350//
351//////////////////////////////////////////////////////////////////////////////
352void t_pppClient::finish(t_irc irc) {
353
354 clearObs();
355
356 _output->_epoTime = _epoTimeRover;
357
358 if (irc == success) {
359 _output->_xyzRover[0] = _staRover->xyzApr()[0] + _filter->x()[0];
360 _output->_xyzRover[1] = _staRover->xyzApr()[1] + _filter->x()[1];
361 _output->_xyzRover[2] = _staRover->xyzApr()[2] + _filter->x()[2];
362
363 xyz2neu(_staRover->ellApr().data(), _filter->x().data(), _output->_neu);
364
365 copy(&_filter->Q().data()[0], &_filter->Q().data()[6], _output->_covMatrix);
366
367 _output->_numSat = _filter->numSat();
368 _output->_pDop = _filter->PDOP();
369 _output->_error = false;
370 }
371 else {
372 _output->_error = true;
373 }
374 _output->_log = _log->str();
375 delete _log; _log = new ostringstream();
376}
377
378//
379//////////////////////////////////////////////////////////////////////////////
380t_irc t_pppClient::cmpModel(t_pppStation* station, const ColumnVector& xyzc,
381 vector<t_pppSatObs*>& obsVector) {
382
383 bncTime time;
384 time = _epoTimeRover;
385 station->setName(OPT->_roverName);
386 station->setAntName(OPT->_antNameRover);
387 if (OPT->xyzAprRoverSet()) {
388 station->setXyzApr(OPT->_xyzAprRover);
389 }
390 else {
391 station->setXyzApr(xyzc.Rows(1,3));
392 }
393 station->setNeuEcc(OPT->_neuEccRover);
394
395 // Receiver Clock
396 // --------------
397 station->setDClk(xyzc[3]);
398
399 // Tides
400 // -----
401 station->setTideDspl( _tides->displacement(time, station->xyzApr()) );
402
403 // Observation model
404 // -----------------
405 vector<t_pppSatObs*>::iterator it = obsVector.begin();
406 while (it != obsVector.end()) {
407 t_pppSatObs* satObs = *it;
408 if (satObs->isValid()) {
409 satObs->cmpModel(station);
410 }
411 if (satObs->isValid() && satObs->eleSat() >= OPT->_minEle) {
412 ++it;
413 }
414 else {
415 delete satObs;
416 it = obsVector.erase(it);
417 }
418 }
419
420 return success;
421}
422
423//
424//////////////////////////////////////////////////////////////////////////////
425void t_pppClient::processEpoch(const vector<t_satObs*>& satObs, t_output* output) {
426
427 try {
428 initOutput(output);
429
430 // Prepare Observations of the Rover
431 // ---------------------------------
432 if (prepareObs(satObs, _obsRover, _epoTimeRover) != success) {
433 return finish(failure);
434 }
435
436 LOG << "\nResults of Epoch ";
437 if (!_epoTimeRover.undef()) LOG << string(_epoTimeRover);
438 LOG << "\n--------------------------------------\n";
439
440 for (int iter = 1; iter <= 2; iter++) {
441 ColumnVector xyzc(4); xyzc = 0.0;
442 bool print = (iter == 2);
443 if (cmpBancroft(_epoTimeRover, _obsRover, xyzc, print) != success) {
444 return finish(failure);
445 }
446 if (cmpModel(_staRover, xyzc, _obsRover) != success) {
447 return finish(failure);
448 }
449 }
450
451 _offGG = cmpOffGG(_obsRover);
452
453 // Store last epoch of data
454 // ------------------------
455 _obsPool->putEpoch(_epoTimeRover, _obsRover);
456
457 // Process Epoch in Filter
458 // -----------------------
459 if (_filter->processEpoch(_obsPool) != success) {
460 return finish(failure);
461 }
462 }
463 catch (Exception& exc) {
464 LOG << exc.what() << endl;
465 return finish(failure);
466 }
467 catch (t_except msg) {
468 LOG << msg.what() << endl;
469 return finish(failure);
470 }
471 catch (const char* msg) {
472 LOG << msg << endl;
473 return finish(failure);
474 }
475 catch (...) {
476 LOG << "unknown exception" << endl;
477 return finish(failure);
478 }
479
480 return finish(success);
481}
482
483//
484////////////////////////////////////////////////////////////////////////////
485double lorentz(const ColumnVector& aa, const ColumnVector& bb) {
486 return aa[0]*bb[0] + aa[1]*bb[1] + aa[2]*bb[2] - aa[3]*bb[3];
487}
488
489//
490////////////////////////////////////////////////////////////////////////////
491void t_pppClient::bancroft(const Matrix& BBpass, ColumnVector& pos) {
492
493 if (pos.Nrows() != 4) {
494 pos.ReSize(4);
495 }
496 pos = 0.0;
497
498 for (int iter = 1; iter <= 2; iter++) {
499 Matrix BB = BBpass;
500 int mm = BB.Nrows();
501 for (int ii = 1; ii <= mm; ii++) {
502 double xx = BB(ii,1);
503 double yy = BB(ii,2);
504 double traveltime = 0.072;
505 if (iter > 1) {
506 double zz = BB(ii,3);
507 double rho = sqrt( (xx-pos(1)) * (xx-pos(1)) +
508 (yy-pos(2)) * (yy-pos(2)) +
509 (zz-pos(3)) * (zz-pos(3)) );
510 traveltime = rho / t_CST::c;
511 }
512 double angle = traveltime * t_CST::omega;
513 double cosa = cos(angle);
514 double sina = sin(angle);
515 BB(ii,1) = cosa * xx + sina * yy;
516 BB(ii,2) = -sina * xx + cosa * yy;
517 }
518
519 Matrix BBB;
520 if (mm > 4) {
521 SymmetricMatrix hlp; hlp << BB.t() * BB;
522 BBB = hlp.i() * BB.t();
523 }
524 else {
525 BBB = BB.i();
526 }
527 ColumnVector ee(mm); ee = 1.0;
528 ColumnVector alpha(mm); alpha = 0.0;
529 for (int ii = 1; ii <= mm; ii++) {
530 alpha(ii) = lorentz(BB.Row(ii).t(),BB.Row(ii).t())/2.0;
531 }
532 ColumnVector BBBe = BBB * ee;
533 ColumnVector BBBalpha = BBB * alpha;
534 double aa = lorentz(BBBe, BBBe);
535 double bb = lorentz(BBBe, BBBalpha)-1;
536 double cc = lorentz(BBBalpha, BBBalpha);
537 double root = sqrt(bb*bb-aa*cc);
538
539 Matrix hlpPos(4,2);
540 hlpPos.Column(1) = (-bb-root)/aa * BBBe + BBBalpha;
541 hlpPos.Column(2) = (-bb+root)/aa * BBBe + BBBalpha;
542
543 ColumnVector omc(2);
544 for (int pp = 1; pp <= 2; pp++) {
545 hlpPos(4,pp) = -hlpPos(4,pp);
546 omc(pp) = BB(1,4) -
547 sqrt( (BB(1,1)-hlpPos(1,pp)) * (BB(1,1)-hlpPos(1,pp)) +
548 (BB(1,2)-hlpPos(2,pp)) * (BB(1,2)-hlpPos(2,pp)) +
549 (BB(1,3)-hlpPos(3,pp)) * (BB(1,3)-hlpPos(3,pp)) ) -
550 hlpPos(4,pp);
551 }
552 if ( fabs(omc(1)) > fabs(omc(2)) ) {
553 pos = hlpPos.Column(2);
554 }
555 else {
556 pos = hlpPos.Column(1);
557 }
558 }
559}
560
Note: See TracBrowser for help on using the repository browser.