source: ntrip/trunk/BNC/bncmodel.cpp@ 3307

Last change on this file since 3307 was 3307, checked in by mervart, 13 years ago
File size: 38.9 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: bncParam, bncModel
30 *
31 * Purpose: Model for PPP
32 *
33 * Author: L. Mervart
34 *
35 * Created: 01-Dec-2009
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iomanip>
42#include <cmath>
43#include <newmatio.h>
44#include <sstream>
45
46#include "bncmodel.h"
47#include "bncapp.h"
48#include "bncpppclient.h"
49#include "bancroft.h"
50#include "bncutils.h"
51#include "bncsettings.h"
52#include "bnctides.h"
53#include "bncantex.h"
54
55using namespace std;
56
57const unsigned MINOBS = 4;
58const double MINELE_GPS = 10.0 * M_PI / 180.0;
59const double MINELE_GLO = 10.0 * M_PI / 180.0;
60const double MINELE_GAL = 10.0 * M_PI / 180.0;
61const double MAXRES_CODE_GPS = 10.0;
62const double MAXRES_PHASE_GPS = 0.10;
63const double MAXRES_PHASE_GLO = 0.05;
64const double MAXRES_CODE_GAL = 10.0;
65const double MAXRES_PHASE_GAL = 0.10;
66
67// Constructor
68////////////////////////////////////////////////////////////////////////////
69bncParam::bncParam(bncParam::parType typeIn, int indexIn,
70 const QString& prnIn) {
71 type = typeIn;
72 index = indexIn;
73 prn = prnIn;
74 index_old = 0;
75 xx = 0.0;
76
77}
78
79// Destructor
80////////////////////////////////////////////////////////////////////////////
81bncParam::~bncParam() {
82}
83
84// Partial
85////////////////////////////////////////////////////////////////////////////
86double bncParam::partial(t_satData* satData, bool phase) {
87
88 // Coordinates
89 // -----------
90 if (type == CRD_X) {
91 return (xx - satData->xx(1)) / satData->rho;
92 }
93 else if (type == CRD_Y) {
94 return (xx - satData->xx(2)) / satData->rho;
95 }
96 else if (type == CRD_Z) {
97 return (xx - satData->xx(3)) / satData->rho;
98 }
99
100 // Receiver Clocks
101 // ---------------
102 else if (type == RECCLK) {
103 return 1.0;
104 }
105
106 // Troposphere
107 // -----------
108 else if (type == TROPO) {
109 return 1.0 / sin(satData->eleSat);
110 }
111
112 // Galileo Offset
113 // --------------
114 else if (type == GALILEO_OFFSET) {
115 if (satData->prn[0] == 'E') {
116 return 1.0;
117 }
118 else {
119 return 0.0;
120 }
121 }
122
123 // Ambiguities
124 // -----------
125 else if (type == AMB_L3) {
126 if (phase && satData->prn == prn) {
127 return 1.0;
128 }
129 else {
130 return 0.0;
131 }
132 }
133
134 // Default return
135 // --------------
136 return 0.0;
137}
138
139// Constructor
140////////////////////////////////////////////////////////////////////////////
141bncModel::bncModel(QByteArray staID) {
142
143 _staID = staID;
144
145 connect(this, SIGNAL(newMessage(QByteArray,bool)),
146 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
147
148 bncSettings settings;
149
150 // Observation Sigmas
151 // ------------------
152 _sigP3 = 5.0;
153 if (!settings.value("pppSigmaCode").toString().isEmpty()) {
154 _sigP3 = settings.value("pppSigmaCode").toDouble();
155 }
156 _sigL3 = 0.02;
157 if (!settings.value("pppSigmaPhase").toString().isEmpty()) {
158 _sigL3 = settings.value("pppSigmaPhase").toDouble();
159 }
160
161 // Parameter Sigmas
162 // ----------------
163 _sigCrd0 = 100.0;
164 if (!settings.value("pppSigCrd0").toString().isEmpty()) {
165 _sigCrd0 = settings.value("pppSigCrd0").toDouble();
166 }
167 _sigCrdP = 100.0;
168 if (!settings.value("pppSigCrdP").toString().isEmpty()) {
169 _sigCrdP = settings.value("pppSigCrdP").toDouble();
170 }
171 _sigTrp0 = 0.1;
172 if (!settings.value("pppSigTrp0").toString().isEmpty()) {
173 _sigTrp0 = settings.value("pppSigTrp0").toDouble();
174 }
175 _sigTrpP = 1e-6;
176 if (!settings.value("pppSigTrpP").toString().isEmpty()) {
177 _sigTrpP = settings.value("pppSigTrpP").toDouble();
178 }
179 _sigClk0 = 1000.0;
180 _sigAmb0 = 1000.0;
181 _sigGalileoOffset0 = 1000.0;
182 _sigGalileoOffsetP = 0.0;
183
184 // Quick-Start Mode
185 // ----------------
186 _quickStart = 0;
187 if (settings.value("pppRefCrdX").toString() != "" &&
188 settings.value("pppRefCrdY").toString() != "" &&
189 settings.value("pppRefCrdZ").toString() != "" &&
190 !settings.value("pppQuickStart").toString().isEmpty()) {
191 _quickStart = settings.value("pppQuickStart").toDouble();
192 }
193
194 // Several options
195 // ---------------
196 _usePhase = false;
197 if ( Qt::CheckState(settings.value("pppUsePhase").toInt()) == Qt::Checked) {
198 _usePhase = true;
199 }
200
201 _estTropo = false;
202 if ( Qt::CheckState(settings.value("pppEstTropo").toInt()) == Qt::Checked) {
203 _estTropo = true;
204 }
205
206 _useGalileo = false;
207 if ( Qt::CheckState(settings.value("pppGalileo").toInt()) == Qt::Checked) {
208 _useGalileo = true;
209 }
210
211 // NMEA Output
212 // -----------
213 QString nmeaFileName = settings.value("nmeaFile").toString();
214 if (nmeaFileName.isEmpty()) {
215 _nmeaFile = 0;
216 _nmeaStream = 0;
217 }
218 else {
219 expandEnvVar(nmeaFileName);
220 _nmeaFile = new QFile(nmeaFileName);
221 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
222 _nmeaFile->open(QIODevice::WriteOnly | QIODevice::Append);
223 }
224 else {
225 _nmeaFile->open(QIODevice::WriteOnly);
226 }
227 _nmeaStream = new QTextStream();
228 _nmeaStream->setDevice(_nmeaFile);
229 }
230
231 // Antenna Name, ANTEX File
232 // ------------------------
233 _antex = 0;
234 QString antexFileName = settings.value("pppAntex").toString();
235 if (!antexFileName.isEmpty()) {
236 _antex = new bncAntex();
237 if (_antex->readFile(antexFileName) != success) {
238 emit newMessage("wrong ANTEX file", true);
239 delete _antex;
240 _antex = 0;
241 }
242 else {
243 _antennaName = settings.value("pppAntenna").toString();
244 }
245 }
246
247 // Antenna Eccentricities
248 // ----------------------
249 _dN = settings.value("pppRefdN").toDouble();
250 _dE = settings.value("pppRefdE").toDouble();
251 _dU = settings.value("pppRefdU").toDouble();
252
253 // Bancroft Coordinates
254 // --------------------
255 _xcBanc.ReSize(4); _xcBanc = 0.0;
256 _ellBanc.ReSize(3); _ellBanc = 0.0;
257}
258
259// Destructor
260////////////////////////////////////////////////////////////////////////////
261bncModel::~bncModel() {
262 delete _nmeaStream;
263 delete _nmeaFile;
264 for (int ii = 0; ii < _posAverage.size(); ++ii) {
265 delete _posAverage[ii];
266 }
267 delete _antex;
268 for (int iPar = 1; iPar <= _params.size(); iPar++) {
269 delete _params[iPar-1];
270 }
271}
272
273// Reset Parameters and Variance-Covariance Matrix
274////////////////////////////////////////////////////////////////////////////
275void bncModel::reset() {
276
277 for (int iPar = 1; iPar <= _params.size(); iPar++) {
278 delete _params[iPar-1];
279 }
280 _params.clear();
281
282 int nextPar = 0;
283 _params.push_back(new bncParam(bncParam::CRD_X, ++nextPar, ""));
284 _params.push_back(new bncParam(bncParam::CRD_Y, ++nextPar, ""));
285 _params.push_back(new bncParam(bncParam::CRD_Z, ++nextPar, ""));
286 _params.push_back(new bncParam(bncParam::RECCLK, ++nextPar, ""));
287 if (_estTropo) {
288 _params.push_back(new bncParam(bncParam::TROPO, ++nextPar, ""));
289 }
290 if (_useGalileo) {
291 _params.push_back(new bncParam(bncParam::GALILEO_OFFSET, ++nextPar, ""));
292 }
293
294 _QQ.ReSize(_params.size());
295 _QQ = 0.0;
296 for (int iPar = 1; iPar <= _params.size(); iPar++) {
297 bncParam* pp = _params[iPar-1];
298 pp->xx = 0.0;
299 if (pp->isCrd()) {
300 _QQ(iPar,iPar) = _sigCrd0 * _sigCrd0;
301 }
302 else if (pp->type == bncParam::RECCLK) {
303 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
304 }
305 else if (pp->type == bncParam::TROPO) {
306 _QQ(iPar,iPar) = _sigTrp0 * _sigTrp0;
307 }
308 else if (pp->type == bncParam::GALILEO_OFFSET) {
309 _QQ(iPar,iPar) = _sigGalileoOffset0 * _sigGalileoOffset0;
310 }
311 }
312}
313
314// Bancroft Solution
315////////////////////////////////////////////////////////////////////////////
316t_irc bncModel::cmpBancroft(t_epoData* epoData) {
317
318 if (epoData->sizeGPS() < MINOBS) {
319 _log += "bncModel::cmpBancroft: not enough data\n";
320 return failure;
321 }
322
323 Matrix BB(epoData->sizeGPS(), 4);
324
325 QMapIterator<QString, t_satData*> it(epoData->satDataGPS);
326 int iObsBanc = 0;
327 while (it.hasNext()) {
328 ++iObsBanc;
329 it.next();
330 QString prn = it.key();
331 t_satData* satData = it.value();
332 BB(iObsBanc, 1) = satData->xx(1);
333 BB(iObsBanc, 2) = satData->xx(2);
334 BB(iObsBanc, 3) = satData->xx(3);
335 BB(iObsBanc, 4) = satData->P3 + satData->clk;
336 }
337
338 bancroft(BB, _xcBanc);
339
340 // Ellipsoidal Coordinates
341 // ------------------------
342 xyz2ell(_xcBanc.data(), _ellBanc.data());
343
344 // Compute Satellite Elevations
345 // ----------------------------
346 QMutableMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
347 while (iGPS.hasNext()) {
348 iGPS.next();
349 t_satData* satData = iGPS.value();
350 cmpEle(satData);
351 if (satData->eleSat < MINELE_GPS) {
352 delete satData;
353 iGPS.remove();
354 }
355 }
356
357 QMutableMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
358 while (iGlo.hasNext()) {
359 iGlo.next();
360 t_satData* satData = iGlo.value();
361 cmpEle(satData);
362 if (satData->eleSat < MINELE_GLO) {
363 delete satData;
364 iGlo.remove();
365 }
366 }
367
368 QMutableMapIterator<QString, t_satData*> iGal(epoData->satDataGal);
369 while (iGal.hasNext()) {
370 iGal.next();
371 t_satData* satData = iGal.value();
372 cmpEle(satData);
373 if (satData->eleSat < MINELE_GAL) {
374 delete satData;
375 iGal.remove();
376 }
377 }
378
379 return success;
380}
381
382// Computed Value
383////////////////////////////////////////////////////////////////////////////
384double bncModel::cmpValue(t_satData* satData, bool phase) {
385
386 ColumnVector xRec(3);
387 xRec(1) = x();
388 xRec(2) = y();
389 xRec(3) = z();
390
391 double rho0 = (satData->xx - xRec).norm_Frobenius();
392 double dPhi = t_CST::omega * rho0 / t_CST::c;
393
394 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
395 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
396 xRec(3) = z();
397
398 tides(_time, xRec);
399
400 satData->rho = (satData->xx - xRec).norm_Frobenius();
401
402 double tropDelay = delay_saast(satData->eleSat) +
403 trp() / sin(satData->eleSat);
404
405 double wind = 0.0;
406 if (phase) {
407 wind = windUp(satData->prn, satData->xx, xRec) * satData->lambda3;
408 }
409
410 double offset = 0.0;
411 if (satData->prn[0] == 'E') {
412 offset = Galileo_offset();
413 }
414
415 double phaseCenter = 0.0;
416 if (_antex) {
417 bool found;
418 phaseCenter = _antex->pco(_antennaName, satData->eleSat, found);
419 if (!found) {
420 emit newMessage("ANTEX: antenna >"
421 + _antennaName.toAscii() + "< not found", true);
422 }
423 }
424
425 double antennaOffset = 0.0;
426 if (_dN != 0.0 || _dE != 0.0 || _dU != 0.0) {
427 double cosa = cos(satData->azSat);
428 double sina = sin(satData->azSat);
429 double cose = cos(satData->eleSat);
430 double sine = sin(satData->eleSat);
431 antennaOffset = -_dN * cosa*cose - _dE * sina*cose - _dU * sine;
432 }
433
434 return satData->rho + phaseCenter + antennaOffset + clk()
435 + offset - satData->clk + tropDelay + wind;
436}
437
438// Tropospheric Model (Saastamoinen)
439////////////////////////////////////////////////////////////////////////////
440double bncModel::delay_saast(double Ele) {
441
442 double xyz[3];
443 xyz[0] = x();
444 xyz[1] = y();
445 xyz[2] = z();
446 double ell[3];
447 xyz2ell(xyz, ell);
448 double height = ell[2];
449
450 double pp = 1013.25 * pow(1.0 - 2.26e-5 * height, 5.225);
451 double TT = 18.0 - height * 0.0065 + 273.15;
452 double hh = 50.0 * exp(-6.396e-4 * height);
453 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
454
455 double h_km = height / 1000.0;
456
457 if (h_km < 0.0) h_km = 0.0;
458 if (h_km > 5.0) h_km = 5.0;
459 int ii = int(h_km + 1);
460 double href = ii - 1;
461
462 double bCor[6];
463 bCor[0] = 1.156;
464 bCor[1] = 1.006;
465 bCor[2] = 0.874;
466 bCor[3] = 0.757;
467 bCor[4] = 0.654;
468 bCor[5] = 0.563;
469
470 double BB = bCor[ii-1] + (bCor[ii]-bCor[ii-1]) * (h_km - href);
471
472 double zen = M_PI/2.0 - Ele;
473
474 return (0.002277/cos(zen)) * (pp + ((1255.0/TT)+0.05)*ee - BB*(tan(zen)*tan(zen)));
475}
476
477// Prediction Step of the Filter
478////////////////////////////////////////////////////////////////////////////
479void bncModel::predict(t_epoData* epoData) {
480
481 bncSettings settings;
482
483 _time = epoData->tt; // current epoch time
484
485 _maxSolGap = settings.value("pppMaxSolGap").toDouble();
486
487 bool firstCrd = false;
488 if (!_lastTimeOK.valid() || (_maxSolGap > 0 && _time - _lastTimeOK > _maxSolGap)) {
489 firstCrd = true;
490 _startTime = epoData->tt;
491 reset();
492 }
493
494 // Use different white noise for Quick-Start mode
495 // ----------------------------------------------
496 double sigCrdP_used = _sigCrdP;
497 if ( _quickStart > 0.0 && _quickStart > (epoData->tt - _startTime) ) {
498 sigCrdP_used = 0.0;
499 }
500
501 // Predict Parameter values, add white noise
502 // -----------------------------------------
503 for (int iPar = 1; iPar <= _params.size(); iPar++) {
504 bncParam* pp = _params[iPar-1];
505
506 // Coordinates
507 // -----------
508 if (pp->type == bncParam::CRD_X) {
509 if (firstCrd) {
510 if (settings.value("pppRefCrdX").toString() != "" &&
511 settings.value("pppRefCrdY").toString() != "" &&
512 settings.value("pppRefCrdZ").toString() != "") {
513 pp->xx = settings.value("pppRefCrdX").toDouble();
514 }
515 else {
516 pp->xx = _xcBanc(1);
517 }
518 }
519 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
520 }
521 else if (pp->type == bncParam::CRD_Y) {
522 if (firstCrd) {
523 if (settings.value("pppRefCrdX").toString() != "" &&
524 settings.value("pppRefCrdY").toString() != "" &&
525 settings.value("pppRefCrdZ").toString() != "") {
526 pp->xx = settings.value("pppRefCrdY").toDouble();
527 }
528 else {
529 pp->xx = _xcBanc(2);
530 }
531 }
532 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
533 }
534 else if (pp->type == bncParam::CRD_Z) {
535 if (firstCrd) {
536 if (settings.value("pppRefCrdX").toString() != "" &&
537 settings.value("pppRefCrdY").toString() != "" &&
538 settings.value("pppRefCrdZ").toString() != "") {
539 pp->xx = settings.value("pppRefCrdZ").toDouble();
540 }
541 else {
542 pp->xx = _xcBanc(3);
543 }
544 }
545 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
546 }
547
548 // Receiver Clocks
549 // ---------------
550 else if (pp->type == bncParam::RECCLK) {
551 pp->xx = _xcBanc(4);
552 for (int jj = 1; jj <= _params.size(); jj++) {
553 _QQ(iPar, jj) = 0.0;
554 }
555 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
556 }
557
558 // Tropospheric Delay
559 // ------------------
560 else if (pp->type == bncParam::TROPO) {
561 _QQ(iPar,iPar) += _sigTrpP * _sigTrpP;
562 }
563
564 // Galileo Offset
565 // --------------
566 else if (pp->type == bncParam::GALILEO_OFFSET) {
567 _QQ(iPar,iPar) += _sigGalileoOffsetP * _sigGalileoOffsetP;
568 }
569 }
570
571 // Add New Ambiguities if necessary
572 // --------------------------------
573 if (_usePhase) {
574
575 // Make a copy of QQ and xx, set parameter indices
576 // -----------------------------------------------
577 SymmetricMatrix QQ_old = _QQ;
578
579 for (int iPar = 1; iPar <= _params.size(); iPar++) {
580 _params[iPar-1]->index_old = _params[iPar-1]->index;
581 _params[iPar-1]->index = 0;
582 }
583
584 // Remove Ambiguity Parameters without observations
585 // ------------------------------------------------
586 int iPar = 0;
587 QMutableVectorIterator<bncParam*> it(_params);
588 while (it.hasNext()) {
589 bncParam* par = it.next();
590 bool removed = false;
591 if (par->type == bncParam::AMB_L3) {
592 if (epoData->satDataGPS.find(par->prn) == epoData->satDataGPS.end() &&
593 epoData->satDataGlo.find(par->prn) == epoData->satDataGlo.end() &&
594 epoData->satDataGal.find(par->prn) == epoData->satDataGal.end() ) {
595 removed = true;
596 delete par;
597 it.remove();
598 }
599 }
600 if (! removed) {
601 ++iPar;
602 par->index = iPar;
603 }
604 }
605
606 // Add new ambiguity parameters
607 // ----------------------------
608 QMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
609 while (iGPS.hasNext()) {
610 iGPS.next();
611 t_satData* satData = iGPS.value();
612 addAmb(satData);
613 }
614
615 QMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
616 while (iGlo.hasNext()) {
617 iGlo.next();
618 t_satData* satData = iGlo.value();
619 addAmb(satData);
620 }
621
622 QMapIterator<QString, t_satData*> iGal(epoData->satDataGal);
623 while (iGal.hasNext()) {
624 iGal.next();
625 t_satData* satData = iGal.value();
626 addAmb(satData);
627 }
628
629 int nPar = _params.size();
630 _QQ.ReSize(nPar); _QQ = 0.0;
631 for (int i1 = 1; i1 <= nPar; i1++) {
632 bncParam* p1 = _params[i1-1];
633 if (p1->index_old != 0) {
634 _QQ(p1->index, p1->index) = QQ_old(p1->index_old, p1->index_old);
635 for (int i2 = 1; i2 <= nPar; i2++) {
636 bncParam* p2 = _params[i2-1];
637 if (p2->index_old != 0) {
638 _QQ(p1->index, p2->index) = QQ_old(p1->index_old, p2->index_old);
639 }
640 }
641 }
642 }
643
644 for (int ii = 1; ii <= nPar; ii++) {
645 bncParam* par = _params[ii-1];
646 if (par->index_old == 0) {
647 _QQ(par->index, par->index) = _sigAmb0 * _sigAmb0;
648 }
649 par->index_old = par->index;
650 }
651 }
652}
653
654// Update Step of the Filter (currently just a single-epoch solution)
655////////////////////////////////////////////////////////////////////////////
656t_irc bncModel::update(t_epoData* epoData) {
657
658 bncSettings settings;
659
660 _log.clear();
661
662 if (settings.value("pppSPP").toString() == "PPP") {
663 _log += "Precise Point Positioning of Epoch "
664 + QByteArray(_time.timestr(1).c_str()) +
665 "\n---------------------------------------------------------------\n";
666 }
667 else {
668 _log += "Single Point Positioning of Epoch "
669 + QByteArray(_time.timestr(1).c_str()) +
670 "\n--------------------------------------------------------------\n";
671 }
672
673 // Outlier Detection Loop
674 // ----------------------
675 ColumnVector dx;
676 if (update_p(epoData, dx) != success) {
677 return failure;
678 }
679
680 // Remember the Epoch-specific Results for the computation of means
681 // ----------------------------------------------------------------
682 pppPos* newPos = new pppPos;
683 newPos->time = epoData->tt;
684
685 // Set Solution Vector
686 // -------------------
687 ostringstream strB;
688 strB.setf(ios::fixed);
689 QVectorIterator<bncParam*> itPar(_params);
690 while (itPar.hasNext()) {
691 bncParam* par = itPar.next();
692 par->xx += dx(par->index);
693
694 if (par->type == bncParam::RECCLK) {
695 strB << "\n clk = " << setw(10) << setprecision(3) << par->xx
696 << " +- " << setw(6) << setprecision(3)
697 << sqrt(_QQ(par->index,par->index));
698 }
699 else if (par->type == bncParam::AMB_L3) {
700 strB << "\n amb " << par->prn.toAscii().data() << " = "
701 << setw(10) << setprecision(3) << par->xx
702 << " +- " << setw(6) << setprecision(3)
703 << sqrt(_QQ(par->index,par->index));
704 }
705 else if (par->type == bncParam::TROPO) {
706 double aprTrp = delay_saast(M_PI/2.0);
707 strB << "\n trp = " << par->prn.toAscii().data()
708 << setw(7) << setprecision(3) << aprTrp << " "
709 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
710 << " +- " << setw(6) << setprecision(3)
711 << sqrt(_QQ(par->index,par->index));
712 newPos->xnt[6] = aprTrp + par->xx;
713 }
714 else if (par->type == bncParam::GALILEO_OFFSET) {
715 strB << "\n offset = " << setw(10) << setprecision(3) << par->xx
716 << " +- " << setw(6) << setprecision(3)
717 << sqrt(_QQ(par->index,par->index));
718 }
719 }
720 strB << '\n';
721 _log += strB.str().c_str();
722 emit newMessage(_log, false);
723
724 // Final Message (both log file and screen)
725 // ----------------------------------------
726 ostringstream strC;
727 strC.setf(ios::fixed);
728 strC << _staID.data() << " PPP "
729 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
730 << setw(14) << setprecision(3) << x() << " +- "
731 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
732 << setw(14) << setprecision(3) << y() << " +- "
733 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
734 << setw(14) << setprecision(3) << z() << " +- "
735 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
736
737 // NEU Output
738 // ----------
739 double xyzRef[3];
740
741 if (settings.value("pppRefCrdX").toString() != "" &&
742 settings.value("pppRefCrdY").toString() != "" &&
743 settings.value("pppRefCrdZ").toString() != "") {
744
745 xyzRef[0] = settings.value("pppRefCrdX").toDouble();
746 xyzRef[1] = settings.value("pppRefCrdY").toDouble();
747 xyzRef[2] = settings.value("pppRefCrdZ").toDouble();
748
749 newPos->xnt[0] = x() - xyzRef[0];
750 newPos->xnt[1] = y() - xyzRef[1];
751 newPos->xnt[2] = z() - xyzRef[2];
752
753 double ellRef[3];
754 xyz2ell(xyzRef, ellRef);
755 xyz2neu(ellRef, newPos->xnt, &newPos->xnt[3]);
756
757 strC << " NEU "
758 << setw(8) << setprecision(3) << newPos->xnt[3] << " "
759 << setw(8) << setprecision(3) << newPos->xnt[4] << " "
760 << setw(8) << setprecision(3) << newPos->xnt[5] << endl;
761
762 }
763
764 emit newMessage(QByteArray(strC.str().c_str()), true);
765
766 if (settings.value("pppAverage").toString() == "") {
767 delete newPos;
768 }
769 else {
770
771 _posAverage.push_back(newPos);
772
773 // Time Span for Average Computation
774 // ---------------------------------
775 double tRangeAverage = settings.value("pppAverage").toDouble() * 60.;
776 if (tRangeAverage < 0) {
777 tRangeAverage = 0;
778 }
779 if (tRangeAverage > 86400) {
780 tRangeAverage = 86400;
781 }
782
783 // Compute the Mean
784 // ----------------
785 ColumnVector mean(7); mean = 0.0;
786
787 QMutableVectorIterator<pppPos*> it(_posAverage);
788 while (it.hasNext()) {
789 pppPos* pp = it.next();
790 if ( (epoData->tt - pp->time) >= tRangeAverage ) {
791 delete pp;
792 it.remove();
793 }
794 else {
795 for (int ii = 0; ii < 7; ++ii) {
796 mean[ii] += pp->xnt[ii];
797 }
798 }
799 }
800
801 int nn = _posAverage.size();
802
803 if (nn > 0) {
804
805 mean /= nn;
806
807 // Compute the Deviation
808 // ---------------------
809 ColumnVector std(7); std = 0.0;
810 QVectorIterator<pppPos*> it2(_posAverage);
811 while (it2.hasNext()) {
812 pppPos* pp = it2.next();
813 for (int ii = 0; ii < 7; ++ii) {
814 std[ii] += (pp->xnt[ii] - mean[ii]) * (pp->xnt[ii] - mean[ii]);
815 }
816 }
817 for (int ii = 0; ii < 7; ++ii) {
818 std[ii] = sqrt(std[ii] / nn);
819 }
820
821 if (settings.value("pppRefCrdX").toString() != "" &&
822 settings.value("pppRefCrdY").toString() != "" &&
823 settings.value("pppRefCrdZ").toString() != "") {
824
825 ostringstream strD; strD.setf(ios::fixed);
826 strD << _staID.data() << " AVE-XYZ "
827 << epoData->tt.timestr(1) << " "
828 << setw(13) << setprecision(3) << mean[0] + xyzRef[0] << " +- "
829 << setw(6) << setprecision(3) << std[0] << " "
830 << setw(14) << setprecision(3) << mean[1] + xyzRef[1] << " +- "
831 << setw(6) << setprecision(3) << std[1] << " "
832 << setw(14) << setprecision(3) << mean[2] + xyzRef[2] << " +- "
833 << setw(6) << setprecision(3) << std[2];
834 emit newMessage(QByteArray(strD.str().c_str()), true);
835
836 ostringstream strE; strE.setf(ios::fixed);
837 strE << _staID.data() << " AVE-NEU "
838 << epoData->tt.timestr(1) << " "
839 << setw(13) << setprecision(3) << mean[3] << " +- "
840 << setw(6) << setprecision(3) << std[3] << " "
841 << setw(14) << setprecision(3) << mean[4] << " +- "
842 << setw(6) << setprecision(3) << std[4] << " "
843 << setw(14) << setprecision(3) << mean[5] << " +- "
844 << setw(6) << setprecision(3) << std[5];
845 emit newMessage(QByteArray(strE.str().c_str()), true);
846
847 if ( Qt::CheckState(settings.value("pppEstTropo").toInt()) == Qt::Checked) {
848 ostringstream strF; strF.setf(ios::fixed);
849 strF << _staID.data() << " AVE-TRP "
850 << epoData->tt.timestr(1) << " "
851 << setw(13) << setprecision(3) << mean[6] << " +- "
852 << setw(6) << setprecision(3) << std[6] << endl;
853 emit newMessage(QByteArray(strF.str().c_str()), true);
854 }
855 }
856 }
857 }
858
859 // NMEA Output
860 // -----------
861 double xyz[3];
862 xyz[0] = x();
863 xyz[1] = y();
864 xyz[2] = z();
865 double ell[3];
866 xyz2ell(xyz, ell);
867 double phiDeg = ell[0] * 180 / M_PI;
868 double lamDeg = ell[1] * 180 / M_PI;
869
870 char phiCh = 'N';
871 if (phiDeg < 0) {
872 phiDeg = -phiDeg;
873 phiCh = 'S';
874 }
875 char lamCh = 'E';
876 if (lamDeg < 0) {
877 lamDeg = -lamDeg;
878 lamCh = 'W';
879 }
880
881 string datestr = epoData->tt.datestr(0); // yyyymmdd
882 ostringstream strRMC;
883 strRMC.setf(ios::fixed);
884 strRMC << "GPRMC,"
885 << epoData->tt.timestr(0,0) << ",A,"
886 << setw(2) << setfill('0') << int(phiDeg)
887 << setw(6) << setprecision(3) << setfill('0')
888 << fmod(60*phiDeg,60) << ',' << phiCh << ','
889 << setw(3) << setfill('0') << int(lamDeg)
890 << setw(6) << setprecision(3) << setfill('0')
891 << fmod(60*lamDeg,60) << ',' << lamCh << ",,,"
892 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
893 << datestr[2] << datestr[3] << ",,";
894
895 writeNMEAstr(QString(strRMC.str().c_str()));
896
897 double dop = 2.0; // TODO
898
899 ostringstream strGGA;
900 strGGA.setf(ios::fixed);
901 strGGA << "GPGGA,"
902 << epoData->tt.timestr(0,0) << ','
903 << setw(2) << setfill('0') << int(phiDeg)
904 << setw(10) << setprecision(7) << setfill('0')
905 << fmod(60*phiDeg,60) << ',' << phiCh << ','
906 << setw(3) << setfill('0') << int(lamDeg)
907 << setw(10) << setprecision(7) << setfill('0')
908 << fmod(60*lamDeg,60) << ',' << lamCh
909 << ",1," << setw(2) << setfill('0') << epoData->sizeAll() << ','
910 << setw(3) << setprecision(1) << dop << ','
911 << setprecision(3) << ell[2] << ",M,0.0,M,,";
912
913 writeNMEAstr(QString(strGGA.str().c_str()));
914
915 _lastTimeOK = _time; // remember time of last successful update
916 return success;
917}
918
919// Outlier Detection
920////////////////////////////////////////////////////////////////////////////
921int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
922 const ColumnVector& vv,
923 QMap<QString, t_satData*>& satDataGPS,
924 QMap<QString, t_satData*>& satDataGlo,
925 QMap<QString, t_satData*>& satDataGal) {
926
927 QString prnCode;
928 QString prnPhase;
929 double maxResCode = 0.0;
930 double maxResPhase = 0.0;
931
932 QString prnRemoved;
933 double maxRes;
934
935 int irc = 0;
936
937 // Check GPS Code
938 // --------------
939 if (irc == 0) {
940 findMaxRes(vv,satDataGPS, prnCode, maxResCode, prnPhase, maxResPhase);
941 if (maxResCode > MAXRES_CODE_GPS) {
942 satDataGPS.remove(prnCode);
943 prnRemoved = prnCode;
944 maxRes = maxResCode;
945 irc = 1;
946 }
947 }
948
949 // Check Galileo Code
950 // ------------------
951 if (irc == 0) {
952 findMaxRes(vv,satDataGal, prnCode, maxResCode, prnPhase, maxResPhase);
953 if (maxResCode > MAXRES_CODE_GAL) {
954 satDataGal.remove(prnCode);
955 prnRemoved = prnCode;
956 maxRes = maxResCode;
957 irc = 1;
958 }
959 }
960
961 // Check Glonass Phase
962 // -------------------
963 if (irc == 0) {
964 findMaxRes(vv,satDataGlo, prnCode, maxResCode, prnPhase, maxResPhase);
965 if (maxResPhase > MAXRES_PHASE_GLO) {
966 satDataGlo.remove(prnPhase);
967 prnRemoved = prnPhase;
968 maxRes = maxResPhase;
969 irc = 1;
970 }
971 }
972
973 // Check Galileo Phase
974 // -------------------
975 if (irc == 0) {
976 findMaxRes(vv,satDataGal, prnCode, maxResCode, prnPhase, maxResPhase);
977 if (maxResPhase > MAXRES_PHASE_GAL) {
978 satDataGal.remove(prnPhase);
979 prnRemoved = prnPhase;
980 maxRes = maxResPhase;
981 irc = 1;
982 }
983 }
984
985 // Check GPS Phase
986 // ---------------
987 if (irc == 0) {
988 findMaxRes(vv,satDataGPS, prnCode, maxResCode, prnPhase, maxResPhase);
989 if (maxResPhase > MAXRES_PHASE_GPS) {
990 satDataGPS.remove(prnPhase);
991 prnRemoved = prnPhase;
992 maxRes = maxResPhase;
993 irc = 1;
994 }
995 }
996
997 if (irc != 0) {
998 _log += "Outlier " + prnRemoved.toAscii() + " "
999 + QByteArray::number(maxRes, 'f', 3) + "\n";
1000 _QQ = QQsav;
1001 }
1002
1003 return irc;
1004}
1005
1006//
1007////////////////////////////////////////////////////////////////////////////
1008void bncModel::writeNMEAstr(const QString& nmStr) {
1009
1010 unsigned char XOR = 0;
1011 for (int ii = 0; ii < nmStr.length(); ii++) {
1012 XOR ^= (unsigned char) nmStr[ii].toAscii();
1013 }
1014
1015 QString outStr = '$' + nmStr
1016 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
1017
1018 if (_nmeaStream) {
1019 *_nmeaStream << outStr;
1020 _nmeaStream->flush();
1021 }
1022
1023 emit newNMEAstr(outStr.toAscii());
1024}
1025
1026////
1027//////////////////////////////////////////////////////////////////////////////
1028void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
1029 const DiagonalMatrix& PP,
1030 SymmetricMatrix& QQ, ColumnVector& dx) {
1031
1032 int nObs = AA.Nrows();
1033 int nPar = AA.Ncols();
1034
1035 UpperTriangularMatrix SS = Cholesky(QQ).t();
1036
1037 Matrix SA = SS*AA.t();
1038 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
1039 for (int ii = 1; ii <= nObs; ++ii) {
1040 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
1041 }
1042
1043 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
1044 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
1045
1046 UpperTriangularMatrix UU;
1047 QRZ(SRF, UU);
1048
1049 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
1050 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
1051 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
1052
1053 UpperTriangularMatrix SHi = SH_rt.i();
1054
1055 Matrix KT = SHi * YY;
1056 SymmetricMatrix Hi; Hi << SHi * SHi.t();
1057
1058 dx = KT.t() * ll;
1059 QQ << (SS.t() * SS);
1060}
1061
1062// Phase Wind-Up Correction
1063///////////////////////////////////////////////////////////////////////////
1064double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
1065 const ColumnVector& rRec) {
1066
1067 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
1068
1069 // First time - initialize to zero
1070 // -------------------------------
1071 if (!_windUpTime.contains(prn)) {
1072 _windUpSum[prn] = 0.0;
1073 }
1074
1075 // Compute the correction for new time
1076 // -----------------------------------
1077 if (!_windUpTime.contains(prn) || _windUpTime[prn] != Mjd) {
1078 _windUpTime[prn] = Mjd;
1079
1080 // Unit Vector GPS Satellite --> Receiver
1081 // --------------------------------------
1082 ColumnVector rho = rRec - rSat;
1083 rho /= rho.norm_Frobenius();
1084
1085 // GPS Satellite unit Vectors sz, sy, sx
1086 // -------------------------------------
1087 ColumnVector sz = -rSat / rSat.norm_Frobenius();
1088
1089 ColumnVector xSun = Sun(Mjd);
1090 xSun /= xSun.norm_Frobenius();
1091
1092 ColumnVector sy = crossproduct(sz, xSun);
1093 ColumnVector sx = crossproduct(sy, sz);
1094
1095 // Effective Dipole of the GPS Satellite Antenna
1096 // ---------------------------------------------
1097 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
1098 - crossproduct(rho, sy);
1099
1100 // Receiver unit Vectors rx, ry
1101 // ----------------------------
1102 ColumnVector rx(3);
1103 ColumnVector ry(3);
1104
1105 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
1106 double neu[3];
1107
1108 neu[0] = 1.0;
1109 neu[1] = 0.0;
1110 neu[2] = 0.0;
1111 neu2xyz(recEll, neu, rx.data());
1112
1113 neu[0] = 0.0;
1114 neu[1] = -1.0;
1115 neu[2] = 0.0;
1116 neu2xyz(recEll, neu, ry.data());
1117
1118 // Effective Dipole of the Receiver Antenna
1119 // ----------------------------------------
1120 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
1121 + crossproduct(rho, ry);
1122
1123 // Resulting Effect
1124 // ----------------
1125 double alpha = DotProduct(dipSat,dipRec) /
1126 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
1127
1128 if (alpha > 1.0) alpha = 1.0;
1129 if (alpha < -1.0) alpha = -1.0;
1130
1131 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
1132
1133 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
1134 dphi = -dphi;
1135 }
1136
1137 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
1138 }
1139
1140 return _windUpSum[prn];
1141}
1142
1143//
1144///////////////////////////////////////////////////////////////////////////
1145void bncModel::cmpEle(t_satData* satData) {
1146 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
1147 double rho = rr.norm_Frobenius();
1148
1149 double neu[3];
1150 xyz2neu(_ellBanc.data(), rr.data(), neu);
1151
1152 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
1153 if (neu[2] < 0) {
1154 satData->eleSat *= -1.0;
1155 }
1156 satData->azSat = atan2(neu[1], neu[0]);
1157}
1158
1159//
1160///////////////////////////////////////////////////////////////////////////
1161void bncModel::addAmb(t_satData* satData) {
1162 bool found = false;
1163 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1164 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1165 _params[iPar-1]->prn == satData->prn) {
1166 found = true;
1167 break;
1168 }
1169 }
1170 if (!found) {
1171 bncParam* par = new bncParam(bncParam::AMB_L3,
1172 _params.size()+1, satData->prn);
1173 _params.push_back(par);
1174 par->xx = satData->L3 - cmpValue(satData, true);
1175 }
1176}
1177
1178//
1179///////////////////////////////////////////////////////////////////////////
1180void bncModel::addObs(int phase, unsigned& iObs, t_satData* satData,
1181 Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP) {
1182
1183 // Phase Observations
1184 // ------------------
1185 if (phase == 1) {
1186 ++iObs;
1187 ll(iObs) = satData->L3 - cmpValue(satData, true);
1188 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
1189 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1190 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1191 _params[iPar-1]->prn == satData->prn) {
1192 ll(iObs) -= _params[iPar-1]->xx;
1193 }
1194 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
1195 }
1196 satData->indexPhase = iObs;
1197 }
1198
1199 // Code Observations
1200 // -----------------
1201 else {
1202 ++iObs;
1203 ll(iObs) = satData->P3 - cmpValue(satData, false);
1204 PP(iObs,iObs) = 1.0 / (_sigP3 * _sigP3);
1205 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1206 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
1207 }
1208 satData->indexCode = iObs;
1209 }
1210}
1211
1212//
1213///////////////////////////////////////////////////////////////////////////
1214void bncModel::printRes(const ColumnVector& vv,
1215 ostringstream& str, t_satData* satData) {
1216 if (satData->indexPhase) {
1217 str << _time.timestr(1)
1218 << " RES " << satData->prn.toAscii().data() << " L3 "
1219 << setw(9) << setprecision(4) << vv(satData->indexPhase) << endl;
1220 }
1221 if (satData->indexCode) {
1222 str << _time.timestr(1)
1223 << " RES " << satData->prn.toAscii().data() << " P3 "
1224 << setw(9) << setprecision(4) << vv(satData->indexCode) << endl;
1225 }
1226}
1227
1228//
1229///////////////////////////////////////////////////////////////////////////
1230void bncModel::findMaxRes(const ColumnVector& vv,
1231 const QMap<QString, t_satData*>& satData,
1232 QString& prnCode, double& maxResCode,
1233 QString& prnPhase, double& maxResPhase) {
1234 maxResCode = 0.0;
1235 maxResPhase = 0.0;
1236
1237 QMapIterator<QString, t_satData*> it(satData);
1238 while (it.hasNext()) {
1239 it.next();
1240 t_satData* satData = it.value();
1241 if (satData->indexCode) {
1242 if (fabs(vv(satData->indexCode)) > maxResCode) {
1243 maxResCode = fabs(vv(satData->indexCode));
1244 prnCode = satData->prn;
1245 }
1246 }
1247 if (satData->indexPhase) {
1248 if (fabs(vv(satData->indexPhase)) > maxResPhase) {
1249 maxResPhase = fabs(vv(satData->indexPhase));
1250 prnPhase = satData->prn;
1251 }
1252 }
1253 }
1254}
1255
1256// Update Step (private - loop over outliers)
1257////////////////////////////////////////////////////////////////////////////
1258t_irc bncModel::update_p(t_epoData* epoData, ColumnVector& dx) {
1259
1260 SymmetricMatrix QQsav;
1261 ColumnVector vv;
1262
1263 for (int iPhase = 0; iPhase <= 1; iPhase++) {
1264
1265 do {
1266
1267 if (iPhase == 0) {
1268
1269 // Bancroft Solution
1270 // -----------------
1271 if (cmpBancroft(epoData) != success) {
1272 emit newMessage(_log, false);
1273 return failure;
1274 }
1275
1276 // Status Prediction
1277 // -----------------
1278 predict(epoData);
1279 }
1280
1281 // Create First-Design Matrix
1282 // --------------------------
1283 unsigned nPar = _params.size();
1284 unsigned nObs = 0;
1285 nObs = epoData->sizeGPS() + epoData->sizeGal(); // Glonass code not used
1286
1287 Matrix AA(nObs, nPar); // first design matrix
1288 ColumnVector ll(nObs); // tems observed-computed
1289 DiagonalMatrix PP(nObs); PP = 0.0;
1290
1291 unsigned iObs = 0;
1292
1293 // GPS
1294 // ---
1295 QMapIterator<QString, t_satData*> itGPS(epoData->satDataGPS);
1296 while (itGPS.hasNext()) {
1297 itGPS.next();
1298 t_satData* satData = itGPS.value();
1299 addObs(iPhase, iObs, satData, AA, ll, PP);
1300 }
1301
1302 // Galileo
1303 // -------
1304 QMapIterator<QString, t_satData*> itGal(epoData->satDataGal);
1305 while (itGal.hasNext()) {
1306 itGal.next();
1307 t_satData* satData = itGal.value();
1308 addObs(iPhase, iObs, satData, AA, ll, PP);
1309 }
1310
1311 // Compute Filter Update
1312 // ---------------------
1313 QQsav = _QQ;
1314
1315 kalman(AA, ll, PP, _QQ, dx);
1316
1317 vv = ll - AA * dx;
1318
1319 // Print Residuals
1320 // ---------------
1321 if (true) {
1322 ostringstream str;
1323 str.setf(ios::fixed);
1324
1325 QMapIterator<QString, t_satData*> itGPS(epoData->satDataGPS);
1326 while (itGPS.hasNext()) {
1327 itGPS.next();
1328 t_satData* satData = itGPS.value();
1329 printRes(vv, str, satData);
1330 }
1331 QMapIterator<QString, t_satData*> itGlo(epoData->satDataGlo);
1332 while (itGlo.hasNext()) {
1333 itGlo.next();
1334 t_satData* satData = itGlo.value();
1335 printRes(vv, str, satData);
1336 }
1337 QMapIterator<QString, t_satData*> itGal(epoData->satDataGal);
1338 while (itGal.hasNext()) {
1339 itGal.next();
1340 t_satData* satData = itGal.value();
1341 printRes(vv, str, satData);
1342 }
1343 _log += str.str().c_str();
1344 }
1345
1346 } while (outlierDetection(QQsav, vv, epoData->satDataGPS,
1347 epoData->satDataGlo, epoData->satDataGal) != 0);
1348 }
1349
1350 return success;
1351}
Note: See TracBrowser for help on using the repository browser.