source: ntrip/branches/BNC_LM/bncpppclient.cpp@ 10334

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