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

Last change on this file since 3272 was 3169, checked in by weber, 14 years ago

Output of mean values only when ref XYZ specified

File size: 38.2 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 if (settings.value("pppRefCrdX").toString() != "" &&
907 settings.value("pppRefCrdY").toString() != "" &&
908 settings.value("pppRefCrdZ").toString() != "") {
909
910 ostringstream strD; strD.setf(ios::fixed);
911 strD << _staID.data() << " AVE-XYZ "
912 << epoData->tt.timestr(1) << " "
913 << setw(13) << setprecision(3) << mean[0] + xyzRef[0] << " +- "
914 << setw(6) << setprecision(3) << std[0] << " "
915 << setw(14) << setprecision(3) << mean[1] + xyzRef[1] << " +- "
916 << setw(6) << setprecision(3) << std[1] << " "
917 << setw(14) << setprecision(3) << mean[2] + xyzRef[2] << " +- "
918 << setw(6) << setprecision(3) << std[2];
919 emit newMessage(QByteArray(strD.str().c_str()), true);
920
921 ostringstream strE; strE.setf(ios::fixed);
922 strE << _staID.data() << " AVE-NEU "
923 << epoData->tt.timestr(1) << " "
924 << setw(13) << setprecision(3) << mean[3] << " +- "
925 << setw(6) << setprecision(3) << std[3] << " "
926 << setw(14) << setprecision(3) << mean[4] << " +- "
927 << setw(6) << setprecision(3) << std[4] << " "
928 << setw(14) << setprecision(3) << mean[5] << " +- "
929 << setw(6) << setprecision(3) << std[5];
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
944 // NMEA Output
945 // -----------
946 double xyz[3];
947 xyz[0] = x();
948 xyz[1] = y();
949 xyz[2] = z();
950 double ell[3];
951 xyz2ell(xyz, ell);
952 double phiDeg = ell[0] * 180 / M_PI;
953 double lamDeg = ell[1] * 180 / M_PI;
954
955 char phiCh = 'N';
956 if (phiDeg < 0) {
957 phiDeg = -phiDeg;
958 phiCh = 'S';
959 }
960 char lamCh = 'E';
961 if (lamDeg < 0) {
962 lamDeg = -lamDeg;
963 lamCh = 'W';
964 }
965
966 string datestr = epoData->tt.datestr(0); // yyyymmdd
967 ostringstream strRMC;
968 strRMC.setf(ios::fixed);
969 strRMC << "GPRMC,"
970 << epoData->tt.timestr(0,0) << ",A,"
971 << setw(2) << setfill('0') << int(phiDeg)
972 << setw(6) << setprecision(3) << setfill('0')
973 << fmod(60*phiDeg,60) << ',' << phiCh << ','
974 << setw(3) << setfill('0') << int(lamDeg)
975 << setw(6) << setprecision(3) << setfill('0')
976 << fmod(60*lamDeg,60) << ',' << lamCh << ",,,"
977 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
978 << datestr[2] << datestr[3] << ",,";
979
980 writeNMEAstr(QString(strRMC.str().c_str()));
981
982 double dop = 2.0; // TODO
983
984 ostringstream strGGA;
985 strGGA.setf(ios::fixed);
986 strGGA << "GPGGA,"
987 << epoData->tt.timestr(0,0) << ','
988 << setw(2) << setfill('0') << int(phiDeg)
989 << setw(10) << setprecision(7) << setfill('0')
990 << fmod(60*phiDeg,60) << ',' << phiCh << ','
991 << setw(3) << setfill('0') << int(lamDeg)
992 << setw(10) << setprecision(7) << setfill('0')
993 << fmod(60*lamDeg,60) << ',' << lamCh
994 << ",1," << setw(2) << setfill('0') << epoData->sizeAll() << ','
995 << setw(3) << setprecision(1) << dop << ','
996 << setprecision(3) << ell[2] << ",M,0.0,M,,";
997
998 writeNMEAstr(QString(strGGA.str().c_str()));
999
1000 _lastTimeOK = _time; // remember time of last successful update
1001 return success;
1002}
1003
1004// Outlier Detection
1005////////////////////////////////////////////////////////////////////////////
1006int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
1007 const ColumnVector& vv,
1008 QMap<QString, t_satData*>& satDataGPS,
1009 QMap<QString, t_satData*>& satDataGlo,
1010 QMap<QString, t_satData*>& satDataGal) {
1011
1012 QString prnCode;
1013 QString prnPhase;
1014 double maxResCode = 0.0;
1015 double maxResPhase = 0.0;
1016
1017 QString prnRemoved;
1018 double maxRes;
1019
1020 int irc = 0;
1021
1022 // Check Glonass
1023 // -------------
1024 if (irc == 0) {
1025 findMaxRes(vv,satDataGlo, prnCode, maxResCode, prnPhase, maxResPhase);
1026 if (maxResPhase > MAXRES_PHASE_GLO) {
1027 satDataGlo.remove(prnPhase);
1028 prnRemoved = prnPhase;
1029 maxRes = maxResPhase;
1030 irc = 1;
1031 }
1032 }
1033
1034 // Check Galileo
1035 // -------------
1036 if (irc == 0) {
1037 findMaxRes(vv,satDataGal, prnCode, maxResCode, prnPhase, maxResPhase);
1038 if (maxResPhase > MAXRES_PHASE_GAL) {
1039 satDataGal.remove(prnPhase);
1040 prnRemoved = prnPhase;
1041 maxRes = maxResPhase;
1042 irc = 1;
1043 }
1044 else if (maxResCode > MAXRES_CODE_GAL) {
1045 satDataGal.remove(prnCode);
1046 prnRemoved = prnCode;
1047 maxRes = maxResCode;
1048 irc = 1;
1049 }
1050 }
1051
1052 // Check GPS
1053 // ---------
1054 if (irc == 0) {
1055 findMaxRes(vv,satDataGPS, prnCode, maxResCode, prnPhase, maxResPhase);
1056 if (maxResPhase > MAXRES_PHASE_GPS) {
1057 satDataGPS.remove(prnPhase);
1058 prnRemoved = prnPhase;
1059 maxRes = maxResPhase;
1060 irc = 1;
1061 }
1062 else if (maxResCode > MAXRES_CODE_GPS) {
1063 satDataGPS.remove(prnCode);
1064 prnRemoved = prnCode;
1065 maxRes = maxResCode;
1066 irc = 1;
1067 }
1068 }
1069
1070 if (irc != 0) {
1071 _log += "Outlier " + prnRemoved.toAscii() + " "
1072 + QByteArray::number(maxRes, 'f', 3) + "\n";
1073 _QQ = QQsav;
1074 }
1075
1076 return irc;
1077}
1078
1079//
1080////////////////////////////////////////////////////////////////////////////
1081void bncModel::writeNMEAstr(const QString& nmStr) {
1082
1083 unsigned char XOR = 0;
1084 for (int ii = 0; ii < nmStr.length(); ii++) {
1085 XOR ^= (unsigned char) nmStr[ii].toAscii();
1086 }
1087
1088 QString outStr = '$' + nmStr
1089 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
1090
1091 if (_nmeaStream) {
1092 *_nmeaStream << outStr;
1093 _nmeaStream->flush();
1094 }
1095
1096 emit newNMEAstr(outStr.toAscii());
1097}
1098
1099////
1100//////////////////////////////////////////////////////////////////////////////
1101void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
1102 const DiagonalMatrix& PP,
1103 SymmetricMatrix& QQ, ColumnVector& dx) {
1104
1105 int nObs = AA.Nrows();
1106 int nPar = AA.Ncols();
1107
1108 UpperTriangularMatrix SS = Cholesky(QQ).t();
1109
1110 Matrix SA = SS*AA.t();
1111 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
1112 for (int ii = 1; ii <= nObs; ++ii) {
1113 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
1114 }
1115
1116 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
1117 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
1118
1119 UpperTriangularMatrix UU;
1120 QRZ(SRF, UU);
1121
1122 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
1123 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
1124 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
1125
1126 UpperTriangularMatrix SHi = SH_rt.i();
1127
1128 Matrix KT = SHi * YY;
1129 SymmetricMatrix Hi; Hi << SHi * SHi.t();
1130
1131 dx = KT.t() * ll;
1132 QQ << (SS.t() * SS);
1133}
1134
1135// Phase Wind-Up Correction
1136///////////////////////////////////////////////////////////////////////////
1137double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
1138 const ColumnVector& rRec) {
1139
1140 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
1141
1142 // First time - initialize to zero
1143 // -------------------------------
1144 if (!_windUpTime.contains(prn)) {
1145 _windUpSum[prn] = 0.0;
1146 }
1147
1148 // Compute the correction for new time
1149 // -----------------------------------
1150 if (!_windUpTime.contains(prn) || _windUpTime[prn] != Mjd) {
1151 _windUpTime[prn] = Mjd;
1152
1153 // Unit Vector GPS Satellite --> Receiver
1154 // --------------------------------------
1155 ColumnVector rho = rRec - rSat;
1156 rho /= rho.norm_Frobenius();
1157
1158 // GPS Satellite unit Vectors sz, sy, sx
1159 // -------------------------------------
1160 ColumnVector sz = -rSat / rSat.norm_Frobenius();
1161
1162 ColumnVector xSun = Sun(Mjd);
1163 xSun /= xSun.norm_Frobenius();
1164
1165 ColumnVector sy = crossproduct(sz, xSun);
1166 ColumnVector sx = crossproduct(sy, sz);
1167
1168 // Effective Dipole of the GPS Satellite Antenna
1169 // ---------------------------------------------
1170 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
1171 - crossproduct(rho, sy);
1172
1173 // Receiver unit Vectors rx, ry
1174 // ----------------------------
1175 ColumnVector rx(3);
1176 ColumnVector ry(3);
1177
1178 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
1179 double neu[3];
1180
1181 neu[0] = 1.0;
1182 neu[1] = 0.0;
1183 neu[2] = 0.0;
1184 neu2xyz(recEll, neu, rx.data());
1185
1186 neu[0] = 0.0;
1187 neu[1] = -1.0;
1188 neu[2] = 0.0;
1189 neu2xyz(recEll, neu, ry.data());
1190
1191 // Effective Dipole of the Receiver Antenna
1192 // ----------------------------------------
1193 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
1194 + crossproduct(rho, ry);
1195
1196 // Resulting Effect
1197 // ----------------
1198 double alpha = DotProduct(dipSat,dipRec) /
1199 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
1200
1201 if (alpha > 1.0) alpha = 1.0;
1202 if (alpha < -1.0) alpha = -1.0;
1203
1204 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
1205
1206 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
1207 dphi = -dphi;
1208 }
1209
1210 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
1211 }
1212
1213 return _windUpSum[prn];
1214}
1215
1216//
1217///////////////////////////////////////////////////////////////////////////
1218void bncModel::cmpEle(t_satData* satData) {
1219 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
1220 double rho = rr.norm_Frobenius();
1221
1222 double neu[3];
1223 xyz2neu(_ellBanc.data(), rr.data(), neu);
1224
1225 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
1226 if (neu[2] < 0) {
1227 satData->eleSat *= -1.0;
1228 }
1229 satData->azSat = atan2(neu[1], neu[0]);
1230}
1231
1232//
1233///////////////////////////////////////////////////////////////////////////
1234void bncModel::addAmb(t_satData* satData) {
1235 bool found = false;
1236 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1237 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1238 _params[iPar-1]->prn == satData->prn) {
1239 found = true;
1240 break;
1241 }
1242 }
1243 if (!found) {
1244 bncParam* par = new bncParam(bncParam::AMB_L3,
1245 _params.size()+1, satData->prn);
1246 _params.push_back(par);
1247 par->xx = satData->L3 - cmpValue(satData, true);
1248 }
1249}
1250
1251//
1252///////////////////////////////////////////////////////////////////////////
1253void bncModel::addObs(unsigned& iObs, t_satData* satData,
1254 Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP) {
1255
1256 // Code Observations
1257 // -----------------
1258 if (satData->system() != 'R') {
1259 ++iObs;
1260 ll(iObs) = satData->P3 - cmpValue(satData, false);
1261 PP(iObs,iObs) = 1.0 / (_sigP3 * _sigP3);
1262 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1263 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
1264 }
1265 satData->indexCode = iObs;
1266 }
1267
1268 // Phase Observations
1269 // ------------------
1270 if (_usePhase) {
1271 ++iObs;
1272 ll(iObs) = satData->L3 - cmpValue(satData, true);
1273 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
1274 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1275 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1276 _params[iPar-1]->prn == satData->prn) {
1277 ll(iObs) -= _params[iPar-1]->xx;
1278 }
1279 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
1280 }
1281 satData->indexPhase = iObs;
1282 }
1283}
1284
1285//
1286///////////////////////////////////////////////////////////////////////////
1287void bncModel::printRes(const ColumnVector& vv,
1288 ostringstream& str, t_satData* satData) {
1289 if (satData->indexPhase) {
1290 str << _time.timestr(1)
1291 << " RES " << satData->prn.toAscii().data() << " L3 "
1292 << setw(9) << setprecision(4) << vv(satData->indexPhase);
1293 if (satData->indexCode) {
1294 str << " P3 " << setw(9) << setprecision(4) << vv(satData->indexCode);
1295 }
1296 str << endl;
1297 }
1298}
1299
1300//
1301///////////////////////////////////////////////////////////////////////////
1302void bncModel::findMaxRes(const ColumnVector& vv,
1303 const QMap<QString, t_satData*>& satData,
1304 QString& prnCode, double& maxResCode,
1305 QString& prnPhase, double& maxResPhase) {
1306 maxResCode = 0.0;
1307 maxResPhase = 0.0;
1308
1309 QMapIterator<QString, t_satData*> it(satData);
1310 while (it.hasNext()) {
1311 it.next();
1312 t_satData* satData = it.value();
1313 if (satData->indexCode) {
1314 if (fabs(vv(satData->indexCode)) > maxResCode) {
1315 maxResCode = fabs(vv(satData->indexCode));
1316 prnCode = satData->prn;
1317 }
1318 }
1319 if (satData->indexPhase) {
1320 if (fabs(vv(satData->indexPhase)) > maxResPhase) {
1321 maxResPhase = fabs(vv(satData->indexPhase));
1322 prnPhase = satData->prn;
1323 }
1324 }
1325 }
1326}
1327
Note: See TracBrowser for help on using the repository browser.