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

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