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

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

* empty log message *

File size: 16.3 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: bncParam, bncModel
30 *
31 * Purpose: Model for PPP
32 *
33 * Author: L. Mervart
34 *
35 * Created: 01-Dec-2009
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iomanip>
42#include <cmath>
43#include <newmatio.h>
44#include <sstream>
45
46#include "bncmodel.h"
47#include "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
164// Destructor
165////////////////////////////////////////////////////////////////////////////
166bncModel::~bncModel() {
167}
168
169// Bancroft Solution
170////////////////////////////////////////////////////////////////////////////
171t_irc bncModel::cmpBancroft(t_epoData* epoData) {
172
173 if (epoData->size() < MINOBS) {
174 _log += "\nNot enough data";
175 return failure;
176 }
177
178 Matrix BB(epoData->size(), 4);
179
180 QMapIterator<QString, t_satData*> it(epoData->satData);
181 int iObs = 0;
182 while (it.hasNext()) {
183 ++iObs;
184 it.next();
185 QString prn = it.key();
186 t_satData* satData = it.value();
187 BB(iObs, 1) = satData->xx(1);
188 BB(iObs, 2) = satData->xx(2);
189 BB(iObs, 3) = satData->xx(3);
190 BB(iObs, 4) = satData->P3 + satData->clk;
191 }
192
193 bancroft(BB, _xcBanc);
194
195 // Ellipsoidal Coordinates
196 // ------------------------
197 xyz2ell(_xcBanc.data(), _ellBanc.data());
198
199 // Compute Satellite Elevations
200 // ----------------------------
201 QMutableMapIterator<QString, t_satData*> it2(epoData->satData);
202 while (it2.hasNext()) {
203 it2.next();
204 QString prn = it2.key();
205 t_satData* satData = it2.value();
206
207 ColumnVector rr = satData->xx - _xcBanc.Rows(1,3);
208 double rho = rr.norm_Frobenius();
209
210 double neu[3];
211 xyz2neu(_ellBanc.data(), rr.data(), neu);
212
213 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
214 if (neu[2] < 0) {
215 satData->eleSat *= -1.0;
216 }
217 satData->azSat = atan2(neu[1], neu[0]);
218
219 if (satData->eleSat < MINELE) {
220 delete satData;
221 it2.remove();
222 }
223 }
224
225 return success;
226}
227
228// Computed Value
229////////////////////////////////////////////////////////////////////////////
230double bncModel::cmpValue(t_satData* satData) {
231
232 ColumnVector xRec(3);
233 xRec(1) = x();
234 xRec(2) = y();
235 xRec(3) = z();
236
237 double rho0 = (satData->xx - xRec).norm_Frobenius();
238 double dPhi = t_CST::omega * rho0 / t_CST::c;
239
240 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
241 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
242 xRec(3) = z();
243
244 satData->rho = (satData->xx - xRec).norm_Frobenius();
245
246 double tropDelay = delay_saast(satData->eleSat) +
247 trp() / sin(satData->eleSat);
248
249 return satData->rho + clk() - satData->clk + tropDelay;
250}
251
252// Tropospheric Model (Saastamoinen)
253////////////////////////////////////////////////////////////////////////////
254double bncModel::delay_saast(double Ele) {
255
256 double height = _ellBanc(3);
257
258 double pp = 1013.25 * pow(1.0 - 2.26e-5 * height, 5.225);
259 double TT = 18.0 - height * 0.0065 + 273.15;
260 double hh = 50.0 * exp(-6.396e-4 * height);
261 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
262
263 double h_km = height / 1000.0;
264
265 if (h_km < 0.0) h_km = 0.0;
266 if (h_km > 5.0) h_km = 5.0;
267 int ii = int(h_km + 1);
268 double href = ii - 1;
269
270 double bCor[6];
271 bCor[0] = 1.156;
272 bCor[1] = 1.006;
273 bCor[2] = 0.874;
274 bCor[3] = 0.757;
275 bCor[4] = 0.654;
276 bCor[5] = 0.563;
277
278 double BB = bCor[ii-1] + (bCor[ii]-bCor[ii-1]) * (h_km - href);
279
280 double zen = M_PI/2.0 - Ele;
281
282 return (0.002277/cos(zen)) * (pp + ((1255.0/TT)+0.05)*ee - BB*(tan(zen)*tan(zen)));
283}
284
285// Prediction Step of the Filter
286////////////////////////////////////////////////////////////////////////////
287void bncModel::predict(t_epoData* epoData) {
288
289 if (_usePhase) {
290
291 // Make a copy of QQ and xx, set parameter indices
292 // -----------------------------------------------
293 SymmetricMatrix QQ_old = _QQ;
294
295 for (int iPar = 1; iPar <= _params.size(); iPar++) {
296 _params[iPar-1]->index_old = _params[iPar-1]->index;
297 _params[iPar-1]->index = 0;
298 }
299
300 // Remove Ambiguity Parameters without observations
301 // ------------------------------------------------
302 int iPar = 0;
303 QMutableVectorIterator<bncParam*> it(_params);
304 while (it.hasNext()) {
305 bncParam* par = it.next();
306 bool removed = false;
307 if (par->type == bncParam::AMB_L3) {
308 if (epoData->satData.find(par->prn) == epoData->satData.end()) {
309 removed = true;
310 delete par;
311 it.remove();
312 }
313 }
314 if (! removed) {
315 ++iPar;
316 par->index = iPar;
317 }
318 }
319
320 // Add new ambiguity parameters
321 // ----------------------------
322 QMapIterator<QString, t_satData*> itObs(epoData->satData);
323 while (itObs.hasNext()) {
324 itObs.next();
325 QString prn = itObs.key();
326 bool found = false;
327 for (int iPar = 1; iPar <= _params.size(); iPar++) {
328 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
329 _params[iPar-1]->prn == prn) {
330 found = true;
331 break;
332 }
333 }
334 if (!found) {
335 bncParam* par = new bncParam(bncParam::AMB_L3, _params.size()+1, prn);
336 _params.push_back(par);
337 }
338 }
339
340 int nPar = _params.size();
341 _QQ.ReSize(nPar); _QQ = 0.0;
342 for (int i1 = 1; i1 <= nPar; i1++) {
343 bncParam* p1 = _params[i1-1];
344 if (p1->index_old != 0) {
345 _QQ(p1->index, p1->index) = QQ_old(p1->index_old, p1->index_old);
346 for (int i2 = 1; i2 <= nPar; i2++) {
347 bncParam* p2 = _params[i2-1];
348 if (p2->index_old != 0) {
349 _QQ(p1->index, p2->index) = QQ_old(p1->index_old, p2->index_old);
350 }
351 }
352 }
353 }
354
355 for (int ii = 1; ii <= nPar; ii++) {
356 bncParam* par = _params[ii-1];
357 if (par->index_old == 0) {
358 _QQ(par->index, par->index) = sig_amb_0 * sig_amb_0;
359 }
360 par->index_old = par->index;
361 }
362 }
363
364 // Coordinates
365 // -----------
366 if (_static) {
367 if (x() == 0.0 && y() == 0.0 && z() == 0.0) {
368 _params[0]->xx = _xcBanc(1);
369 _params[1]->xx = _xcBanc(2);
370 _params[2]->xx = _xcBanc(3);
371 }
372 }
373 else {
374 _params[0]->xx = _xcBanc(1);
375 _params[1]->xx = _xcBanc(2);
376 _params[2]->xx = _xcBanc(3);
377
378 _QQ(1,1) += sig_crd_p * sig_crd_p;
379 _QQ(2,2) += sig_crd_p * sig_crd_p;
380 _QQ(3,3) += sig_crd_p * sig_crd_p;
381 }
382
383 // Receiver Clocks
384 // ---------------
385 _params[3]->xx = _xcBanc(4);
386 for (int iPar = 1; iPar <= _params.size(); iPar++) {
387 _QQ(iPar, 4) = 0.0;
388 }
389 _QQ(4,4) = sig_clk_0 * sig_clk_0;
390
391 // Tropospheric Delay
392 // ------------------
393 if (_estTropo) {
394 _QQ(5,5) += sig_trp_p * sig_trp_p;
395 }
396}
397
398// Update Step of the Filter (currently just a single-epoch solution)
399////////////////////////////////////////////////////////////////////////////
400t_irc bncModel::update(t_epoData* epoData) {
401
402 _log = "Precise Point Positioning";
403
404 SymmetricMatrix QQsav;
405 ColumnVector dx;
406 ColumnVector vv;
407
408 // Loop over all outliers
409 // ----------------------
410 do {
411
412 // Bancroft Solution
413 // -----------------
414 if (cmpBancroft(epoData) != success) {
415 _log += "\nBancroft failed";
416 emit newMessage(_log, false);
417 return failure;
418 }
419
420 if (epoData->size() < MINOBS) {
421 _log += "\nNot enough data";
422 emit newMessage(_log, false);
423 return failure;
424 }
425
426 // Status Prediction
427 // -----------------
428 predict(epoData);
429
430 // Create First-Design Matrix
431 // --------------------------
432 unsigned nPar = _params.size();
433 unsigned nObs = _usePhase ? 2 * epoData->size() : epoData->size();
434
435 Matrix AA(nObs, nPar); // first design matrix
436 ColumnVector ll(nObs); // tems observed-computed
437 SymmetricMatrix PP(nObs); PP = 0.0;
438
439 unsigned iObs = 0;
440 QMapIterator<QString, t_satData*> itObs(epoData->satData);
441 while (itObs.hasNext()) {
442 ++iObs;
443 itObs.next();
444 QString prn = itObs.key();
445 t_satData* satData = itObs.value();
446
447 double rhoCmp = cmpValue(satData);
448
449 double ellWgtCoeff = 1.0;
450 //// double eleD = satData->eleSat * 180.0 / M_PI;
451 //// if (eleD < 25.0) {
452 //// ellWgtCoeff = 2.5 - (eleD - 10.0) * 0.1;
453 //// ellWgtCoeff *= ellWgtCoeff;
454 //// }
455
456 ll(iObs) = satData->P3 - rhoCmp;
457 PP(iObs,iObs) = 1.0 / (sig_P3 * sig_P3) / ellWgtCoeff;
458 for (int iPar = 1; iPar <= _params.size(); iPar++) {
459 AA(iObs, iPar) = _params[iPar-1]->partial(satData, "");
460 }
461
462 if (_usePhase) {
463 ++iObs;
464 ll(iObs) = satData->L3 - rhoCmp;
465 PP(iObs,iObs) = 1.0 / (sig_L3 * sig_L3) / ellWgtCoeff;
466 for (int iPar = 1; iPar <= _params.size(); iPar++) {
467 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
468 _params[iPar-1]->prn == prn) {
469 ll(iObs) -= _params[iPar-1]->xx;
470 }
471 AA(iObs, iPar) = _params[iPar-1]->partial(satData, prn);
472 }
473 }
474 }
475
476 // Compute Filter Update
477 // ---------------------
478 QQsav = _QQ;
479
480 Matrix ATP = AA.t() * PP;
481 SymmetricMatrix NN = _QQ.i();
482 NN << NN + ATP * AA;
483 _QQ = NN.i();
484 dx = _QQ * ATP * ll;
485 vv = ll - AA * dx;
486
487 } while (outlierDetection(QQsav, vv, epoData->satData) != 0);
488
489 // Set Solution Vector
490 // -------------------
491 ostringstream str1;
492 str1.setf(ios::fixed);
493 QVectorIterator<bncParam*> itPar(_params);
494 while (itPar.hasNext()) {
495 bncParam* par = itPar.next();
496 par->xx += dx(par->index);
497 if (par->type == bncParam::RECCLK) {
498 str1 << "\n clk = " << setw(6) << setprecision(3) << par->xx
499 << " +- " << setw(6) << setprecision(3)
500 << sqrt(_QQ(par->index,par->index));
501 }
502 else if (par->type == bncParam::AMB_L3) {
503 str1 << "\n amb " << par->prn.toAscii().data() << " = "
504 << setw(6) << setprecision(3) << par->xx
505 << " +- " << setw(6) << setprecision(3)
506 << sqrt(_QQ(par->index,par->index));
507 }
508 }
509 _log += str1.str().c_str();
510
511 // Message (both log file and screen)
512 // ----------------------------------
513 ostringstream str2;
514 str2.setf(ios::fixed);
515 str2 << " PPP " << _staID.data() << " "
516 << epoData->tt.timestr(1) << " " << epoData->size() << " "
517 << setw(14) << setprecision(3) << x() << " +- "
518 << setw(6) << setprecision(3) << sqrt(_QQ(1,1)) << " "
519 << setw(14) << setprecision(3) << y() << " +- "
520 << setw(6) << setprecision(3) << sqrt(_QQ(2,2)) << " "
521 << setw(14) << setprecision(3) << z() << " +- "
522 << setw(6) << setprecision(3) << sqrt(_QQ(3,3));
523 if (_estTropo) {
524 str2 << " " << setw(6) << setprecision(3) << trp() << " +- "
525 << setw(6) << setprecision(3) << sqrt(_QQ(5,5));
526 }
527
528 emit newMessage(_log, false);
529 emit newMessage(QByteArray(str2.str().c_str()), true);
530
531 return success;
532}
533
534// Outlier Detection
535////////////////////////////////////////////////////////////////////////////
536int bncModel::outlierDetection(const SymmetricMatrix& QQsav,
537 const ColumnVector& vv,
538 QMap<QString, t_satData*>& satData) {
539
540 double vvMaxCode = 0.0;
541 double vvMaxPhase = 0.0;
542 QMutableMapIterator<QString, t_satData*> itMaxCode(satData);
543 QMutableMapIterator<QString, t_satData*> itMaxPhase(satData);
544
545 int ii = 0;
546 QMutableMapIterator<QString, t_satData*> it(satData);
547 while (it.hasNext()) {
548 it.next();
549 ++ii;
550
551 if (vvMaxCode == 0.0 || fabs(vv(ii)) > vvMaxCode) {
552 vvMaxCode = fabs(vv(ii));
553 itMaxCode = it;
554 }
555
556 if (_usePhase) {
557 ++ii;
558 if (vvMaxPhase == 0.0 || fabs(vv(ii)) > vvMaxPhase) {
559 vvMaxPhase = fabs(vv(ii));
560 itMaxPhase = it;
561 }
562 }
563 }
564
565 if (vvMaxCode > MAXRES_CODE) {
566 QString prn = itMaxCode.key();
567 t_satData* satData = itMaxCode.value();
568 delete satData;
569 itMaxCode.remove();
570 _QQ = QQsav;
571
572 _log += "\nOutlier Code " + prn.toAscii() + " "
573 + QByteArray::number(vvMaxCode, 'f', 3);
574
575 return 1;
576 }
577 else if (vvMaxPhase > MAXRES_PHASE) {
578 QString prn = itMaxPhase.key();
579 t_satData* satData = itMaxPhase.value();
580 delete satData;
581 itMaxPhase.remove();
582 _QQ = QQsav;
583
584 _log += "\nOutlier Phase " + prn.toAscii() + " "
585 + QByteArray::number(vvMaxPhase, 'f', 3);
586
587 return 1;
588 }
589
590 return 0;
591}
Note: See TracBrowser for help on using the repository browser.