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

Last change on this file since 2671 was 2671, checked in by mervart, 13 years ago
File size: 33.6 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: bncParam, bncModel
30 *
31 * Purpose: Model for PPP
32 *
33 * Author: L. Mervart
34 *
35 * Created: 01-Dec-2009
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iomanip>
42#include <cmath>
43#include <newmatio.h>
44#include <sstream>
45
46#include "bncmodel.h"
47#include "bncapp.h"
48#include "bncpppclient.h"
49#include "bancroft.h"
50#include "bncutils.h"
51#include "bncsettings.h"
52#include "bnctides.h"
53
54using namespace std;
55
56const unsigned MINOBS = 4;
57const double MINELE_GPS = 10.0 * M_PI / 180.0;
58const double MINELE_GLO = 10.0 * M_PI / 180.0;
59const double MAXRES_CODE_GPS = 10.0;
60const double MAXRES_PHASE_GPS = 0.10;
61const double MAXRES_PHASE_GLO = 0.05;
62const double QUICKSTART = 120.0;
63const double sig_clk_0 = 1000.0;
64const double sig_trp_0 = 0.10;
65const double sig_trp_p = 1e-8;
66const double sig_amb_0_GPS = 1000.0;
67const double sig_amb_0_GLO = 1000.0;
68const double sig_L3_GPS = 0.02;
69const double sig_L3_GLO = 0.05;
70
71// Constructor
72////////////////////////////////////////////////////////////////////////////
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;
80
81}
82
83// Destructor
84////////////////////////////////////////////////////////////////////////////
85bncParam::~bncParam() {
86}
87
88// Partial
89////////////////////////////////////////////////////////////////////////////
90double bncParam::partial(t_satData* satData, bool phase) {
91
92 // Coordinates
93 // -----------
94 if (type == CRD_X) {
95 return (xx - satData->xx(1)) / satData->rho;
96 }
97 else if (type == CRD_Y) {
98 return (xx - satData->xx(2)) / satData->rho;
99 }
100 else if (type == CRD_Z) {
101 return (xx - satData->xx(3)) / satData->rho;
102 }
103
104 // Receiver Clocks
105 // ---------------
106 else if (type == RECCLK) {
107 return 1.0;
108 }
109
110 // Troposphere
111 // -----------
112 else if (type == TROPO) {
113 return 1.0 / sin(satData->eleSat);
114 }
115
116 // Ambiguities
117 // -----------
118 else if (type == AMB_L3) {
119 if (phase && satData->prn == prn) {
120 return 1.0;
121 }
122 else {
123 return 0.0;
124 }
125 }
126
127 // Default return
128 // --------------
129 return 0.0;
130}
131
132// Constructor
133////////////////////////////////////////////////////////////////////////////
134bncModel::bncModel(QByteArray staID) {
135
136 _staID = staID;
137
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
149 connect(this, SIGNAL(newMessage(QByteArray,bool)),
150 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
151
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
170 if (_usePhase &&
171 Qt::CheckState(settings.value("pppGLONASS").toInt()) == Qt::Checked) {
172 _useGlonass = true;
173 }
174 else {
175 _useGlonass = false;
176 }
177
178 int nextPar = 0;
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, ""));
183 if (_estTropo) {
184 _params.push_back(new bncParam(bncParam::TROPO, ++nextPar, ""));
185 }
186
187 unsigned nPar = _params.size();
188
189 _QQ.ReSize(nPar);
190
191 _QQ = 0.0;
192
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 }
198 else if (pp->type == bncParam::RECCLK) {
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 }
204 }
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 }
225}
226
227// Destructor
228////////////////////////////////////////////////////////////////////////////
229bncModel::~bncModel() {
230 delete _nmeaStream;
231 delete _nmeaFile;
232 for (int ii = 0; ii < _posAverage.size(); ++ii) {
233 delete _posAverage[ii];
234 }
235}
236
237// Bancroft Solution
238////////////////////////////////////////////////////////////////////////////
239t_irc bncModel::cmpBancroft(t_epoData* epoData) {
240
241 if (epoData->sizeGPS() < MINOBS) {
242 _log += "bncModel::cmpBancroft: not enough data\n";
243 return failure;
244 }
245
246 Matrix BB(epoData->sizeGPS(), 4);
247
248 QMapIterator<QString, t_satData*> it(epoData->satDataGPS);
249 int iObs = 0;
250 while (it.hasNext()) {
251 ++iObs;
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
263 // Ellipsoidal Coordinates
264 // ------------------------
265 xyz2ell(_xcBanc.data(), _ellBanc.data());
266
267 // Compute Satellite Elevations
268 // ----------------------------
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();
274
275 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
276 double rho = rr.norm_Frobenius();
277
278 double neu[3];
279 xyz2neu(_ellBanc.data(), rr.data(), neu);
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]);
286
287 if (satData->eleSat < MINELE_GPS) {
288 delete satData;
289 iGPS.remove();
290 }
291 }
292
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
311 if (satData->eleSat < MINELE_GLO) {
312 delete satData;
313 iGlo.remove();
314 }
315 }
316
317 return success;
318}
319
320// Computed Value
321////////////////////////////////////////////////////////////////////////////
322double bncModel::cmpValue(t_satData* satData, bool phase) {
323
324 ColumnVector xRec(3);
325 xRec(1) = x();
326 xRec(2) = y();
327 xRec(3) = z();
328
329 double rho0 = (satData->xx - xRec).norm_Frobenius();
330 double dPhi = t_CST::omega * rho0 / t_CST::c;
331
332 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
333 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
334 xRec(3) = z();
335
336 tides(_time, xRec);
337
338 satData->rho = (satData->xx - xRec).norm_Frobenius();
339
340 double tropDelay = delay_saast(satData->eleSat) +
341 trp() / sin(satData->eleSat);
342
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;
349}
350
351// Tropospheric Model (Saastamoinen)
352////////////////////////////////////////////////////////////////////////////
353double bncModel::delay_saast(double Ele) {
354
355 double height = _ellBanc(3);
356
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);
360 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
361
362 double h_km = height / 1000.0;
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
384// Prediction Step of the Filter
385////////////////////////////////////////////////////////////////////////////
386void bncModel::predict(t_epoData* epoData) {
387
388 bncSettings settings;
389
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
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) {
414 if (quickStartInit) {
415 pp->xx = settings.value("pppRefCrdX").toDouble();
416 }
417 else {
418 pp->xx = _xcBanc(1);
419 }
420 }
421 _QQ(iPar,iPar) += sig_crd_p * sig_crd_p;
422 }
423 else if (pp->type == bncParam::CRD_Y) {
424 if (firstCrd || !_static) {
425 if (quickStartInit) {
426 pp->xx = settings.value("pppRefCrdY").toDouble();
427 }
428 else {
429 pp->xx = _xcBanc(2);
430 }
431 }
432 _QQ(iPar,iPar) += sig_crd_p * sig_crd_p;
433 }
434 else if (pp->type == bncParam::CRD_Z) {
435 if (firstCrd || !_static) {
436 if (quickStartInit) {
437 pp->xx = settings.value("pppRefCrdZ").toDouble();
438 }
439 else {
440 pp->xx = _xcBanc(3);
441 }
442 }
443 _QQ(iPar,iPar) += sig_crd_p * sig_crd_p;
444 }
445
446 // Receiver Clocks
447 // ---------------
448 else if (pp->type == bncParam::RECCLK) {
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 // --------------------------------
465 if (_usePhase) {
466
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) {
484 if (epoData->satDataGPS.find(par->prn) == epoData->satDataGPS.end() &&
485 epoData->satDataGlo.find(par->prn) == epoData->satDataGlo.end() ) {
486 removed = true;
487 delete par;
488 it.remove();
489 }
490 }
491 if (! removed) {
492 ++iPar;
493 par->index = iPar;
494 }
495 }
496
497 // Add new ambiguity parameters
498 // ----------------------------
499 QMapIterator<QString, t_satData*> iGPS(epoData->satDataGPS);
500 while (iGPS.hasNext()) {
501 iGPS.next();
502 QString prn = iGPS.key();
503 t_satData* satData = iGPS.value();
504 bool found = false;
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 }
511 }
512 if (!found) {
513 bncParam* par = new bncParam(bncParam::AMB_L3, _params.size()+1, prn);
514 _params.push_back(par);
515 par->xx = satData->L3 - cmpValue(satData, true);
516 }
517 }
518
519 QMapIterator<QString, t_satData*> iGlo(epoData->satDataGlo);
520 while (iGlo.hasNext()) {
521 iGlo.next();
522 QString prn = iGlo.key();
523 t_satData* satData = iGlo.value();
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);
535 par->xx = satData->L3 - cmpValue(satData, true);
536 }
537 }
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 }
550 }
551 }
552 }
553
554 for (int ii = 1; ii <= nPar; ii++) {
555 bncParam* par = _params[ii-1];
556 if (par->index_old == 0) {
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 }
563 }
564 par->index_old = par->index;
565 }
566 }
567
568}
569
570// Update Step of the Filter (currently just a single-epoch solution)
571////////////////////////////////////////////////////////////////////////////
572t_irc bncModel::update(t_epoData* epoData) {
573
574 bncSettings settings;
575 double sig_P3;
576 sig_P3 = 5.0;
577 if ( Qt::CheckState(settings.value("pppUsePhase").toInt()) == Qt::Checked ) {
578 sig_P3 = settings.value("pppSigmaCode").toDouble();
579 if (sig_P3 < 0.3 || sig_P3 > 50.0) {
580 sig_P3 = 5.0;
581 }
582 }
583
584 _log.clear();
585
586 _time = epoData->tt;
587
588 _log += "Single Point Positioning of Epoch "
589 + QByteArray(_time.timestr(1).c_str()) +
590 "\n--------------------------------------------------------------\n";
591
592 SymmetricMatrix QQsav;
593 ColumnVector dx;
594 ColumnVector vv;
595
596 // Loop over all outliers
597 // ----------------------
598 do {
599
600 // Bancroft Solution
601 // -----------------
602 if (cmpBancroft(epoData) != success) {
603 emit newMessage(_log, false);
604 return failure;
605 }
606
607 // Status Prediction
608 // -----------------
609 predict(epoData);
610
611 // Create First-Design Matrix
612 // --------------------------
613 unsigned nPar = _params.size();
614 unsigned nObs = 0;
615 if (_usePhase) {
616 nObs = 2 * epoData->sizeGPS() + epoData->sizeGlo();
617 }
618 else {
619 nObs = epoData->sizeGPS(); // Glonass pseudoranges are not used
620 }
621
622 if (nObs < nPar) {
623 _log += "bncModel::update: nObs < nPar\n";
624 emit newMessage(_log, false);
625 return failure;
626 }
627
628 Matrix AA(nObs, nPar); // first design matrix
629 ColumnVector ll(nObs); // tems observed-computed
630 DiagonalMatrix PP(nObs); PP = 0.0;
631
632 unsigned iObs = 0;
633
634 // GPS code and (optionally) phase observations
635 // --------------------------------------------
636 QMapIterator<QString, t_satData*> itGPS(epoData->satDataGPS);
637 while (itGPS.hasNext()) {
638 ++iObs;
639 itGPS.next();
640 QString prn = itGPS.key();
641 t_satData* satData = itGPS.value();
642
643 ll(iObs) = satData->P3 - cmpValue(satData, false);
644 PP(iObs,iObs) = 1.0 / (sig_P3 * sig_P3);
645 for (int iPar = 1; iPar <= _params.size(); iPar++) {
646 AA(iObs, iPar) = _params[iPar-1]->partial(satData, false);
647 }
648
649 if (_usePhase) {
650 ++iObs;
651 ll(iObs) = satData->L3 - cmpValue(satData, true);
652 PP(iObs,iObs) = 1.0 / (sig_L3_GPS * sig_L3_GPS);
653 for (int iPar = 1; iPar <= _params.size(); iPar++) {
654 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
655 _params[iPar-1]->prn == prn) {
656 ll(iObs) -= _params[iPar-1]->xx;
657 }
658 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
659 }
660 }
661 }
662
663 // Glonass phase observations
664 // --------------------------
665 if (_usePhase) {
666 QMapIterator<QString, t_satData*> itGlo(epoData->satDataGlo);
667 while (itGlo.hasNext()) {
668 ++iObs;
669 itGlo.next();
670 QString prn = itGlo.key();
671 t_satData* satData = itGlo.value();
672
673 ll(iObs) = satData->L3 - cmpValue(satData, true);
674 PP(iObs,iObs) = 1.0 / (sig_L3_GLO * sig_L3_GLO);
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 }
680 AA(iObs, iPar) = _params[iPar-1]->partial(satData, true);
681 }
682 }
683 }
684
685 // Compute Filter Update
686 // ---------------------
687 QQsav = _QQ;
688
689 kalman(AA, ll, PP, _QQ, dx);
690
691 vv = ll - AA * dx;
692
693 ostringstream strA;
694 strA.setf(ios::fixed);
695 ColumnVector vv_code(epoData->sizeGPS());
696 ColumnVector vv_phase(epoData->sizeGPS());
697 ColumnVector vv_glo(epoData->sizeGlo());
698
699 for (unsigned iobs = 1; iobs <= epoData->sizeGPS(); ++iobs) {
700 if (_usePhase) {
701 vv_code(iobs) = vv(2*iobs-1);
702 vv_phase(iobs) = vv(2*iobs);
703 }
704 else {
705 vv_code(iobs) = vv(iobs);
706 }
707 }
708 if (_useGlonass) {
709 for (unsigned iobs = 1; iobs <= epoData->sizeGlo(); ++iobs) {
710 vv_glo(iobs) = vv(2*epoData->sizeGPS()+iobs);
711 }
712 }
713
714 strA << "residuals code " << setw(8) << setprecision(3) << vv_code.t();
715 if (_usePhase) {
716 strA << "residuals phase " << setw(8) << setprecision(3) << vv_phase.t();
717 }
718 if (_useGlonass) {
719 strA << "residuals glo " << setw(8) << setprecision(3) << vv_glo.t();
720 }
721 _log += strA.str().c_str();
722
723 } while (outlierDetection(QQsav, vv, epoData->satDataGPS,
724 epoData->satDataGlo) != 0);
725
726 // Remember the Epoch-specific Results for the computation of means
727 // ----------------------------------------------------------------
728 pppPos* newPos = new pppPos;
729 newPos->time = epoData->tt;
730
731 // Set Solution Vector
732 // -------------------
733 ostringstream strB;
734 strB.setf(ios::fixed);
735 QVectorIterator<bncParam*> itPar(_params);
736 while (itPar.hasNext()) {
737 bncParam* par = itPar.next();
738 par->xx += dx(par->index);
739
740 if (par->type == bncParam::RECCLK) {
741 strB << "\n clk = " << setw(6) << setprecision(3) << par->xx
742 << " +- " << setw(6) << setprecision(3)
743 << sqrt(_QQ(par->index,par->index));
744 }
745 else if (par->type == bncParam::AMB_L3) {
746 strB << "\n amb " << par->prn.toAscii().data() << " = "
747 << setw(6) << setprecision(3) << par->xx
748 << " +- " << setw(6) << setprecision(3)
749 << sqrt(_QQ(par->index,par->index));
750 }
751 else if (par->type == bncParam::TROPO) {
752 double aprTrp = delay_saast(M_PI/2.0);
753 strB << "\n trp = " << par->prn.toAscii().data()
754 << setw(7) << setprecision(3) << aprTrp << " "
755 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
756 << " +- " << setw(6) << setprecision(3)
757 << sqrt(_QQ(par->index,par->index));
758 newPos->xnt[6] = aprTrp + par->xx;
759 }
760 }
761 strB << '\n';
762 _log += strB.str().c_str();
763 emit newMessage(_log, false);
764
765 // Final Message (both log file and screen)
766 // ----------------------------------------
767 ostringstream strC;
768 strC.setf(ios::fixed);
769 strC << _staID.data() << " PPP "
770 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
771 << setw(14) << setprecision(3) << x() << " +- "
772 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
773 << setw(14) << setprecision(3) << y() << " +- "
774 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
775 << setw(14) << setprecision(3) << z() << " +- "
776 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
777
778 // NEU Output
779 // ----------
780 if (settings.value("pppOrigin").toString() == "Plot - X Y Z" ||
781 settings.value("pppOrigin").toString() == "QuickStart - Static") {
782
783 double xyzRef[3];
784 xyzRef[0] = settings.value("pppRefCrdX").toDouble();
785 xyzRef[1] = settings.value("pppRefCrdY").toDouble();
786 xyzRef[2] = settings.value("pppRefCrdZ").toDouble();
787
788 newPos->xnt[0] = x() - xyzRef[0];
789 newPos->xnt[1] = y() - xyzRef[1];
790 newPos->xnt[2] = z() - xyzRef[2];
791
792 double ellRef[3];
793 xyz2ell(xyzRef, ellRef);
794 xyz2neu(ellRef, newPos->xnt, &newPos->xnt[3]);
795
796 strC << " NEU "
797 << setw(8) << setprecision(3) << newPos->xnt[3] << " "
798 << setw(8) << setprecision(3) << newPos->xnt[4] << " "
799 << setw(8) << setprecision(3) << newPos->xnt[5];
800
801 }
802
803 emit newMessage(QByteArray(strC.str().c_str()), true);
804
805 if (settings.value("pppAverage").toString() == "") {
806 delete newPos;
807 }
808 else {
809
810 _posAverage.push_back(newPos);
811
812 // Time Span for Average Computation
813 // ---------------------------------
814 double tRangeAverage = settings.value("pppAverage").toDouble() * 60.;
815 if (tRangeAverage < 0) {
816 tRangeAverage = 0;
817 }
818 if (tRangeAverage > 86400) {
819 tRangeAverage = 86400;
820 }
821
822 // Compute the Mean
823 // ----------------
824 ColumnVector mean(7); mean = 0.0;
825
826 QMutableVectorIterator<pppPos*> it(_posAverage);
827 while (it.hasNext()) {
828 pppPos* pp = it.next();
829 if ( (epoData->tt - pp->time) >= tRangeAverage ) {
830 delete pp;
831 it.remove();
832 }
833 else {
834 for (int ii = 0; ii < 7; ++ii) {
835 mean[ii] += pp->xnt[ii];
836 }
837 }
838 }
839
840 int nn = _posAverage.size();
841
842 if (nn > 0) {
843
844 mean /= nn;
845
846 // Compute the Deviation
847 // ---------------------
848 ColumnVector std(7); std = 0.0;
849 QVectorIterator<pppPos*> it2(_posAverage);
850 while (it2.hasNext()) {
851 pppPos* pp = it2.next();
852 for (int ii = 0; ii < 7; ++ii) {
853 std[ii] += (pp->xnt[ii] - mean[ii]) * (pp->xnt[ii] - mean[ii]);
854 }
855 }
856 for (int ii = 0; ii < 7; ++ii) {
857 std[ii] = sqrt(std[ii] / nn);
858 }
859
860 ostringstream strD; strD.setf(ios::fixed);
861 strD << _staID.data() << " AVE-XYZ "
862 << epoData->tt.timestr(1) << " "
863 << setw(13) << setprecision(3) << mean[0] << " +- "
864 << setw(6) << setprecision(3) << std[0] << " "
865 << setw(14) << setprecision(3) << mean[1] << " +- "
866 << setw(6) << setprecision(3) << std[1] << " "
867 << setw(14) << setprecision(3) << mean[2] << " +- "
868 << setw(6) << setprecision(3) << std[2];
869 emit newMessage(QByteArray(strD.str().c_str()), true);
870
871 ostringstream strE; strE.setf(ios::fixed);
872 strE << _staID.data() << " AVE-NEU "
873 << epoData->tt.timestr(1) << " "
874 << setw(13) << setprecision(3) << mean[3] << " +- "
875 << setw(6) << setprecision(3) << std[3] << " "
876 << setw(14) << setprecision(3) << mean[4] << " +- "
877 << setw(6) << setprecision(3) << std[4] << " "
878 << setw(14) << setprecision(3) << mean[5] << " +- "
879 << setw(6) << setprecision(3) << std[5];
880
881 emit newMessage(QByteArray(strE.str().c_str()), true);
882
883 ostringstream strF; strF.setf(ios::fixed);
884 strF << _staID.data() << " AVE-TRP "
885 << epoData->tt.timestr(1) << " "
886 << setw(13) << setprecision(3) << mean[6] << " +- "
887 << setw(6) << setprecision(3) << std[6] << endl;
888
889 emit newMessage(QByteArray(strF.str().c_str()), true);
890 }
891 }
892
893 // NMEA Output
894 // -----------
895 double xyz[3];
896 xyz[0] = x();
897 xyz[1] = y();
898 xyz[2] = z();
899 double ell[3];
900 xyz2ell(xyz, ell);
901 double phiDeg = ell[0] * 180 / M_PI;
902 double lamDeg = ell[1] * 180 / M_PI;
903
904 char phiCh = 'N';
905 if (phiDeg < 0) {
906 phiDeg = -phiDeg;
907 phiCh = 'S';
908 }
909 char lamCh = 'E';
910 if (lamDeg < 0) {
911 lamDeg = -lamDeg;
912 lamCh = 'W';
913 }
914
915 string datestr = epoData->tt.datestr(0); // yyyymmdd
916 ostringstream strRMC;
917 strRMC.setf(ios::fixed);
918 strRMC << "GPRMC,"
919 << epoData->tt.timestr(0,0) << ",A,"
920 << setw(2) << setfill('0') << int(phiDeg)
921 << setw(6) << setprecision(3) << setfill('0')
922 << fmod(60*phiDeg,60) << ',' << phiCh << ','
923 << setw(3) << setfill('0') << int(lamDeg)
924 << setw(6) << setprecision(3) << setfill('0')
925 << fmod(60*lamDeg,60) << ',' << lamCh << ",,,"
926 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
927 << datestr[2] << datestr[3] << ",,";
928
929 writeNMEAstr(QString(strRMC.str().c_str()));
930
931 double dop = 2.0; // TODO
932
933 ostringstream strGGA;
934 strGGA.setf(ios::fixed);
935 strGGA << "GPGGA,"
936 << epoData->tt.timestr(0,0) << ','
937 << setw(2) << setfill('0') << int(phiDeg)
938 << setw(10) << setprecision(7) << setfill('0')
939 << fmod(60*phiDeg,60) << ',' << phiCh << ','
940 << setw(3) << setfill('0') << int(lamDeg)
941 << setw(10) << setprecision(7) << setfill('0')
942 << fmod(60*lamDeg,60) << ',' << lamCh
943 << ",1," << setw(2) << setfill('0') << epoData->sizeAll() << ','
944 << setw(3) << setprecision(1) << dop << ','
945 << setprecision(3) << ell[2] << ",M,0.0,M,,";
946
947 writeNMEAstr(QString(strGGA.str().c_str()));
948
949 return success;
950}
951
952// Outlier Detection
953////////////////////////////////////////////////////////////////////////////
954int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
955 const ColumnVector& vv,
956 QMap<QString, t_satData*>& satDataGPS,
957 QMap<QString, t_satData*>& satDataGlo) {
958
959 double vvMaxCodeGPS = 0.0;
960 double vvMaxPhaseGPS = 0.0;
961 double vvMaxPhaseGlo = 0.0;
962 QMutableMapIterator<QString, t_satData*> itMaxCodeGPS(satDataGPS);
963 QMutableMapIterator<QString, t_satData*> itMaxPhaseGPS(satDataGPS);
964 QMutableMapIterator<QString, t_satData*> itMaxPhaseGlo(satDataGlo);
965
966 int ii = 0;
967
968 // GPS code and (optionally) phase residuals
969 // -----------------------------------------
970 QMutableMapIterator<QString, t_satData*> itGPS(satDataGPS);
971 while (itGPS.hasNext()) {
972 itGPS.next();
973 ++ii;
974
975 if (vvMaxCodeGPS == 0.0 || fabs(vv(ii)) > vvMaxCodeGPS) {
976 vvMaxCodeGPS = fabs(vv(ii));
977 itMaxCodeGPS = itGPS;
978 }
979
980 if (_usePhase) {
981 ++ii;
982 if (vvMaxPhaseGPS == 0.0 || fabs(vv(ii)) > vvMaxPhaseGPS) {
983 vvMaxPhaseGPS = fabs(vv(ii));
984 itMaxPhaseGPS = itGPS;
985 }
986 }
987 }
988
989 // Glonass phase residuals
990 // -----------------------
991 if (_usePhase) {
992 QMutableMapIterator<QString, t_satData*> itGlo(satDataGlo);
993 while (itGlo.hasNext()) {
994 itGlo.next();
995 ++ii;
996 if (vvMaxPhaseGlo == 0.0 || fabs(vv(ii)) > vvMaxPhaseGlo) {
997 vvMaxPhaseGlo = fabs(vv(ii));
998 itMaxPhaseGlo = itGlo;
999 }
1000 }
1001 }
1002
1003 if (vvMaxPhaseGlo > MAXRES_PHASE_GLO) {
1004 QString prn = itMaxPhaseGlo.key();
1005 t_satData* satData = itMaxPhaseGlo.value();
1006 delete satData;
1007 itMaxPhaseGlo.remove();
1008 _QQ = QQsav;
1009
1010 _log += "Outlier Phase " + prn.toAscii() + " "
1011 + QByteArray::number(vvMaxPhaseGlo, 'f', 3) + "\n";
1012
1013 return 1;
1014 }
1015
1016 else if (vvMaxCodeGPS > MAXRES_CODE_GPS) {
1017 QString prn = itMaxCodeGPS.key();
1018 t_satData* satData = itMaxCodeGPS.value();
1019 delete satData;
1020 itMaxCodeGPS.remove();
1021 _QQ = QQsav;
1022
1023 _log += "Outlier Code " + prn.toAscii() + " "
1024 + QByteArray::number(vvMaxCodeGPS, 'f', 3) + "\n";
1025
1026 return 1;
1027 }
1028 else if (vvMaxPhaseGPS > MAXRES_PHASE_GPS) {
1029 QString prn = itMaxPhaseGPS.key();
1030 t_satData* satData = itMaxPhaseGPS.value();
1031 delete satData;
1032 itMaxPhaseGPS.remove();
1033 _QQ = QQsav;
1034
1035 _log += "Outlier Phase " + prn.toAscii() + " "
1036 + QByteArray::number(vvMaxPhaseGPS, 'f', 3) + "\n";
1037
1038 return 1;
1039 }
1040
1041 return 0;
1042}
1043
1044//
1045////////////////////////////////////////////////////////////////////////////
1046void bncModel::writeNMEAstr(const QString& nmStr) {
1047
1048 unsigned char XOR = 0;
1049 for (int ii = 0; ii < nmStr.length(); ii++) {
1050 XOR ^= (unsigned char) nmStr[ii].toAscii();
1051 }
1052
1053 QString outStr = '$' + nmStr
1054 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
1055
1056 if (_nmeaStream) {
1057 *_nmeaStream << outStr;
1058 _nmeaStream->flush();
1059 }
1060
1061 emit newNMEAstr(outStr.toAscii());
1062}
1063
1064////
1065//////////////////////////////////////////////////////////////////////////////
1066void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
1067 const DiagonalMatrix& PP,
1068 SymmetricMatrix& QQ, ColumnVector& dx) {
1069
1070 int nObs = AA.Nrows();
1071 int nPar = AA.Ncols();
1072
1073 UpperTriangularMatrix SS = Cholesky(QQ).t();
1074
1075 Matrix SA = SS*AA.t();
1076 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
1077 for (int ii = 1; ii <= nObs; ++ii) {
1078 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
1079 }
1080
1081 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
1082 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
1083
1084 UpperTriangularMatrix UU;
1085 QRZ(SRF, UU);
1086
1087 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
1088 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
1089 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
1090
1091 UpperTriangularMatrix SHi = SH_rt.i();
1092
1093 Matrix KT = SHi * YY;
1094 SymmetricMatrix Hi; Hi << SHi * SHi.t();
1095
1096 dx = KT.t() * ll;
1097 QQ << (SS.t() * SS);
1098}
1099
1100// Phase Wind-Up Correction
1101///////////////////////////////////////////////////////////////////////////
1102double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
1103 const ColumnVector& rRec) {
1104
1105 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
1106
1107 // First time - initialize to zero
1108 // -------------------------------
1109 if (!_windUpTime.contains(prn)) {
1110 _windUpTime[prn] = Mjd;
1111 _windUpSum[prn] = 0.0;
1112 }
1113
1114 // Compute the correction for new time
1115 // -----------------------------------
1116 else if (_windUpTime[prn] != Mjd) {
1117 _windUpTime[prn] = Mjd;
1118
1119 // Unit Vector GPS Satellite --> Receiver
1120 // --------------------------------------
1121 ColumnVector rho = rRec - rSat;
1122 rho /= rho.norm_Frobenius();
1123
1124 // GPS Satellite unit Vectors sz, sy, sx
1125 // -------------------------------------
1126 ColumnVector sz = -rSat / rSat.norm_Frobenius();
1127
1128 ColumnVector xSun = Sun(Mjd);
1129 xSun /= xSun.norm_Frobenius();
1130
1131 ColumnVector sy = crossproduct(sz, xSun);
1132 ColumnVector sx = crossproduct(sy, sz);
1133
1134 // Effective Dipole of the GPS Satellite Antenna
1135 // ---------------------------------------------
1136 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
1137 - crossproduct(rho, sy);
1138
1139 // Receiver unit Vectors rx, ry
1140 // ----------------------------
1141 ColumnVector rx(3);
1142 ColumnVector ry(3);
1143
1144 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
1145 double neu[3];
1146
1147 neu[0] = 1.0;
1148 neu[1] = 0.0;
1149 neu[2] = 0.0;
1150 neu2xyz(recEll, neu, rx.data());
1151
1152 neu[0] = 0.0;
1153 neu[1] = -1.0;
1154 neu[2] = 0.0;
1155 neu2xyz(recEll, neu, ry.data());
1156
1157 // Effective Dipole of the Receiver Antenna
1158 // ----------------------------------------
1159 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
1160 + crossproduct(rho, ry);
1161
1162 // Resulting Effect
1163 // ----------------
1164 double alpha = DotProduct(dipSat,dipRec) /
1165 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
1166
1167 if (alpha > 1.0) alpha = 1.0;
1168 if (alpha < -1.0) alpha = -1.0;
1169
1170 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
1171
1172 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
1173 dphi = -dphi;
1174 }
1175
1176 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
1177 }
1178
1179 return _windUpSum[prn];
1180}
Note: See TracBrowser for help on using the repository browser.