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

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