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

Last change on this file since 2797 was 2797, checked in by mervart, 13 years ago
File size: 36.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
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] << endl;
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 QString prnCode;
965 QString prnPhase;
966 double maxResCode = 0.0;
967 double maxResPhase = 0.0;
968
969 QString prnRemoved;
970 double maxRes;
971
972 int irc = 0;
973
974 // Check Glonass
975 // -------------
976 if (irc == 0) {
977 findMaxRes(vv,satDataGlo, prnCode, maxResCode, prnPhase, maxResPhase);
978 if (maxResPhase > MAXRES_PHASE_GLO) {
979 satDataGlo.remove(prnPhase);
980 prnRemoved = prnPhase;
981 maxRes = maxResPhase;
982 irc = 1;
983 }
984 }
985
986 // Check Galileo
987 // -------------
988 if (irc == 0) {
989 findMaxRes(vv,satDataGal, prnCode, maxResCode, prnPhase, maxResPhase);
990 if (maxResPhase > MAXRES_PHASE_GAL) {
991 satDataGal.remove(prnPhase);
992 prnRemoved = prnPhase;
993 maxRes = maxResPhase;
994 irc = 1;
995 }
996 else if (maxResCode > MAXRES_CODE_GAL) {
997 satDataGal.remove(prnCode);
998 prnRemoved = prnCode;
999 maxRes = maxResCode;
1000 irc = 1;
1001 }
1002 }
1003
1004 // Check GPS
1005 // ---------
1006 if (irc == 0) {
1007 findMaxRes(vv,satDataGPS, prnCode, maxResCode, prnPhase, maxResPhase);
1008 if (maxResPhase > MAXRES_PHASE_GPS) {
1009 satDataGPS.remove(prnPhase);
1010 prnRemoved = prnPhase;
1011 maxRes = maxResPhase;
1012 irc = 1;
1013 }
1014 else if (maxResCode > MAXRES_CODE_GPS) {
1015 satDataGPS.remove(prnCode);
1016 prnRemoved = prnCode;
1017 maxRes = maxResCode;
1018 irc = 1;
1019 }
1020 }
1021
1022 if (irc != 0) {
1023 _log += "Outlier " + prnRemoved.toAscii() + " "
1024 + QByteArray::number(maxRes, 'f', 3) + "\n";
1025 _QQ = QQsav;
1026 }
1027
1028 return irc;
1029}
1030
1031//
1032////////////////////////////////////////////////////////////////////////////
1033void bncModel::writeNMEAstr(const QString& nmStr) {
1034
1035 unsigned char XOR = 0;
1036 for (int ii = 0; ii < nmStr.length(); ii++) {
1037 XOR ^= (unsigned char) nmStr[ii].toAscii();
1038 }
1039
1040 QString outStr = '$' + nmStr
1041 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
1042
1043 if (_nmeaStream) {
1044 *_nmeaStream << outStr;
1045 _nmeaStream->flush();
1046 }
1047
1048 emit newNMEAstr(outStr.toAscii());
1049}
1050
1051////
1052//////////////////////////////////////////////////////////////////////////////
1053void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
1054 const DiagonalMatrix& PP,
1055 SymmetricMatrix& QQ, ColumnVector& dx) {
1056
1057 int nObs = AA.Nrows();
1058 int nPar = AA.Ncols();
1059
1060 UpperTriangularMatrix SS = Cholesky(QQ).t();
1061
1062 Matrix SA = SS*AA.t();
1063 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
1064 for (int ii = 1; ii <= nObs; ++ii) {
1065 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
1066 }
1067
1068 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
1069 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
1070
1071 UpperTriangularMatrix UU;
1072 QRZ(SRF, UU);
1073
1074 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
1075 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
1076 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
1077
1078 UpperTriangularMatrix SHi = SH_rt.i();
1079
1080 Matrix KT = SHi * YY;
1081 SymmetricMatrix Hi; Hi << SHi * SHi.t();
1082
1083 dx = KT.t() * ll;
1084 QQ << (SS.t() * SS);
1085}
1086
1087// Phase Wind-Up Correction
1088///////////////////////////////////////////////////////////////////////////
1089double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
1090 const ColumnVector& rRec) {
1091
1092 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
1093
1094 // First time - initialize to zero
1095 // -------------------------------
1096 if (!_windUpTime.contains(prn)) {
1097 _windUpTime[prn] = Mjd;
1098 _windUpSum[prn] = 0.0;
1099 }
1100
1101 // Compute the correction for new time
1102 // -----------------------------------
1103 else if (_windUpTime[prn] != Mjd) {
1104 _windUpTime[prn] = Mjd;
1105
1106 // Unit Vector GPS Satellite --> Receiver
1107 // --------------------------------------
1108 ColumnVector rho = rRec - rSat;
1109 rho /= rho.norm_Frobenius();
1110
1111 // GPS Satellite unit Vectors sz, sy, sx
1112 // -------------------------------------
1113 ColumnVector sz = -rSat / rSat.norm_Frobenius();
1114
1115 ColumnVector xSun = Sun(Mjd);
1116 xSun /= xSun.norm_Frobenius();
1117
1118 ColumnVector sy = crossproduct(sz, xSun);
1119 ColumnVector sx = crossproduct(sy, sz);
1120
1121 // Effective Dipole of the GPS Satellite Antenna
1122 // ---------------------------------------------
1123 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
1124 - crossproduct(rho, sy);
1125
1126 // Receiver unit Vectors rx, ry
1127 // ----------------------------
1128 ColumnVector rx(3);
1129 ColumnVector ry(3);
1130
1131 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
1132 double neu[3];
1133
1134 neu[0] = 1.0;
1135 neu[1] = 0.0;
1136 neu[2] = 0.0;
1137 neu2xyz(recEll, neu, rx.data());
1138
1139 neu[0] = 0.0;
1140 neu[1] = -1.0;
1141 neu[2] = 0.0;
1142 neu2xyz(recEll, neu, ry.data());
1143
1144 // Effective Dipole of the Receiver Antenna
1145 // ----------------------------------------
1146 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
1147 + crossproduct(rho, ry);
1148
1149 // Resulting Effect
1150 // ----------------
1151 double alpha = DotProduct(dipSat,dipRec) /
1152 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
1153
1154 if (alpha > 1.0) alpha = 1.0;
1155 if (alpha < -1.0) alpha = -1.0;
1156
1157 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
1158
1159 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
1160 dphi = -dphi;
1161 }
1162
1163 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
1164 }
1165
1166 return _windUpSum[prn];
1167}
1168
1169//
1170///////////////////////////////////////////////////////////////////////////
1171void bncModel::cmpEle(t_satData* satData) {
1172 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
1173 double rho = rr.norm_Frobenius();
1174
1175 double neu[3];
1176 xyz2neu(_ellBanc.data(), rr.data(), neu);
1177
1178 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
1179 if (neu[2] < 0) {
1180 satData->eleSat *= -1.0;
1181 }
1182 satData->azSat = atan2(neu[1], neu[0]);
1183}
1184
1185//
1186///////////////////////////////////////////////////////////////////////////
1187void bncModel::addAmb(t_satData* satData) {
1188 bool found = false;
1189 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1190 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1191 _params[iPar-1]->prn == satData->prn) {
1192 found = true;
1193 break;
1194 }
1195 }
1196 if (!found) {
1197 bncParam* par = new bncParam(bncParam::AMB_L3,
1198 _params.size()+1, satData->prn);
1199 _params.push_back(par);
1200 par->xx = satData->L3 - cmpValue(satData, true);
1201 }
1202}
1203
1204//
1205///////////////////////////////////////////////////////////////////////////
1206void bncModel::addObs(unsigned& iObs, t_satData* satData,
1207 Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP) {
1208
1209 // Code Observations
1210 // -----------------
1211 if (satData->system() != 'R') {
1212 ++iObs;
1213 ll(iObs) = satData->P3 - cmpValue(satData, false);
1214 PP(iObs,iObs) = 1.0 / (_sigP3 * _sigP3);
1215 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1216 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
1217 }
1218 satData->indexCode = iObs;
1219 }
1220
1221 // Phase Observations
1222 // ------------------
1223 if (_usePhase) {
1224 ++iObs;
1225 ll(iObs) = satData->L3 - cmpValue(satData, true);
1226 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
1227 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1228 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1229 _params[iPar-1]->prn == satData->prn) {
1230 ll(iObs) -= _params[iPar-1]->xx;
1231 }
1232 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
1233 }
1234 satData->indexPhase = iObs;
1235 }
1236}
1237
1238//
1239///////////////////////////////////////////////////////////////////////////
1240void bncModel::printRes(const ColumnVector& vv,
1241 ostringstream& str, t_satData* satData) {
1242 if (satData->indexCode) {
1243 str << _time.timestr(1)
1244 << " RES " << satData->prn.toAscii().data() << " P3 "
1245 << setw(9) << setprecision(4) << vv(satData->indexCode);
1246 if (satData->indexPhase) {
1247 str << " L3 " << setw(9) << setprecision(4) << vv(satData->indexPhase);
1248 }
1249 str << endl;
1250 }
1251}
1252
1253//
1254///////////////////////////////////////////////////////////////////////////
1255void bncModel::findMaxRes(const ColumnVector& vv,
1256 const QMap<QString, t_satData*>& satData,
1257 QString& prnCode, double& maxResCode,
1258 QString& prnPhase, double& maxResPhase) {
1259 maxResCode = 0.0;
1260 maxResPhase = 0.0;
1261
1262 QMapIterator<QString, t_satData*> it(satData);
1263 while (it.hasNext()) {
1264 it.next();
1265 t_satData* satData = it.value();
1266 if (satData->indexCode) {
1267 if (fabs(vv(satData->indexCode)) > maxResCode) {
1268 maxResCode = fabs(vv(satData->indexCode));
1269 prnCode = satData->prn;
1270 }
1271 }
1272 if (satData->indexPhase) {
1273 if (fabs(vv(satData->indexPhase)) > maxResPhase) {
1274 maxResPhase = fabs(vv(satData->indexPhase));
1275 prnPhase = satData->prn;
1276 }
1277 }
1278 }
1279}
1280
Note: See TracBrowser for help on using the repository browser.