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

Last change on this file since 2670 was 2670, 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 _posAverage.push_back(newPos);
731
732 // Set Solution Vector
733 // -------------------
734 ostringstream strB;
735 strB.setf(ios::fixed);
736 QVectorIterator<bncParam*> itPar(_params);
737 while (itPar.hasNext()) {
738 bncParam* par = itPar.next();
739 par->xx += dx(par->index);
740
741 if (par->type == bncParam::RECCLK) {
742 strB << "\n clk = " << setw(6) << setprecision(3) << par->xx
743 << " +- " << setw(6) << setprecision(3)
744 << sqrt(_QQ(par->index,par->index));
745 }
746 else if (par->type == bncParam::AMB_L3) {
747 strB << "\n amb " << par->prn.toAscii().data() << " = "
748 << setw(6) << setprecision(3) << par->xx
749 << " +- " << setw(6) << setprecision(3)
750 << sqrt(_QQ(par->index,par->index));
751 }
752 else if (par->type == bncParam::TROPO) {
753 double aprTrp = delay_saast(M_PI/2.0);
754 strB << "\n trp = " << par->prn.toAscii().data()
755 << setw(7) << setprecision(3) << aprTrp << " "
756 << setw(6) << setprecision(3) << showpos << par->xx << noshowpos
757 << " +- " << setw(6) << setprecision(3)
758 << sqrt(_QQ(par->index,par->index));
759 newPos->xnt[6] = aprTrp + par->xx;
760 }
761 }
762 strB << '\n';
763 _log += strB.str().c_str();
764 emit newMessage(_log, false);
765
766 // Final Message (both log file and screen)
767 // ----------------------------------------
768 ostringstream strC;
769 strC.setf(ios::fixed);
770 strC << _staID.data() << " PPP "
771 << epoData->tt.timestr(1) << " " << epoData->sizeAll() << " "
772 << setw(14) << setprecision(3) << x() << " +- "
773 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
774 << setw(14) << setprecision(3) << y() << " +- "
775 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
776 << setw(14) << setprecision(3) << z() << " +- "
777 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
778
779 // NEU Output
780 // ----------
781 if (settings.value("pppOrigin").toString() == "Plot - X Y Z" ||
782 settings.value("pppOrigin").toString() == "QuickStart - Static") {
783
784 double xyzRef[3];
785 xyzRef[0] = settings.value("pppRefCrdX").toDouble();
786 xyzRef[1] = settings.value("pppRefCrdY").toDouble();
787 xyzRef[2] = settings.value("pppRefCrdZ").toDouble();
788
789 newPos->xnt[0] = x() - xyzRef[0];
790 newPos->xnt[1] = y() - xyzRef[1];
791 newPos->xnt[2] = z() - xyzRef[2];
792
793 double ellRef[3];
794 xyz2ell(xyzRef, ellRef);
795 xyz2neu(ellRef, newPos->xnt, &newPos->xnt[3]);
796
797 strC << " NEU "
798 << setw(8) << setprecision(3) << newPos->xnt[3] << " "
799 << setw(8) << setprecision(3) << newPos->xnt[4] << " "
800 << setw(8) << setprecision(3) << newPos->xnt[5];
801
802 }
803
804 emit newMessage(QByteArray(strC.str().c_str()), true);
805
806 if (settings.value("pppAverage").toString() != "") {
807
808 // Time Span for Average Computation
809 // ---------------------------------
810 double tRangeAverage = settings.value("pppAverage").toDouble() * 60.;
811 if (tRangeAverage < 0) {
812 tRangeAverage = 0;
813 }
814 if (tRangeAverage > 86400) {
815 tRangeAverage = 86400;
816 }
817
818 // Compute the Mean
819 // ----------------
820 ColumnVector mean(7); mean = 0.0;
821
822 QMutableVectorIterator<pppPos*> it(_posAverage);
823 while (it.hasNext()) {
824 pppPos* pp = it.next();
825 if ( (epoData->tt - pp->time) >= tRangeAverage ) {
826 delete pp;
827 it.remove();
828 }
829 else {
830 for (int ii = 0; ii < 7; ++ii) {
831 mean[ii] += pp->xnt[ii];
832 }
833 }
834 }
835
836 int nn = _posAverage.size();
837
838 if (nn > 0) {
839
840 mean /= nn;
841
842 // Compute the Deviation
843 // ---------------------
844 ColumnVector std(7); std = 0.0;
845 QVectorIterator<pppPos*> it2(_posAverage);
846 while (it2.hasNext()) {
847 pppPos* pp = it2.next();
848 for (int ii = 0; ii < 7; ++ii) {
849 std[ii] += (pp->xnt[ii] - mean[ii]) * (pp->xnt[ii] - mean[ii]);
850 }
851 }
852 for (int ii = 0; ii < 7; ++ii) {
853 std[ii] = sqrt(std[ii] / nn);
854 }
855
856 ostringstream strD; strD.setf(ios::fixed);
857 strD << _staID.data() << " AVE-XYZ "
858 << epoData->tt.timestr(1) << " "
859 << setw(13) << setprecision(3) << mean[0] << " +- "
860 << setw(6) << setprecision(3) << std[0] << " "
861 << setw(14) << setprecision(3) << mean[1] << " +- "
862 << setw(6) << setprecision(3) << std[1] << " "
863 << setw(14) << setprecision(3) << mean[2] << " +- "
864 << setw(6) << setprecision(3) << std[2];
865 emit newMessage(QByteArray(strD.str().c_str()), true);
866
867 ostringstream strE; strE.setf(ios::fixed);
868 strE << _staID.data() << " AVE-NEU "
869 << epoData->tt.timestr(1) << " "
870 << setw(13) << setprecision(3) << mean[3] << " +- "
871 << setw(6) << setprecision(3) << std[3] << " "
872 << setw(14) << setprecision(3) << mean[4] << " +- "
873 << setw(6) << setprecision(3) << std[4] << " "
874 << setw(14) << setprecision(3) << mean[5] << " +- "
875 << setw(6) << setprecision(3) << std[5];
876
877 emit newMessage(QByteArray(strE.str().c_str()), true);
878
879 ostringstream strF; strF.setf(ios::fixed);
880 strF << _staID.data() << " AVE-TRP "
881 << epoData->tt.timestr(1) << " "
882 << setw(13) << setprecision(3) << mean[6] << " +- "
883 << setw(6) << setprecision(3) << std[6] << endl;
884
885 emit newMessage(QByteArray(strF.str().c_str()), true);
886 }
887 }
888
889 // NMEA Output
890 // -----------
891 double xyz[3];
892 xyz[0] = x();
893 xyz[1] = y();
894 xyz[2] = z();
895 double ell[3];
896 xyz2ell(xyz, ell);
897 double phiDeg = ell[0] * 180 / M_PI;
898 double lamDeg = ell[1] * 180 / M_PI;
899
900 char phiCh = 'N';
901 if (phiDeg < 0) {
902 phiDeg = -phiDeg;
903 phiCh = 'S';
904 }
905 char lamCh = 'E';
906 if (lamDeg < 0) {
907 lamDeg = -lamDeg;
908 lamCh = 'W';
909 }
910
911 string datestr = epoData->tt.datestr(0); // yyyymmdd
912 ostringstream strRMC;
913 strRMC.setf(ios::fixed);
914 strRMC << "GPRMC,"
915 << epoData->tt.timestr(0,0) << ",A,"
916 << setw(2) << setfill('0') << int(phiDeg)
917 << setw(6) << setprecision(3) << setfill('0')
918 << fmod(60*phiDeg,60) << ',' << phiCh << ','
919 << setw(3) << setfill('0') << int(lamDeg)
920 << setw(6) << setprecision(3) << setfill('0')
921 << fmod(60*lamDeg,60) << ',' << lamCh << ",,,"
922 << datestr[6] << datestr[7] << datestr[4] << datestr[5]
923 << datestr[2] << datestr[3] << ",,";
924
925 writeNMEAstr(QString(strRMC.str().c_str()));
926
927 double dop = 2.0; // TODO
928
929 ostringstream strGGA;
930 strGGA.setf(ios::fixed);
931 strGGA << "GPGGA,"
932 << epoData->tt.timestr(0,0) << ','
933 << setw(2) << setfill('0') << int(phiDeg)
934 << setw(10) << setprecision(7) << setfill('0')
935 << fmod(60*phiDeg,60) << ',' << phiCh << ','
936 << setw(3) << setfill('0') << int(lamDeg)
937 << setw(10) << setprecision(7) << setfill('0')
938 << fmod(60*lamDeg,60) << ',' << lamCh
939 << ",1," << setw(2) << setfill('0') << epoData->sizeAll() << ','
940 << setw(3) << setprecision(1) << dop << ','
941 << setprecision(3) << ell[2] << ",M,0.0,M,,";
942
943 writeNMEAstr(QString(strGGA.str().c_str()));
944
945 return success;
946}
947
948// Outlier Detection
949////////////////////////////////////////////////////////////////////////////
950int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
951 const ColumnVector& vv,
952 QMap<QString, t_satData*>& satDataGPS,
953 QMap<QString, t_satData*>& satDataGlo) {
954
955 double vvMaxCodeGPS = 0.0;
956 double vvMaxPhaseGPS = 0.0;
957 double vvMaxPhaseGlo = 0.0;
958 QMutableMapIterator<QString, t_satData*> itMaxCodeGPS(satDataGPS);
959 QMutableMapIterator<QString, t_satData*> itMaxPhaseGPS(satDataGPS);
960 QMutableMapIterator<QString, t_satData*> itMaxPhaseGlo(satDataGlo);
961
962 int ii = 0;
963
964 // GPS code and (optionally) phase residuals
965 // -----------------------------------------
966 QMutableMapIterator<QString, t_satData*> itGPS(satDataGPS);
967 while (itGPS.hasNext()) {
968 itGPS.next();
969 ++ii;
970
971 if (vvMaxCodeGPS == 0.0 || fabs(vv(ii)) > vvMaxCodeGPS) {
972 vvMaxCodeGPS = fabs(vv(ii));
973 itMaxCodeGPS = itGPS;
974 }
975
976 if (_usePhase) {
977 ++ii;
978 if (vvMaxPhaseGPS == 0.0 || fabs(vv(ii)) > vvMaxPhaseGPS) {
979 vvMaxPhaseGPS = fabs(vv(ii));
980 itMaxPhaseGPS = itGPS;
981 }
982 }
983 }
984
985 // Glonass phase residuals
986 // -----------------------
987 if (_usePhase) {
988 QMutableMapIterator<QString, t_satData*> itGlo(satDataGlo);
989 while (itGlo.hasNext()) {
990 itGlo.next();
991 ++ii;
992 if (vvMaxPhaseGlo == 0.0 || fabs(vv(ii)) > vvMaxPhaseGlo) {
993 vvMaxPhaseGlo = fabs(vv(ii));
994 itMaxPhaseGlo = itGlo;
995 }
996 }
997 }
998
999 if (vvMaxPhaseGlo > MAXRES_PHASE_GLO) {
1000 QString prn = itMaxPhaseGlo.key();
1001 t_satData* satData = itMaxPhaseGlo.value();
1002 delete satData;
1003 itMaxPhaseGlo.remove();
1004 _QQ = QQsav;
1005
1006 _log += "Outlier Phase " + prn.toAscii() + " "
1007 + QByteArray::number(vvMaxPhaseGlo, 'f', 3) + "\n";
1008
1009 return 1;
1010 }
1011
1012 else if (vvMaxCodeGPS > MAXRES_CODE_GPS) {
1013 QString prn = itMaxCodeGPS.key();
1014 t_satData* satData = itMaxCodeGPS.value();
1015 delete satData;
1016 itMaxCodeGPS.remove();
1017 _QQ = QQsav;
1018
1019 _log += "Outlier Code " + prn.toAscii() + " "
1020 + QByteArray::number(vvMaxCodeGPS, 'f', 3) + "\n";
1021
1022 return 1;
1023 }
1024 else if (vvMaxPhaseGPS > MAXRES_PHASE_GPS) {
1025 QString prn = itMaxPhaseGPS.key();
1026 t_satData* satData = itMaxPhaseGPS.value();
1027 delete satData;
1028 itMaxPhaseGPS.remove();
1029 _QQ = QQsav;
1030
1031 _log += "Outlier Phase " + prn.toAscii() + " "
1032 + QByteArray::number(vvMaxPhaseGPS, 'f', 3) + "\n";
1033
1034 return 1;
1035 }
1036
1037 return 0;
1038}
1039
1040//
1041////////////////////////////////////////////////////////////////////////////
1042void bncModel::writeNMEAstr(const QString& nmStr) {
1043
1044 unsigned char XOR = 0;
1045 for (int ii = 0; ii < nmStr.length(); ii++) {
1046 XOR ^= (unsigned char) nmStr[ii].toAscii();
1047 }
1048
1049 QString outStr = '$' + nmStr
1050 + QString("*%1\n").arg(int(XOR), 0, 16).toUpper();
1051
1052 if (_nmeaStream) {
1053 *_nmeaStream << outStr;
1054 _nmeaStream->flush();
1055 }
1056
1057 emit newNMEAstr(outStr.toAscii());
1058}
1059
1060////
1061//////////////////////////////////////////////////////////////////////////////
1062void bncModel::kalman(const Matrix& AA, const ColumnVector& ll,
1063 const DiagonalMatrix& PP,
1064 SymmetricMatrix& QQ, ColumnVector& dx) {
1065
1066 int nObs = AA.Nrows();
1067 int nPar = AA.Ncols();
1068
1069 UpperTriangularMatrix SS = Cholesky(QQ).t();
1070
1071 Matrix SA = SS*AA.t();
1072 Matrix SRF(nObs+nPar, nObs+nPar); SRF = 0;
1073 for (int ii = 1; ii <= nObs; ++ii) {
1074 SRF(ii,ii) = 1.0 / sqrt(PP(ii,ii));
1075 }
1076
1077 SRF.SubMatrix (nObs+1, nObs+nPar, 1, nObs) = SA;
1078 SRF.SymSubMatrix(nObs+1, nObs+nPar) = SS;
1079
1080 UpperTriangularMatrix UU;
1081 QRZ(SRF, UU);
1082
1083 SS = UU.SymSubMatrix(nObs+1, nObs+nPar);
1084 UpperTriangularMatrix SH_rt = UU.SymSubMatrix(1, nObs);
1085 Matrix YY = UU.SubMatrix(1, nObs, nObs+1, nObs+nPar);
1086
1087 UpperTriangularMatrix SHi = SH_rt.i();
1088
1089 Matrix KT = SHi * YY;
1090 SymmetricMatrix Hi; Hi << SHi * SHi.t();
1091
1092 dx = KT.t() * ll;
1093 QQ << (SS.t() * SS);
1094}
1095
1096// Phase Wind-Up Correction
1097///////////////////////////////////////////////////////////////////////////
1098double bncModel::windUp(const QString& prn, const ColumnVector& rSat,
1099 const ColumnVector& rRec) {
1100
1101 double Mjd = _time.mjd() + _time.daysec() / 86400.0;
1102
1103 // First time - initialize to zero
1104 // -------------------------------
1105 if (!_windUpTime.contains(prn)) {
1106 _windUpTime[prn] = Mjd;
1107 _windUpSum[prn] = 0.0;
1108 }
1109
1110 // Compute the correction for new time
1111 // -----------------------------------
1112 else if (_windUpTime[prn] != Mjd) {
1113 _windUpTime[prn] = Mjd;
1114
1115 // Unit Vector GPS Satellite --> Receiver
1116 // --------------------------------------
1117 ColumnVector rho = rRec - rSat;
1118 rho /= rho.norm_Frobenius();
1119
1120 // GPS Satellite unit Vectors sz, sy, sx
1121 // -------------------------------------
1122 ColumnVector sz = -rSat / rSat.norm_Frobenius();
1123
1124 ColumnVector xSun = Sun(Mjd);
1125 xSun /= xSun.norm_Frobenius();
1126
1127 ColumnVector sy = crossproduct(sz, xSun);
1128 ColumnVector sx = crossproduct(sy, sz);
1129
1130 // Effective Dipole of the GPS Satellite Antenna
1131 // ---------------------------------------------
1132 ColumnVector dipSat = sx - rho * DotProduct(rho,sx)
1133 - crossproduct(rho, sy);
1134
1135 // Receiver unit Vectors rx, ry
1136 // ----------------------------
1137 ColumnVector rx(3);
1138 ColumnVector ry(3);
1139
1140 double recEll[3]; xyz2ell(rRec.data(), recEll) ;
1141 double neu[3];
1142
1143 neu[0] = 1.0;
1144 neu[1] = 0.0;
1145 neu[2] = 0.0;
1146 neu2xyz(recEll, neu, rx.data());
1147
1148 neu[0] = 0.0;
1149 neu[1] = -1.0;
1150 neu[2] = 0.0;
1151 neu2xyz(recEll, neu, ry.data());
1152
1153 // Effective Dipole of the Receiver Antenna
1154 // ----------------------------------------
1155 ColumnVector dipRec = rx - rho * DotProduct(rho,rx)
1156 + crossproduct(rho, ry);
1157
1158 // Resulting Effect
1159 // ----------------
1160 double alpha = DotProduct(dipSat,dipRec) /
1161 (dipSat.norm_Frobenius() * dipRec.norm_Frobenius());
1162
1163 if (alpha > 1.0) alpha = 1.0;
1164 if (alpha < -1.0) alpha = -1.0;
1165
1166 double dphi = acos(alpha) / 2.0 / M_PI; // in cycles
1167
1168 if ( DotProduct(rho, crossproduct(dipSat, dipRec)) < 0.0 ) {
1169 dphi = -dphi;
1170 }
1171
1172 _windUpSum[prn] = floor(_windUpSum[prn] - dphi + 0.5) + dphi;
1173 }
1174
1175 return _windUpSum[prn];
1176}
Note: See TracBrowser for help on using the repository browser.