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

Last change on this file since 2761 was 2747, checked in by mervart, 14 years ago
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 bncSettings settings;
131
[2720]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()) {
[2741]140 _sigL3 = settings.value("pppSigmaPhase").toDouble();
[2720]141 }
[2648]142
[2720]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;
[2648]163
[2720]164 // Quick-Start Mode
165 // ----------------
166 _quickStart = 0;
[2726]167 if (settings.value("pppRefCrdX").toString() != "" &&
168 settings.value("pppRefCrdY").toString() != "" &&
169 settings.value("pppRefCrdZ").toString() != "" &&
[2720]170 !settings.value("pppQuickStart").toString().isEmpty()) {
[2745]171 _quickStart = settings.value("pppQuickStart").toDouble();
[2720]172 }
173
[2113]174 connect(this, SIGNAL(newMessage(QByteArray,bool)),
[2548]175 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
[2113]176
[2084]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
[2265]190 if (_usePhase &&
191 Qt::CheckState(settings.value("pppGLONASS").toInt()) == Qt::Checked) {
[2239]192 _useGlonass = true;
193 }
194 else {
195 _useGlonass = false;
196 }
197
198 int nextPar = 0;
[2265]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, ""));
[2084]203 if (_estTropo) {
[2239]204 _params.push_back(new bncParam(bncParam::TROPO, ++nextPar, ""));
[2084]205 }
[2073]206
207 unsigned nPar = _params.size();
[2084]208
[2073]209 _QQ.ReSize(nPar);
[2239]210
[2073]211 _QQ = 0.0;
212
[2239]213 for (int iPar = 1; iPar <= _params.size(); iPar++) {
214 bncParam* pp = _params[iPar-1];
215 if (pp->isCrd()) {
[2720]216 _QQ(iPar,iPar) = _sigCrd0 * _sigCrd0;
[2239]217 }
[2265]218 else if (pp->type == bncParam::RECCLK) {
[2720]219 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
[2239]220 }
221 else if (pp->type == bncParam::TROPO) {
[2720]222 _QQ(iPar,iPar) = _sigTrp0 * _sigTrp0;
[2239]223 }
[2077]224 }
[2125]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 }
[2058]245}
246
247// Destructor
248////////////////////////////////////////////////////////////////////////////
249bncModel::~bncModel() {
[2126]250 delete _nmeaStream;
251 delete _nmeaFile;
[2648]252 for (int ii = 0; ii < _posAverage.size(); ++ii) {
253 delete _posAverage[ii];
254 }
[2058]255}
256
257// Bancroft Solution
258////////////////////////////////////////////////////////////////////////////
259t_irc bncModel::cmpBancroft(t_epoData* epoData) {
260
[2231]261 if (epoData->sizeGPS() < MINOBS) {
[2547]262 _log += "bncModel::cmpBancroft: not enough data\n";
[2058]263 return failure;
264 }
265
[2231]266 Matrix BB(epoData->sizeGPS(), 4);
[2058]267
[2231]268 QMapIterator<QString, t_satData*> it(epoData->satDataGPS);
[2058]269 int iObs = 0;
270 while (it.hasNext()) {
[2060]271 ++iObs;
[2058]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
[2064]283 // Ellipsoidal Coordinates
284 // ------------------------
285 xyz2ell(_xcBanc.data(), _ellBanc.data());
[2063]286
[2064]287 // Compute Satellite Elevations
288 // ----------------------------
[2231]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();
[2063]294
[2109]295 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
296 double rho = rr.norm_Frobenius();
[2064]297
298 double neu[3];
[2109]299 xyz2neu(_ellBanc.data(), rr.data(), neu);
[2064]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]);
[2070]306
[2287]307 if (satData->eleSat < MINELE_GPS) {
[2070]308 delete satData;
[2231]309 iGPS.remove();
[2070]310 }
[2064]311 }
312
[2231]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
[2287]331 if (satData->eleSat < MINELE_GLO) {
[2231]332 delete satData;
333 iGlo.remove();
334 }
335 }
336
[2058]337 return success;
338}
[2060]339
340// Computed Value
341////////////////////////////////////////////////////////////////////////////
[2583]342double bncModel::cmpValue(t_satData* satData, bool phase) {
[2060]343
[2073]344 ColumnVector xRec(3);
345 xRec(1) = x();
346 xRec(2) = y();
347 xRec(3) = z();
[2060]348
[2073]349 double rho0 = (satData->xx - xRec).norm_Frobenius();
[2060]350 double dPhi = t_CST::omega * rho0 / t_CST::c;
351
[2073]352 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
353 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
354 xRec(3) = z();
355
[2580]356 tides(_time, xRec);
357
[2060]358 satData->rho = (satData->xx - xRec).norm_Frobenius();
359
[2084]360 double tropDelay = delay_saast(satData->eleSat) +
361 trp() / sin(satData->eleSat);
[2060]362
[2583]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;
[2060]369}
370
[2063]371// Tropospheric Model (Saastamoinen)
372////////////////////////////////////////////////////////////////////////////
[2065]373double bncModel::delay_saast(double Ele) {
[2063]374
[2064]375 double height = _ellBanc(3);
[2063]376
[2064]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);
[2063]380 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
381
[2064]382 double h_km = height / 1000.0;
[2063]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
[2073]404// Prediction Step of the Filter
405////////////////////////////////////////////////////////////////////////////
[2080]406void bncModel::predict(t_epoData* epoData) {
[2073]407
[2648]408 bncSettings settings;
[2244]409
[2648]410 bool firstCrd = false;
411 if (x() == 0.0 && y() == 0.0 && z() == 0.0) {
412 firstCrd = true;
[2747]413 _startTime = QDateTime::currentDateTime();
[2648]414 }
415
[2726]416 // Use different white noise for Quick-Start mode
[2720]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) {
[2726]433 if (settings.value("pppRefCrdX").toString() != "" &&
434 settings.value("pppRefCrdY").toString() != "" &&
435 settings.value("pppRefCrdZ").toString() != "") {
[2648]436 pp->xx = settings.value("pppRefCrdX").toDouble();
437 }
438 else {
439 pp->xx = _xcBanc(1);
440 }
[2244]441 }
[2720]442 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
[2244]443 }
444 else if (pp->type == bncParam::CRD_Y) {
[2720]445 if (firstCrd) {
[2726]446 if (settings.value("pppRefCrdX").toString() != "" &&
447 settings.value("pppRefCrdY").toString() != "" &&
448 settings.value("pppRefCrdZ").toString() != "") {
[2648]449 pp->xx = settings.value("pppRefCrdY").toDouble();
450 }
451 else {
452 pp->xx = _xcBanc(2);
453 }
[2244]454 }
[2720]455 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
[2244]456 }
457 else if (pp->type == bncParam::CRD_Z) {
[2720]458 if (firstCrd) {
[2726]459 if (settings.value("pppRefCrdX").toString() != "" &&
460 settings.value("pppRefCrdY").toString() != "" &&
461 settings.value("pppRefCrdZ").toString() != "") {
[2648]462 pp->xx = settings.value("pppRefCrdZ").toDouble();
463 }
464 else {
465 pp->xx = _xcBanc(3);
466 }
[2244]467 }
[2720]468 _QQ(iPar,iPar) += sigCrdP_used * sigCrdP_used;
[2244]469 }
470
471 // Receiver Clocks
472 // ---------------
[2265]473 else if (pp->type == bncParam::RECCLK) {
[2244]474 pp->xx = _xcBanc(4);
475 for (int jj = 1; jj <= _params.size(); jj++) {
476 _QQ(iPar, jj) = 0.0;
477 }
[2720]478 _QQ(iPar,iPar) = _sigClk0 * _sigClk0;
[2244]479 }
480
481 // Tropospheric Delay
482 // ------------------
483 else if (pp->type == bncParam::TROPO) {
[2720]484 _QQ(iPar,iPar) += _sigTrpP * _sigTrpP;
[2244]485 }
486 }
487
488 // Add New Ambiguities if necessary
489 // --------------------------------
[2083]490 if (_usePhase) {
[2080]491
[2083]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) {
[2231]509 if (epoData->satDataGPS.find(par->prn) == epoData->satDataGPS.end() &&
510 epoData->satDataGlo.find(par->prn) == epoData->satDataGlo.end() ) {
[2083]511 removed = true;
512 delete par;
513 it.remove();
514 }
[2080]515 }
[2083]516 if (! removed) {
517 ++iPar;
518 par->index = iPar;
519 }
[2080]520 }
[2083]521
522 // Add new ambiguity parameters
523 // ----------------------------
[2231]524 QMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
525 while (iGPS.hasNext()) {
526 iGPS.next();
[2233]527 QString prn = iGPS.key();
[2244]528 t_satData* satData = iGPS.value();
[2231]529 bool found = false;
[2083]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 }
[2080]536 }
[2083]537 if (!found) {
538 bncParam* par = new bncParam(bncParam::AMB_L3, _params.size()+1, prn);
539 _params.push_back(par);
[2583]540 par->xx = satData->L3 - cmpValue(satData, true);
[2083]541 }
[2080]542 }
[2231]543
544 QMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
545 while (iGlo.hasNext()) {
546 iGlo.next();
[2233]547 QString prn = iGlo.key();
548 t_satData* satData = iGlo.value();
[2231]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);
[2583]560 par->xx = satData->L3 - cmpValue(satData, true);
[2231]561 }
562 }
[2083]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 }
[2080]575 }
576 }
577 }
[2083]578
579 for (int ii = 1; ii <= nPar; ii++) {
580 bncParam* par = _params[ii-1];
581 if (par->index_old == 0) {
[2720]582 _QQ(par->index, par->index) = _sigAmb0 * _sigAmb0;
[2083]583 }
584 par->index_old = par->index;
[2080]585 }
586 }
[2073]587}
588
[2060]589// Update Step of the Filter (currently just a single-epoch solution)
590////////////////////////////////////////////////////////////////////////////
591t_irc bncModel::update(t_epoData* epoData) {
592
[2473]593 bncSettings settings;
594
[2248]595 _log.clear();
[2124]596
[2143]597 _time = epoData->tt;
598
[2547]599 _log += "Single Point Positioning of Epoch "
600 + QByteArray(_time.timestr(1).c_str()) +
601 "\n--------------------------------------------------------------\n";
602
[2112]603 SymmetricMatrix QQsav;
[2109]604 ColumnVector dx;
[2112]605 ColumnVector vv;
[2073]606
[2113]607 // Loop over all outliers
608 // ----------------------
[2108]609 do {
610
611 // Bancroft Solution
612 // -----------------
613 if (cmpBancroft(epoData) != success) {
[2124]614 emit newMessage(_log, false);
[2108]615 return failure;
616 }
[2080]617
[2108]618 // Status Prediction
619 // -----------------
620 predict(epoData);
621
[2109]622 // Create First-Design Matrix
623 // --------------------------
[2108]624 unsigned nPar = _params.size();
[2231]625 unsigned nObs = 0;
626 if (_usePhase) {
[2238]627 nObs = 2 * epoData->sizeGPS() + epoData->sizeGlo();
[2231]628 }
629 else {
630 nObs = epoData->sizeGPS(); // Glonass pseudoranges are not used
631 }
[2108]632
[2540]633 if (nObs < nPar) {
[2547]634 _log += "bncModel::update: nObs < nPar\n";
[2540]635 emit newMessage(_log, false);
636 return failure;
637 }
638
[2108]639 Matrix AA(nObs, nPar); // first design matrix
640 ColumnVector ll(nObs); // tems observed-computed
[2283]641 DiagonalMatrix PP(nObs); PP = 0.0;
[2108]642
643 unsigned iObs = 0;
[2231]644
645 // GPS code and (optionally) phase observations
646 // --------------------------------------------
647 QMapIterator<QString, t_satData*> itGPS(epoData->satDataGPS);
648 while (itGPS.hasNext()) {
[2083]649 ++iObs;
[2231]650 itGPS.next();
651 QString prn = itGPS.key();
652 t_satData* satData = itGPS.value();
[2108]653
[2583]654 ll(iObs) = satData->P3 - cmpValue(satData, false);
[2742]655 PP(iObs,iObs) = 1.0 / (_sigP3 * _sigP3);
[2083]656 for (int iPar = 1; iPar <= _params.size(); iPar++) {
[2239]657 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
[2083]658 }
[2108]659
660 if (_usePhase) {
661 ++iObs;
[2583]662 ll(iObs) = satData->L3 - cmpValue(satData, true);
[2720]663 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
[2108]664 for (int iPar = 1; iPar <= _params.size(); iPar++) {
665 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
666 _params[iPar-1]->prn == prn) {
[2109]667 ll(iObs) -= _params[iPar-1]->xx;
[2108]668 }
[2239]669 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
[2108]670 }
671 }
[2080]672 }
[2231]673
674 // Glonass phase observations
675 // --------------------------
676 if (_usePhase) {
677 QMapIterator<QString, t_satData*> itGlo(epoData->satDataGlo);
678 while (itGlo.hasNext()) {
[2238]679 ++iObs;
[2231]680 itGlo.next();
681 QString prn = itGlo.key();
682 t_satData* satData = itGlo.value();
[2238]683
[2583]684 ll(iObs) = satData->L3 - cmpValue(satData, true);
[2720]685 PP(iObs,iObs) = 1.0 / (_sigL3 * _sigL3);
[2238]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 }
[2239]691 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
[2238]692 }
[2231]693 }
694 }
695
[2112]696 // Compute Filter Update
[2108]697 // ---------------------
[2112]698 QQsav = _QQ;
699
[2283]700 kalman(AA, ll, PP, _QQ, dx);
701
702 vv = ll - AA * dx;
703
[2547]704 ostringstream strA;
705 strA.setf(ios::fixed);
[2265]706 ColumnVector vv_code(epoData->sizeGPS());
707 ColumnVector vv_phase(epoData->sizeGPS());
708 ColumnVector vv_glo(epoData->sizeGlo());
[2245]709
[2265]710 for (unsigned iobs = 1; iobs <= epoData->sizeGPS(); ++iobs) {
711 if (_usePhase) {
[2246]712 vv_code(iobs) = vv(2*iobs-1);
713 vv_phase(iobs) = vv(2*iobs);
714 }
[2265]715 else {
716 vv_code(iobs) = vv(iobs);
717 }
718 }
719 if (_useGlonass) {
[2246]720 for (unsigned iobs = 1; iobs <= epoData->sizeGlo(); ++iobs) {
721 vv_glo(iobs) = vv(2*epoData->sizeGPS()+iobs);
722 }
[2265]723 }
[2246]724
[2547]725 strA << "residuals code " << setw(8) << setprecision(3) << vv_code.t();
[2265]726 if (_usePhase) {
[2547]727 strA << "residuals phase " << setw(8) << setprecision(3) << vv_phase.t();
[2265]728 }
729 if (_useGlonass) {
[2547]730 strA << "residuals glo " << setw(8) << setprecision(3) << vv_glo.t();
[2246]731 }
[2547]732 _log += strA.str().c_str();
[2246]733
[2231]734 } while (outlierDetection(QQsav, vv, epoData->satDataGPS,
[2233]735 epoData->satDataGlo) != 0);
[2111]736
[2670]737 // Remember the Epoch-specific Results for the computation of means
738 // ----------------------------------------------------------------
739 pppPos* newPos = new pppPos;
740 newPos->time = epoData->tt;
741
[2109]742 // Set Solution Vector
743 // -------------------
[2265]744 ostringstream strB;
745 strB.setf(ios::fixed);
[2073]746 QVectorIterator<bncParam*> itPar(_params);
[2060]747 while (itPar.hasNext()) {
748 bncParam* par = itPar.next();
[2109]749 par->xx += dx(par->index);
[2265]750
751 if (par->type == bncParam::RECCLK) {
[2547]752 strB << "\n clk = " << setw(6) << setprecision(3) << par->xx
[2124]753 << " +- " << setw(6) << setprecision(3)
754 << sqrt(_QQ(par->index,par->index));
755 }
756 else if (par->type == bncParam::AMB_L3) {
[2265]757 strB << "\n amb " << par->prn.toAscii().data() << " = "
[2124]758 << setw(6) << setprecision(3) << par->xx
759 << " +- " << setw(6) << setprecision(3)
760 << sqrt(_QQ(par->index,par->index));
761 }
[2212]762 else if (par->type == bncParam::TROPO) {
[2670]763 double aprTrp = delay_saast(M_PI/2.0);
[2547]764 strB << "\n trp = " << par->prn.toAscii().data()
[2670]765 << setw(7) << setprecision(3) << aprTrp << " "
[2213]766 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
[2212]767 << " +- " << setw(6) << setprecision(3)
768 << sqrt(_QQ(par->index,par->index));
[2670]769 newPos->xnt[6] = aprTrp + par->xx;
[2212]770 }
[2060]771 }
[2265]772 strB << '\n';
[2547]773 _log += strB.str().c_str();
774 emit newMessage(_log, false);
[2060]775
[2547]776 // Final Message (both log file and screen)
777 // ----------------------------------------
778 ostringstream strC;
779 strC.setf(ios::fixed);
780 strC << _staID.data() << " PPP "
[2599]781 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
[2231]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() << " +- "
[2124]787 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
[2113]788
[2370]789 // NEU Output
790 // ----------
[2726]791 if (settings.value("pppRefCrdX").toString() != "" &&
792 settings.value("pppRefCrdY").toString() != "" &&
793 settings.value("pppRefCrdZ").toString() != "") {
[2649]794
[2370]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();
[2649]799
[2670]800 newPos->xnt[0] = x() - xyzRef[0];
801 newPos->xnt[1] = y() - xyzRef[1];
802 newPos->xnt[2] = z() - xyzRef[2];
[2649]803
804 double ellRef[3];
[2370]805 xyz2ell(xyzRef, ellRef);
[2670]806 xyz2neu(ellRef, newPos->xnt, &newPos->xnt[3]);
[2649]807
[2547]808 strC << " NEU "
[2670]809 << setw(8) << setprecision(3) << newPos->xnt[3] << " "
810 << setw(8) << setprecision(3) << newPos->xnt[4] << " "
811 << setw(8) << setprecision(3) << newPos->xnt[5];
[2649]812
[2370]813 }
814
[2547]815 emit newMessage(QByteArray(strC.str().c_str()), true);
816
[2671]817 if (settings.value("pppAverage").toString() == "") {
818 delete newPos;
819 }
820 else {
821
822 _posAverage.push_back(newPos);
[2599]823
[2648]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 }
[2599]833
[2648]834 // Compute the Mean
835 // ----------------
[2670]836 ColumnVector mean(7); mean = 0.0;
[2648]837
[2599]838 QMutableVectorIterator<pppPos*> it(_posAverage);
839 while (it.hasNext()) {
840 pppPos* pp = it.next();
[2648]841 if ( (epoData->tt - pp->time) >= tRangeAverage ) {
[2599]842 delete pp;
843 it.remove();
844 }
[2648]845 else {
[2670]846 for (int ii = 0; ii < 7; ++ii) {
847 mean[ii] += pp->xnt[ii];
[2649]848 }
[2648]849 }
[2599]850 }
[2648]851
852 int nn = _posAverage.size();
853
[2649]854 if (nn > 0) {
[2648]855
[2649]856 mean /= nn;
857
858 // Compute the Deviation
859 // ---------------------
[2670]860 ColumnVector std(7); std = 0.0;
[2649]861 QVectorIterator<pppPos*> it2(_posAverage);
862 while (it2.hasNext()) {
863 pppPos* pp = it2.next();
[2670]864 for (int ii = 0; ii < 7; ++ii) {
865 std[ii] += (pp->xnt[ii] - mean[ii]) * (pp->xnt[ii] - mean[ii]);
[2649]866 }
867 }
[2670]868 for (int ii = 0; ii < 7; ++ii) {
[2649]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] << " +- "
[2650]880 << setw(6) << setprecision(3) << std[2];
881 emit newMessage(QByteArray(strD.str().c_str()), true);
[2649]882
[2650]883 ostringstream strE; strE.setf(ios::fixed);
884 strE << _staID.data() << " AVE-NEU "
[2649]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] << " +- "
[2670]891 << setw(6) << setprecision(3) << std[5];
[2649]892
[2650]893 emit newMessage(QByteArray(strE.str().c_str()), true);
[2670]894
[2726]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 }
[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.