source: ntrip/trunk/BNC/bncmodel.cpp@ 2126

Last change on this file since 2126 was 2126, checked in by mervart, 14 years ago

* empty log message *

File size: 16.8 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 "bncapp.h"
48#include "bncpppclient.h"
49#include "bancroft.h"
50#include "bncutils.h"
51#include "bncsettings.h"
52
53using namespace std;
54
55const unsigned MINOBS = 4;
56const double MINELE = 10.0 * M_PI / 180.0;
57const double MAXRES_CODE = 10.0;
58const double MAXRES_PHASE = 0.10;
59const double sig_crd_0 = 100.0;
60const double sig_crd_p = 100.0;
61const double sig_clk_0 = 1000.0;
62const double sig_trp_0 = 0.01;
63const double sig_trp_p = 1e-6;
64const double sig_amb_0 = 100.0;
65const double sig_P3 = 1.0;
66const double sig_L3 = 0.01;
67
68// Constructor
69////////////////////////////////////////////////////////////////////////////
70bncParam::bncParam(bncParam::parType typeIn, int indexIn,
71 const QString& prnIn) {
72 type = typeIn;
73 index = indexIn;
74 prn = prnIn;
75 index_old = 0;
76 xx = 0.0;
77}
78
79// Destructor
80////////////////////////////////////////////////////////////////////////////
81bncParam::~bncParam() {
82}
83
84// Partial
85////////////////////////////////////////////////////////////////////////////
86double bncParam::partial(t_satData* satData, const QString& prnIn) {
87 if (type == CRD_X) {
88 return (xx - satData->xx(1)) / satData->rho;
89 }
90 else if (type == CRD_Y) {
91 return (xx - satData->xx(2)) / satData->rho;
92 }
93 else if (type == CRD_Z) {
94 return (xx - satData->xx(3)) / satData->rho;
95 }
96 else if (type == RECCLK) {
97 return 1.0;
98 }
99 else if (type == TROPO) {
100 return 1.0 / sin(satData->eleSat);
101 }
102 else if (type == AMB_L3) {
103 if (prnIn == prn) {
104 return 1.0;
105 }
106 else {
107 return 0.0;
108 }
109 }
110 return 0.0;
111}
112
113// Constructor
114////////////////////////////////////////////////////////////////////////////
115bncModel::bncModel(QByteArray staID) {
116
117 _staID = staID;
118
119 connect(this, SIGNAL(newMessage(QByteArray,bool)),
120 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
121
122 bncSettings settings;
123
124 _static = false;
125 if ( Qt::CheckState(settings.value("pppStatic").toInt()) == Qt::Checked) {
126 _static = true;
127 }
128
129 _usePhase = false;
130 if ( Qt::CheckState(settings.value("pppUsePhase").toInt()) == Qt::Checked) {
131 _usePhase = true;
132 }
133
134 _estTropo = false;
135 if ( Qt::CheckState(settings.value("pppEstTropo").toInt()) == Qt::Checked) {
136 _estTropo = true;
137 }
138
139 _xcBanc.ReSize(4); _xcBanc = 0.0;
140 _ellBanc.ReSize(3); _ellBanc = 0.0;
141
142 _params.push_back(new bncParam(bncParam::CRD_X, 1, ""));
143 _params.push_back(new bncParam(bncParam::CRD_Y, 2, ""));
144 _params.push_back(new bncParam(bncParam::CRD_Z, 3, ""));
145 _params.push_back(new bncParam(bncParam::RECCLK, 4, ""));
146 if (_estTropo) {
147 _params.push_back(new bncParam(bncParam::TROPO, 5, ""));
148 }
149
150 unsigned nPar = _params.size();
151
152 _QQ.ReSize(nPar);
153 _QQ = 0.0;
154
155 _QQ(1,1) = sig_crd_0 * sig_crd_0;
156 _QQ(2,2) = sig_crd_0 * sig_crd_0;
157 _QQ(3,3) = sig_crd_0 * sig_crd_0;
158 _QQ(4,4) = sig_clk_0 * sig_clk_0;
159 if (_estTropo) {
160 _QQ(5,5) = sig_trp_0 * sig_trp_0;
161 }
162
163 // NMEA Output
164 // -----------
165 QString nmeaFileName = settings.value("nmeaFile").toString();
166 if (nmeaFileName.isEmpty()) {
167 _nmeaFile = 0;
168 _nmeaStream = 0;
169 }
170 else {
171 expandEnvVar(nmeaFileName);
172 _nmeaFile = new QFile(nmeaFileName);
173 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
174 _nmeaFile->open(QIODevice::WriteOnly | QIODevice::Append);
175 }
176 else {
177 _nmeaFile->open(QIODevice::WriteOnly);
178 }
179 _nmeaStream = new QTextStream();
180 _nmeaStream->setDevice(_nmeaFile);
181 }
182}
183
184// Destructor
185////////////////////////////////////////////////////////////////////////////
186bncModel::~bncModel() {
187 delete _nmeaStream;
188 delete _nmeaFile;
189}
190
191// Bancroft Solution
192////////////////////////////////////////////////////////////////////////////
193t_irc bncModel::cmpBancroft(t_epoData* epoData) {
194
195 if (epoData->size() < MINOBS) {
196 _log += "\nNot enough data";
197 return failure;
198 }
199
200 Matrix BB(epoData->size(), 4);
201
202 QMapIterator<QString, t_satData*> it(epoData->satData);
203 int iObs = 0;
204 while (it.hasNext()) {
205 ++iObs;
206 it.next();
207 QString prn = it.key();
208 t_satData* satData = it.value();
209 BB(iObs, 1) = satData->xx(1);
210 BB(iObs, 2) = satData->xx(2);
211 BB(iObs, 3) = satData->xx(3);
212 BB(iObs, 4) = satData->P3 + satData->clk;
213 }
214
215 bancroft(BB, _xcBanc);
216
217 // Ellipsoidal Coordinates
218 // ------------------------
219 xyz2ell(_xcBanc.data(), _ellBanc.data());
220
221 // Compute Satellite Elevations
222 // ----------------------------
223 QMutableMapIterator<QString, t_satData*> it2(epoData->satData);
224 while (it2.hasNext()) {
225 it2.next();
226 QString prn = it2.key();
227 t_satData* satData = it2.value();
228
229 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
230 double rho = rr.norm_Frobenius();
231
232 double neu[3];
233 xyz2neu(_ellBanc.data(), rr.data(), neu);
234
235 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
236 if (neu[2] < 0) {
237 satData->eleSat *= -1.0;
238 }
239 satData->azSat = atan2(neu[1], neu[0]);
240
241 if (satData->eleSat < MINELE) {
242 delete satData;
243 it2.remove();
244 }
245 }
246
247 return success;
248}
249
250// Computed Value
251////////////////////////////////////////////////////////////////////////////
252double bncModel::cmpValue(t_satData* satData) {
253
254 ColumnVector xRec(3);
255 xRec(1) = x();
256 xRec(2) = y();
257 xRec(3) = z();
258
259 double rho0 = (satData->xx - xRec).norm_Frobenius();
260 double dPhi = t_CST::omega * rho0 / t_CST::c;
261
262 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
263 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
264 xRec(3) = z();
265
266 satData->rho = (satData->xx - xRec).norm_Frobenius();
267
268 double tropDelay = delay_saast(satData->eleSat) +
269 trp() / sin(satData->eleSat);
270
271 return satData->rho + clk() - satData->clk + tropDelay;
272}
273
274// Tropospheric Model (Saastamoinen)
275////////////////////////////////////////////////////////////////////////////
276double bncModel::delay_saast(double Ele) {
277
278 double height = _ellBanc(3);
279
280 double pp = 1013.25 * pow(1.0 - 2.26e-5 * height, 5.225);
281 double TT = 18.0 - height * 0.0065 + 273.15;
282 double hh = 50.0 * exp(-6.396e-4 * height);
283 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
284
285 double h_km = height / 1000.0;
286
287 if (h_km < 0.0) h_km = 0.0;
288 if (h_km > 5.0) h_km = 5.0;
289 int ii = int(h_km + 1);
290 double href = ii - 1;
291
292 double bCor[6];
293 bCor[0] = 1.156;
294 bCor[1] = 1.006;
295 bCor[2] = 0.874;
296 bCor[3] = 0.757;
297 bCor[4] = 0.654;
298 bCor[5] = 0.563;
299
300 double BB = bCor[ii-1] + (bCor[ii]-bCor[ii-1]) * (h_km - href);
301
302 double zen = M_PI/2.0 - Ele;
303
304 return (0.002277/cos(zen)) * (pp + ((1255.0/TT)+0.05)*ee - BB*(tan(zen)*tan(zen)));
305}
306
307// Prediction Step of the Filter
308////////////////////////////////////////////////////////////////////////////
309void bncModel::predict(t_epoData* epoData) {
310
311 if (_usePhase) {
312
313 // Make a copy of QQ and xx, set parameter indices
314 // -----------------------------------------------
315 SymmetricMatrix QQ_old = _QQ;
316
317 for (int iPar = 1; iPar <= _params.size(); iPar++) {
318 _params[iPar-1]->index_old = _params[iPar-1]->index;
319 _params[iPar-1]->index = 0;
320 }
321
322 // Remove Ambiguity Parameters without observations
323 // ------------------------------------------------
324 int iPar = 0;
325 QMutableVectorIterator<bncParam*> it(_params);
326 while (it.hasNext()) {
327 bncParam* par = it.next();
328 bool removed = false;
329 if (par->type == bncParam::AMB_L3) {
330 if (epoData->satData.find(par->prn) == epoData->satData.end()) {
331 removed = true;
332 delete par;
333 it.remove();
334 }
335 }
336 if (! removed) {
337 ++iPar;
338 par->index = iPar;
339 }
340 }
341
342 // Add new ambiguity parameters
343 // ----------------------------
344 QMapIterator<QString, t_satData*> itObs(epoData->satData);
345 while (itObs.hasNext()) {
346 itObs.next();
347 QString prn = itObs.key();
348 bool found = false;
349 for (int iPar = 1; iPar <= _params.size(); iPar++) {
350 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
351 _params[iPar-1]->prn == prn) {
352 found = true;
353 break;
354 }
355 }
356 if (!found) {
357 bncParam* par = new bncParam(bncParam::AMB_L3, _params.size()+1, prn);
358 _params.push_back(par);
359 }
360 }
361
362 int nPar = _params.size();
363 _QQ.ReSize(nPar); _QQ = 0.0;
364 for (int i1 = 1; i1 <= nPar; i1++) {
365 bncParam* p1 = _params[i1-1];
366 if (p1->index_old != 0) {
367 _QQ(p1->index, p1->index) = QQ_old(p1->index_old, p1->index_old);
368 for (int i2 = 1; i2 <= nPar; i2++) {
369 bncParam* p2 = _params[i2-1];
370 if (p2->index_old != 0) {
371 _QQ(p1->index, p2->index) = QQ_old(p1->index_old, p2->index_old);
372 }
373 }
374 }
375 }
376
377 for (int ii = 1; ii <= nPar; ii++) {
378 bncParam* par = _params[ii-1];
379 if (par->index_old == 0) {
380 _QQ(par->index, par->index) = sig_amb_0 * sig_amb_0;
381 }
382 par->index_old = par->index;
383 }
384 }
385
386 // Coordinates
387 // -----------
388 if (_static) {
389 if (x() == 0.0 && y() == 0.0 && z() == 0.0) {
390 _params[0]->xx = _xcBanc(1);
391 _params[1]->xx = _xcBanc(2);
392 _params[2]->xx = _xcBanc(3);
393 }
394 }
395 else {
396 _params[0]->xx = _xcBanc(1);
397 _params[1]->xx = _xcBanc(2);
398 _params[2]->xx = _xcBanc(3);
399
400 _QQ(1,1) += sig_crd_p * sig_crd_p;
401 _QQ(2,2) += sig_crd_p * sig_crd_p;
402 _QQ(3,3) += sig_crd_p * sig_crd_p;
403 }
404
405 // Receiver Clocks
406 // ---------------
407 _params[3]->xx = _xcBanc(4);
408 for (int iPar = 1; iPar <= _params.size(); iPar++) {
409 _QQ(iPar, 4) = 0.0;
410 }
411 _QQ(4,4) = sig_clk_0 * sig_clk_0;
412
413 // Tropospheric Delay
414 // ------------------
415 if (_estTropo) {
416 _QQ(5,5) += sig_trp_p * sig_trp_p;
417 }
418}
419
420// Update Step of the Filter (currently just a single-epoch solution)
421////////////////////////////////////////////////////////////////////////////
422t_irc bncModel::update(t_epoData* epoData) {
423
424 _log = "Precise Point Positioning";
425
426 SymmetricMatrix QQsav;
427 ColumnVector dx;
428 ColumnVector vv;
429
430 // Loop over all outliers
431 // ----------------------
432 do {
433
434 // Bancroft Solution
435 // -----------------
436 if (cmpBancroft(epoData) != success) {
437 _log += "\nBancroft failed";
438 emit newMessage(_log, false);
439 return failure;
440 }
441
442 if (epoData->size() < MINOBS) {
443 _log += "\nNot enough data";
444 emit newMessage(_log, false);
445 return failure;
446 }
447
448 // Status Prediction
449 // -----------------
450 predict(epoData);
451
452 // Create First-Design Matrix
453 // --------------------------
454 unsigned nPar = _params.size();
455 unsigned nObs = _usePhase ? 2 * epoData->size() : epoData->size();
456
457 Matrix AA(nObs, nPar); // first design matrix
458 ColumnVector ll(nObs); // tems observed-computed
459 SymmetricMatrix PP(nObs); PP = 0.0;
460
461 unsigned iObs = 0;
462 QMapIterator<QString, t_satData*> itObs(epoData->satData);
463 while (itObs.hasNext()) {
464 ++iObs;
465 itObs.next();
466 QString prn = itObs.key();
467 t_satData* satData = itObs.value();
468
469 double rhoCmp = cmpValue(satData);
470
471 double ellWgtCoeff = 1.0;
472 //// double eleD = satData->eleSat * 180.0 / M_PI;
473 //// if (eleD < 25.0) {
474 //// ellWgtCoeff = 2.5 - (eleD - 10.0) * 0.1;
475 //// ellWgtCoeff *= ellWgtCoeff;
476 //// }
477
478 ll(iObs) = satData->P3 - rhoCmp;
479 PP(iObs,iObs) = 1.0 / (sig_P3 * sig_P3) / ellWgtCoeff;
480 for (int iPar = 1; iPar <= _params.size(); iPar++) {
481 AA(iObs, iPar) = _params[iPar-1]->partial(satData, "");
482 }
483
484 if (_usePhase) {
485 ++iObs;
486 ll(iObs) = satData->L3 - rhoCmp;
487 PP(iObs,iObs) = 1.0 / (sig_L3 * sig_L3) / ellWgtCoeff;
488 for (int iPar = 1; iPar <= _params.size(); iPar++) {
489 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
490 _params[iPar-1]->prn == prn) {
491 ll(iObs) -= _params[iPar-1]->xx;
492 }
493 AA(iObs, iPar) = _params[iPar-1]->partial(satData, prn);
494 }
495 }
496 }
497
498 // Compute Filter Update
499 // ---------------------
500 QQsav = _QQ;
501
502 Matrix ATP = AA.t() * PP;
503 SymmetricMatrix NN = _QQ.i();
504 NN << NN + ATP * AA;
505 _QQ = NN.i();
506 dx = _QQ * ATP * ll;
507 vv = ll - AA * dx;
508
509 } while (outlierDetection(QQsav, vv, epoData->satData) != 0);
510
511 // Set Solution Vector
512 // -------------------
513 ostringstream str1;
514 str1.setf(ios::fixed);
515 QVectorIterator<bncParam*> itPar(_params);
516 while (itPar.hasNext()) {
517 bncParam* par = itPar.next();
518 par->xx += dx(par->index);
519 if (par->type == bncParam::RECCLK) {
520 str1 << "\n clk = " << setw(6) << setprecision(3) << par->xx
521 << " +- " << setw(6) << setprecision(3)
522 << sqrt(_QQ(par->index,par->index));
523 }
524 else if (par->type == bncParam::AMB_L3) {
525 str1 << "\n amb " << par->prn.toAscii().data() << " = "
526 << setw(6) << setprecision(3) << par->xx
527 << " +- " << setw(6) << setprecision(3)
528 << sqrt(_QQ(par->index,par->index));
529 }
530 }
531 _log += str1.str().c_str();
532
533 // Message (both log file and screen)
534 // ----------------------------------
535 ostringstream str2;
536 str2.setf(ios::fixed);
537 str2 << " PPP " << _staID.data() << " "
538 << epoData->tt.timestr(1) << " " << epoData->size() << " "
539 << setw(14) << setprecision(3) << x() << " +- "
540 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
541 << setw(14) << setprecision(3) << y() << " +- "
542 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
543 << setw(14) << setprecision(3) << z() << " +- "
544 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
545 if (_estTropo) {
546 str2 << " " << setw(6) << setprecision(3) << trp() << " +- "
547 << setw(6) << setprecision(3) << sqrt(_QQ(5,5));
548 }
549
550 emit newMessage(_log, false);
551 emit newMessage(QByteArray(str2.str().c_str()), true);
552
553 return success;
554}
555
556// Outlier Detection
557////////////////////////////////////////////////////////////////////////////
558int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
559 const ColumnVector& vv,
560 QMap<QString, t_satData*>& satData) {
561
562 double vvMaxCode = 0.0;
563 double vvMaxPhase = 0.0;
564 QMutableMapIterator<QString, t_satData*> itMaxCode(satData);
565 QMutableMapIterator<QString, t_satData*> itMaxPhase(satData);
566
567 int ii = 0;
568 QMutableMapIterator<QString, t_satData*> it(satData);
569 while (it.hasNext()) {
570 it.next();
571 ++ii;
572
573 if (vvMaxCode == 0.0 || fabs(vv(ii)) > vvMaxCode) {
574 vvMaxCode = fabs(vv(ii));
575 itMaxCode = it;
576 }
577
578 if (_usePhase) {
579 ++ii;
580 if (vvMaxPhase == 0.0 || fabs(vv(ii)) > vvMaxPhase) {
581 vvMaxPhase = fabs(vv(ii));
582 itMaxPhase = it;
583 }
584 }
585 }
586
587 if (vvMaxCode > MAXRES_CODE) {
588 QString prn = itMaxCode.key();
589 t_satData* satData = itMaxCode.value();
590 delete satData;
591 itMaxCode.remove();
592 _QQ = QQsav;
593
594 _log += "\nOutlier Code " + prn.toAscii() + " "
595 + QByteArray::number(vvMaxCode, 'f', 3);
596
597 return 1;
598 }
599 else if (vvMaxPhase > MAXRES_PHASE) {
600 QString prn = itMaxPhase.key();
601 t_satData* satData = itMaxPhase.value();
602 delete satData;
603 itMaxPhase.remove();
604 _QQ = QQsav;
605
606 _log += "\nOutlier Phase " + prn.toAscii() + " "
607 + QByteArray::number(vvMaxPhase, 'f', 3);
608
609 return 1;
610 }
611
612 return 0;
613}
Note: See TracBrowser for help on using the repository browser.