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

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