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

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

Maximuml Solution Gap introduced

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