source: ntrip/trunk/BNC/src/bncpppclient.cpp@ 4394

Last change on this file since 4394 was 4391, checked in by mervart, 13 years ago
File size: 14.7 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') {
[4391]162 double C1C = obs.measdata("C1C");
163 double C1P = obs.measdata("C1P");
164 double C2C = obs.measdata("C2C");
165 double C2P = obs.measdata("C2P");
166 double L1 = obs.measdata("L1");
167 double L2 = obs.measdata("L2");
168 if ( (C1P || C1C) && (C2P || C2C) && L1 && L2 ) {
[2786]169 double f1 = t_CST::freq1;
170 double f2 = t_CST::freq2;
171 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
172 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
[4391]173 if (C1P) {
174 satData->P1 = C1P + (bb ? bb->p1 : 0.0);
[2786]175 }
176 else {
[4391]177 satData->P1 = C1C + (bb ? bb->c1 : 0.0);
[2786]178 }
[4391]179 if (C2P) {
180 satData->P2 = C2P + (bb ? bb->p2 : 0.0);
[2786]181 }
182 else {
[4391]183 satData->P2 = C2C;
[2786]184 }
[4391]185 satData->L1 = L1 * t_CST::c / f1;
186 satData->L2 = L2 * t_CST::c / f2;
[2786]187 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
188 satData->L3 = c1 * satData->L1 + c2 * satData->L2;
189 satData->lambda3 = c1 * t_CST::c / f1 + c2 * t_CST::c / f2;
[2778]190
[3408]191 _epoData.back()->satData[satData->prn] = satData;
[2778]192 }
193 else {
194 delete satData;
195 }
[2786]196 }
[2778]197
[2786]198 // Set Observations GLONASS
199 // ------------------------
[2711]200 else if (obs.satSys == 'R') {
[4391]201 double C1C = obs.measdata("C1C");
202 double C1P = obs.measdata("C1P");
203 double C2C = obs.measdata("C2C");
204 double C2P = obs.measdata("C2P");
205 double L1 = obs.measdata("L1");
206 double L2 = obs.measdata("L2");
207 if ( (C1P || C1C) && (C2P || C2C) && L1 && L2 ) {
[4265]208 double f1 = t_CST::f1(obs.satSys, obs.slotNum);
[4373]209 double f2 = t_CST::f2(obs.satSys, obs.slotNum);
[2786]210 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
211 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
[4391]212 if (C1P) {
213 satData->P1 = C1P + (bb ? bb->p1 : 0.0);
[2786]214 }
215 else {
[4391]216 satData->P1 = C1C + (bb ? bb->c1 : 0.0);
[2786]217 }
[4391]218 if (C2P) {
219 satData->P2 = C2P + (bb ? bb->p2 : 0.0);
[2786]220 }
221 else {
[4391]222 satData->P2 = C2C;
[2786]223 }
[4391]224 satData->L1 = L1 * t_CST::c / f1;
225 satData->L2 = L2 * t_CST::c / f2;
[2786]226 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
227 satData->L3 = c1 * satData->L1 + c2 * satData->L2;
228 satData->lambda3 = c1 * t_CST::c / f1 + c2 * t_CST::c / f2;
[2778]229
[3408]230 _epoData.back()->satData[satData->prn] = satData;
[2778]231 }
232 else {
233 delete satData;
234 }
[2786]235 }
[2778]236
[2786]237 // Set Observations Galileo
238 // ------------------------
[2778]239 else if (obs.satSys == 'E') {
[4391]240 double C1 = obs.measdata("C1");
241 double L1 = obs.measdata("L1");
242 double C5 = obs.measdata("C5");
243 double L5 = obs.measdata("L5");
244 if ( C1 && C5 && L1 && L5) {
[2786]245 double f1 = t_CST::freq1;
246 double f5 = t_CST::freq5;
247 double c1 = f1 * f1 / (f1 * f1 - f5 * f5);
248 double c5 = - f5 * f5 / (f1 * f1 - f5 * f5);
[2778]249
[4391]250 satData->P1 = C1;
251 satData->P5 = C5;
252 satData->L1 = L1 * t_CST::c / f1;
253 satData->L5 = L5 * t_CST::c / f5;
[2786]254 satData->P3 = c1 * satData->P1 + c5 * satData->P5;
255 satData->L3 = c1 * satData->L1 + c5 * satData->L5;
256 satData->lambda3 = c1 * t_CST::c / f1 + c5 * t_CST::c / f5;
[3408]257 _epoData.back()->satData[satData->prn] = satData;
[2778]258 }
259 else {
260 delete satData;
261 }
262 }
[2035]263}
264
265//
266////////////////////////////////////////////////////////////////////////////
267void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
268 QMutexLocker locker(&_mutex);
[2069]269
[2966]270 // Check the Mountpoint (source of corrections)
271 // --------------------------------------------
[3635]272 if (!_opt->pppCorrMount.isEmpty()) {
[2966]273 QMutableListIterator<QString> itm(corrList);
274 while (itm.hasNext()) {
275 QStringList hlp = itm.next().split(" ");
276 if (hlp.size() > 0) {
277 QString mountpoint = hlp[hlp.size()-1];
[3635]278 if (mountpoint != _opt->pppCorrMount) {
[2966]279 itm.remove();
280 }
281 }
282 }
283 }
284
[2069]285 if (corrList.size() == 0) {
286 return;
287 }
288
[2035]289 QListIterator<QString> it(corrList);
290 while (it.hasNext()) {
[2911]291 QString line = it.next();
292
293 QTextStream in(&line);
[2035]294 int messageType;
295 int updateInterval;
296 int GPSweek;
297 double GPSweeks;
298 QString prn;
299 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
[2809]300
[2911]301 if ( t_corr::relevantMessageType(messageType) ) {
[2035]302 t_corr* cc = 0;
303 if (_corr.contains(prn)) {
304 cc = _corr.value(prn);
305 }
306 else {
307 cc = new t_corr();
308 _corr[prn] = cc;
309 }
[2207]310
[2911]311 cc->readLine(line);
[3751]312 _corr_tt = cc->tClk;
[2035]313 }
[2317]314 else if ( messageType == BTYPE_GPS ) {
[2274]315
316 t_bias* bb = 0;
317 if (_bias.contains(prn)) {
[2341]318 bb = _bias.value(prn);
[2274]319 }
320 else {
321 bb = new t_bias();
322 _bias[prn] = bb;
323 }
324
325 bb->tt.set(GPSweek, GPSweeks);
326
327 int numBiases;
328 in >> numBiases;
329 for (int ii = 0; ii < numBiases; ++ii) {
330 int bType;
331 double bValue;
332 in >> bType >> bValue;
[2426]333 if (bType == CODETYPEGPS_L1_Z) {
[2429]334 bb->p1 = bValue;
[2274]335 }
[2426]336 else if (bType == CODETYPEGPS_L1_CA) {
[2429]337 bb->c1 = bValue;
[2426]338 }
[2365]339 else if (bType == CODETYPEGPS_L2_Z) {
[2429]340 bb->p2 = bValue;
[2274]341 }
342 }
343 }
[2035]344 }
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);
[3751]359 if (cc->ready() && tt - cc->tClk < MAXAGE) {
[2576]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
[3751]389 double dtRao = tt - cc->tRao;
390 ColumnVector raoHlp = cc->rao + cc->dotRao * dtRao
[4148]391 + 0.5 * cc->dotDotRao * dtRao * dtRao;
[2296]392
[3319]393 if (raoHlp.norm_Frobenius() > 20.0) {
394 return failure;
395 }
396
[3521]397 ColumnVector dx(3);
398 RSW_to_XYZ(xc.Rows(1,3), vv, raoHlp, dx);
399 xc[0] -= dx[0];
400 xc[1] -= dx[1];
401 xc[2] -= dx[2];
[3751]402
403 double dtClk = tt - cc->tClk;
404
405 xc[3] += cc->dClk + cc->dotDClk * dtClk + cc->dotDotDClk * dtClk * dtClk
[3521]406 + cc->hrClk;
[2296]407
[3319]408 return success;
[2041]409}
410
[2049]411// Correct Time of Transmission
412////////////////////////////////////////////////////////////////////////////
[2240]413t_irc bncPPPclient::cmpToT(t_satData* satData) {
[2049]414
[2051]415 double prange = satData->P3;
[2049]416 if (prange == 0.0) {
417 return failure;
418 }
419
420 double clkSat = 0.0;
421 for (int ii = 1; ii <= 10; ii++) {
422
[2808]423 bncTime ToT = satData->tt - prange / t_CST::c - clkSat;
[2049]424
425 ColumnVector xc(4);
426 ColumnVector vv(3);
[2357]427 if (getSatPos(ToT, satData->prn, xc, vv) != success) {
[2049]428 return failure;
429 }
430
431 double clkSatOld = clkSat;
[2050]432 clkSat = xc(4);
[2049]433
434 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
435 satData->xx = xc.Rows(1,3);
436 satData->vv = vv;
[2050]437 satData->clk = clkSat * t_CST::c;
[2049]438 return success;
439 }
440 }
441
442 return failure;
443}
444
[2041]445//
446////////////////////////////////////////////////////////////////////////////
[2808]447void bncPPPclient::processFrontEpoch() {
[2035]448
[4149]449#ifdef BNC_DEBUG
450 QString msg = "List of Corrections\n";
451 QMapIterator<QString, t_corr*> itC(_corr);
452 while (itC.hasNext()) {
453 itC.next();
454 QString src = itC.key();
455 const t_corr* corr = itC.value();
456 msg += QString("%1 %2 %3 %4\n")
457 .arg(corr->prn)
458 .arg(corr->iod)
459 .arg(QString(corr->tClk.datestr().c_str()) + "_" + QString(corr->tClk.timestr().c_str()))
460 .arg(QString(corr->tRao.datestr().c_str()) + "_" + QString(corr->tRao.timestr().c_str()));
461 }
462
463 msg += "List of Ephemeris\n";
464 QMapIterator<QString, t_ephPair*> itE(_eph);
465 while (itE.hasNext()) {
466 itE.next();
467 QString prn = itE.key();
468 const t_ephPair* ephPair = itE.value();
469 if (ephPair->prev) {
470 msg += QString("%1 %2 %3 %4 %5\n")
471 .arg(prn)
472 .arg(ephPair->last->IOD())
473 .arg(QString(ephPair->last->TOC().datestr().c_str()) + "_" +
474 QString(ephPair->last->TOC().timestr().c_str()))
475 .arg(ephPair->prev->IOD())
476 .arg(QString(ephPair->prev->TOC().datestr().c_str()) + "_" +
477 QString(ephPair->prev->TOC().timestr().c_str()));
478 }
479 else {
480 msg += QString("%1 %2 %3\n")
481 .arg(prn)
482 .arg(ephPair->last->IOD())
483 .arg(QString(ephPair->last->TOC().datestr().c_str()) + "_" +
484 QString(ephPair->last->TOC().timestr().c_str()));
485 }
486 }
487
488 emit newMessage(msg.toAscii(), false);
489#endif // BNC_DEBUG
490
[2053]491 // Data Pre-Processing
492 // -------------------
[3408]493 QMutableMapIterator<QString, t_satData*> it(_epoData.front()->satData);
494 while (it.hasNext()) {
495 it.next();
496 QString prn = it.key();
497 t_satData* satData = it.value();
[2049]498
[2240]499 if (cmpToT(satData) != success) {
[2049]500 delete satData;
[3408]501 it.remove();
[2049]502 continue;
503 }
[2053]504 }
[2035]505
[2060]506 // Filter Solution
507 // ---------------
[2808]508 if (_model->update(_epoData.front()) == success) {
[2145]509 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
[2060]510 }
[2035]511}
[2808]512
513//
514////////////////////////////////////////////////////////////////////////////
515void bncPPPclient::processEpochs() {
[2809]516
[2810]517 // Make sure the buffer does not grow beyond any limit
518 // ---------------------------------------------------
519 const unsigned MAX_EPODATA_SIZE = 120;
520 if (_epoData.size() > MAX_EPODATA_SIZE) {
521 delete _epoData.front();
522 _epoData.pop();
523 }
524
525 // Loop over all unprocessed epochs
526 // --------------------------------
[2809]527 while (!_epoData.empty()) {
[2810]528
[2809]529 t_epoData* frontEpoData = _epoData.front();
530
531 // No corrections yet, skip the epoch
532 // ----------------------------------
[3635]533 if (_opt->corrSync != 0.0 && !_corr_tt.valid()) {
[2810]534 return;
[2809]535 }
536
[2810]537 // Process the front epoch
538 // -----------------------
[3635]539 if (_opt->corrSync == 0 || frontEpoData->tt - _corr_tt < _opt->corrSync) {
[2809]540 processFrontEpoch();
541 delete _epoData.front();
542 _epoData.pop();
543 }
544 else {
545 return;
546 }
547 }
[2808]548}
Note: See TracBrowser for help on using the repository browser.