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

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