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

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