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

Last change on this file since 3114 was 3114, checked in by weber, 13 years ago

Option Max Solution Gap added

File size: 38.1 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 // Bancroft Coordinates
248 // --------------------
249 _xcBanc.ReSize(4); _xcBanc = 0.0;
250 _ellBanc.ReSize(3); _ellBanc = 0.0;
251}
252
253// Destructor
254////////////////////////////////////////////////////////////////////////////
255bncModel::~bncModel() {
256 delete _nmeaStream;
257 delete _nmeaFile;
258 for (int ii = 0; ii < _posAverage.size(); ++ii) {
259 delete _posAverage[ii];
260 }
261 delete _antex;
262 for (int iPar = 1; iPar <= _params.size(); iPar++) {
263 delete _params[iPar-1];
264 }
265}
266
267// Reset Parameters and Variance-Covariance Matrix
268////////////////////////////////////////////////////////////////////////////
269void bncModel::reset() {
270
271 for (int iPar = 1; iPar <= _params.size(); iPar++) {
272 delete _params[iPar-1];
273 }
274 _params.clear();
275
276 int nextPar = 0;
277 _params.push_back(new bncParam(bncParam::CRD_X, ++nextPar, ""));
278 _params.push_back(new bncParam(bncParam::CRD_Y, ++nextPar, ""));
279 _params.push_back(new bncParam(bncParam::CRD_Z, ++nextPar, ""));
280 _params.push_back(new bncParam(bncParam::RECCLK, ++nextPar, ""));
281 if (_estTropo) {
282 _params.push_back(new bncParam(bncParam::TROPO, ++nextPar, ""));
283 }
284 if (_useGalileo) {
285 _params.push_back(new bncParam(bncParam::GALILEO_OFFSET, ++nextPar, ""));
286 }
287
288 _QQ.ReSize(_params.size());
289 _QQ = 0.0;
290 for (int iPar = 1; iPar <= _params.size(); iPar++) {
291 bncParam* pp = _params[iPar-1];
292 pp->xx = 0.0;
293 if (pp->isCrd()) {
294 _QQ(iPar,iPar) = _sigCrd0 * _sigCrd0;
295 }
296 else if (pp->type == bncParam::RECCLK) {
297 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
298 }
299 else if (pp->type == bncParam::TROPO) {
300 _QQ(iPar,iPar) = _sigTrp0 * _sigTrp0;
301 }
302 else if (pp->type == bncParam::GALILEO_OFFSET) {
303 _QQ(iPar,iPar) = _sigGalileoOffset0 * _sigGalileoOffset0;
304 }
305 }
306}
307
308// Bancroft Solution
309////////////////////////////////////////////////////////////////////////////
310t_irc bncModel::cmpBancroft(t_epoData* epoData) {
311
312 if (epoData->sizeGPS() < MINOBS) {
313 _log += "bncModel::cmpBancroft: not enough data\n";
314 return failure;
315 }
316
317 Matrix BB(epoData->sizeGPS(), 4);
318
319 QMapIterator<QString, t_satData*> it(epoData->satDataGPS);
320 int iObsBanc = 0;
321 while (it.hasNext()) {
322 ++iObsBanc;
323 it.next();
324 QString prn = it.key();
325 t_satData* satData = it.value();
326 BB(iObsBanc, 1) = satData->xx(1);
327 BB(iObsBanc, 2) = satData->xx(2);
328 BB(iObsBanc, 3) = satData->xx(3);
329 BB(iObsBanc, 4) = satData->P3 + satData->clk;
330 }
331
332 bancroft(BB, _xcBanc);
333
334 // Ellipsoidal Coordinates
335 // ------------------------
336 xyz2ell(_xcBanc.data(), _ellBanc.data());
337
338 // Compute Satellite Elevations
339 // ----------------------------
340 QMutableMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
341 while (iGPS.hasNext()) {
342 iGPS.next();
343 t_satData* satData = iGPS.value();
344 cmpEle(satData);
345 if (satData->eleSat < MINELE_GPS) {
346 delete satData;
347 iGPS.remove();
348 }
349 }
350
351 QMutableMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
352 while (iGlo.hasNext()) {
353 iGlo.next();
354 t_satData* satData = iGlo.value();
355 cmpEle(satData);
356 if (satData->eleSat < MINELE_GLO) {
357 delete satData;
358 iGlo.remove();
359 }
360 }
361
362 QMutableMapIterator<QString, t_satData*> iGal(epoData->satDataGal);
363 while (iGal.hasNext()) {
364 iGal.next();
365 t_satData* satData = iGal.value();
366 cmpEle(satData);
367 if (satData->eleSat < MINELE_GAL) {
368 delete satData;
369 iGal.remove();
370 }
371 }
372
373 return success;
374}
375
376// Computed Value
377////////////////////////////////////////////////////////////////////////////
378double bncModel::cmpValue(t_satData* satData, bool phase) {
379
380 ColumnVector xRec(3);
381 xRec(1) = x();
382 xRec(2) = y();
383 xRec(3) = z();
384
385 double rho0 = (satData->xx - xRec).norm_Frobenius();
386 double dPhi = t_CST::omega * rho0 / t_CST::c;
387
388 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
389 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
390 xRec(3) = z();
391
392 tides(_time, xRec);
393
394 satData->rho = (satData->xx - xRec).norm_Frobenius();
395
396 double tropDelay = delay_saast(satData->eleSat) +
397 trp() / sin(satData->eleSat);
398
399 double wind = 0.0;
400 if (phase) {
401 wind = windUp(satData->prn, satData->xx, xRec) * satData->lambda3;
402 }
403
404 double offset = 0.0;
405 if (satData->prn[0] == 'E') {
406 offset = Galileo_offset();
407 }
408
409 double phaseCenter = 0.0;
410 if (_antex) {
411 bool found;
412 phaseCenter = _antex->pco(_antennaName, satData->eleSat, found);
413 if (!found) {
414 emit newMessage("ANTEX: antenna >"
415 + _antennaName.toAscii() + "< not found", true);
416 }
417 }
418
419 return satData->rho + phaseCenter + clk()
420 + offset - satData->clk + tropDelay + wind;
421}
422
423// Tropospheric Model (Saastamoinen)
424////////////////////////////////////////////////////////////////////////////
425double bncModel::delay_saast(double Ele) {
426
427 double xyz[3];
428 xyz[0] = x();
429 xyz[1] = y();
430 xyz[2] = z();
431 double ell[3];
432 xyz2ell(xyz, ell);
433 double height = ell[2];
434
435 double pp = 1013.25 * pow(1.0 - 2.26e-5 * height, 5.225);
436 double TT = 18.0 - height * 0.0065 + 273.15;
437 double hh = 50.0 * exp(-6.396e-4 * height);
438 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
439
440 double h_km = height / 1000.0;
441
442 if (h_km < 0.0) h_km = 0.0;
443 if (h_km > 5.0) h_km = 5.0;
444 int ii = int(h_km + 1);
445 double href = ii - 1;
446
447 double bCor[6];
448 bCor[0] = 1.156;
449 bCor[1] = 1.006;
450 bCor[2] = 0.874;
451 bCor[3] = 0.757;
452 bCor[4] = 0.654;
453 bCor[5] = 0.563;
454
455 double BB = bCor[ii-1] + (bCor[ii]-bCor[ii-1]) * (h_km - href);
456
457 double zen = M_PI/2.0 - Ele;
458
459 return (0.002277/cos(zen)) * (pp + ((1255.0/TT)+0.05)*ee - BB*(tan(zen)*tan(zen)));
460}
461
462// Prediction Step of the Filter
463////////////////////////////////////////////////////////////////////////////
464void bncModel::predict(t_epoData* epoData) {
465
466 bncSettings settings;
467
468 _time = epoData->tt; // current epoch time
469
470 bool firstCrd = false;
471 if (!_lastTimeOK.valid() || _time - _lastTimeOK > settings.value("pppMaxSolGap").toDouble() ) {
472 firstCrd = true;
473 _startTime = epoData->tt;
474 reset();
475 }
476
477 // Use different white noise for Quick-Start mode
478 // ----------------------------------------------
479 double sigCrdP_used = _sigCrdP;
480 if ( _quickStart > 0.0 && _quickStart > (epoData->tt - _startTime) ) {
481 sigCrdP_used = 0.0;
482 }
483
484 // Predict Parameter values, add white noise
485 // -----------------------------------------
486 for (int iPar = 1; iPar <= _params.size(); iPar++) {
487 bncParam* pp = _params[iPar-1];
488
489 // Coordinates
490 // -----------
491 if (pp->type == bncParam::CRD_X) {
492 if (firstCrd) {
493 if (settings.value("pppRefCrdX").toString() != "" &&
494 settings.value("pppRefCrdY").toString() != "" &&
495 settings.value("pppRefCrdZ").toString() != "") {
496 pp->xx = settings.value("pppRefCrdX").toDouble();
497 }
498 else {
499 pp->xx = _xcBanc(1);
500 }
501 }
502 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
503 }
504 else if (pp->type == bncParam::CRD_Y) {
505 if (firstCrd) {
506 if (settings.value("pppRefCrdX").toString() != "" &&
507 settings.value("pppRefCrdY").toString() != "" &&
508 settings.value("pppRefCrdZ").toString() != "") {
509 pp->xx = settings.value("pppRefCrdY").toDouble();
510 }
511 else {
512 pp->xx = _xcBanc(2);
513 }
514 }
515 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
516 }
517 else if (pp->type == bncParam::CRD_Z) {
518 if (firstCrd) {
519 if (settings.value("pppRefCrdX").toString() != "" &&
520 settings.value("pppRefCrdY").toString() != "" &&
521 settings.value("pppRefCrdZ").toString() != "") {
522 pp->xx = settings.value("pppRefCrdZ").toDouble();
523 }
524 else {
525 pp->xx = _xcBanc(3);
526 }
527 }
528 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
529 }
530
531 // Receiver Clocks
532 // ---------------
533 else if (pp->type == bncParam::RECCLK) {
534 pp->xx = _xcBanc(4);
535 for (int jj = 1; jj <= _params.size(); jj++) {
536 _QQ(iPar, jj) = 0.0;
537 }
538 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
539 }
540
541 // Tropospheric Delay
542 // ------------------
543 else if (pp->type == bncParam::TROPO) {
544 _QQ(iPar,iPar) += _sigTrpP * _sigTrpP;
545 }
546
547 // Galileo Offset
548 // --------------
549 else if (pp->type == bncParam::GALILEO_OFFSET) {
550 _QQ(iPar,iPar) += _sigGalileoOffsetP * _sigGalileoOffsetP;
551 }
552 }
553
554 // Add New Ambiguities if necessary
555 // --------------------------------
556 if (_usePhase) {
557
558 // Make a copy of QQ and xx, set parameter indices
559 // -----------------------------------------------
560 SymmetricMatrix QQ_old = _QQ;
561
562 for (int iPar = 1; iPar <= _params.size(); iPar++) {
563 _params[iPar-1]->index_old = _params[iPar-1]->index;
564 _params[iPar-1]->index = 0;
565 }
566
567 // Remove Ambiguity Parameters without observations
568 // ------------------------------------------------
569 int iPar = 0;
570 QMutableVectorIterator<bncParam*> it(_params);
571 while (it.hasNext()) {
572 bncParam* par = it.next();
573 bool removed = false;
574 if (par->type == bncParam::AMB_L3) {
575 if (epoData->satDataGPS.find(par->prn) == epoData->satDataGPS.end() &&
576 epoData->satDataGlo.find(par->prn) == epoData->satDataGlo.end() &&
577 epoData->satDataGal.find(par->prn) == epoData->satDataGal.end() ) {
578 removed = true;
579 delete par;
580 it.remove();
581 }
582 }
583 if (! removed) {
584 ++iPar;
585 par->index = iPar;
586 }
587 }
588
589 // Add new ambiguity parameters
590 // ----------------------------
591 QMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
592 while (iGPS.hasNext()) {
593 iGPS.next();
594 t_satData* satData = iGPS.value();
595 addAmb(satData);
596 }
597
598 QMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
599 while (iGlo.hasNext()) {
600 iGlo.next();
601 t_satData* satData = iGlo.value();
602 addAmb(satData);
603 }
604
605 QMapIterator<QString, t_satData*> iGal(epoData->satDataGal);
606 while (iGal.hasNext()) {
607 iGal.next();
608 t_satData* satData = iGal.value();
609 addAmb(satData);
610 }
611
612 int nPar = _params.size();
613 _QQ.ReSize(nPar); _QQ = 0.0;
614 for (int i1 = 1; i1 <= nPar; i1++) {
615 bncParam* p1 = _params[i1-1];
616 if (p1->index_old != 0) {
617 _QQ(p1->index, p1->index) = QQ_old(p1->index_old, p1->index_old);
618 for (int i2 = 1; i2 <= nPar; i2++) {
619 bncParam* p2 = _params[i2-1];
620 if (p2->index_old != 0) {
621 _QQ(p1->index, p2->index) = QQ_old(p1->index_old, p2->index_old);
622 }
623 }
624 }
625 }
626
627 for (int ii = 1; ii <= nPar; ii++) {
628 bncParam* par = _params[ii-1];
629 if (par->index_old == 0) {
630 _QQ(par->index, par->index) = _sigAmb0 * _sigAmb0;
631 }
632 par->index_old = par->index;
633 }
634 }
635}
636
637// Update Step of the Filter (currently just a single-epoch solution)
638////////////////////////////////////////////////////////////////////////////
639t_irc bncModel::update(t_epoData* epoData) {
640
641 bncSettings settings;
642
643 _log.clear();
644
645 if (settings.value("pppSPP").toString() == "PPP") {
646 _log += "Precise Point Positioning of Epoch "
647 + QByteArray(_time.timestr(1).c_str()) +
648 "\n---------------------------------------------------------------\n";
649 }
650 else {
651 _log += "Single Point Positioning of Epoch "
652 + QByteArray(_time.timestr(1).c_str()) +
653 "\n--------------------------------------------------------------\n";
654 }
655
656 SymmetricMatrix QQsav;
657 ColumnVector dx;
658 ColumnVector vv;
659
660 // Loop over all outliers
661 // ----------------------
662 do {
663
664 // Bancroft Solution
665 // -----------------
666 if (cmpBancroft(epoData) != success) {
667 emit newMessage(_log, false);
668 return failure;
669 }
670
671 // Status Prediction
672 // -----------------
673 predict(epoData);
674
675 // Create First-Design Matrix
676 // --------------------------
677 unsigned nPar = _params.size();
678 unsigned nObs = 0;
679 if (_usePhase) {
680 nObs = 2 * (epoData->sizeGPS() + epoData->sizeGal()) + epoData->sizeGlo();
681 }
682 else {
683 nObs = epoData->sizeGPS() + epoData->sizeGal(); // Glonass code not used
684 }
685
686 if (nObs < nPar) {
687 _log += "bncModel::update: nObs < nPar\n";
688 emit newMessage(_log, false);
689 return failure;
690 }
691
692 Matrix AA(nObs, nPar); // first design matrix
693 ColumnVector ll(nObs); // tems observed-computed
694 DiagonalMatrix PP(nObs); PP = 0.0;
695
696 unsigned iObs = 0;
697
698 // GPS code and (optionally) phase observations
699 // --------------------------------------------
700 QMapIterator<QString, t_satData*> itGPS(epoData->satDataGPS);
701 while (itGPS.hasNext()) {
702 itGPS.next();
703 t_satData* satData = itGPS.value();
704 addObs(iObs, satData, AA, ll, PP);
705 }
706
707 // Glonass phase observations
708 // --------------------------
709 QMapIterator<QString, t_satData*> itGlo(epoData->satDataGlo);
710 while (itGlo.hasNext()) {
711 itGlo.next();
712 t_satData* satData = itGlo.value();
713 addObs(iObs, satData, AA, ll, PP);
714 }
715
716 // Galileo code and (optionally) phase observations
717 // ------------------------------------------------
718 QMapIterator<QString, t_satData*> itGal(epoData->satDataGal);
719 while (itGal.hasNext()) {
720 itGal.next();
721 t_satData* satData = itGal.value();
722 addObs(iObs, satData, AA, ll, PP);
723 }
724
725 // Compute Filter Update
726 // ---------------------
727 QQsav = _QQ;
728
729 kalman(AA, ll, PP, _QQ, dx);
730
731 vv = ll - AA * dx;
732
733 // Print Residuals
734 // ---------------
735 if (true) {
736 ostringstream str;
737 str.setf(ios::fixed);
738
739 QMapIterator<QString, t_satData*> itGPS(epoData->satDataGPS);
740 while (itGPS.hasNext()) {
741 itGPS.next();
742 t_satData* satData = itGPS.value();
743 printRes(vv, str, satData);
744 }
745 QMapIterator<QString, t_satData*> itGlo(epoData->satDataGlo);
746 while (itGlo.hasNext()) {
747 itGlo.next();
748 t_satData* satData = itGlo.value();
749 printRes(vv, str, satData);
750 }
751 QMapIterator<QString, t_satData*> itGal(epoData->satDataGal);
752 while (itGal.hasNext()) {
753 itGal.next();
754 t_satData* satData = itGal.value();
755 printRes(vv, str, satData);
756 }
757 _log += str.str().c_str();
758 }
759
760 } while (outlierDetection(QQsav, vv, epoData->satDataGPS,
761 epoData->satDataGlo, epoData->satDataGal) != 0);
762
763 // Remember the Epoch-specific Results for the computation of means
764 // ----------------------------------------------------------------
765 pppPos* newPos = new pppPos;
766 newPos->time = epoData->tt;
767
768 // Set Solution Vector
769 // -------------------
770 ostringstream strB;
771 strB.setf(ios::fixed);
772 QVectorIterator<bncParam*> itPar(_params);
773 while (itPar.hasNext()) {
774 bncParam* par = itPar.next();
775 par->xx += dx(par->index);
776
777 if (par->type == bncParam::RECCLK) {
778 strB << "\n clk = " << setw(10) << setprecision(3) << par->xx
779 << " +- " << setw(6) << setprecision(3)
780 << sqrt(_QQ(par->index,par->index));
781 }
782 else if (par->type == bncParam::AMB_L3) {
783 strB << "\n amb " << par->prn.toAscii().data() << " = "
784 << setw(10) << setprecision(3) << par->xx
785 << " +- " << setw(6) << setprecision(3)
786 << sqrt(_QQ(par->index,par->index));
787 }
788 else if (par->type == bncParam::TROPO) {
789 double aprTrp = delay_saast(M_PI/2.0);
790 strB << "\n trp = " << par->prn.toAscii().data()
791 << setw(7) << setprecision(3) << aprTrp << " "
792 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
793 << " +- " << setw(6) << setprecision(3)
794 << sqrt(_QQ(par->index,par->index));
795 newPos->xnt[6] = aprTrp + par->xx;
796 }
797 else if (par->type == bncParam::GALILEO_OFFSET) {
798 strB << "\n offset = " << setw(10) << setprecision(3) << par->xx
799 << " +- " << setw(6) << setprecision(3)
800 << sqrt(_QQ(par->index,par->index));
801 }
802 }
803 strB << '\n';
804 _log += strB.str().c_str();
805 emit newMessage(_log, false);
806
807 // Final Message (both log file and screen)
808 // ----------------------------------------
809 ostringstream strC;
810 strC.setf(ios::fixed);
811 strC << _staID.data() << " PPP "
812 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
813 << setw(14) << setprecision(3) << x() << " +- "
814 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
815 << setw(14) << setprecision(3) << y() << " +- "
816 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
817 << setw(14) << setprecision(3) << z() << " +- "
818 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
819
820 // NEU Output
821 // ----------
822 double xyzRef[3];
823
824 if (settings.value("pppRefCrdX").toString() != "" &&
825 settings.value("pppRefCrdY").toString() != "" &&
826 settings.value("pppRefCrdZ").toString() != "") {
827
828 xyzRef[0] = settings.value("pppRefCrdX").toDouble();
829 xyzRef[1] = settings.value("pppRefCrdY").toDouble();
830 xyzRef[2] = settings.value("pppRefCrdZ").toDouble();
831
832 newPos->xnt[0] = x() - xyzRef[0];
833 newPos->xnt[1] = y() - xyzRef[1];
834 newPos->xnt[2] = z() - xyzRef[2];
835
836 double ellRef[3];
837 xyz2ell(xyzRef, ellRef);
838 xyz2neu(ellRef, newPos->xnt, &newPos->xnt[3]);
839
840 strC << " NEU "
841 << setw(8) << setprecision(3) << newPos->xnt[3] << " "
842 << setw(8) << setprecision(3) << newPos->xnt[4] << " "
843 << setw(8) << setprecision(3) << newPos->xnt[5] << endl;
844
845 }
846
847 emit newMessage(QByteArray(strC.str().c_str()), true);
848
849 if (settings.value("pppAverage").toString() == "") {
850 delete newPos;
851 }
852 else {
853
854 _posAverage.push_back(newPos);
855
856 // Time Span for Average Computation
857 // ---------------------------------
858 double tRangeAverage = settings.value("pppAverage").toDouble() * 60.;
859 if (tRangeAverage < 0) {
860 tRangeAverage = 0;
861 }
862 if (tRangeAverage > 86400) {
863 tRangeAverage = 86400;
864 }
865
866 // Compute the Mean
867 // ----------------
868 ColumnVector mean(7); mean = 0.0;
869
870 QMutableVectorIterator<pppPos*> it(_posAverage);
871 while (it.hasNext()) {
872 pppPos* pp = it.next();
873 if ( (epoData->tt - pp->time) >= tRangeAverage ) {
874 delete pp;
875 it.remove();
876 }
877 else {
878 for (int ii = 0; ii < 7; ++ii) {
879 mean[ii] += pp->xnt[ii];
880 }
881 }
882 }
883
884 int nn = _posAverage.size();
885
886 if (nn > 0) {
887
888 mean /= nn;
889
890 // Compute the Deviation
891 // ---------------------
892 ColumnVector std(7); std = 0.0;
893 QVectorIterator<pppPos*> it2(_posAverage);
894 while (it2.hasNext()) {
895 pppPos* pp = it2.next();
896 for (int ii = 0; ii < 7; ++ii) {
897 std[ii] += (pp->xnt[ii] - mean[ii]) * (pp->xnt[ii] - mean[ii]);
898 }
899 }
900 for (int ii = 0; ii < 7; ++ii) {
901 std[ii] = sqrt(std[ii] / nn);
902 }
903
904 ostringstream strD; strD.setf(ios::fixed);
905 strD << _staID.data() << " AVE-XYZ "
906 << epoData->tt.timestr(1) << " "
907// << setw(13) << setprecision(3) << mean[0] << " +- "
908 << setw(13) << setprecision(3) << mean[0] + xyzRef[0] << " +- "
909 << setw(6) << setprecision(3) << std[0] << " "
910// << setw(14) << setprecision(3) << mean[1] << " +- "
911 << setw(14) << setprecision(3) << mean[1] + xyzRef[1] << " +- "
912 << setw(6) << setprecision(3) << std[1] << " "
913// << setw(14) << setprecision(3) << mean[2] << " +- "
914 << setw(14) << setprecision(3) << mean[2] + xyzRef[2] << " +- "
915 << setw(6) << setprecision(3) << std[2];
916 emit newMessage(QByteArray(strD.str().c_str()), true);
917
918 ostringstream strE; strE.setf(ios::fixed);
919 strE << _staID.data() << " AVE-NEU "
920 << epoData->tt.timestr(1) << " "
921 << setw(13) << setprecision(3) << mean[3] << " +- "
922 << setw(6) << setprecision(3) << std[3] << " "
923 << setw(14) << setprecision(3) << mean[4] << " +- "
924 << setw(6) << setprecision(3) << std[4] << " "
925 << setw(14) << setprecision(3) << mean[5] << " +- "
926 << setw(6) << setprecision(3) << std[5];
927
928 emit newMessage(QByteArray(strE.str().c_str()), true);
929
930 if ( Qt::CheckState(settings.value("pppEstTropo").toInt()) == Qt::Checked) {
931 ostringstream strF; strF.setf(ios::fixed);
932 strF << _staID.data() << " AVE-TRP "
933 << epoData->tt.timestr(1) << " "
934 << setw(13) << setprecision(3) << mean[6] << " +- "
935 << setw(6) << setprecision(3) << std[6] << endl;
936 emit newMessage(QByteArray(strF.str().c_str()), true);
937 }
938 }
939 }
940
941 // NMEA Output
942 // -----------
943 double xyz[3];
944 xyz[0] = x();
945 xyz[1] = y();
946 xyz[2] = z();
947 double ell[3];
948 xyz2ell(xyz, ell);
949 double phiDeg = ell[0] * 180 / M_PI;
950 double lamDeg = ell[1] * 180 / M_PI;
951
952 char phiCh = 'N';
953 if (phiDeg < 0) {
954 phiDeg = -phiDeg;
955 phiCh = 'S';
956 }
957 char lamCh = 'E';
958 if (lamDeg < 0) {
959 lamDeg = -lamDeg;
960 lamCh = 'W';
961 }
962
963 string datestr = epoData->tt.datestr(0); // yyyymmdd
964 ostringstream strRMC;
965 strRMC.setf(ios::fixed);
966 strRMC << "GPRMC,"
967 << epoData->tt.timestr(0,0) << ",A,"
968 << setw(2) << setfill('0') << int(phiDeg)
969 << setw(6) << setprecision(3) << setfill('0')
970 << fmod(60*phiDeg,60) << ',' << phiCh << ','
971 << setw(3) << setfill('0') << int(lamDeg)
972 << setw(6) << setprecision(3) << setfill('0')
973 << fmod(60*lamDeg,60) << ',' << lamCh << ",,,"
974 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
975 << datestr[2] << datestr[3] << ",,";
976
977 writeNMEAstr(QString(strRMC.str().c_str()));
978
979 double dop = 2.0; // TODO
980
981 ostringstream strGGA;
982 strGGA.setf(ios::fixed);
983 strGGA << "GPGGA,"
984 << epoData->tt.timestr(0,0) << ','
985 << setw(2) << setfill('0') << int(phiDeg)
986 << setw(10) << setprecision(7) << setfill('0')
987 << fmod(60*phiDeg,60) << ',' << phiCh << ','
988 << setw(3) << setfill('0') << int(lamDeg)
989 << setw(10) << setprecision(7) << setfill('0')
990 << fmod(60*lamDeg,60) << ',' << lamCh
991 << ",1," << setw(2) << setfill('0') << epoData->sizeAll() << ','
992 << setw(3) << setprecision(1) << dop << ','
993 << setprecision(3) << ell[2] << ",M,0.0,M,,";
994
995 writeNMEAstr(QString(strGGA.str().c_str()));
996
997 _lastTimeOK = _time; // remember time of last successful update
998 return success;
999}
1000
1001// Outlier Detection
1002////////////////////////////////////////////////////////////////////////////
1003int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
1004 const ColumnVector& vv,
1005 QMap<QString, t_satData*>& satDataGPS,
1006 QMap<QString, t_satData*>& satDataGlo,
1007 QMap<QString, t_satData*>& satDataGal) {
1008
1009 QString prnCode;
1010 QString prnPhase;
1011 double maxResCode = 0.0;
1012 double maxResPhase = 0.0;
1013
1014 QString prnRemoved;
1015 double maxRes;
1016
1017 int irc = 0;
1018
1019 // Check Glonass
1020 // -------------
1021 if (irc == 0) {
1022 findMaxRes(vv,satDataGlo, prnCode, maxResCode, prnPhase, maxResPhase);
1023 if (maxResPhase > MAXRES_PHASE_GLO) {
1024 satDataGlo.remove(prnPhase);
1025 prnRemoved = prnPhase;
1026 maxRes = maxResPhase;
1027 irc = 1;
1028 }
1029 }
1030
1031 // Check Galileo
1032 // -------------
1033 if (irc == 0) {
1034 findMaxRes(vv,satDataGal, prnCode, maxResCode, prnPhase, maxResPhase);
1035 if (maxResPhase > MAXRES_PHASE_GAL) {
1036 satDataGal.remove(prnPhase);
1037 prnRemoved = prnPhase;
1038 maxRes = maxResPhase;
1039 irc = 1;
1040 }
1041 else if (maxResCode > MAXRES_CODE_GAL) {
1042 satDataGal.remove(prnCode);
1043 prnRemoved = prnCode;
1044 maxRes = maxResCode;
1045 irc = 1;
1046 }
1047 }
1048
1049 // Check GPS
1050 // ---------
1051 if (irc == 0) {
1052 findMaxRes(vv,satDataGPS, prnCode, maxResCode, prnPhase, maxResPhase);
1053 if (maxResPhase > MAXRES_PHASE_GPS) {
1054 satDataGPS.remove(prnPhase);
1055 prnRemoved = prnPhase;
1056 maxRes = maxResPhase;
1057 irc = 1;
1058 }
1059 else if (maxResCode > MAXRES_CODE_GPS) {
1060 satDataGPS.remove(prnCode);
1061 prnRemoved = prnCode;
1062 maxRes = maxResCode;
1063 irc = 1;
1064 }
1065 }
1066
1067 if (irc != 0) {
1068 _log += "Outlier " + prnRemoved.toAscii() + " "
1069 + QByteArray::number(maxRes, 'f', 3) + "\n";
1070 _QQ = QQsav;
1071 }
1072
1073 return irc;
1074}
1075
1076//
1077////////////////////////////////////////////////////////////////////////////
1078void bncModel::writeNMEAstr(const QString& nmStr) {
1079
1080 unsigned char XOR = 0;
1081 for (int ii = 0; ii < nmStr.length(); ii++) {
1082 XOR ^= (unsigned char) nmStr[ii].toAscii();
1083 }
1084
1085 QString outStr = '$' + nmStr
1086 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
1087
1088 if (_nmeaStream) {
1089 *_nmeaStream << outStr;
1090 _nmeaStream->flush();
1091 }
1092
1093 emit newNMEAstr(outStr.toAscii());
1094}
1095
1096////
1097//////////////////////////////////////////////////////////////////////////////
1098void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
1099 const DiagonalMatrix& PP,
1100 SymmetricMatrix& QQ, ColumnVector& dx) {
1101
1102 int nObs = AA.Nrows();
1103 int nPar = AA.Ncols();
1104
1105 UpperTriangularMatrix SS = Cholesky(QQ).t();
1106
1107 Matrix SA = SS*AA.t();
1108 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
1109 for (int ii = 1; ii <= nObs; ++ii) {
1110 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
1111 }
1112
1113 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
1114 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
1115
1116 UpperTriangularMatrix UU;
1117 QRZ(SRF, UU);
1118
1119 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
1120 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
1121 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
1122
1123 UpperTriangularMatrix SHi = SH_rt.i();
1124
1125 Matrix KT = SHi * YY;
1126 SymmetricMatrix Hi; Hi << SHi * SHi.t();
1127
1128 dx = KT.t() * ll;
1129 QQ << (SS.t() * SS);
1130}
1131
1132// Phase Wind-Up Correction
1133///////////////////////////////////////////////////////////////////////////
1134double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
1135 const ColumnVector& rRec) {
1136
1137 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
1138
1139 // First time - initialize to zero
1140 // -------------------------------
1141 if (!_windUpTime.contains(prn)) {
1142 _windUpSum[prn] = 0.0;
1143 }
1144
1145 // Compute the correction for new time
1146 // -----------------------------------
1147 if (!_windUpTime.contains(prn) || _windUpTime[prn] != Mjd) {
1148 _windUpTime[prn] = Mjd;
1149
1150 // Unit Vector GPS Satellite --> Receiver
1151 // --------------------------------------
1152 ColumnVector rho = rRec - rSat;
1153 rho /= rho.norm_Frobenius();
1154
1155 // GPS Satellite unit Vectors sz, sy, sx
1156 // -------------------------------------
1157 ColumnVector sz = -rSat / rSat.norm_Frobenius();
1158
1159 ColumnVector xSun = Sun(Mjd);
1160 xSun /= xSun.norm_Frobenius();
1161
1162 ColumnVector sy = crossproduct(sz, xSun);
1163 ColumnVector sx = crossproduct(sy, sz);
1164
1165 // Effective Dipole of the GPS Satellite Antenna
1166 // ---------------------------------------------
1167 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
1168 - crossproduct(rho, sy);
1169
1170 // Receiver unit Vectors rx, ry
1171 // ----------------------------
1172 ColumnVector rx(3);
1173 ColumnVector ry(3);
1174
1175 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
1176 double neu[3];
1177
1178 neu[0] = 1.0;
1179 neu[1] = 0.0;
1180 neu[2] = 0.0;
1181 neu2xyz(recEll, neu, rx.data());
1182
1183 neu[0] = 0.0;
1184 neu[1] = -1.0;
1185 neu[2] = 0.0;
1186 neu2xyz(recEll, neu, ry.data());
1187
1188 // Effective Dipole of the Receiver Antenna
1189 // ----------------------------------------
1190 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
1191 + crossproduct(rho, ry);
1192
1193 // Resulting Effect
1194 // ----------------
1195 double alpha = DotProduct(dipSat,dipRec) /
1196 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
1197
1198 if (alpha > 1.0) alpha = 1.0;
1199 if (alpha < -1.0) alpha = -1.0;
1200
1201 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
1202
1203 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
1204 dphi = -dphi;
1205 }
1206
1207 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
1208 }
1209
1210 return _windUpSum[prn];
1211}
1212
1213//
1214///////////////////////////////////////////////////////////////////////////
1215void bncModel::cmpEle(t_satData* satData) {
1216 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
1217 double rho = rr.norm_Frobenius();
1218
1219 double neu[3];
1220 xyz2neu(_ellBanc.data(), rr.data(), neu);
1221
1222 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
1223 if (neu[2] < 0) {
1224 satData->eleSat *= -1.0;
1225 }
1226 satData->azSat = atan2(neu[1], neu[0]);
1227}
1228
1229//
1230///////////////////////////////////////////////////////////////////////////
1231void bncModel::addAmb(t_satData* satData) {
1232 bool found = false;
1233 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1234 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1235 _params[iPar-1]->prn == satData->prn) {
1236 found = true;
1237 break;
1238 }
1239 }
1240 if (!found) {
1241 bncParam* par = new bncParam(bncParam::AMB_L3,
1242 _params.size()+1, satData->prn);
1243 _params.push_back(par);
1244 par->xx = satData->L3 - cmpValue(satData, true);
1245 }
1246}
1247
1248//
1249///////////////////////////////////////////////////////////////////////////
1250void bncModel::addObs(unsigned& iObs, t_satData* satData,
1251 Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP) {
1252
1253 // Code Observations
1254 // -----------------
1255 if (satData->system() != 'R') {
1256 ++iObs;
1257 ll(iObs) = satData->P3 - cmpValue(satData, false);
1258 PP(iObs,iObs) = 1.0 / (_sigP3 * _sigP3);
1259 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1260 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
1261 }
1262 satData->indexCode = iObs;
1263 }
1264
1265 // Phase Observations
1266 // ------------------
1267 if (_usePhase) {
1268 ++iObs;
1269 ll(iObs) = satData->L3 - cmpValue(satData, true);
1270 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
1271 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1272 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1273 _params[iPar-1]->prn == satData->prn) {
1274 ll(iObs) -= _params[iPar-1]->xx;
1275 }
1276 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
1277 }
1278 satData->indexPhase = iObs;
1279 }
1280}
1281
1282//
1283///////////////////////////////////////////////////////////////////////////
1284void bncModel::printRes(const ColumnVector& vv,
1285 ostringstream& str, t_satData* satData) {
1286 if (satData->indexPhase) {
1287 str << _time.timestr(1)
1288 << " RES " << satData->prn.toAscii().data() << " L3 "
1289 << setw(9) << setprecision(4) << vv(satData->indexPhase);
1290 if (satData->indexCode) {
1291 str << " P3 " << setw(9) << setprecision(4) << vv(satData->indexCode);
1292 }
1293 str << endl;
1294 }
1295}
1296
1297//
1298///////////////////////////////////////////////////////////////////////////
1299void bncModel::findMaxRes(const ColumnVector& vv,
1300 const QMap<QString, t_satData*>& satData,
1301 QString& prnCode, double& maxResCode,
1302 QString& prnPhase, double& maxResPhase) {
1303 maxResCode = 0.0;
1304 maxResPhase = 0.0;
1305
1306 QMapIterator<QString, t_satData*> it(satData);
1307 while (it.hasNext()) {
1308 it.next();
1309 t_satData* satData = it.value();
1310 if (satData->indexCode) {
1311 if (fabs(vv(satData->indexCode)) > maxResCode) {
1312 maxResCode = fabs(vv(satData->indexCode));
1313 prnCode = satData->prn;
1314 }
1315 }
1316 if (satData->indexPhase) {
1317 if (fabs(vv(satData->indexPhase)) > maxResPhase) {
1318 maxResPhase = fabs(vv(satData->indexPhase));
1319 prnPhase = satData->prn;
1320 }
1321 }
1322 }
1323}
1324
Note: See TracBrowser for help on using the repository browser.