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

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