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

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