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

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