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

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