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

Last change on this file since 2726 was 2726, checked in by weber, 13 years ago

PPP section restructured

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