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

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

* empty log message *

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