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

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