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

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