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

Last change on this file since 2790 was 2790, 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 iObs = 0;
299 while (it.hasNext()) {
300 ++iObs;
301 it.next();
302 QString prn = it.key();
303 t_satData* satData = it.value();
304 BB(iObs, 1) = satData->xx(1);
305 BB(iObs, 2) = satData->xx(2);
306 BB(iObs, 3) = satData->xx(3);
307 BB(iObs, 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 ostringstream strA;
694 strA.setf(ios::fixed);
695 ColumnVector vv_code(epoData->sizeGPS());
696 ColumnVector vv_phase(epoData->sizeGPS());
697 ColumnVector vv_glo(epoData->sizeGlo());
698 ColumnVector vv_gal_code(epoData->sizeGal());
699 ColumnVector vv_gal_phase(epoData->sizeGal());
700
701 for (unsigned iobs = 1; iobs <= epoData->sizeGPS(); ++iobs) {
702 if (_usePhase) {
703 vv_code(iobs) = vv(2*iobs-1);
704 vv_phase(iobs) = vv(2*iobs);
705 }
706 else {
707 vv_code(iobs) = vv(iobs);
708 }
709 }
710 if (_useGlonass) {
711 for (unsigned iobs = 1; iobs <= epoData->sizeGlo(); ++iobs) {
712 vv_glo(iobs) = vv(2*epoData->sizeGPS()+iobs);
713 }
714 }
715 if (_useGalileo) {
716 for (unsigned iobs = 1; iobs <= epoData->sizeGal(); ++iobs) {
717 if (_usePhase) {
718 vv_gal_code(iobs) = vv(2*iobs-1);
719 vv_gal_phase(iobs) = vv(2*iobs);
720 }
721 else {
722 vv_gal_code(iobs) = vv(iobs);
723 }
724 }
725 }
726
727 strA << "residuals code " << setw(8) << setprecision(3) << vv_code.t();
728 if (_usePhase) {
729 strA << "residuals phase " << setw(8) << setprecision(3) << vv_phase.t();
730 }
731 if (_useGlonass) {
732 strA << "residuals glo " << setw(8) << setprecision(3) << vv_glo.t();
733 }
734 if (_useGalileo) {
735 strA << "Galileo code " << setw(8) << setprecision(3) << vv_gal_code.t();
736 if (_usePhase) {
737 strA << "Galileo phase " << setw(8) << setprecision(3) << vv_gal_phase.t();
738 }
739 }
740 _log += strA.str().c_str();
741
742 } while (outlierDetection(QQsav, vv, epoData->satDataGPS,
743 epoData->satDataGlo, epoData->satDataGal) != 0);
744
745 // Remember the Epoch-specific Results for the computation of means
746 // ----------------------------------------------------------------
747 pppPos* newPos = new pppPos;
748 newPos->time = epoData->tt;
749
750 // Set Solution Vector
751 // -------------------
752 ostringstream strB;
753 strB.setf(ios::fixed);
754 QVectorIterator<bncParam*> itPar(_params);
755 while (itPar.hasNext()) {
756 bncParam* par = itPar.next();
757 par->xx += dx(par->index);
758
759 if (par->type == bncParam::RECCLK) {
760 strB << "\n clk = " << setw(6) << setprecision(3) << par->xx
761 << " +- " << setw(6) << setprecision(3)
762 << sqrt(_QQ(par->index,par->index));
763 }
764 else if (par->type == bncParam::AMB_L3) {
765 strB << "\n amb " << par->prn.toAscii().data() << " = "
766 << setw(6) << setprecision(3) << par->xx
767 << " +- " << setw(6) << setprecision(3)
768 << sqrt(_QQ(par->index,par->index));
769 }
770 else if (par->type == bncParam::TROPO) {
771 double aprTrp = delay_saast(M_PI/2.0);
772 strB << "\n trp = " << par->prn.toAscii().data()
773 << setw(7) << setprecision(3) << aprTrp << " "
774 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
775 << " +- " << setw(6) << setprecision(3)
776 << sqrt(_QQ(par->index,par->index));
777 newPos->xnt[6] = aprTrp + par->xx;
778 }
779 else if (par->type == bncParam::GALILEO_OFFSET) {
780 strB << "\n offset = " << setw(6) << setprecision(3) << par->xx
781 << " +- " << setw(6) << setprecision(3)
782 << sqrt(_QQ(par->index,par->index));
783 }
784 }
785 strB << '\n';
786 _log += strB.str().c_str();
787 emit newMessage(_log, false);
788
789 // Final Message (both log file and screen)
790 // ----------------------------------------
791 ostringstream strC;
792 strC.setf(ios::fixed);
793 strC << _staID.data() << " PPP "
794 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
795 << setw(14) << setprecision(3) << x() << " +- "
796 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
797 << setw(14) << setprecision(3) << y() << " +- "
798 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
799 << setw(14) << setprecision(3) << z() << " +- "
800 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
801
802 // NEU Output
803 // ----------
804 if (settings.value("pppRefCrdX").toString() != "" &&
805 settings.value("pppRefCrdY").toString() != "" &&
806 settings.value("pppRefCrdZ").toString() != "") {
807
808 double xyzRef[3];
809 xyzRef[0] = settings.value("pppRefCrdX").toDouble();
810 xyzRef[1] = settings.value("pppRefCrdY").toDouble();
811 xyzRef[2] = settings.value("pppRefCrdZ").toDouble();
812
813 newPos->xnt[0] = x() - xyzRef[0];
814 newPos->xnt[1] = y() - xyzRef[1];
815 newPos->xnt[2] = z() - xyzRef[2];
816
817 double ellRef[3];
818 xyz2ell(xyzRef, ellRef);
819 xyz2neu(ellRef, newPos->xnt, &newPos->xnt[3]);
820
821 strC << " NEU "
822 << setw(8) << setprecision(3) << newPos->xnt[3] << " "
823 << setw(8) << setprecision(3) << newPos->xnt[4] << " "
824 << setw(8) << setprecision(3) << newPos->xnt[5];
825
826 }
827
828 emit newMessage(QByteArray(strC.str().c_str()), true);
829
830 if (settings.value("pppAverage").toString() == "") {
831 delete newPos;
832 }
833 else {
834
835 _posAverage.push_back(newPos);
836
837 // Time Span for Average Computation
838 // ---------------------------------
839 double tRangeAverage = settings.value("pppAverage").toDouble() * 60.;
840 if (tRangeAverage < 0) {
841 tRangeAverage = 0;
842 }
843 if (tRangeAverage > 86400) {
844 tRangeAverage = 86400;
845 }
846
847 // Compute the Mean
848 // ----------------
849 ColumnVector mean(7); mean = 0.0;
850
851 QMutableVectorIterator<pppPos*> it(_posAverage);
852 while (it.hasNext()) {
853 pppPos* pp = it.next();
854 if ( (epoData->tt - pp->time) >= tRangeAverage ) {
855 delete pp;
856 it.remove();
857 }
858 else {
859 for (int ii = 0; ii < 7; ++ii) {
860 mean[ii] += pp->xnt[ii];
861 }
862 }
863 }
864
865 int nn = _posAverage.size();
866
867 if (nn > 0) {
868
869 mean /= nn;
870
871 // Compute the Deviation
872 // ---------------------
873 ColumnVector std(7); std = 0.0;
874 QVectorIterator<pppPos*> it2(_posAverage);
875 while (it2.hasNext()) {
876 pppPos* pp = it2.next();
877 for (int ii = 0; ii < 7; ++ii) {
878 std[ii] += (pp->xnt[ii] - mean[ii]) * (pp->xnt[ii] - mean[ii]);
879 }
880 }
881 for (int ii = 0; ii < 7; ++ii) {
882 std[ii] = sqrt(std[ii] / nn);
883 }
884
885 ostringstream strD; strD.setf(ios::fixed);
886 strD << _staID.data() << " AVE-XYZ "
887 << epoData->tt.timestr(1) << " "
888 << setw(13) << setprecision(3) << mean[0] << " +- "
889 << setw(6) << setprecision(3) << std[0] << " "
890 << setw(14) << setprecision(3) << mean[1] << " +- "
891 << setw(6) << setprecision(3) << std[1] << " "
892 << setw(14) << setprecision(3) << mean[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 double vvMaxCodeGPS = 0.0;
987 double vvMaxPhaseGPS = 0.0;
988 double vvMaxPhaseGlo = 0.0;
989 double vvMaxCodeGal = 0.0;
990 double vvMaxPhaseGal = 0.0;
991 QMutableMapIterator<QString, t_satData*> itMaxCodeGPS(satDataGPS);
992 QMutableMapIterator<QString, t_satData*> itMaxPhaseGPS(satDataGPS);
993 QMutableMapIterator<QString, t_satData*> itMaxPhaseGlo(satDataGlo);
994 QMutableMapIterator<QString, t_satData*> itMaxCodeGal(satDataGPS);
995 QMutableMapIterator<QString, t_satData*> itMaxPhaseGal(satDataGPS);
996
997 int ii = 0;
998
999 // GPS code and (optionally) phase residuals
1000 // -----------------------------------------
1001 QMutableMapIterator<QString, t_satData*> itGPS(satDataGPS);
1002 while (itGPS.hasNext()) {
1003 itGPS.next();
1004 ++ii;
1005
1006 if (vvMaxCodeGPS == 0.0 || fabs(vv(ii)) > vvMaxCodeGPS) {
1007 vvMaxCodeGPS = fabs(vv(ii));
1008 itMaxCodeGPS = itGPS;
1009 }
1010
1011 if (_usePhase) {
1012 ++ii;
1013 if (vvMaxPhaseGPS == 0.0 || fabs(vv(ii)) > vvMaxPhaseGPS) {
1014 vvMaxPhaseGPS = fabs(vv(ii));
1015 itMaxPhaseGPS = itGPS;
1016 }
1017 }
1018 }
1019
1020 // Glonass phase residuals
1021 // -----------------------
1022 if (_usePhase) {
1023 QMutableMapIterator<QString, t_satData*> itGlo(satDataGlo);
1024 while (itGlo.hasNext()) {
1025 itGlo.next();
1026 ++ii;
1027 if (vvMaxPhaseGlo == 0.0 || fabs(vv(ii)) > vvMaxPhaseGlo) {
1028 vvMaxPhaseGlo = fabs(vv(ii));
1029 itMaxPhaseGlo = itGlo;
1030 }
1031 }
1032 }
1033
1034 // Galileo code and (optionally) phase residuals
1035 // ---------------------------------------------
1036 QMutableMapIterator<QString, t_satData*> itGal(satDataGal);
1037 while (itGal.hasNext()) {
1038 itGal.next();
1039 ++ii;
1040
1041 if (vvMaxCodeGal == 0.0 || fabs(vv(ii)) > vvMaxCodeGal) {
1042 vvMaxCodeGal = fabs(vv(ii));
1043 itMaxCodeGal = itGal;
1044 }
1045
1046 if (_usePhase) {
1047 ++ii;
1048 if (vvMaxPhaseGal == 0.0 || fabs(vv(ii)) > vvMaxPhaseGal) {
1049 vvMaxPhaseGal = fabs(vv(ii));
1050 itMaxPhaseGal = itGal;
1051 }
1052 }
1053 }
1054
1055 if (vvMaxPhaseGlo > MAXRES_PHASE_GLO) {
1056 QString prn = itMaxPhaseGlo.key();
1057 t_satData* satData = itMaxPhaseGlo.value();
1058 delete satData;
1059 itMaxPhaseGlo.remove();
1060 _QQ = QQsav;
1061
1062 _log += "Outlier Phase " + prn.toAscii() + " "
1063 + QByteArray::number(vvMaxPhaseGlo, 'f', 3) + "\n";
1064
1065 return 1;
1066 }
1067
1068 else if (vvMaxCodeGPS > MAXRES_CODE_GPS) {
1069 QString prn = itMaxCodeGPS.key();
1070 t_satData* satData = itMaxCodeGPS.value();
1071 delete satData;
1072 itMaxCodeGPS.remove();
1073 _QQ = QQsav;
1074
1075 _log += "Outlier Code " + prn.toAscii() + " "
1076 + QByteArray::number(vvMaxCodeGPS, 'f', 3) + "\n";
1077
1078 return 1;
1079 }
1080 else if (vvMaxPhaseGPS > MAXRES_PHASE_GPS) {
1081 QString prn = itMaxPhaseGPS.key();
1082 t_satData* satData = itMaxPhaseGPS.value();
1083 delete satData;
1084 itMaxPhaseGPS.remove();
1085 _QQ = QQsav;
1086
1087 _log += "Outlier Phase " + prn.toAscii() + " "
1088 + QByteArray::number(vvMaxPhaseGPS, 'f', 3) + "\n";
1089
1090 return 1;
1091 }
1092
1093 else if (vvMaxCodeGal > MAXRES_CODE_GAL) {
1094 QString prn = itMaxCodeGal.key();
1095 t_satData* satData = itMaxCodeGal.value();
1096 delete satData;
1097 itMaxCodeGal.remove();
1098 _QQ = QQsav;
1099
1100 _log += "Outlier Code " + prn.toAscii() + " "
1101 + QByteArray::number(vvMaxCodeGal, 'f', 3) + "\n";
1102
1103 return 1;
1104 }
1105 else if (vvMaxPhaseGal > MAXRES_PHASE_GAL) {
1106 QString prn = itMaxPhaseGal.key();
1107 t_satData* satData = itMaxPhaseGal.value();
1108 delete satData;
1109 itMaxPhaseGal.remove();
1110 _QQ = QQsav;
1111
1112 _log += "Outlier Phase " + prn.toAscii() + " "
1113 + QByteArray::number(vvMaxPhaseGal, 'f', 3) + "\n";
1114
1115 return 1;
1116 }
1117
1118 return 0;
1119}
1120
1121//
1122////////////////////////////////////////////////////////////////////////////
1123void bncModel::writeNMEAstr(const QString& nmStr) {
1124
1125 unsigned char XOR = 0;
1126 for (int ii = 0; ii < nmStr.length(); ii++) {
1127 XOR ^= (unsigned char) nmStr[ii].toAscii();
1128 }
1129
1130 QString outStr = '$' + nmStr
1131 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
1132
1133 if (_nmeaStream) {
1134 *_nmeaStream << outStr;
1135 _nmeaStream->flush();
1136 }
1137
1138 emit newNMEAstr(outStr.toAscii());
1139}
1140
1141////
1142//////////////////////////////////////////////////////////////////////////////
1143void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
1144 const DiagonalMatrix& PP,
1145 SymmetricMatrix& QQ, ColumnVector& dx) {
1146
1147 int nObs = AA.Nrows();
1148 int nPar = AA.Ncols();
1149
1150 UpperTriangularMatrix SS = Cholesky(QQ).t();
1151
1152 Matrix SA = SS*AA.t();
1153 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
1154 for (int ii = 1; ii <= nObs; ++ii) {
1155 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
1156 }
1157
1158 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
1159 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
1160
1161 UpperTriangularMatrix UU;
1162 QRZ(SRF, UU);
1163
1164 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
1165 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
1166 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
1167
1168 UpperTriangularMatrix SHi = SH_rt.i();
1169
1170 Matrix KT = SHi * YY;
1171 SymmetricMatrix Hi; Hi << SHi * SHi.t();
1172
1173 dx = KT.t() * ll;
1174 QQ << (SS.t() * SS);
1175}
1176
1177// Phase Wind-Up Correction
1178///////////////////////////////////////////////////////////////////////////
1179double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
1180 const ColumnVector& rRec) {
1181
1182 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
1183
1184 // First time - initialize to zero
1185 // -------------------------------
1186 if (!_windUpTime.contains(prn)) {
1187 _windUpTime[prn] = Mjd;
1188 _windUpSum[prn] = 0.0;
1189 }
1190
1191 // Compute the correction for new time
1192 // -----------------------------------
1193 else if (_windUpTime[prn] != Mjd) {
1194 _windUpTime[prn] = Mjd;
1195
1196 // Unit Vector GPS Satellite --> Receiver
1197 // --------------------------------------
1198 ColumnVector rho = rRec - rSat;
1199 rho /= rho.norm_Frobenius();
1200
1201 // GPS Satellite unit Vectors sz, sy, sx
1202 // -------------------------------------
1203 ColumnVector sz = -rSat / rSat.norm_Frobenius();
1204
1205 ColumnVector xSun = Sun(Mjd);
1206 xSun /= xSun.norm_Frobenius();
1207
1208 ColumnVector sy = crossproduct(sz, xSun);
1209 ColumnVector sx = crossproduct(sy, sz);
1210
1211 // Effective Dipole of the GPS Satellite Antenna
1212 // ---------------------------------------------
1213 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
1214 - crossproduct(rho, sy);
1215
1216 // Receiver unit Vectors rx, ry
1217 // ----------------------------
1218 ColumnVector rx(3);
1219 ColumnVector ry(3);
1220
1221 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
1222 double neu[3];
1223
1224 neu[0] = 1.0;
1225 neu[1] = 0.0;
1226 neu[2] = 0.0;
1227 neu2xyz(recEll, neu, rx.data());
1228
1229 neu[0] = 0.0;
1230 neu[1] = -1.0;
1231 neu[2] = 0.0;
1232 neu2xyz(recEll, neu, ry.data());
1233
1234 // Effective Dipole of the Receiver Antenna
1235 // ----------------------------------------
1236 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
1237 + crossproduct(rho, ry);
1238
1239 // Resulting Effect
1240 // ----------------
1241 double alpha = DotProduct(dipSat,dipRec) /
1242 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
1243
1244 if (alpha > 1.0) alpha = 1.0;
1245 if (alpha < -1.0) alpha = -1.0;
1246
1247 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
1248
1249 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
1250 dphi = -dphi;
1251 }
1252
1253 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
1254 }
1255
1256 return _windUpSum[prn];
1257}
1258
1259//
1260///////////////////////////////////////////////////////////////////////////
1261void bncModel::cmpEle(t_satData* satData) {
1262 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
1263 double rho = rr.norm_Frobenius();
1264
1265 double neu[3];
1266 xyz2neu(_ellBanc.data(), rr.data(), neu);
1267
1268 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
1269 if (neu[2] < 0) {
1270 satData->eleSat *= -1.0;
1271 }
1272 satData->azSat = atan2(neu[1], neu[0]);
1273}
1274
1275//
1276///////////////////////////////////////////////////////////////////////////
1277void bncModel::addAmb(t_satData* satData) {
1278 bool found = false;
1279 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1280 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1281 _params[iPar-1]->prn == satData->prn) {
1282 found = true;
1283 break;
1284 }
1285 }
1286 if (!found) {
1287 bncParam* par = new bncParam(bncParam::AMB_L3,
1288 _params.size()+1, satData->prn);
1289 _params.push_back(par);
1290 par->xx = satData->L3 - cmpValue(satData, true);
1291 }
1292}
1293
1294//
1295///////////////////////////////////////////////////////////////////////////
1296void bncModel::addObs(unsigned& iObs, t_satData* satData,
1297 Matrix& AA, ColumnVector& ll, DiagonalMatrix& PP) {
1298
1299 if (satData->system() != 'R') {
1300 ++iObs;
1301 ll(iObs) = satData->P3 - cmpValue(satData, false);
1302 PP(iObs,iObs) = 1.0 / (_sigP3 * _sigP3);
1303 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1304 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
1305 }
1306 }
1307
1308 if (_usePhase) {
1309 ++iObs;
1310 ll(iObs) = satData->L3 - cmpValue(satData, true);
1311 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
1312 for (int iPar = 1; iPar <= _params.size(); iPar++) {
1313 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
1314 _params[iPar-1]->prn == satData->prn) {
1315 ll(iObs) -= _params[iPar-1]->xx;
1316 }
1317 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
1318 }
1319 }
1320}
Note: See TracBrowser for help on using the repository browser.