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

Last change on this file since 2745 was 2745, checked in by weber, 16 years ago

Quick-Start period to be entered in seconds (not minutes)

File size: 34.3 KB
RevLine 
[2057]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>
[2063]42#include <cmath>
[2060]43#include <newmatio.h>
[2113]44#include <sstream>
[2057]45
46#include "bncmodel.h"
[2113]47#include "bncapp.h"
[2058]48#include "bncpppclient.h"
49#include "bancroft.h"
[2063]50#include "bncutils.h"
[2077]51#include "bncsettings.h"
[2580]52#include "bnctides.h"
[2057]53
54using namespace std;
55
[2243]56const unsigned MINOBS = 4;
[2287]57const double MINELE_GPS = 10.0 * M_PI / 180.0;
58const double MINELE_GLO = 10.0 * M_PI / 180.0;
[2243]59const double MAXRES_CODE_GPS = 10.0;
60const double MAXRES_PHASE_GPS = 0.10;
[2539]61const double MAXRES_PHASE_GLO = 0.05;
[2070]62
[2057]63// Constructor
64////////////////////////////////////////////////////////////////////////////
[2080]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;
[2473]72
[2057]73}
74
75// Destructor
76////////////////////////////////////////////////////////////////////////////
77bncParam::~bncParam() {
78}
79
[2060]80// Partial
81////////////////////////////////////////////////////////////////////////////
[2239]82double bncParam::partial(t_satData* satData, bool phase) {
83
84 // Coordinates
85 // -----------
[2060]86 if (type == CRD_X) {
[2109]87 return (xx - satData->xx(1)) / satData->rho;
[2060]88 }
89 else if (type == CRD_Y) {
[2109]90 return (xx - satData->xx(2)) / satData->rho;
[2060]91 }
92 else if (type == CRD_Z) {
[2109]93 return (xx - satData->xx(3)) / satData->rho;
[2060]94 }
[2239]95
96 // Receiver Clocks
97 // ---------------
[2265]98 else if (type == RECCLK) {
99 return 1.0;
[2060]100 }
[2239]101
102 // Troposphere
103 // -----------
[2084]104 else if (type == TROPO) {
105 return 1.0 / sin(satData->eleSat);
106 }
[2239]107
108 // Ambiguities
109 // -----------
[2080]110 else if (type == AMB_L3) {
[2239]111 if (phase && satData->prn == prn) {
[2080]112 return 1.0;
113 }
114 else {
115 return 0.0;
116 }
117 }
[2239]118
119 // Default return
120 // --------------
[2060]121 return 0.0;
122}
123
[2058]124// Constructor
125////////////////////////////////////////////////////////////////////////////
[2113]126bncModel::bncModel(QByteArray staID) {
[2084]127
[2113]128 _staID = staID;
129
[2648]130 _startTime = QDateTime::currentDateTime();
131
132 bncSettings settings;
133
[2720]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()) {
[2741]142 _sigL3 = settings.value("pppSigmaPhase").toDouble();
[2720]143 }
[2648]144
[2720]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;
[2648]165
[2720]166 // Quick-Start Mode
167 // ----------------
168 _quickStart = 0;
[2726]169 if (settings.value("pppRefCrdX").toString() != "" &&
170 settings.value("pppRefCrdY").toString() != "" &&
171 settings.value("pppRefCrdZ").toString() != "" &&
[2720]172 !settings.value("pppQuickStart").toString().isEmpty()) {
[2745]173 _quickStart = settings.value("pppQuickStart").toDouble();
[2720]174 }
175
[2113]176 connect(this, SIGNAL(newMessage(QByteArray,bool)),
[2548]177 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
[2113]178
[2084]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
[2265]192 if (_usePhase &&
193 Qt::CheckState(settings.value("pppGLONASS").toInt()) == Qt::Checked) {
[2239]194 _useGlonass = true;
195 }
196 else {
197 _useGlonass = false;
198 }
199
200 int nextPar = 0;
[2265]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, ""));
[2084]205 if (_estTropo) {
[2239]206 _params.push_back(new bncParam(bncParam::TROPO, ++nextPar, ""));
[2084]207 }
[2073]208
209 unsigned nPar = _params.size();
[2084]210
[2073]211 _QQ.ReSize(nPar);
[2239]212
[2073]213 _QQ = 0.0;
214
[2239]215 for (int iPar = 1; iPar <= _params.size(); iPar++) {
216 bncParam* pp = _params[iPar-1];
217 if (pp->isCrd()) {
[2720]218 _QQ(iPar,iPar) = _sigCrd0 * _sigCrd0;
[2239]219 }
[2265]220 else if (pp->type == bncParam::RECCLK) {
[2720]221 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
[2239]222 }
223 else if (pp->type == bncParam::TROPO) {
[2720]224 _QQ(iPar,iPar) = _sigTrp0 * _sigTrp0;
[2239]225 }
[2077]226 }
[2125]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 }
[2058]247}
248
249// Destructor
250////////////////////////////////////////////////////////////////////////////
251bncModel::~bncModel() {
[2126]252 delete _nmeaStream;
253 delete _nmeaFile;
[2648]254 for (int ii = 0; ii < _posAverage.size(); ++ii) {
255 delete _posAverage[ii];
256 }
[2058]257}
258
259// Bancroft Solution
260////////////////////////////////////////////////////////////////////////////
261t_irc bncModel::cmpBancroft(t_epoData* epoData) {
262
[2231]263 if (epoData->sizeGPS() < MINOBS) {
[2547]264 _log += "bncModel::cmpBancroft: not enough data\n";
[2058]265 return failure;
266 }
267
[2231]268 Matrix BB(epoData->sizeGPS(), 4);
[2058]269
[2231]270 QMapIterator<QString, t_satData*> it(epoData->satDataGPS);
[2058]271 int iObs = 0;
272 while (it.hasNext()) {
[2060]273 ++iObs;
[2058]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
[2064]285 // Ellipsoidal Coordinates
286 // ------------------------
287 xyz2ell(_xcBanc.data(), _ellBanc.data());
[2063]288
[2064]289 // Compute Satellite Elevations
290 // ----------------------------
[2231]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();
[2063]296
[2109]297 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
298 double rho = rr.norm_Frobenius();
[2064]299
300 double neu[3];
[2109]301 xyz2neu(_ellBanc.data(), rr.data(), neu);
[2064]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]);
[2070]308
[2287]309 if (satData->eleSat < MINELE_GPS) {
[2070]310 delete satData;
[2231]311 iGPS.remove();
[2070]312 }
[2064]313 }
314
[2231]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
[2287]333 if (satData->eleSat < MINELE_GLO) {
[2231]334 delete satData;
335 iGlo.remove();
336 }
337 }
338
[2058]339 return success;
340}
[2060]341
342// Computed Value
343////////////////////////////////////////////////////////////////////////////
[2583]344double bncModel::cmpValue(t_satData* satData, bool phase) {
[2060]345
[2073]346 ColumnVector xRec(3);
347 xRec(1) = x();
348 xRec(2) = y();
349 xRec(3) = z();
[2060]350
[2073]351 double rho0 = (satData->xx - xRec).norm_Frobenius();
[2060]352 double dPhi = t_CST::omega * rho0 / t_CST::c;
353
[2073]354 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
355 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
356 xRec(3) = z();
357
[2580]358 tides(_time, xRec);
359
[2060]360 satData->rho = (satData->xx - xRec).norm_Frobenius();
361
[2084]362 double tropDelay = delay_saast(satData->eleSat) +
363 trp() / sin(satData->eleSat);
[2060]364
[2583]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;
[2060]371}
372
[2063]373// Tropospheric Model (Saastamoinen)
374////////////////////////////////////////////////////////////////////////////
[2065]375double bncModel::delay_saast(double Ele) {
[2063]376
[2064]377 double height = _ellBanc(3);
[2063]378
[2064]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);
[2063]382 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
383
[2064]384 double h_km = height / 1000.0;
[2063]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
[2073]406// Prediction Step of the Filter
407////////////////////////////////////////////////////////////////////////////
[2080]408void bncModel::predict(t_epoData* epoData) {
[2073]409
[2648]410 bncSettings settings;
[2244]411
[2648]412 bool firstCrd = false;
413 if (x() == 0.0 && y() == 0.0 && z() == 0.0) {
414 firstCrd = true;
415 }
416
[2726]417 // Use different white noise for Quick-Start mode
[2720]418 // ----------------------------------------------
419 double sigCrdP_used = _sigCrdP;
420 if ( _quickStart > 0.0 &&
421 _quickStart > _startTime.secsTo(QDateTime::currentDateTime()) ) {
422 sigCrdP_used = 0.0;
[2648]423 }
424
[2244]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) {
[2720]433 if (firstCrd) {
[2726]434 if (settings.value("pppRefCrdX").toString() != "" &&
435 settings.value("pppRefCrdY").toString() != "" &&
436 settings.value("pppRefCrdZ").toString() != "") {
[2648]437 pp->xx = settings.value("pppRefCrdX").toDouble();
438 }
439 else {
440 pp->xx = _xcBanc(1);
441 }
[2244]442 }
[2720]443 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
[2244]444 }
445 else if (pp->type == bncParam::CRD_Y) {
[2720]446 if (firstCrd) {
[2726]447 if (settings.value("pppRefCrdX").toString() != "" &&
448 settings.value("pppRefCrdY").toString() != "" &&
449 settings.value("pppRefCrdZ").toString() != "") {
[2648]450 pp->xx = settings.value("pppRefCrdY").toDouble();
451 }
452 else {
453 pp->xx = _xcBanc(2);
454 }
[2244]455 }
[2720]456 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
[2244]457 }
458 else if (pp->type == bncParam::CRD_Z) {
[2720]459 if (firstCrd) {
[2726]460 if (settings.value("pppRefCrdX").toString() != "" &&
461 settings.value("pppRefCrdY").toString() != "" &&
462 settings.value("pppRefCrdZ").toString() != "") {
[2648]463 pp->xx = settings.value("pppRefCrdZ").toDouble();
464 }
465 else {
466 pp->xx = _xcBanc(3);
467 }
[2244]468 }
[2720]469 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
[2244]470 }
471
472 // Receiver Clocks
473 // ---------------
[2265]474 else if (pp->type == bncParam::RECCLK) {
[2244]475 pp->xx = _xcBanc(4);
476 for (int jj = 1; jj <= _params.size(); jj++) {
477 _QQ(iPar, jj) = 0.0;
478 }
[2720]479 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
[2244]480 }
481
482 // Tropospheric Delay
483 // ------------------
484 else if (pp->type == bncParam::TROPO) {
[2720]485 _QQ(iPar,iPar) += _sigTrpP * _sigTrpP;
[2244]486 }
487 }
488
489 // Add New Ambiguities if necessary
490 // --------------------------------
[2083]491 if (_usePhase) {
[2080]492
[2083]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) {
[2231]510 if (epoData->satDataGPS.find(par->prn) == epoData->satDataGPS.end() &&
511 epoData->satDataGlo.find(par->prn) == epoData->satDataGlo.end() ) {
[2083]512 removed = true;
513 delete par;
514 it.remove();
515 }
[2080]516 }
[2083]517 if (! removed) {
518 ++iPar;
519 par->index = iPar;
520 }
[2080]521 }
[2083]522
523 // Add new ambiguity parameters
524 // ----------------------------
[2231]525 QMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
526 while (iGPS.hasNext()) {
527 iGPS.next();
[2233]528 QString prn = iGPS.key();
[2244]529 t_satData* satData = iGPS.value();
[2231]530 bool found = false;
[2083]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 }
[2080]537 }
[2083]538 if (!found) {
539 bncParam* par = new bncParam(bncParam::AMB_L3, _params.size()+1, prn);
540 _params.push_back(par);
[2583]541 par->xx = satData->L3 - cmpValue(satData, true);
[2083]542 }
[2080]543 }
[2231]544
545 QMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
546 while (iGlo.hasNext()) {
547 iGlo.next();
[2233]548 QString prn = iGlo.key();
549 t_satData* satData = iGlo.value();
[2231]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);
[2583]561 par->xx = satData->L3 - cmpValue(satData, true);
[2231]562 }
563 }
[2083]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 }
[2080]576 }
577 }
578 }
[2083]579
580 for (int ii = 1; ii <= nPar; ii++) {
581 bncParam* par = _params[ii-1];
582 if (par->index_old == 0) {
[2720]583 _QQ(par->index, par->index) = _sigAmb0 * _sigAmb0;
[2083]584 }
585 par->index_old = par->index;
[2080]586 }
587 }
[2073]588}
589
[2060]590// Update Step of the Filter (currently just a single-epoch solution)
591////////////////////////////////////////////////////////////////////////////
592t_irc bncModel::update(t_epoData* epoData) {
593
[2473]594 bncSettings settings;
595
[2248]596 _log.clear();
[2124]597
[2143]598 _time = epoData->tt;
599
[2547]600 _log += "Single Point Positioning of Epoch "
601 + QByteArray(_time.timestr(1).c_str()) +
602 "\n--------------------------------------------------------------\n";
603
[2112]604 SymmetricMatrix QQsav;
[2109]605 ColumnVector dx;
[2112]606 ColumnVector vv;
[2073]607
[2113]608 // Loop over all outliers
609 // ----------------------
[2108]610 do {
611
612 // Bancroft Solution
613 // -----------------
614 if (cmpBancroft(epoData) != success) {
[2124]615 emit newMessage(_log, false);
[2108]616 return failure;
617 }
[2080]618
[2108]619 // Status Prediction
620 // -----------------
621 predict(epoData);
622
[2109]623 // Create First-Design Matrix
624 // --------------------------
[2108]625 unsigned nPar = _params.size();
[2231]626 unsigned nObs = 0;
627 if (_usePhase) {
[2238]628 nObs = 2 * epoData->sizeGPS() + epoData->sizeGlo();
[2231]629 }
630 else {
631 nObs = epoData->sizeGPS(); // Glonass pseudoranges are not used
632 }
[2108]633
[2540]634 if (nObs < nPar) {
[2547]635 _log += "bncModel::update: nObs < nPar\n";
[2540]636 emit newMessage(_log, false);
637 return failure;
638 }
639
[2108]640 Matrix AA(nObs, nPar); // first design matrix
641 ColumnVector ll(nObs); // tems observed-computed
[2283]642 DiagonalMatrix PP(nObs); PP = 0.0;
[2108]643
644 unsigned iObs = 0;
[2231]645
646 // GPS code and (optionally) phase observations
647 // --------------------------------------------
648 QMapIterator<QString, t_satData*> itGPS(epoData->satDataGPS);
649 while (itGPS.hasNext()) {
[2083]650 ++iObs;
[2231]651 itGPS.next();
652 QString prn = itGPS.key();
653 t_satData* satData = itGPS.value();
[2108]654
[2583]655 ll(iObs) = satData->P3 - cmpValue(satData, false);
[2742]656 PP(iObs,iObs) = 1.0 / (_sigP3 * _sigP3);
[2083]657 for (int iPar = 1; iPar <= _params.size(); iPar++) {
[2239]658 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
[2083]659 }
[2108]660
661 if (_usePhase) {
662 ++iObs;
[2583]663 ll(iObs) = satData->L3 - cmpValue(satData, true);
[2720]664 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
[2108]665 for (int iPar = 1; iPar <= _params.size(); iPar++) {
666 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
667 _params[iPar-1]->prn == prn) {
[2109]668 ll(iObs) -= _params[iPar-1]->xx;
[2108]669 }
[2239]670 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
[2108]671 }
672 }
[2080]673 }
[2231]674
675 // Glonass phase observations
676 // --------------------------
677 if (_usePhase) {
678 QMapIterator<QString, t_satData*> itGlo(epoData->satDataGlo);
679 while (itGlo.hasNext()) {
[2238]680 ++iObs;
[2231]681 itGlo.next();
682 QString prn = itGlo.key();
683 t_satData* satData = itGlo.value();
[2238]684
[2583]685 ll(iObs) = satData->L3 - cmpValue(satData, true);
[2720]686 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
[2238]687 for (int iPar = 1; iPar <= _params.size(); iPar++) {
688 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
689 _params[iPar-1]->prn == prn) {
690 ll(iObs) -= _params[iPar-1]->xx;
691 }
[2239]692 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
[2238]693 }
[2231]694 }
695 }
696
[2112]697 // Compute Filter Update
[2108]698 // ---------------------
[2112]699 QQsav = _QQ;
700
[2283]701 kalman(AA, ll, PP, _QQ, dx);
702
703 vv = ll - AA * dx;
704
[2547]705 ostringstream strA;
706 strA.setf(ios::fixed);
[2265]707 ColumnVector vv_code(epoData->sizeGPS());
708 ColumnVector vv_phase(epoData->sizeGPS());
709 ColumnVector vv_glo(epoData->sizeGlo());
[2245]710
[2265]711 for (unsigned iobs = 1; iobs <= epoData->sizeGPS(); ++iobs) {
712 if (_usePhase) {
[2246]713 vv_code(iobs) = vv(2*iobs-1);
714 vv_phase(iobs) = vv(2*iobs);
715 }
[2265]716 else {
717 vv_code(iobs) = vv(iobs);
718 }
719 }
720 if (_useGlonass) {
[2246]721 for (unsigned iobs = 1; iobs <= epoData->sizeGlo(); ++iobs) {
722 vv_glo(iobs) = vv(2*epoData->sizeGPS()+iobs);
723 }
[2265]724 }
[2246]725
[2547]726 strA << "residuals code " << setw(8) << setprecision(3) << vv_code.t();
[2265]727 if (_usePhase) {
[2547]728 strA << "residuals phase " << setw(8) << setprecision(3) << vv_phase.t();
[2265]729 }
730 if (_useGlonass) {
[2547]731 strA << "residuals glo " << setw(8) << setprecision(3) << vv_glo.t();
[2246]732 }
[2547]733 _log += strA.str().c_str();
[2246]734
[2231]735 } while (outlierDetection(QQsav, vv, epoData->satDataGPS,
[2233]736 epoData->satDataGlo) != 0);
[2111]737
[2670]738 // Remember the Epoch-specific Results for the computation of means
739 // ----------------------------------------------------------------
740 pppPos* newPos = new pppPos;
741 newPos->time = epoData->tt;
742
[2109]743 // Set Solution Vector
744 // -------------------
[2265]745 ostringstream strB;
746 strB.setf(ios::fixed);
[2073]747 QVectorIterator<bncParam*> itPar(_params);
[2060]748 while (itPar.hasNext()) {
749 bncParam* par = itPar.next();
[2109]750 par->xx += dx(par->index);
[2265]751
752 if (par->type == bncParam::RECCLK) {
[2547]753 strB << "\n clk = " << setw(6) << setprecision(3) << par->xx
[2124]754 << " +- " << setw(6) << setprecision(3)
755 << sqrt(_QQ(par->index,par->index));
756 }
757 else if (par->type == bncParam::AMB_L3) {
[2265]758 strB << "\n amb " << par->prn.toAscii().data() << " = "
[2124]759 << setw(6) << setprecision(3) << par->xx
760 << " +- " << setw(6) << setprecision(3)
761 << sqrt(_QQ(par->index,par->index));
762 }
[2212]763 else if (par->type == bncParam::TROPO) {
[2670]764 double aprTrp = delay_saast(M_PI/2.0);
[2547]765 strB << "\n trp = " << par->prn.toAscii().data()
[2670]766 << setw(7) << setprecision(3) << aprTrp << " "
[2213]767 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
[2212]768 << " +- " << setw(6) << setprecision(3)
769 << sqrt(_QQ(par->index,par->index));
[2670]770 newPos->xnt[6] = aprTrp + par->xx;
[2212]771 }
[2060]772 }
[2265]773 strB << '\n';
[2547]774 _log += strB.str().c_str();
775 emit newMessage(_log, false);
[2060]776
[2547]777 // Final Message (both log file and screen)
778 // ----------------------------------------
779 ostringstream strC;
780 strC.setf(ios::fixed);
781 strC << _staID.data() << " PPP "
[2599]782 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
[2231]783 << setw(14) << setprecision(3) << x() << " +- "
784 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
785 << setw(14) << setprecision(3) << y() << " +- "
786 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
787 << setw(14) << setprecision(3) << z() << " +- "
[2124]788 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
[2113]789
[2370]790 // NEU Output
791 // ----------
[2726]792 if (settings.value("pppRefCrdX").toString() != "" &&
793 settings.value("pppRefCrdY").toString() != "" &&
794 settings.value("pppRefCrdZ").toString() != "") {
[2649]795
[2370]796 double xyzRef[3];
797 xyzRef[0] = settings.value("pppRefCrdX").toDouble();
798 xyzRef[1] = settings.value("pppRefCrdY").toDouble();
799 xyzRef[2] = settings.value("pppRefCrdZ").toDouble();
[2649]800
[2670]801 newPos->xnt[0] = x() - xyzRef[0];
802 newPos->xnt[1] = y() - xyzRef[1];
803 newPos->xnt[2] = z() - xyzRef[2];
[2649]804
805 double ellRef[3];
[2370]806 xyz2ell(xyzRef, ellRef);
[2670]807 xyz2neu(ellRef, newPos->xnt, &newPos->xnt[3]);
[2649]808
[2547]809 strC << " NEU "
[2670]810 << setw(8) << setprecision(3) << newPos->xnt[3] << " "
811 << setw(8) << setprecision(3) << newPos->xnt[4] << " "
812 << setw(8) << setprecision(3) << newPos->xnt[5];
[2649]813
[2370]814 }
815
[2547]816 emit newMessage(QByteArray(strC.str().c_str()), true);
817
[2671]818 if (settings.value("pppAverage").toString() == "") {
819 delete newPos;
820 }
821 else {
822
823 _posAverage.push_back(newPos);
[2599]824
[2648]825 // Time Span for Average Computation
826 // ---------------------------------
827 double tRangeAverage = settings.value("pppAverage").toDouble() * 60.;
828 if (tRangeAverage < 0) {
829 tRangeAverage = 0;
830 }
831 if (tRangeAverage > 86400) {
832 tRangeAverage = 86400;
833 }
[2599]834
[2648]835 // Compute the Mean
836 // ----------------
[2670]837 ColumnVector mean(7); mean = 0.0;
[2648]838
[2599]839 QMutableVectorIterator<pppPos*> it(_posAverage);
840 while (it.hasNext()) {
841 pppPos* pp = it.next();
[2648]842 if ( (epoData->tt - pp->time) >= tRangeAverage ) {
[2599]843 delete pp;
844 it.remove();
845 }
[2648]846 else {
[2670]847 for (int ii = 0; ii < 7; ++ii) {
848 mean[ii] += pp->xnt[ii];
[2649]849 }
[2648]850 }
[2599]851 }
[2648]852
853 int nn = _posAverage.size();
854
[2649]855 if (nn > 0) {
[2648]856
[2649]857 mean /= nn;
858
859 // Compute the Deviation
860 // ---------------------
[2670]861 ColumnVector std(7); std = 0.0;
[2649]862 QVectorIterator<pppPos*> it2(_posAverage);
863 while (it2.hasNext()) {
864 pppPos* pp = it2.next();
[2670]865 for (int ii = 0; ii < 7; ++ii) {
866 std[ii] += (pp->xnt[ii] - mean[ii]) * (pp->xnt[ii] - mean[ii]);
[2649]867 }
868 }
[2670]869 for (int ii = 0; ii < 7; ++ii) {
[2649]870 std[ii] = sqrt(std[ii] / nn);
871 }
872
873 ostringstream strD; strD.setf(ios::fixed);
874 strD << _staID.data() << " AVE-XYZ "
875 << epoData->tt.timestr(1) << " "
876 << setw(13) << setprecision(3) << mean[0] << " +- "
877 << setw(6) << setprecision(3) << std[0] << " "
878 << setw(14) << setprecision(3) << mean[1] << " +- "
879 << setw(6) << setprecision(3) << std[1] << " "
880 << setw(14) << setprecision(3) << mean[2] << " +- "
[2650]881 << setw(6) << setprecision(3) << std[2];
882 emit newMessage(QByteArray(strD.str().c_str()), true);
[2649]883
[2650]884 ostringstream strE; strE.setf(ios::fixed);
885 strE << _staID.data() << " AVE-NEU "
[2649]886 << epoData->tt.timestr(1) << " "
887 << setw(13) << setprecision(3) << mean[3] << " +- "
888 << setw(6) << setprecision(3) << std[3] << " "
889 << setw(14) << setprecision(3) << mean[4] << " +- "
890 << setw(6) << setprecision(3) << std[4] << " "
891 << setw(14) << setprecision(3) << mean[5] << " +- "
[2670]892 << setw(6) << setprecision(3) << std[5];
[2649]893
[2650]894 emit newMessage(QByteArray(strE.str().c_str()), true);
[2670]895
[2726]896 if ( Qt::CheckState(settings.value("pppEstTropo").toInt()) == Qt::Checked) {
897 ostringstream strF; strF.setf(ios::fixed);
898 strF << _staID.data() << " AVE-TRP "
899 << epoData->tt.timestr(1) << " "
900 << setw(13) << setprecision(3) << mean[6] << " +- "
901 << setw(6) << setprecision(3) << std[6] << endl;
902 emit newMessage(QByteArray(strF.str().c_str()), true);
903 }
[2599]904 }
905 }
906
[2131]907 // NMEA Output
908 // -----------
[2181]909 double xyz[3];
910 xyz[0] = x();
911 xyz[1] = y();
912 xyz[2] = z();
913 double ell[3];
914 xyz2ell(xyz, ell);
915 double phiDeg = ell[0] * 180 / M_PI;
916 double lamDeg = ell[1] * 180 / M_PI;
[2132]917
[2181]918 char phiCh = 'N';
919 if (phiDeg < 0) {
920 phiDeg = -phiDeg;
921 phiCh = 'S';
922 }
923 char lamCh = 'E';
924 if (lamDeg < 0) {
[2563]925 lamDeg = -lamDeg;
926 lamCh = 'W';
[2181]927 }
[2132]928
[2566]929 string datestr = epoData->tt.datestr(0); // yyyymmdd
930 ostringstream strRMC;
931 strRMC.setf(ios::fixed);
932 strRMC << "GPRMC,"
933 << epoData->tt.timestr(0,0) << ",A,"
934 << setw(2) << setfill('0') << int(phiDeg)
935 << setw(6) << setprecision(3) << setfill('0')
936 << fmod(60*phiDeg,60) << ',' << phiCh << ','
937 << setw(3) << setfill('0') << int(lamDeg)
938 << setw(6) << setprecision(3) << setfill('0')
939 << fmod(60*lamDeg,60) << ',' << lamCh << ",,,"
[2569]940 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
941 << datestr[2] << datestr[3] << ",,";
[2566]942
943 writeNMEAstr(QString(strRMC.str().c_str()));
944
[2181]945 double dop = 2.0; // TODO
[2133]946
[2566]947 ostringstream strGGA;
948 strGGA.setf(ios::fixed);
949 strGGA << "GPGGA,"
950 << epoData->tt.timestr(0,0) << ','
951 << setw(2) << setfill('0') << int(phiDeg)
952 << setw(10) << setprecision(7) << setfill('0')
953 << fmod(60*phiDeg,60) << ',' << phiCh << ','
954 << setw(3) << setfill('0') << int(lamDeg)
955 << setw(10) << setprecision(7) << setfill('0')
956 << fmod(60*lamDeg,60) << ',' << lamCh
957 << ",1," << setw(2) << setfill('0') << epoData->sizeAll() << ','
958 << setw(3) << setprecision(1) << dop << ','
[2569]959 << setprecision(3) << ell[2] << ",M,0.0,M,,";
[2181]960
[2566]961 writeNMEAstr(QString(strGGA.str().c_str()));
[2131]962
[2060]963 return success;
964}
[2112]965
966// Outlier Detection
967////////////////////////////////////////////////////////////////////////////
968int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
969 const ColumnVector& vv,
[2231]970 QMap<QString, t_satData*>& satDataGPS,
971 QMap<QString, t_satData*>& satDataGlo) {
[2112]972
[2231]973 double vvMaxCodeGPS = 0.0;
974 double vvMaxPhaseGPS = 0.0;
975 double vvMaxPhaseGlo = 0.0;
976 QMutableMapIterator<QString, t_satData*> itMaxCodeGPS(satDataGPS);
977 QMutableMapIterator<QString, t_satData*> itMaxPhaseGPS(satDataGPS);
978 QMutableMapIterator<QString, t_satData*> itMaxPhaseGlo(satDataGlo);
[2112]979
980 int ii = 0;
[2231]981
982 // GPS code and (optionally) phase residuals
983 // -----------------------------------------
984 QMutableMapIterator<QString, t_satData*> itGPS(satDataGPS);
985 while (itGPS.hasNext()) {
986 itGPS.next();
[2112]987 ++ii;
988
[2231]989 if (vvMaxCodeGPS == 0.0 || fabs(vv(ii)) > vvMaxCodeGPS) {
990 vvMaxCodeGPS = fabs(vv(ii));
991 itMaxCodeGPS = itGPS;
[2112]992 }
993
994 if (_usePhase) {
995 ++ii;
[2231]996 if (vvMaxPhaseGPS == 0.0 || fabs(vv(ii)) > vvMaxPhaseGPS) {
997 vvMaxPhaseGPS = fabs(vv(ii));
998 itMaxPhaseGPS = itGPS;
[2112]999 }
1000 }
1001 }
[2231]1002
[2238]1003 // Glonass phase residuals
1004 // -----------------------
1005 if (_usePhase) {
1006 QMutableMapIterator<QString, t_satData*> itGlo(satDataGlo);
1007 while (itGlo.hasNext()) {
1008 itGlo.next();
1009 ++ii;
1010 if (vvMaxPhaseGlo == 0.0 || fabs(vv(ii)) > vvMaxPhaseGlo) {
1011 vvMaxPhaseGlo = fabs(vv(ii));
1012 itMaxPhaseGlo = itGlo;
1013 }
1014 }
1015 }
[2112]1016
[2248]1017 if (vvMaxPhaseGlo > MAXRES_PHASE_GLO) {
1018 QString prn = itMaxPhaseGlo.key();
1019 t_satData* satData = itMaxPhaseGlo.value();
1020 delete satData;
1021 itMaxPhaseGlo.remove();
1022 _QQ = QQsav;
1023
[2547]1024 _log += "Outlier Phase " + prn.toAscii() + " "
1025 + QByteArray::number(vvMaxPhaseGlo, 'f', 3) + "\n";
[2248]1026
1027 return 1;
1028 }
1029
1030 else if (vvMaxCodeGPS > MAXRES_CODE_GPS) {
[2231]1031 QString prn = itMaxCodeGPS.key();
1032 t_satData* satData = itMaxCodeGPS.value();
[2112]1033 delete satData;
[2231]1034 itMaxCodeGPS.remove();
[2112]1035 _QQ = QQsav;
[2114]1036
[2547]1037 _log += "Outlier Code " + prn.toAscii() + " "
1038 + QByteArray::number(vvMaxCodeGPS, 'f', 3) + "\n";
[2114]1039
[2112]1040 return 1;
1041 }
[2243]1042 else if (vvMaxPhaseGPS > MAXRES_PHASE_GPS) {
[2231]1043 QString prn = itMaxPhaseGPS.key();
1044 t_satData* satData = itMaxPhaseGPS.value();
[2112]1045 delete satData;
[2231]1046 itMaxPhaseGPS.remove();
[2112]1047 _QQ = QQsav;
[2114]1048
[2547]1049 _log += "Outlier Phase " + prn.toAscii() + " "
1050 + QByteArray::number(vvMaxPhaseGPS, 'f', 3) + "\n";
[2114]1051
[2112]1052 return 1;
1053 }
[2231]1054
[2112]1055 return 0;
1056}
[2130]1057
1058//
1059////////////////////////////////////////////////////////////////////////////
1060void bncModel::writeNMEAstr(const QString& nmStr) {
1061
1062 unsigned char XOR = 0;
1063 for (int ii = 0; ii < nmStr.length(); ii++) {
1064 XOR ^= (unsigned char) nmStr[ii].toAscii();
1065 }
[2181]1066
1067 QString outStr = '$' + nmStr
1068 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
[2130]1069
[2178]1070 if (_nmeaStream) {
[2181]1071 *_nmeaStream << outStr;
[2178]1072 _nmeaStream->flush();
1073 }
[2130]1074
[2181]1075 emit newNMEAstr(outStr.toAscii());
[2130]1076}
[2283]1077
1078////
1079//////////////////////////////////////////////////////////////////////////////
1080void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
1081 const DiagonalMatrix& PP,
1082 SymmetricMatrix& QQ, ColumnVector& dx) {
1083
1084 int nObs = AA.Nrows();
1085 int nPar = AA.Ncols();
1086
1087 UpperTriangularMatrix SS = Cholesky(QQ).t();
1088
1089 Matrix SA = SS*AA.t();
1090 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
1091 for (int ii = 1; ii <= nObs; ++ii) {
1092 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
1093 }
1094
1095 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
1096 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
1097
1098 UpperTriangularMatrix UU;
1099 QRZ(SRF, UU);
1100
1101 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
1102 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
1103 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
1104
1105 UpperTriangularMatrix SHi = SH_rt.i();
1106
1107 Matrix KT = SHi * YY;
1108 SymmetricMatrix Hi; Hi << SHi * SHi.t();
1109
1110 dx = KT.t() * ll;
1111 QQ << (SS.t() * SS);
1112}
[2582]1113
1114// Phase Wind-Up Correction
1115///////////////////////////////////////////////////////////////////////////
1116double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
1117 const ColumnVector& rRec) {
1118
1119 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
1120
1121 // First time - initialize to zero
1122 // -------------------------------
1123 if (!_windUpTime.contains(prn)) {
1124 _windUpTime[prn] = Mjd;
1125 _windUpSum[prn] = 0.0;
1126 }
1127
1128 // Compute the correction for new time
1129 // -----------------------------------
1130 else if (_windUpTime[prn] != Mjd) {
1131 _windUpTime[prn] = Mjd;
1132
1133 // Unit Vector GPS Satellite --> Receiver
1134 // --------------------------------------
1135 ColumnVector rho = rRec - rSat;
1136 rho /= rho.norm_Frobenius();
1137
1138 // GPS Satellite unit Vectors sz, sy, sx
1139 // -------------------------------------
1140 ColumnVector sz = -rSat / rSat.norm_Frobenius();
1141
1142 ColumnVector xSun = Sun(Mjd);
1143 xSun /= xSun.norm_Frobenius();
1144
1145 ColumnVector sy = crossproduct(sz, xSun);
1146 ColumnVector sx = crossproduct(sy, sz);
1147
1148 // Effective Dipole of the GPS Satellite Antenna
1149 // ---------------------------------------------
1150 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
1151 - crossproduct(rho, sy);
1152
1153 // Receiver unit Vectors rx, ry
1154 // ----------------------------
1155 ColumnVector rx(3);
1156 ColumnVector ry(3);
1157
1158 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
1159 double neu[3];
1160
1161 neu[0] = 1.0;
1162 neu[1] = 0.0;
1163 neu[2] = 0.0;
1164 neu2xyz(recEll, neu, rx.data());
1165
1166 neu[0] = 0.0;
1167 neu[1] = -1.0;
1168 neu[2] = 0.0;
1169 neu2xyz(recEll, neu, ry.data());
1170
1171 // Effective Dipole of the Receiver Antenna
1172 // ----------------------------------------
1173 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
1174 + crossproduct(rho, ry);
1175
1176 // Resulting Effect
1177 // ----------------
1178 double alpha = DotProduct(dipSat,dipRec) /
1179 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
1180
1181 if (alpha > 1.0) alpha = 1.0;
1182 if (alpha < -1.0) alpha = -1.0;
1183
1184 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
1185
1186 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
1187 dphi = -dphi;
1188 }
1189
1190 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
1191 }
1192
1193 return _windUpSum[prn];
1194}
Note: See TracBrowser for help on using the repository browser.