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

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