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

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