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

Last change on this file since 2285 was 2285, checked in by mervart, 14 years ago

* empty log message *

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