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

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