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

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