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

Last change on this file since 2720 was 2720, checked in by mervart, 13 years ago
File size: 34.0 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()) {
142 _sigP3 = settings.value("pppSigmaPhase").toDouble();
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;
169 if (settings.value("pppOrigin").toString() == "PPP" &&
170 !settings.value("pppQuickStart").toString().isEmpty()) {
171 _quickStart = settings.value("pppQuickStart").toDouble() * 60.0;
172 _sigCrd0 = 0.01;
173 }
174
[2113]175 connect(this, SIGNAL(newMessage(QByteArray,bool)),
[2548]176 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
[2113]177
[2084]178 _usePhase = false;
179 if ( Qt::CheckState(settings.value("pppUsePhase").toInt()) == Qt::Checked) {
180 _usePhase = true;
181 }
182
183 _estTropo = false;
184 if ( Qt::CheckState(settings.value("pppEstTropo").toInt()) == Qt::Checked) {
185 _estTropo = true;
186 }
187
188 _xcBanc.ReSize(4); _xcBanc = 0.0;
189 _ellBanc.ReSize(3); _ellBanc = 0.0;
190
[2265]191 if (_usePhase &&
192 Qt::CheckState(settings.value("pppGLONASS").toInt()) == Qt::Checked) {
[2239]193 _useGlonass = true;
194 }
195 else {
196 _useGlonass = false;
197 }
198
199 int nextPar = 0;
[2265]200 _params.push_back(new bncParam(bncParam::CRD_X, ++nextPar, ""));
201 _params.push_back(new bncParam(bncParam::CRD_Y, ++nextPar, ""));
202 _params.push_back(new bncParam(bncParam::CRD_Z, ++nextPar, ""));
203 _params.push_back(new bncParam(bncParam::RECCLK, ++nextPar, ""));
[2084]204 if (_estTropo) {
[2239]205 _params.push_back(new bncParam(bncParam::TROPO, ++nextPar, ""));
[2084]206 }
[2073]207
208 unsigned nPar = _params.size();
[2084]209
[2073]210 _QQ.ReSize(nPar);
[2239]211
[2073]212 _QQ = 0.0;
213
[2239]214 for (int iPar = 1; iPar <= _params.size(); iPar++) {
215 bncParam* pp = _params[iPar-1];
216 if (pp->isCrd()) {
[2720]217 _QQ(iPar,iPar) = _sigCrd0 * _sigCrd0;
[2239]218 }
[2265]219 else if (pp->type == bncParam::RECCLK) {
[2720]220 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
[2239]221 }
222 else if (pp->type == bncParam::TROPO) {
[2720]223 _QQ(iPar,iPar) = _sigTrp0 * _sigTrp0;
[2239]224 }
[2077]225 }
[2125]226
227 // NMEA Output
228 // -----------
229 QString nmeaFileName = settings.value("nmeaFile").toString();
230 if (nmeaFileName.isEmpty()) {
231 _nmeaFile = 0;
232 _nmeaStream = 0;
233 }
234 else {
235 expandEnvVar(nmeaFileName);
236 _nmeaFile = new QFile(nmeaFileName);
237 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
238 _nmeaFile->open(QIODevice::WriteOnly | QIODevice::Append);
239 }
240 else {
241 _nmeaFile->open(QIODevice::WriteOnly);
242 }
243 _nmeaStream = new QTextStream();
244 _nmeaStream->setDevice(_nmeaFile);
245 }
[2058]246}
247
248// Destructor
249////////////////////////////////////////////////////////////////////////////
250bncModel::~bncModel() {
[2126]251 delete _nmeaStream;
252 delete _nmeaFile;
[2648]253 for (int ii = 0; ii < _posAverage.size(); ++ii) {
254 delete _posAverage[ii];
255 }
[2058]256}
257
258// Bancroft Solution
259////////////////////////////////////////////////////////////////////////////
260t_irc bncModel::cmpBancroft(t_epoData* epoData) {
261
[2231]262 if (epoData->sizeGPS() < MINOBS) {
[2547]263 _log += "bncModel::cmpBancroft: not enough data\n";
[2058]264 return failure;
265 }
266
[2231]267 Matrix BB(epoData->sizeGPS(), 4);
[2058]268
[2231]269 QMapIterator<QString, t_satData*> it(epoData->satDataGPS);
[2058]270 int iObs = 0;
271 while (it.hasNext()) {
[2060]272 ++iObs;
[2058]273 it.next();
274 QString prn = it.key();
275 t_satData* satData = it.value();
276 BB(iObs, 1) = satData->xx(1);
277 BB(iObs, 2) = satData->xx(2);
278 BB(iObs, 3) = satData->xx(3);
279 BB(iObs, 4) = satData->P3 + satData->clk;
280 }
281
282 bancroft(BB, _xcBanc);
283
[2064]284 // Ellipsoidal Coordinates
285 // ------------------------
286 xyz2ell(_xcBanc.data(), _ellBanc.data());
[2063]287
[2064]288 // Compute Satellite Elevations
289 // ----------------------------
[2231]290 QMutableMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
291 while (iGPS.hasNext()) {
292 iGPS.next();
293 QString prn = iGPS.key();
294 t_satData* satData = iGPS.value();
[2063]295
[2109]296 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
297 double rho = rr.norm_Frobenius();
[2064]298
299 double neu[3];
[2109]300 xyz2neu(_ellBanc.data(), rr.data(), neu);
[2064]301
302 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
303 if (neu[2] < 0) {
304 satData->eleSat *= -1.0;
305 }
306 satData->azSat = atan2(neu[1], neu[0]);
[2070]307
[2287]308 if (satData->eleSat < MINELE_GPS) {
[2070]309 delete satData;
[2231]310 iGPS.remove();
[2070]311 }
[2064]312 }
313
[2231]314 QMutableMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
315 while (iGlo.hasNext()) {
316 iGlo.next();
317 QString prn = iGlo.key();
318 t_satData* satData = iGlo.value();
319
320 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
321 double rho = rr.norm_Frobenius();
322
323 double neu[3];
324 xyz2neu(_ellBanc.data(), rr.data(), neu);
325
326 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
327 if (neu[2] < 0) {
328 satData->eleSat *= -1.0;
329 }
330 satData->azSat = atan2(neu[1], neu[0]);
331
[2287]332 if (satData->eleSat < MINELE_GLO) {
[2231]333 delete satData;
334 iGlo.remove();
335 }
336 }
337
[2058]338 return success;
339}
[2060]340
341// Computed Value
342////////////////////////////////////////////////////////////////////////////
[2583]343double bncModel::cmpValue(t_satData* satData, bool phase) {
[2060]344
[2073]345 ColumnVector xRec(3);
346 xRec(1) = x();
347 xRec(2) = y();
348 xRec(3) = z();
[2060]349
[2073]350 double rho0 = (satData->xx - xRec).norm_Frobenius();
[2060]351 double dPhi = t_CST::omega * rho0 / t_CST::c;
352
[2073]353 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
354 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
355 xRec(3) = z();
356
[2580]357 tides(_time, xRec);
358
[2060]359 satData->rho = (satData->xx - xRec).norm_Frobenius();
360
[2084]361 double tropDelay = delay_saast(satData->eleSat) +
362 trp() / sin(satData->eleSat);
[2060]363
[2583]364 double wind = 0.0;
365 if (phase) {
366 wind = windUp(satData->prn, satData->xx, xRec) * satData->lambda3;
367 }
368
369 return satData->rho + clk() - satData->clk + tropDelay + wind;
[2060]370}
371
[2063]372// Tropospheric Model (Saastamoinen)
373////////////////////////////////////////////////////////////////////////////
[2065]374double bncModel::delay_saast(double Ele) {
[2063]375
[2064]376 double height = _ellBanc(3);
[2063]377
[2064]378 double pp = 1013.25 * pow(1.0 - 2.26e-5 * height, 5.225);
379 double TT = 18.0 - height * 0.0065 + 273.15;
380 double hh = 50.0 * exp(-6.396e-4 * height);
[2063]381 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
382
[2064]383 double h_km = height / 1000.0;
[2063]384
385 if (h_km < 0.0) h_km = 0.0;
386 if (h_km > 5.0) h_km = 5.0;
387 int ii = int(h_km + 1);
388 double href = ii - 1;
389
390 double bCor[6];
391 bCor[0] = 1.156;
392 bCor[1] = 1.006;
393 bCor[2] = 0.874;
394 bCor[3] = 0.757;
395 bCor[4] = 0.654;
396 bCor[5] = 0.563;
397
398 double BB = bCor[ii-1] + (bCor[ii]-bCor[ii-1]) * (h_km - href);
399
400 double zen = M_PI/2.0 - Ele;
401
402 return (0.002277/cos(zen)) * (pp + ((1255.0/TT)+0.05)*ee - BB*(tan(zen)*tan(zen)));
403}
404
[2073]405// Prediction Step of the Filter
406////////////////////////////////////////////////////////////////////////////
[2080]407void bncModel::predict(t_epoData* epoData) {
[2073]408
[2648]409 bncSettings settings;
[2244]410
[2648]411 bool firstCrd = false;
412 if (x() == 0.0 && y() == 0.0 && z() == 0.0) {
413 firstCrd = true;
414 }
415
[2720]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;
[2648]422 }
423
[2244]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) {
[2720]432 if (firstCrd) {
433 if (settings.value("pppOrigin").toString() == "PPP") {
[2648]434 pp->xx = settings.value("pppRefCrdX").toDouble();
435 }
436 else {
437 pp->xx = _xcBanc(1);
438 }
[2244]439 }
[2720]440 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
[2244]441 }
442 else if (pp->type == bncParam::CRD_Y) {
[2720]443 if (firstCrd) {
444 if (settings.value("pppOrigin").toString() == "PPP") {
[2648]445 pp->xx = settings.value("pppRefCrdY").toDouble();
446 }
447 else {
448 pp->xx = _xcBanc(2);
449 }
[2244]450 }
[2720]451 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
[2244]452 }
453 else if (pp->type == bncParam::CRD_Z) {
[2720]454 if (firstCrd) {
455 if (settings.value("pppOrigin").toString() == "PPP") {
[2648]456 pp->xx = settings.value("pppRefCrdZ").toDouble();
457 }
458 else {
459 pp->xx = _xcBanc(3);
460 }
[2244]461 }
[2720]462 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
[2244]463 }
464
465 // Receiver Clocks
466 // ---------------
[2265]467 else if (pp->type == bncParam::RECCLK) {
[2244]468 pp->xx = _xcBanc(4);
469 for (int jj = 1; jj <= _params.size(); jj++) {
470 _QQ(iPar, jj) = 0.0;
471 }
[2720]472 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
[2244]473 }
474
475 // Tropospheric Delay
476 // ------------------
477 else if (pp->type == bncParam::TROPO) {
[2720]478 _QQ(iPar,iPar) += _sigTrpP * _sigTrpP;
[2244]479 }
480 }
481
482 // Add New Ambiguities if necessary
483 // --------------------------------
[2083]484 if (_usePhase) {
[2080]485
[2083]486 // Make a copy of QQ and xx, set parameter indices
487 // -----------------------------------------------
488 SymmetricMatrix QQ_old = _QQ;
489
490 for (int iPar = 1; iPar <= _params.size(); iPar++) {
491 _params[iPar-1]->index_old = _params[iPar-1]->index;
492 _params[iPar-1]->index = 0;
493 }
494
495 // Remove Ambiguity Parameters without observations
496 // ------------------------------------------------
497 int iPar = 0;
498 QMutableVectorIterator<bncParam*> it(_params);
499 while (it.hasNext()) {
500 bncParam* par = it.next();
501 bool removed = false;
502 if (par->type == bncParam::AMB_L3) {
[2231]503 if (epoData->satDataGPS.find(par->prn) == epoData->satDataGPS.end() &&
504 epoData->satDataGlo.find(par->prn) == epoData->satDataGlo.end() ) {
[2083]505 removed = true;
506 delete par;
507 it.remove();
508 }
[2080]509 }
[2083]510 if (! removed) {
511 ++iPar;
512 par->index = iPar;
513 }
[2080]514 }
[2083]515
516 // Add new ambiguity parameters
517 // ----------------------------
[2231]518 QMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
519 while (iGPS.hasNext()) {
520 iGPS.next();
[2233]521 QString prn = iGPS.key();
[2244]522 t_satData* satData = iGPS.value();
[2231]523 bool found = false;
[2083]524 for (int iPar = 1; iPar <= _params.size(); iPar++) {
525 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
526 _params[iPar-1]->prn == prn) {
527 found = true;
528 break;
529 }
[2080]530 }
[2083]531 if (!found) {
532 bncParam* par = new bncParam(bncParam::AMB_L3, _params.size()+1, prn);
533 _params.push_back(par);
[2583]534 par->xx = satData->L3 - cmpValue(satData, true);
[2083]535 }
[2080]536 }
[2231]537
538 QMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
539 while (iGlo.hasNext()) {
540 iGlo.next();
[2233]541 QString prn = iGlo.key();
542 t_satData* satData = iGlo.value();
[2231]543 bool found = false;
544 for (int iPar = 1; iPar <= _params.size(); iPar++) {
545 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
546 _params[iPar-1]->prn == prn) {
547 found = true;
548 break;
549 }
550 }
551 if (!found) {
552 bncParam* par = new bncParam(bncParam::AMB_L3, _params.size()+1, prn);
553 _params.push_back(par);
[2583]554 par->xx = satData->L3 - cmpValue(satData, true);
[2231]555 }
556 }
[2083]557
558 int nPar = _params.size();
559 _QQ.ReSize(nPar); _QQ = 0.0;
560 for (int i1 = 1; i1 <= nPar; i1++) {
561 bncParam* p1 = _params[i1-1];
562 if (p1->index_old != 0) {
563 _QQ(p1->index, p1->index) = QQ_old(p1->index_old, p1->index_old);
564 for (int i2 = 1; i2 <= nPar; i2++) {
565 bncParam* p2 = _params[i2-1];
566 if (p2->index_old != 0) {
567 _QQ(p1->index, p2->index) = QQ_old(p1->index_old, p2->index_old);
568 }
[2080]569 }
570 }
571 }
[2083]572
573 for (int ii = 1; ii <= nPar; ii++) {
574 bncParam* par = _params[ii-1];
575 if (par->index_old == 0) {
[2720]576 _QQ(par->index, par->index) = _sigAmb0 * _sigAmb0;
[2083]577 }
578 par->index_old = par->index;
[2080]579 }
580 }
[2073]581}
582
[2060]583// Update Step of the Filter (currently just a single-epoch solution)
584////////////////////////////////////////////////////////////////////////////
585t_irc bncModel::update(t_epoData* epoData) {
586
[2473]587 bncSettings settings;
588 double sig_P3;
589 sig_P3 = 5.0;
590 if ( Qt::CheckState(settings.value("pppUsePhase").toInt()) == Qt::Checked ) {
[2482]591 sig_P3 = settings.value("pppSigmaCode").toDouble();
[2473]592 if (sig_P3 < 0.3 || sig_P3 > 50.0) {
593 sig_P3 = 5.0;
594 }
595 }
596
[2248]597 _log.clear();
[2124]598
[2143]599 _time = epoData->tt;
600
[2547]601 _log += "Single Point Positioning of Epoch "
602 + QByteArray(_time.timestr(1).c_str()) +
603 "\n--------------------------------------------------------------\n";
604
[2112]605 SymmetricMatrix QQsav;
[2109]606 ColumnVector dx;
[2112]607 ColumnVector vv;
[2073]608
[2113]609 // Loop over all outliers
610 // ----------------------
[2108]611 do {
612
613 // Bancroft Solution
614 // -----------------
615 if (cmpBancroft(epoData) != success) {
[2124]616 emit newMessage(_log, false);
[2108]617 return failure;
618 }
[2080]619
[2108]620 // Status Prediction
621 // -----------------
622 predict(epoData);
623
[2109]624 // Create First-Design Matrix
625 // --------------------------
[2108]626 unsigned nPar = _params.size();
[2231]627 unsigned nObs = 0;
628 if (_usePhase) {
[2238]629 nObs = 2 * epoData->sizeGPS() + epoData->sizeGlo();
[2231]630 }
631 else {
632 nObs = epoData->sizeGPS(); // Glonass pseudoranges are not used
633 }
[2108]634
[2540]635 if (nObs < nPar) {
[2547]636 _log += "bncModel::update: nObs < nPar\n";
[2540]637 emit newMessage(_log, false);
638 return failure;
639 }
640
[2108]641 Matrix AA(nObs, nPar); // first design matrix
642 ColumnVector ll(nObs); // tems observed-computed
[2283]643 DiagonalMatrix PP(nObs); PP = 0.0;
[2108]644
645 unsigned iObs = 0;
[2231]646
647 // GPS code and (optionally) phase observations
648 // --------------------------------------------
649 QMapIterator<QString, t_satData*> itGPS(epoData->satDataGPS);
650 while (itGPS.hasNext()) {
[2083]651 ++iObs;
[2231]652 itGPS.next();
653 QString prn = itGPS.key();
654 t_satData* satData = itGPS.value();
[2108]655
[2583]656 ll(iObs) = satData->P3 - cmpValue(satData, false);
[2244]657 PP(iObs,iObs) = 1.0 / (sig_P3 * sig_P3);
[2083]658 for (int iPar = 1; iPar <= _params.size(); iPar++) {
[2239]659 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
[2083]660 }
[2108]661
662 if (_usePhase) {
663 ++iObs;
[2583]664 ll(iObs) = satData->L3 - cmpValue(satData, true);
[2720]665 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
[2108]666 for (int iPar = 1; iPar <= _params.size(); iPar++) {
667 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
668 _params[iPar-1]->prn == prn) {
[2109]669 ll(iObs) -= _params[iPar-1]->xx;
[2108]670 }
[2239]671 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
[2108]672 }
673 }
[2080]674 }
[2231]675
676 // Glonass phase observations
677 // --------------------------
678 if (_usePhase) {
679 QMapIterator<QString, t_satData*> itGlo(epoData->satDataGlo);
680 while (itGlo.hasNext()) {
[2238]681 ++iObs;
[2231]682 itGlo.next();
683 QString prn = itGlo.key();
684 t_satData* satData = itGlo.value();
[2238]685
[2583]686 ll(iObs) = satData->L3 - cmpValue(satData, true);
[2720]687 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
[2238]688 for (int iPar = 1; iPar <= _params.size(); iPar++) {
689 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
690 _params[iPar-1]->prn == prn) {
691 ll(iObs) -= _params[iPar-1]->xx;
692 }
[2239]693 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
[2238]694 }
[2231]695 }
696 }
697
[2112]698 // Compute Filter Update
[2108]699 // ---------------------
[2112]700 QQsav = _QQ;
701
[2283]702 kalman(AA, ll, PP, _QQ, dx);
703
704 vv = ll - AA * dx;
705
[2547]706 ostringstream strA;
707 strA.setf(ios::fixed);
[2265]708 ColumnVector vv_code(epoData->sizeGPS());
709 ColumnVector vv_phase(epoData->sizeGPS());
710 ColumnVector vv_glo(epoData->sizeGlo());
[2245]711
[2265]712 for (unsigned iobs = 1; iobs <= epoData->sizeGPS(); ++iobs) {
713 if (_usePhase) {
[2246]714 vv_code(iobs) = vv(2*iobs-1);
715 vv_phase(iobs) = vv(2*iobs);
716 }
[2265]717 else {
718 vv_code(iobs) = vv(iobs);
719 }
720 }
721 if (_useGlonass) {
[2246]722 for (unsigned iobs = 1; iobs <= epoData->sizeGlo(); ++iobs) {
723 vv_glo(iobs) = vv(2*epoData->sizeGPS()+iobs);
724 }
[2265]725 }
[2246]726
[2547]727 strA << "residuals code " << setw(8) << setprecision(3) << vv_code.t();
[2265]728 if (_usePhase) {
[2547]729 strA << "residuals phase " << setw(8) << setprecision(3) << vv_phase.t();
[2265]730 }
731 if (_useGlonass) {
[2547]732 strA << "residuals glo " << setw(8) << setprecision(3) << vv_glo.t();
[2246]733 }
[2547]734 _log += strA.str().c_str();
[2246]735
[2231]736 } while (outlierDetection(QQsav, vv, epoData->satDataGPS,
[2233]737 epoData->satDataGlo) != 0);
[2111]738
[2670]739 // Remember the Epoch-specific Results for the computation of means
740 // ----------------------------------------------------------------
741 pppPos* newPos = new pppPos;
742 newPos->time = epoData->tt;
743
[2109]744 // Set Solution Vector
745 // -------------------
[2265]746 ostringstream strB;
747 strB.setf(ios::fixed);
[2073]748 QVectorIterator<bncParam*> itPar(_params);
[2060]749 while (itPar.hasNext()) {
750 bncParam* par = itPar.next();
[2109]751 par->xx += dx(par->index);
[2265]752
753 if (par->type == bncParam::RECCLK) {
[2547]754 strB << "\n clk = " << setw(6) << setprecision(3) << par->xx
[2124]755 << " +- " << setw(6) << setprecision(3)
756 << sqrt(_QQ(par->index,par->index));
757 }
758 else if (par->type == bncParam::AMB_L3) {
[2265]759 strB << "\n amb " << par->prn.toAscii().data() << " = "
[2124]760 << setw(6) << setprecision(3) << par->xx
761 << " +- " << setw(6) << setprecision(3)
762 << sqrt(_QQ(par->index,par->index));
763 }
[2212]764 else if (par->type == bncParam::TROPO) {
[2670]765 double aprTrp = delay_saast(M_PI/2.0);
[2547]766 strB << "\n trp = " << par->prn.toAscii().data()
[2670]767 << setw(7) << setprecision(3) << aprTrp << " "
[2213]768 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
[2212]769 << " +- " << setw(6) << setprecision(3)
770 << sqrt(_QQ(par->index,par->index));
[2670]771 newPos->xnt[6] = aprTrp + par->xx;
[2212]772 }
[2060]773 }
[2265]774 strB << '\n';
[2547]775 _log += strB.str().c_str();
776 emit newMessage(_log, false);
[2060]777
[2547]778 // Final Message (both log file and screen)
779 // ----------------------------------------
780 ostringstream strC;
781 strC.setf(ios::fixed);
782 strC << _staID.data() << " PPP "
[2599]783 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
[2231]784 << setw(14) << setprecision(3) << x() << " +- "
785 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
786 << setw(14) << setprecision(3) << y() << " +- "
787 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
788 << setw(14) << setprecision(3) << z() << " +- "
[2124]789 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
[2113]790
[2370]791 // NEU Output
792 // ----------
[2648]793 if (settings.value("pppOrigin").toString() == "Plot - X Y Z" ||
794 settings.value("pppOrigin").toString() == "QuickStart - Static") {
[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
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
902 emit newMessage(QByteArray(strF.str().c_str()), true);
[2599]903 }
904 }
905
[2131]906 // NMEA Output
907 // -----------
[2181]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;
[2132]916
[2181]917 char phiCh = 'N';
918 if (phiDeg < 0) {
919 phiDeg = -phiDeg;
920 phiCh = 'S';
921 }
922 char lamCh = 'E';
923 if (lamDeg < 0) {
[2563]924 lamDeg = -lamDeg;
925 lamCh = 'W';
[2181]926 }
[2132]927
[2566]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 << ",,,"
[2569]939 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
940 << datestr[2] << datestr[3] << ",,";
[2566]941
942 writeNMEAstr(QString(strRMC.str().c_str()));
943
[2181]944 double dop = 2.0; // TODO
[2133]945
[2566]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 << ','
[2569]958 << setprecision(3) << ell[2] << ",M,0.0,M,,";
[2181]959
[2566]960 writeNMEAstr(QString(strGGA.str().c_str()));
[2131]961
[2060]962 return success;
963}
[2112]964
965// Outlier Detection
966////////////////////////////////////////////////////////////////////////////
967int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
968 const ColumnVector& vv,
[2231]969 QMap<QString, t_satData*>& satDataGPS,
970 QMap<QString, t_satData*>& satDataGlo) {
[2112]971
[2231]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);
[2112]978
979 int ii = 0;
[2231]980
981 // GPS code and (optionally) phase residuals
982 // -----------------------------------------
983 QMutableMapIterator<QString, t_satData*> itGPS(satDataGPS);
984 while (itGPS.hasNext()) {
985 itGPS.next();
[2112]986 ++ii;
987
[2231]988 if (vvMaxCodeGPS == 0.0 || fabs(vv(ii)) > vvMaxCodeGPS) {
989 vvMaxCodeGPS = fabs(vv(ii));
990 itMaxCodeGPS = itGPS;
[2112]991 }
992
993 if (_usePhase) {
994 ++ii;
[2231]995 if (vvMaxPhaseGPS == 0.0 || fabs(vv(ii)) > vvMaxPhaseGPS) {
996 vvMaxPhaseGPS = fabs(vv(ii));
997 itMaxPhaseGPS = itGPS;
[2112]998 }
999 }
1000 }
[2231]1001
[2238]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 }
[2112]1015
[2248]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
[2547]1023 _log += "Outlier Phase " + prn.toAscii() + " "
1024 + QByteArray::number(vvMaxPhaseGlo, 'f', 3) + "\n";
[2248]1025
1026 return 1;
1027 }
1028
1029 else if (vvMaxCodeGPS > MAXRES_CODE_GPS) {
[2231]1030 QString prn = itMaxCodeGPS.key();
1031 t_satData* satData = itMaxCodeGPS.value();
[2112]1032 delete satData;
[2231]1033 itMaxCodeGPS.remove();
[2112]1034 _QQ = QQsav;
[2114]1035
[2547]1036 _log += "Outlier Code " + prn.toAscii() + " "
1037 + QByteArray::number(vvMaxCodeGPS, 'f', 3) + "\n";
[2114]1038
[2112]1039 return 1;
1040 }
[2243]1041 else if (vvMaxPhaseGPS > MAXRES_PHASE_GPS) {
[2231]1042 QString prn = itMaxPhaseGPS.key();
1043 t_satData* satData = itMaxPhaseGPS.value();
[2112]1044 delete satData;
[2231]1045 itMaxPhaseGPS.remove();
[2112]1046 _QQ = QQsav;
[2114]1047
[2547]1048 _log += "Outlier Phase " + prn.toAscii() + " "
1049 + QByteArray::number(vvMaxPhaseGPS, 'f', 3) + "\n";
[2114]1050
[2112]1051 return 1;
1052 }
[2231]1053
[2112]1054 return 0;
1055}
[2130]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 }
[2181]1065
1066 QString outStr = '$' + nmStr
1067 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
[2130]1068
[2178]1069 if (_nmeaStream) {
[2181]1070 *_nmeaStream << outStr;
[2178]1071 _nmeaStream->flush();
1072 }
[2130]1073
[2181]1074 emit newNMEAstr(outStr.toAscii());
[2130]1075}
[2283]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}
[2582]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.