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

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