source: ntrip/trunk/BNC/bncpppclient.cpp@ 3639

Last change on this file since 3639 was 3639, checked in by mervart, 14 years ago
File size: 13.1 KB
RevLine 
[2035]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: bncPPPclient
30 *
31 * Purpose: Precise Point Positioning
32 *
33 * Author: L. Mervart
34 *
35 * Created: 21-Nov-2009
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
[2249]41#include <newmatio.h>
[2238]42#include <iomanip>
[2549]43#include <sstream>
[2238]44
[2035]45#include "bncpppclient.h"
[2113]46#include "bncapp.h"
[2043]47#include "bncutils.h"
[2044]48#include "bncconst.h"
[2058]49#include "bncmodel.h"
[3635]50#include "pppopt.h"
[2035]51
52using namespace std;
53
54// Constructor
55////////////////////////////////////////////////////////////////////////////
[3635]56bncPPPclient::bncPPPclient(QByteArray staID, t_pppOpt* opt) {
[2113]57
[3635]58 if (opt) {
59 _opt = opt;
60 _optOwner = false;
[2226]61 }
62 else {
[3635]63 _opt = new t_pppOpt();
64 _optOwner = true;
[2226]65 }
66
[3635]67 _staID = staID;
[2776]68
[3638]69 _model = new bncModel(this);
70
[2548]71 connect(this, SIGNAL(newMessage(QByteArray,bool)),
72 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
73
[2113]74 connect(((bncApp*)qApp), SIGNAL(newCorrections(QList<QString>)),
[2585]75 this, SLOT(slotNewCorrections(QList<QString>)));
[2035]76}
77
78// Destructor
79////////////////////////////////////////////////////////////////////////////
80bncPPPclient::~bncPPPclient() {
[2058]81 delete _model;
[2808]82 while (!_epoData.empty()) {
83 delete _epoData.front();
84 _epoData.pop();
85 }
[2035]86 QMapIterator<QString, t_corr*> ic(_corr);
87 while (ic.hasNext()) {
88 ic.next();
89 delete ic.value();
90 }
[2274]91 QMapIterator<QString, t_bias*> ib(_bias);
92 while (ib.hasNext()) {
93 ib.next();
94 delete ib.value();
95 }
[3635]96 if (_optOwner) {
97 delete _opt;
98 }
[2035]99}
100
101//
102////////////////////////////////////////////////////////////////////////////
[2711]103void bncPPPclient::putNewObs(const t_obs& obs) {
[2037]104 QMutexLocker locker(&_mutex);
[2051]105
[2777]106 if (obs.satSys == 'R') {
[3635]107 if (!_opt->useGlonass) return;
[2225]108 }
[2777]109 else if (obs.satSys == 'E') {
[3635]110 if (!_opt->useGalileo) return;
[2776]111 }
112 else if (obs.satSys != 'G') {
113 return;
114 }
[2225]115
[2051]116 t_satData* satData = new t_satData();
[2808]117 satData->tt = bncTime(obs.GPSWeek, obs.GPSWeeks);
[2051]118
[2274]119 // Satellite Number
120 // ----------------
[2777]121 satData->prn = QString("%1%2").arg(obs.satSys).arg(obs.satNum,2,10,QChar('0'));
[2274]122
[2505]123 // Check Slips
124 // -----------
125 slipInfo& sInfo = _slips[satData->prn];
[2711]126 if ( sInfo.slipCntL1 == obs.slip_cnt_L1 &&
[2778]127 sInfo.slipCntL2 == obs.slip_cnt_L2 &&
128 sInfo.slipCntL5 == obs.slip_cnt_L5 ) {
[2505]129 satData->slipFlag = false;
130 }
131 else {
132 satData->slipFlag = true;
133 }
[2711]134 sInfo.slipCntL1 = obs.slip_cnt_L1;
135 sInfo.slipCntL2 = obs.slip_cnt_L2;
[2505]136
[2274]137 // Handle Code Biases
138 // ------------------
139 t_bias* bb = 0;
140 if (_bias.contains(satData->prn)) {
141 bb = _bias.value(satData->prn);
142 }
143
[2808]144 // Add new epoch, process the older ones
145 // -------------------------------------
146 if (_epoData.size() == 0) {
147 _epoData.push(new t_epoData());
148 _epoData.back()->tt = satData->tt;
[2037]149 }
[2808]150 else if (satData->tt != _epoData.back()->tt) {
151 processEpochs();
152 _epoData.push(new t_epoData());
153 _epoData.back()->tt = satData->tt;
[2037]154 }
[2039]155
[2786]156 // Set Observations GPS
157 // --------------------
[2711]158 if (obs.satSys == 'G') {
[2786]159 if ( (obs.P1 || obs.C1) && (obs.P2 || obs.C2) && obs.L1() && obs.L2() ) {
160 double f1 = t_CST::freq1;
161 double f2 = t_CST::freq2;
162 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
163 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
164 if (obs.P1) {
165 satData->P1 = obs.P1 + (bb ? bb->p1 : 0.0);
166 }
167 else {
168 satData->P1 = obs.C1 + (bb ? bb->c1 : 0.0);
169 }
170 if (obs.P2) {
171 satData->P2 = obs.P2 + (bb ? bb->p2 : 0.0);
172 }
173 else {
174 satData->P2 = obs.C2;
175 }
176 satData->L1 = obs.L1() * t_CST::c / f1;
177 satData->L2 = obs.L2() * t_CST::c / f2;
178 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
179 satData->L3 = c1 * satData->L1 + c2 * satData->L2;
180 satData->lambda3 = c1 * t_CST::c / f1 + c2 * t_CST::c / f2;
[2778]181
[3408]182 _epoData.back()->satData[satData->prn] = satData;
[2778]183 }
184 else {
185 delete satData;
186 }
[2786]187 }
[2778]188
[2786]189 // Set Observations GLONASS
190 // ------------------------
[2711]191 else if (obs.satSys == 'R') {
[2786]192 if ( (obs.P1 || obs.C1) && (obs.P2 || obs.C2) && obs.L1() && obs.L2() ) {
193 double f1 = 1602000000.0 + 562500.0 * obs.slotNum;
194 double f2 = 1246000000.0 + 437500.0 * obs.slotNum;
195 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
196 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
197 if (obs.P1) {
198 satData->P1 = obs.P1 + (bb ? bb->p1 : 0.0);
199 }
200 else {
201 satData->P1 = obs.C1 + (bb ? bb->c1 : 0.0);
202 }
203 if (obs.P2) {
204 satData->P2 = obs.P2 + (bb ? bb->p2 : 0.0);
205 }
206 else {
207 satData->P2 = obs.C2;
208 }
209 satData->L1 = obs.L1() * t_CST::c / f1;
210 satData->L2 = obs.L2() * t_CST::c / f2;
211 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
212 satData->L3 = c1 * satData->L1 + c2 * satData->L2;
213 satData->lambda3 = c1 * t_CST::c / f1 + c2 * t_CST::c / f2;
[2778]214
[3408]215 _epoData.back()->satData[satData->prn] = satData;
[2778]216 }
217 else {
218 delete satData;
219 }
[2786]220 }
[2778]221
[2786]222 // Set Observations Galileo
223 // ------------------------
[2778]224 else if (obs.satSys == 'E') {
[2787]225 if ( obs.C1 && obs.C5 && obs.L1() && obs.L5) {
[2786]226 double f1 = t_CST::freq1;
227 double f5 = t_CST::freq5;
228 double c1 = f1 * f1 / (f1 * f1 - f5 * f5);
229 double c5 = - f5 * f5 / (f1 * f1 - f5 * f5);
[2778]230
[2786]231 satData->P1 = obs.C1;
232 satData->P5 = obs.C5;
233 satData->L1 = obs.L1() * t_CST::c / f1;
234 satData->L5 = obs.L5 * t_CST::c / f5;
235 satData->P3 = c1 * satData->P1 + c5 * satData->P5;
236 satData->L3 = c1 * satData->L1 + c5 * satData->L5;
237 satData->lambda3 = c1 * t_CST::c / f1 + c5 * t_CST::c / f5;
[3408]238 _epoData.back()->satData[satData->prn] = satData;
[2778]239 }
240 else {
241 delete satData;
242 }
243 }
[2035]244}
245
246//
247////////////////////////////////////////////////////////////////////////////
248void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
249 QMutexLocker locker(&_mutex);
[2069]250
[2966]251 // Check the Mountpoint (source of corrections)
252 // --------------------------------------------
[3635]253 if (!_opt->pppCorrMount.isEmpty()) {
[2966]254 QMutableListIterator<QString> itm(corrList);
255 while (itm.hasNext()) {
256 QStringList hlp = itm.next().split(" ");
257 if (hlp.size() > 0) {
258 QString mountpoint = hlp[hlp.size()-1];
[3635]259 if (mountpoint != _opt->pppCorrMount) {
[2966]260 itm.remove();
261 }
262 }
263 }
264 }
265
[2069]266 if (corrList.size() == 0) {
267 return;
268 }
269
270 // Remove All Corrections
271 // ----------------------
[3491]272 // QMapIterator<QString, t_corr*> ic(_corr);
273 // while (ic.hasNext()) {
274 // ic.next();
275 // delete ic.value();
276 // }
277 // _corr.clear();
[2069]278
[2035]279 QListIterator<QString> it(corrList);
280 while (it.hasNext()) {
[2911]281 QString line = it.next();
282
283 QTextStream in(&line);
[2035]284 int messageType;
285 int updateInterval;
286 int GPSweek;
287 double GPSweeks;
288 QString prn;
289 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
[2809]290
[2911]291 if ( t_corr::relevantMessageType(messageType) ) {
[2035]292 t_corr* cc = 0;
293 if (_corr.contains(prn)) {
294 cc = _corr.value(prn);
295 }
296 else {
297 cc = new t_corr();
298 _corr[prn] = cc;
299 }
[2207]300
[2911]301 cc->readLine(line);
302 _corr_tt = cc->tt;
[2035]303 }
[2317]304 else if ( messageType == BTYPE_GPS ) {
[2274]305
306 t_bias* bb = 0;
307 if (_bias.contains(prn)) {
[2341]308 bb = _bias.value(prn);
[2274]309 }
310 else {
311 bb = new t_bias();
312 _bias[prn] = bb;
313 }
314
315 bb->tt.set(GPSweek, GPSweeks);
316
317 int numBiases;
318 in >> numBiases;
319 for (int ii = 0; ii < numBiases; ++ii) {
320 int bType;
321 double bValue;
322 in >> bType >> bValue;
[2426]323 if (bType == CODETYPEGPS_L1_Z) {
[2429]324 bb->p1 = bValue;
[2274]325 }
[2426]326 else if (bType == CODETYPEGPS_L1_CA) {
[2429]327 bb->c1 = bValue;
[2426]328 }
[2365]329 else if (bType == CODETYPEGPS_L2_Z) {
[2429]330 bb->p2 = bValue;
[2274]331 }
332 }
333 }
[2035]334 }
[2208]335
336 QMutableMapIterator<QString, t_corr*> im(_corr);
337 while (im.hasNext()) {
338 im.next();
339 t_corr* cc = im.value();
340 if (!cc->ready()) {
341 delete cc;
342 im.remove();
343 }
344 }
[2035]345}
346
347// Satellite Position
348////////////////////////////////////////////////////////////////////////////
[2123]349t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
[2357]350 ColumnVector& xc, ColumnVector& vv) {
[2035]351
[2576]352 const double MAXAGE = 120.0;
[2041]353
[2035]354 if (_eph.contains(prn)) {
[2040]355
[3635]356 if (_opt->pppMode) {
[2068]357 if (_corr.contains(prn)) {
358 t_corr* cc = _corr.value(prn);
[2576]359 if (tt - cc->tt < MAXAGE) {
360 t_eph* eLast = _eph.value(prn)->last;
361 t_eph* ePrev = _eph.value(prn)->prev;
362 if (eLast && eLast->IOD() == cc->iod) {
363 eLast->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
[3521]364 return applyCorr(tt, cc, xc, vv);
[2576]365 }
366 else if (ePrev && ePrev->IOD() == cc->iod) {
367 ePrev->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
[3521]368 return applyCorr(tt, cc, xc, vv);
[2576]369 }
370 }
[2041]371 }
[2040]372 }
[2576]373
374 else {
375 t_eph* ee = _eph.value(prn)->last;
376 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
377 return success;
378 }
[2035]379 }
380
381 return failure;
382}
383
384//
385////////////////////////////////////////////////////////////////////////////
[3319]386t_irc bncPPPclient::applyCorr(const bncTime& tt, const t_corr* cc,
[3521]387 ColumnVector& xc, ColumnVector& vv) {
[2041]388
[2367]389 double dt = tt - cc->tt;
390 ColumnVector raoHlp = cc->rao + cc->dotRao * dt + cc->dotDotRao * dt * dt;
[2296]391
[3319]392 if (raoHlp.norm_Frobenius() > 20.0) {
393 return failure;
394 }
395
[3521]396 ColumnVector dx(3);
397 RSW_to_XYZ(xc.Rows(1,3), vv, raoHlp, dx);
398 xc[0] -= dx[0];
399 xc[1] -= dx[1];
400 xc[2] -= dx[2];
401 xc[3] += cc->dClk + cc->dotDClk * dt + cc->dotDotDClk * dt * dt
402 + cc->hrClk;
[2296]403
[3319]404 return success;
[2041]405}
406
[2049]407// Correct Time of Transmission
408////////////////////////////////////////////////////////////////////////////
[2240]409t_irc bncPPPclient::cmpToT(t_satData* satData) {
[2049]410
[2051]411 double prange = satData->P3;
[2049]412 if (prange == 0.0) {
413 return failure;
414 }
415
416 double clkSat = 0.0;
417 for (int ii = 1; ii <= 10; ii++) {
418
[2808]419 bncTime ToT = satData->tt - prange / t_CST::c - clkSat;
[2049]420
421 ColumnVector xc(4);
422 ColumnVector vv(3);
[2357]423 if (getSatPos(ToT, satData->prn, xc, vv) != success) {
[2049]424 return failure;
425 }
426
427 double clkSatOld = clkSat;
[2050]428 clkSat = xc(4);
[2049]429
430 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
431 satData->xx = xc.Rows(1,3);
432 satData->vv = vv;
[2050]433 satData->clk = clkSat * t_CST::c;
[2049]434 return success;
435 }
436 }
437
438 return failure;
439}
440
[2041]441//
442////////////////////////////////////////////////////////////////////////////
[2808]443void bncPPPclient::processFrontEpoch() {
[2035]444
[2053]445 // Data Pre-Processing
446 // -------------------
[3408]447 QMutableMapIterator<QString, t_satData*> it(_epoData.front()->satData);
448 while (it.hasNext()) {
449 it.next();
450 QString prn = it.key();
451 t_satData* satData = it.value();
[2049]452
[2240]453 if (cmpToT(satData) != success) {
[2049]454 delete satData;
[3408]455 it.remove();
[2049]456 continue;
457 }
[2053]458 }
[2035]459
[2060]460 // Filter Solution
461 // ---------------
[2808]462 if (_model->update(_epoData.front()) == success) {
[2145]463 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
[2060]464 }
[2035]465}
[2808]466
467//
468////////////////////////////////////////////////////////////////////////////
469void bncPPPclient::processEpochs() {
[2809]470
[2810]471 // Make sure the buffer does not grow beyond any limit
472 // ---------------------------------------------------
473 const unsigned MAX_EPODATA_SIZE = 120;
474 if (_epoData.size() > MAX_EPODATA_SIZE) {
475 delete _epoData.front();
476 _epoData.pop();
477 }
478
479 // Loop over all unprocessed epochs
480 // --------------------------------
[2809]481 while (!_epoData.empty()) {
[2810]482
[2809]483 t_epoData* frontEpoData = _epoData.front();
484
485 // No corrections yet, skip the epoch
486 // ----------------------------------
[3635]487 if (_opt->corrSync != 0.0 && !_corr_tt.valid()) {
[2810]488 return;
[2809]489 }
490
[2810]491 // Process the front epoch
492 // -----------------------
[3635]493 if (_opt->corrSync == 0 || frontEpoData->tt - _corr_tt < _opt->corrSync) {
[2809]494 processFrontEpoch();
495 delete _epoData.front();
496 _epoData.pop();
497 }
498 else {
499 return;
500 }
501 }
[2808]502}
Note: See TracBrowser for help on using the repository browser.