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

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

* empty log message *

File size: 12.5 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
45#include "bncmodel.h"
46#include "bncpppclient.h"
47#include "bancroft.h"
48#include "bncutils.h"
49#include "bncsettings.h"
50
51using namespace std;
52
53const unsigned MINOBS = 4;
54const double MINELE = 10.0 * M_PI / 180.0;
55const double sig_crd_0 = 100.0;
56const double sig_crd_p = 100.0;
57const double sig_clk_0 = 1000.0;
58const double sig_amb_0 = 100.0;
59const double sig_P3 = 1.0;
60const double sig_L3 = 0.01;
61
62// Constructor
63////////////////////////////////////////////////////////////////////////////
64bncParam::bncParam(bncParam::parType typeIn, int indexIn,
65 const QString& prnIn) {
66 type = typeIn;
67 index = indexIn;
68 prn = prnIn;
69 index_old = 0;
70 x0 = 0.0;
71 xx = 0.0;
72}
73
74// Destructor
75////////////////////////////////////////////////////////////////////////////
76bncParam::~bncParam() {
77}
78
79// Partial
80////////////////////////////////////////////////////////////////////////////
81double bncParam::partial(t_satData* satData, const QString& prnIn) {
82 if (type == CRD_X) {
83 return (x0 - satData->xx(1)) / satData->rho;
84 }
85 else if (type == CRD_Y) {
86 return (x0 - satData->xx(2)) / satData->rho;
87 }
88 else if (type == CRD_Z) {
89 return (x0 - satData->xx(3)) / satData->rho;
90 }
91 else if (type == RECCLK) {
92 return 1.0;
93 }
94 else if (type == AMB_L3) {
95 if (prnIn == prn) {
96 return 1.0;
97 }
98 else {
99 return 0.0;
100 }
101 }
102 return 0.0;
103}
104
105// Constructor
106////////////////////////////////////////////////////////////////////////////
107bncModel::bncModel() {
108 _xcBanc.ReSize(4); _xcBanc = 0.0;
109 _params.push_back(new bncParam(bncParam::CRD_X, 1, ""));
110 _params.push_back(new bncParam(bncParam::CRD_Y, 2, ""));
111 _params.push_back(new bncParam(bncParam::CRD_Z, 3, ""));
112 _params.push_back(new bncParam(bncParam::RECCLK, 4, ""));
113 _ellBanc.ReSize(3);
114
115 unsigned nPar = _params.size();
116 _QQ.ReSize(nPar);
117 _QQ = 0.0;
118
119 _QQ(1,1) = sig_crd_0 * sig_crd_0;
120 _QQ(2,2) = sig_crd_0 * sig_crd_0;
121 _QQ(3,3) = sig_crd_0 * sig_crd_0;
122 _QQ(4,4) = sig_clk_0 * sig_clk_0;
123
124 _xx.ReSize(nPar);
125 _xx = 0.0;
126
127 bncSettings settings;
128
129 _static = false;
130 if ( Qt::CheckState(settings.value("pppStatic").toInt()) == Qt::Checked) {
131 _static = true;
132 }
133
134 _usePhase = false;
135 if ( Qt::CheckState(settings.value("pppUsePhase").toInt()) == Qt::Checked) {
136 _usePhase = true;
137 }
138}
139
140// Destructor
141////////////////////////////////////////////////////////////////////////////
142bncModel::~bncModel() {
143}
144
145// Bancroft Solution
146////////////////////////////////////////////////////////////////////////////
147t_irc bncModel::cmpBancroft(t_epoData* epoData) {
148
149 if (epoData->size() < MINOBS) {
150 return failure;
151 }
152
153 Matrix BB(epoData->size(), 4);
154
155 QMapIterator<QString, t_satData*> it(epoData->satData);
156 int iObs = 0;
157 while (it.hasNext()) {
158 ++iObs;
159 it.next();
160 QString prn = it.key();
161 t_satData* satData = it.value();
162 BB(iObs, 1) = satData->xx(1);
163 BB(iObs, 2) = satData->xx(2);
164 BB(iObs, 3) = satData->xx(3);
165 BB(iObs, 4) = satData->P3 + satData->clk;
166 }
167
168 bancroft(BB, _xcBanc);
169
170 // Ellipsoidal Coordinates
171 // ------------------------
172 xyz2ell(_xcBanc.data(), _ellBanc.data());
173
174 // Compute Satellite Elevations
175 // ----------------------------
176 QMutableMapIterator<QString, t_satData*> it2(epoData->satData);
177 while (it2.hasNext()) {
178 it2.next();
179 QString prn = it2.key();
180 t_satData* satData = it2.value();
181
182 ColumnVector dx = satData->xx - _xcBanc.Rows(1,3);
183 double rho = dx.norm_Frobenius();
184
185 double neu[3];
186 xyz2neu(_ellBanc.data(), dx.data(), neu);
187
188 satData->eleSat = acos( sqrt(neu[0]*neu[0] + neu[1]*neu[1]) / rho );
189 if (neu[2] < 0) {
190 satData->eleSat *= -1.0;
191 }
192 satData->azSat = atan2(neu[1], neu[0]);
193
194 if (satData->eleSat < MINELE) {
195 delete satData;
196 it2.remove();
197 }
198 }
199
200 return success;
201}
202
203// Computed Value
204////////////////////////////////////////////////////////////////////////////
205double bncModel::cmpValue(t_satData* satData) {
206
207 ColumnVector xRec(3);
208 xRec(1) = x();
209 xRec(2) = y();
210 xRec(3) = z();
211
212 double rho0 = (satData->xx - xRec).norm_Frobenius();
213 double dPhi = t_CST::omega * rho0 / t_CST::c;
214
215 xRec(1) = x() * cos(dPhi) - y() * sin(dPhi);
216 xRec(2) = y() * cos(dPhi) + x() * sin(dPhi);
217 xRec(3) = z();
218
219 satData->rho = (satData->xx - xRec).norm_Frobenius();
220
221 double tropDelay = delay_saast(satData->eleSat);
222
223 return satData->rho + clk() - satData->clk + tropDelay;
224}
225
226// Tropospheric Model (Saastamoinen)
227////////////////////////////////////////////////////////////////////////////
228double bncModel::delay_saast(double Ele) {
229
230 double height = _ellBanc(3);
231
232 double pp = 1013.25 * pow(1.0 - 2.26e-5 * height, 5.225);
233 double TT = 18.0 - height * 0.0065 + 273.15;
234 double hh = 50.0 * exp(-6.396e-4 * height);
235 double ee = hh / 100.0 * exp(-37.2465 + 0.213166*TT - 0.000256908*TT*TT);
236
237 double h_km = height / 1000.0;
238
239 if (h_km < 0.0) h_km = 0.0;
240 if (h_km > 5.0) h_km = 5.0;
241 int ii = int(h_km + 1);
242 double href = ii - 1;
243
244 double bCor[6];
245 bCor[0] = 1.156;
246 bCor[1] = 1.006;
247 bCor[2] = 0.874;
248 bCor[3] = 0.757;
249 bCor[4] = 0.654;
250 bCor[5] = 0.563;
251
252 double BB = bCor[ii-1] + (bCor[ii]-bCor[ii-1]) * (h_km - href);
253
254 double zen = M_PI/2.0 - Ele;
255
256 return (0.002277/cos(zen)) * (pp + ((1255.0/TT)+0.05)*ee - BB*(tan(zen)*tan(zen)));
257}
258
259// Prediction Step of the Filter
260////////////////////////////////////////////////////////////////////////////
261void bncModel::predict(t_epoData* epoData) {
262
263 if (_usePhase) {
264
265 // Make a copy of QQ and xx, set parameter indices
266 // -----------------------------------------------
267 SymmetricMatrix QQ_old = _QQ;
268 ColumnVector xx_old = _xx;
269
270 for (int iPar = 1; iPar <= _params.size(); iPar++) {
271 _params[iPar-1]->index_old = _params[iPar-1]->index;
272 _params[iPar-1]->index = 0;
273 }
274
275 // Remove Ambiguity Parameters without observations
276 // ------------------------------------------------
277 int iPar = 0;
278 QMutableVectorIterator<bncParam*> it(_params);
279 while (it.hasNext()) {
280 bncParam* par = it.next();
281 bool removed = false;
282 if (par->type == bncParam::AMB_L3) {
283 if (epoData->satData.find(par->prn) == epoData->satData.end()) {
284 removed = true;
285 delete par;
286 it.remove();
287 }
288 }
289 if (! removed) {
290 ++iPar;
291 par->index = iPar;
292 }
293 }
294
295 // Add new ambiguity parameters
296 // ----------------------------
297 QMapIterator<QString, t_satData*> itObs(epoData->satData);
298 while (itObs.hasNext()) {
299 itObs.next();
300 QString prn = itObs.key();
301 bool found = false;
302 for (int iPar = 1; iPar <= _params.size(); iPar++) {
303 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
304 _params[iPar-1]->prn == prn) {
305 found = true;
306 break;
307 }
308 }
309 if (!found) {
310 bncParam* par = new bncParam(bncParam::AMB_L3, _params.size()+1, prn);
311 _params.push_back(par);
312 }
313 }
314
315 int nPar = _params.size();
316 _xx.ReSize(nPar); _xx = 0.0;
317 _QQ.ReSize(nPar); _QQ = 0.0;
318 for (int i1 = 1; i1 <= nPar; i1++) {
319 bncParam* p1 = _params[i1-1];
320 if (p1->index_old != 0) {
321 _xx(p1->index) = xx_old(p1->index_old);
322 _QQ(p1->index, p1->index) = QQ_old(p1->index_old, p1->index_old);
323 for (int i2 = 1; i2 <= nPar; i2++) {
324 bncParam* p2 = _params[i2-1];
325 if (p2->index_old != 0) {
326 _QQ(p1->index, p2->index) = QQ_old(p1->index_old, p2->index_old);
327 }
328 }
329 }
330 }
331
332 for (int ii = 1; ii <= nPar; ii++) {
333 bncParam* par = _params[ii-1];
334 if (par->index_old == 0) {
335 _QQ(par->index, par->index) = sig_amb_0 * sig_amb_0;
336 }
337 par->index_old = par->index;
338 }
339 }
340
341 // Coordinates
342 // -----------
343 if (_static) {
344 if (x() == 0.0 && y() == 0.0 && z() == 0.0) {
345 _params[0]->x0 = _xcBanc(1);
346 _params[1]->x0 = _xcBanc(2);
347 _params[2]->x0 = _xcBanc(3);
348 }
349 else {
350 _params[0]->x0 += _params[0]->xx;
351 _params[1]->x0 += _params[1]->xx;
352 _params[2]->x0 += _params[2]->xx;
353 }
354 }
355 else {
356 _params[0]->x0 = _xcBanc(1);
357 _params[1]->x0 = _xcBanc(2);
358 _params[2]->x0 = _xcBanc(3);
359
360 _QQ(1,1) += sig_crd_p * sig_crd_p;
361 _QQ(2,2) += sig_crd_p * sig_crd_p;
362 _QQ(3,3) += sig_crd_p * sig_crd_p;
363 }
364
365 // Receiver Clocks
366 // ---------------
367 _params[3]->x0 = _xcBanc(4);
368 for (int iPar = 1; iPar <= _params.size(); iPar++) {
369 _QQ(iPar, 4) = 0.0;
370 }
371 _QQ(4,4) = sig_clk_0 * sig_clk_0;
372
373 // Ambiguities
374 // -----------
375 for (int iPar = 1; iPar <= _params.size(); iPar++) {
376 if (_params[iPar-1]->type == bncParam::AMB_L3) {
377 _params[iPar-1]->x0 += _params[iPar-1]->xx;
378 }
379 }
380
381 // Nullify the Solution Vector
382 // ---------------------------
383 for (int iPar = 1; iPar <= _params.size(); iPar++) {
384 _params[iPar-1]->xx = 0.0;
385 }
386 _xx = 0.0;
387}
388
389// Update Step of the Filter (currently just a single-epoch solution)
390////////////////////////////////////////////////////////////////////////////
391t_irc bncModel::update(t_epoData* epoData) {
392
393 if (epoData->size() < MINOBS) {
394 return failure;
395 }
396
397 predict(epoData);
398
399 unsigned nPar = _params.size();
400 unsigned nObs = _usePhase ? 2 * epoData->size() : epoData->size();
401
402 // Create First-Design Matrix
403 // --------------------------
404 Matrix AA(nObs, nPar); // first design matrix
405 ColumnVector ll(nObs); // tems observed-computed
406 SymmetricMatrix PP(nObs); PP = 0.0;
407
408 unsigned iObs = 0;
409 QMapIterator<QString, t_satData*> itObs(epoData->satData);
410 while (itObs.hasNext()) {
411 ++iObs;
412 itObs.next();
413 QString prn = itObs.key();
414 t_satData* satData = itObs.value();
415
416 double rhoCmp = cmpValue(satData);
417
418 ll(iObs) = satData->P3 - rhoCmp;
419 PP(iObs,iObs) = 1.0 / (sig_P3 * sig_P3);
420 for (int iPar = 1; iPar <= _params.size(); iPar++) {
421 AA(iObs, iPar) = _params[iPar-1]->partial(satData, "");
422 }
423
424 if (_usePhase) {
425 ++iObs;
426 ll(iObs) = satData->L3 - rhoCmp;
427 PP(iObs,iObs) = 1.0 / (sig_L3 * sig_L3);
428 for (int iPar = 1; iPar <= _params.size(); iPar++) {
429 if (_params[iPar-1]->type == bncParam::AMB_L3 &&
430 _params[iPar-1]->prn == prn) {
431 ll(iObs) -= _params[iPar-1]->x0;
432 }
433 AA(iObs, iPar) = _params[iPar-1]->partial(satData, prn);
434 }
435 }
436 }
437
438 // Compute Kalman Update
439 // ---------------------
440 if (false) {
441 SymmetricMatrix HH; HH << PP + AA * _QQ * AA.t();
442 SymmetricMatrix Hi = HH.i();
443 Matrix KK = _QQ * AA.t() * Hi;
444 ColumnVector v1 = ll - AA * _xx;
445 _xx = _xx + KK * v1;
446 IdentityMatrix Id(nPar);
447 _QQ << (Id - KK * AA) * _QQ;
448 }
449 else {
450 Matrix ATP = AA.t() * PP;
451 SymmetricMatrix NN = _QQ.i();
452 ColumnVector bb = NN * _xx + ATP * ll;
453 NN << NN + ATP * AA;
454 _QQ = NN.i();
455 _xx = _QQ * bb;
456 }
457
458 // Set Solution Vector
459 // -------------------
460 QVectorIterator<bncParam*> itPar(_params);
461 while (itPar.hasNext()) {
462 bncParam* par = itPar.next();
463 par->xx = _xx(par->index);
464 }
465
466 return success;
467}
Note: See TracBrowser for help on using the repository browser.