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

Last change on this file since 3090 was 3034, checked in by mervart, 14 years ago
File size: 37.5 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 _antex = 0;
262 QString antexFileName = settings.value("pppAntex").toString();
263 if (!antexFileName.isEmpty()) {
264 _antex = new bncAntex();
265 if (_antex->readFile(antexFileName) != success) {
266 emit newMessage("wrong ANTEX file", true);
267 delete _antex;
268 _antex = 0;
269 }
270 else {
271 _antennaName = settings.value("pppAntenna").toString();
272 }
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 for (int iPar = 1; iPar <= _params.size(); iPar++) {
286 delete _params[iPar-1];
287 }
288}
289
290// Bancroft Solution
291////////////////////////////////////////////////////////////////////////////
292t_irc bncModel::cmpBancroft(t_epoData* epoData) {
293
294 if (epoData->sizeGPS() < MINOBS) {
295 _log += "bncModel::cmpBancroft: not enough data\n";
296 return failure;
297 }
298
299 Matrix BB(epoData->sizeGPS(), 4);
300
301 QMapIterator<QString, t_satData*> it(epoData->satDataGPS);
302 int iObsBanc = 0;
303 while (it.hasNext()) {
304 ++iObsBanc;
305 it.next();
306 QString prn = it.key();
307 t_satData* satData = it.value();
308 BB(iObsBanc, 1) = satData->xx(1);
309 BB(iObsBanc, 2) = satData->xx(2);
310 BB(iObsBanc, 3) = satData->xx(3);
311 BB(iObsBanc, 4) = satData->P3 + satData->clk;
312 }
313
314 bancroft(BB, _xcBanc);
315
316 // Ellipsoidal Coordinates
317 // ------------------------
318 xyz2ell(_xcBanc.data(), _ellBanc.data());
319
320 // Compute Satellite Elevations
321 // ----------------------------
322 QMutableMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
323 while (iGPS.hasNext()) {
324 iGPS.next();
325 t_satData* satData = iGPS.value();
326 cmpEle(satData);
327 if (satData->eleSat < MINELE_GPS) {
328 delete satData;
329 iGPS.remove();
330 }
331 }
332
333 QMutableMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
334 while (iGlo.hasNext()) {
335 iGlo.next();
336 t_satData* satData = iGlo.value();
337 cmpEle(satData);
338 if (satData->eleSat < MINELE_GLO) {
339 delete satData;
340 iGlo.remove();
341 }
342 }
343
344 QMutableMapIterator<QString, t_satData*> iGal(epoData->satDataGal);
345 while (iGal.hasNext()) {
346 iGal.next();
347 t_satData* satData = iGal.value();
348 cmpEle(satData);
349 if (satData->eleSat < MINELE_GAL) {
350 delete satData;
351 iGal.remove();
352 }
353 }
354
355 return success;
356}
357
358// Computed Value
359////////////////////////////////////////////////////////////////////////////
360double bncModel::cmpValue(t_satData* satData, bool phase) {
361
362 ColumnVector xRec(3);
363 xRec(1) = x();
364 xRec(2) = y();
365 xRec(3) = z();
366
367 double rho0 = (satData->xx - xRec).norm_Frobenius();
368 double dPhi = t_CST::omega * rho0 / t_CST::c;
369
370 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
371 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
372 xRec(3) = z();
373
374 tides(_time, xRec);
375
376 satData->rho = (satData->xx - xRec).norm_Frobenius();
377
378 double tropDelay = delay_saast(satData->eleSat) +
379 trp() / sin(satData->eleSat);
380
381 double wind = 0.0;
382 if (phase) {
383 wind = windUp(satData->prn, satData->xx, xRec) * satData->lambda3;
384 }
385
386 double offset = 0.0;
387 if (satData->prn[0] == 'E') {
388 offset = Galileo_offset();
389 }
390
391 double phaseCenter = 0.0;
392 if (_antex) {
393 bool found;
394 phaseCenter = _antex->pco(_antennaName, satData->eleSat, found);
395 if (!found) {
396 emit newMessage("ANTEX: antenna >"
397 + _antennaName.toAscii() + "< not found", true);
398 }
399 }
400
401 return satData->rho + phaseCenter + clk()
402 + offset - satData->clk + tropDelay + wind;
403}
404
405// Tropospheric Model (Saastamoinen)
406////////////////////////////////////////////////////////////////////////////
407double bncModel::delay_saast(double Ele) {
408
409 double xyz[3];
410 xyz[0] = x();
411 xyz[1] = y();
412 xyz[2] = z();
413 double ell[3];
414 xyz2ell(xyz, ell);
415 double height = ell[2];
416
417 double pp = 1013.25 * pow(1.0 - 2.26e-5 * height, 5.225);
418 double TT = 18.0 - height * 0.0065 + 273.15;
419 double hh = 50.0 * exp(-6.396e-4 * height);
420 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
421
422 double h_km = height / 1000.0;
423
424 if (h_km < 0.0) h_km = 0.0;
425 if (h_km > 5.0) h_km = 5.0;
426 int ii = int(h_km + 1);
427 double href = ii - 1;
428
429 double bCor[6];
430 bCor[0] = 1.156;
431 bCor[1] = 1.006;
432 bCor[2] = 0.874;
433 bCor[3] = 0.757;
434 bCor[4] = 0.654;
435 bCor[5] = 0.563;
436
437 double BB = bCor[ii-1] + (bCor[ii]-bCor[ii-1]) * (h_km - href);
438
439 double zen = M_PI/2.0 - Ele;
440
441 return (0.002277/cos(zen)) * (pp + ((1255.0/TT)+0.05)*ee - BB*(tan(zen)*tan(zen)));
442}
443
444// Prediction Step of the Filter
445////////////////////////////////////////////////////////////////////////////
446void bncModel::predict(t_epoData* epoData) {
447
448 bncSettings settings;
449
450 bool firstCrd = false;
451 if (x() == 0.0 && y() == 0.0 && z() == 0.0) {
452 firstCrd = true;
453 _startTime = epoData->tt;
454 }
455
456 // Use different white noise for Quick-Start mode
457 // ----------------------------------------------
458 double sigCrdP_used = _sigCrdP;
459 if ( _quickStart > 0.0 && _quickStart > (epoData->tt - _startTime) ) {
460 sigCrdP_used = 0.0;
461 }
462
463 // Predict Parameter values, add white noise
464 // -----------------------------------------
465 for (int iPar = 1; iPar <= _params.size(); iPar++) {
466 bncParam* pp = _params[iPar-1];
467
468 // Coordinates
469 // -----------
470 if (pp->type == bncParam::CRD_X) {
471 if (firstCrd) {
472 if (settings.value("pppRefCrdX").toString() != "" &&
473 settings.value("pppRefCrdY").toString() != "" &&
474 settings.value("pppRefCrdZ").toString() != "") {
475 pp->xx = settings.value("pppRefCrdX").toDouble();
476 }
477 else {
478 pp->xx = _xcBanc(1);
479 }
480 }
481 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
482 }
483 else if (pp->type == bncParam::CRD_Y) {
484 if (firstCrd) {
485 if (settings.value("pppRefCrdX").toString() != "" &&
486 settings.value("pppRefCrdY").toString() != "" &&
487 settings.value("pppRefCrdZ").toString() != "") {
488 pp->xx = settings.value("pppRefCrdY").toDouble();
489 }
490 else {
491 pp->xx = _xcBanc(2);
492 }
493 }
494 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
495 }
496 else if (pp->type == bncParam::CRD_Z) {
497 if (firstCrd) {
498 if (settings.value("pppRefCrdX").toString() != "" &&
499 settings.value("pppRefCrdY").toString() != "" &&
500 settings.value("pppRefCrdZ").toString() != "") {
501 pp->xx = settings.value("pppRefCrdZ").toDouble();
502 }
503 else {
504 pp->xx = _xcBanc(3);
505 }
506 }
507 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
508 }
509
510 // Receiver Clocks
511 // ---------------
512 else if (pp->type == bncParam::RECCLK) {
513 pp->xx = _xcBanc(4);
514 for (int jj = 1; jj <= _params.size(); jj++) {
515 _QQ(iPar, jj) = 0.0;
516 }
517 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
518 }
519
520 // Tropospheric Delay
521 // ------------------
522 else if (pp->type == bncParam::TROPO) {
523 _QQ(iPar,iPar) += _sigTrpP * _sigTrpP;
524 }
525
526 // Galileo Offset
527 // --------------
528 else if (pp->type == bncParam::GALILEO_OFFSET) {
529 _QQ(iPar,iPar) += _sigGalileoOffsetP * _sigGalileoOffsetP;
530 }
531 }
532
533 // Add New Ambiguities if necessary
534 // --------------------------------
535 if (_usePhase) {
536
537 // Make a copy of QQ and xx, set parameter indices
538 // -----------------------------------------------
539 SymmetricMatrix QQ_old = _QQ;
540
541 for (int iPar = 1; iPar <= _params.size(); iPar++) {
542 _params[iPar-1]->index_old = _params[iPar-1]->index;
543 _params[iPar-1]->index = 0;
544 }
545
546 // Remove Ambiguity Parameters without observations
547 // ------------------------------------------------
548 int iPar = 0;
549 QMutableVectorIterator<bncParam*> it(_params);
550 while (it.hasNext()) {
551 bncParam* par = it.next();
552 bool removed = false;
553 if (par->type == bncParam::AMB_L3) {
554 if (epoData->satDataGPS.find(par->prn) == epoData->satDataGPS.end() &&
555 epoData->satDataGlo.find(par->prn) == epoData->satDataGlo.end() &&
556 epoData->satDataGal.find(par->prn) == epoData->satDataGal.end() ) {
557 removed = true;
558 delete par;
559 it.remove();
560 }
561 }
562 if (! removed) {
563 ++iPar;
564 par->index = iPar;
565 }
566 }
567
568 // Add new ambiguity parameters
569 // ----------------------------
570 QMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
571 while (iGPS.hasNext()) {
572 iGPS.next();
573 t_satData* satData = iGPS.value();
574 addAmb(satData);
575 }
576
577 QMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
578 while (iGlo.hasNext()) {
579 iGlo.next();
580 t_satData* satData = iGlo.value();
581 addAmb(satData);
582 }
583
584 QMapIterator<QString, t_satData*> iGal(epoData->satDataGal);
585 while (iGal.hasNext()) {
586 iGal.next();
587 t_satData* satData = iGal.value();
588 addAmb(satData);
589 }
590
591 int nPar = _params.size();
592 _QQ.ReSize(nPar); _QQ = 0.0;
593 for (int i1 = 1; i1 <= nPar; i1++) {
594 bncParam* p1 = _params[i1-1];
595 if (p1->index_old != 0) {
596 _QQ(p1->index, p1->index) = QQ_old(p1->index_old, p1->index_old);
597 for (int i2 = 1; i2 <= nPar; i2++) {
598 bncParam* p2 = _params[i2-1];
599 if (p2->index_old != 0) {
600 _QQ(p1->index, p2->index) = QQ_old(p1->index_old, p2->index_old);
601 }
602 }
603 }
604 }
605
606 for (int ii = 1; ii <= nPar; ii++) {
607 bncParam* par = _params[ii-1];
608 if (par->index_old == 0) {
609 _QQ(par->index, par->index) = _sigAmb0 * _sigAmb0;
610 }
611 par->index_old = par->index;
612 }
613 }
614}
615
616// Update Step of the Filter (currently just a single-epoch solution)
617////////////////////////////////////////////////////////////////////////////
618t_irc bncModel::update(t_epoData* epoData) {
619
620 bncSettings settings;
621
622 _log.clear();
623
624 _time = epoData->tt;
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 return success;
979}
980
981// Outlier Detection
982////////////////////////////////////////////////////////////////////////////
983int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
984 const ColumnVector& vv,
985 QMap<QString, t_satData*>& satDataGPS,
986 QMap<QString, t_satData*>& satDataGlo,
987 QMap<QString, t_satData*>& satDataGal) {
988
989 QString prnCode;
990 QString prnPhase;
991 double maxResCode = 0.0;
992 double maxResPhase = 0.0;
993
994 QString prnRemoved;
995 double maxRes;
996
997 int irc = 0;
998
999 // Check Glonass
1000 // -------------
1001 if (irc == 0) {
1002 findMaxRes(vv,satDataGlo, prnCode, maxResCode, prnPhase, maxResPhase);
1003 if (maxResPhase > MAXRES_PHASE_GLO) {
1004 satDataGlo.remove(prnPhase);
1005 prnRemoved = prnPhase;
1006 maxRes = maxResPhase;
1007 irc = 1;
1008 }
1009 }
1010
1011 // Check Galileo
1012 // -------------
1013 if (irc == 0) {
1014 findMaxRes(vv,satDataGal, prnCode, maxResCode, prnPhase, maxResPhase);
1015 if (maxResPhase > MAXRES_PHASE_GAL) {
1016 satDataGal.remove(prnPhase);
1017 prnRemoved = prnPhase;
1018 maxRes = maxResPhase;
1019 irc = 1;
1020 }
1021 else if (maxResCode > MAXRES_CODE_GAL) {
1022 satDataGal.remove(prnCode);
1023 prnRemoved = prnCode;
1024 maxRes = maxResCode;
1025 irc = 1;
1026 }
1027 }
1028
1029 // Check GPS
1030 // ---------
1031 if (irc == 0) {
1032 findMaxRes(vv,satDataGPS, prnCode, maxResCode, prnPhase, maxResPhase);
1033 if (maxResPhase > MAXRES_PHASE_GPS) {
1034 satDataGPS.remove(prnPhase);
1035 prnRemoved = prnPhase;
1036 maxRes = maxResPhase;
1037 irc = 1;
1038 }
1039 else if (maxResCode > MAXRES_CODE_GPS) {
1040 satDataGPS.remove(prnCode);
1041 prnRemoved = prnCode;
1042 maxRes = maxResCode;
1043 irc = 1;
1044 }
1045 }
1046
1047 if (irc != 0) {
1048 _log += "Outlier " + prnRemoved.toAscii() + " "
1049 + QByteArray::number(maxRes, 'f', 3) + "\n";
1050 _QQ = QQsav;
1051 }
1052
1053 return irc;
1054}
1055
1056//
1057////////////////////////////////////////////////////////////////////////////
1058void bncModel::writeNMEAstr(const QString& nmStr) {
1059
1060 unsigned char XOR = 0;
1061 for (int ii = 0; ii < nmStr.length(); ii++) {
1062 XOR ^= (unsigned char) nmStr[ii].toAscii();
1063 }
1064
1065 QString outStr = '$' + nmStr
1066 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
1067
1068 if (_nmeaStream) {
1069 *_nmeaStream << outStr;
1070 _nmeaStream->flush();
1071 }
1072
1073 emit newNMEAstr(outStr.toAscii());
1074}
1075
1076////
1077//////////////////////////////////////////////////////////////////////////////
1078void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
1079 const DiagonalMatrix& PP,
1080 SymmetricMatrix& QQ, ColumnVector& dx) {
1081
1082 int nObs = AA.Nrows();
1083 int nPar = AA.Ncols();
1084
1085 UpperTriangularMatrix SS = Cholesky(QQ).t();
1086
1087 Matrix SA = SS*AA.t();
1088 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
1089 for (int ii = 1; ii <= nObs; ++ii) {
1090 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
1091 }
1092
1093 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
1094 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
1095
1096 UpperTriangularMatrix UU;
1097 QRZ(SRF, UU);
1098
1099 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
1100 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
1101 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
1102
1103 UpperTriangularMatrix SHi = SH_rt.i();
1104
1105 Matrix KT = SHi * YY;
1106 SymmetricMatrix Hi; Hi << SHi * SHi.t();
1107
1108 dx = KT.t() * ll;
1109 QQ << (SS.t() * SS);
1110}
1111
1112// Phase Wind-Up Correction
1113///////////////////////////////////////////////////////////////////////////
1114double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
1115 const ColumnVector& rRec) {
1116
1117 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
1118
1119 // First time - initialize to zero
1120 // -------------------------------
1121 if (!_windUpTime.contains(prn)) {
1122 _windUpSum[prn] = 0.0;
1123 }
1124
1125 // Compute the correction for new time
1126 // -----------------------------------
1127 if (!_windUpTime.contains(prn) || _windUpTime[prn] != Mjd) {
1128 _windUpTime[prn] = Mjd;
1129
1130 // Unit Vector GPS Satellite --> Receiver
1131 // --------------------------------------
1132 ColumnVector rho = rRec - rSat;
1133 rho /= rho.norm_Frobenius();
1134
1135 // GPS Satellite unit Vectors sz, sy, sx
1136 // -------------------------------------
1137 ColumnVector sz = -rSat / rSat.norm_Frobenius();
1138
1139 ColumnVector xSun = Sun(Mjd);
1140 xSun /= xSun.norm_Frobenius();
1141
1142 ColumnVector sy = crossproduct(sz, xSun);
1143 ColumnVector sx = crossproduct(sy, sz);
1144
1145 // Effective Dipole of the GPS Satellite Antenna
1146 // ---------------------------------------------
1147 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
1148 - crossproduct(rho, sy);
1149
1150 // Receiver unit Vectors rx, ry
1151 // ----------------------------
1152 ColumnVector rx(3);
1153 ColumnVector ry(3);
1154
1155 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
1156 double neu[3];
1157
1158 neu[0] = 1.0;
1159 neu[1] = 0.0;
1160 neu[2] = 0.0;
1161 neu2xyz(recEll, neu, rx.data());
1162
1163 neu[0] = 0.0;
1164 neu[1] = -1.0;
1165 neu[2] = 0.0;
1166 neu2xyz(recEll, neu, ry.data());
1167
1168 // Effective Dipole of the Receiver Antenna
1169 // ----------------------------------------
1170 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
1171 + crossproduct(rho, ry);
1172
1173 // Resulting Effect
1174 // ----------------
1175 double alpha = DotProduct(dipSat,dipRec) /
1176 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
1177
1178 if (alpha > 1.0) alpha = 1.0;
1179 if (alpha < -1.0) alpha = -1.0;
1180
1181 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
1182
1183 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
1184 dphi = -dphi;
1185 }
1186
1187 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
1188 }
1189
1190 return _windUpSum[prn];
1191}
1192
1193//
1194///////////////////////////////////////////////////////////////////////////
1195void bncModel::cmpEle(t_satData* satData) {
1196 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
1197 double rho = rr.norm_Frobenius();
1198
1199 double neu[3];
1200 xyz2neu(_ellBanc.data(), rr.data(), neu);
1201
1202 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
1203 if (neu[2] < 0) {
1204 satData->eleSat *= -1.0;
1205 }
1206 satData->azSat = atan2(neu[1], neu[0]);
1207}
1208
1209//
1210///////////////////////////////////////////////////////////////////////////
1211void bncModel::addAmb(t_satData* satData) {
1212 bool found = false;
1213 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1214 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1215 _params[iPar-1]->prn == satData->prn) {
1216 found = true;
1217 break;
1218 }
1219 }
1220 if (!found) {
1221 bncParam* par = new bncParam(bncParam::AMB_L3,
1222 _params.size()+1, satData->prn);
1223 _params.push_back(par);
1224 par->xx = satData->L3 - cmpValue(satData, true);
1225 }
1226}
1227
1228//
1229///////////////////////////////////////////////////////////////////////////
1230void bncModel::addObs(unsigned& iObs, t_satData* satData,
1231 Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP) {
1232
1233 // Code Observations
1234 // -----------------
1235 if (satData->system() != 'R') {
1236 ++iObs;
1237 ll(iObs) = satData->P3 - cmpValue(satData, false);
1238 PP(iObs,iObs) = 1.0 / (_sigP3 * _sigP3);
1239 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1240 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
1241 }
1242 satData->indexCode = iObs;
1243 }
1244
1245 // Phase Observations
1246 // ------------------
1247 if (_usePhase) {
1248 ++iObs;
1249 ll(iObs) = satData->L3 - cmpValue(satData, true);
1250 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
1251 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1252 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1253 _params[iPar-1]->prn == satData->prn) {
1254 ll(iObs) -= _params[iPar-1]->xx;
1255 }
1256 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
1257 }
1258 satData->indexPhase = iObs;
1259 }
1260}
1261
1262//
1263///////////////////////////////////////////////////////////////////////////
1264void bncModel::printRes(const ColumnVector& vv,
1265 ostringstream& str, t_satData* satData) {
1266 if (satData->indexPhase) {
1267 str << _time.timestr(1)
1268 << " RES " << satData->prn.toAscii().data() << " L3 "
1269 << setw(9) << setprecision(4) << vv(satData->indexPhase);
1270 if (satData->indexCode) {
1271 str << " P3 " << setw(9) << setprecision(4) << vv(satData->indexCode);
1272 }
1273 str << endl;
1274 }
1275}
1276
1277//
1278///////////////////////////////////////////////////////////////////////////
1279void bncModel::findMaxRes(const ColumnVector& vv,
1280 const QMap<QString, t_satData*>& satData,
1281 QString& prnCode, double& maxResCode,
1282 QString& prnPhase, double& maxResPhase) {
1283 maxResCode = 0.0;
1284 maxResPhase = 0.0;
1285
1286 QMapIterator<QString, t_satData*> it(satData);
1287 while (it.hasNext()) {
1288 it.next();
1289 t_satData* satData = it.value();
1290 if (satData->indexCode) {
1291 if (fabs(vv(satData->indexCode)) > maxResCode) {
1292 maxResCode = fabs(vv(satData->indexCode));
1293 prnCode = satData->prn;
1294 }
1295 }
1296 if (satData->indexPhase) {
1297 if (fabs(vv(satData->indexPhase)) > maxResPhase) {
1298 maxResPhase = fabs(vv(satData->indexPhase));
1299 prnPhase = satData->prn;
1300 }
1301 }
1302 }
1303}
1304
Note: See TracBrowser for help on using the repository browser.