source: ntrip/trunk/BNC/src/bncmodel.cpp@ 4904

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