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

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