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

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