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

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