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

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