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

Last change on this file since 7271 was 7271, checked in by stuerze, 9 years ago

minor changes to consider BDS ephemeris messages in PPP mode

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