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

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