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

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