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

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