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

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