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

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