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

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