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

Last change on this file since 2348 was 2342, checked in by weber, 16 years ago

* empty log message *

File size: 12.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>
43
[2035]44#include "bncpppclient.h"
[2113]45#include "bncapp.h"
[2043]46#include "bncutils.h"
[2044]47#include "bncconst.h"
[2058]48#include "bncmodel.h"
[2226]49#include "bncsettings.h"
[2035]50
51extern "C" {
52#include "clock_orbit_rtcm.h"
53}
54
55using namespace std;
56
57// Constructor
58////////////////////////////////////////////////////////////////////////////
59bncPPPclient::bncPPPclient(QByteArray staID) {
[2113]60
[2226]61 bncSettings settings;
62
63 if ( Qt::CheckState(settings.value("pppGLONASS").toInt()) == Qt::Checked) {
64 _useGlonass = true;
65 }
66 else {
67 _useGlonass = false;
68 }
69
[2113]70 connect(((bncApp*)qApp), SIGNAL(newEphGPS(gpsephemeris)),
71 this, SLOT(slotNewEphGPS(gpsephemeris)));
[2224]72 connect(((bncApp*)qApp), SIGNAL(newEphGlonass(glonassephemeris)),
73 this, SLOT(slotNewEphGlonass(glonassephemeris)));
[2113]74 connect(((bncApp*)qApp), SIGNAL(newCorrections(QList<QString>)),
75 this, SLOT(slotNewCorrections(QList<QString>)));
76
[2039]77 _staID = staID;
78 _epoData = 0;
[2113]79 _model = new bncModel(staID);
[2182]80 connect(_model, SIGNAL(newNMEAstr(QByteArray)),
81 this, SIGNAL(newNMEAstr(QByteArray)));
[2035]82}
83
84// Destructor
85////////////////////////////////////////////////////////////////////////////
86bncPPPclient::~bncPPPclient() {
[2058]87 delete _model;
[2039]88 delete _epoData;
[2035]89 QMapIterator<QString, t_eph*> it(_eph);
90 while (it.hasNext()) {
91 it.next();
92 delete it.value();
93 }
94 QMapIterator<QString, t_corr*> ic(_corr);
95 while (ic.hasNext()) {
96 ic.next();
97 delete ic.value();
98 }
[2274]99 QMapIterator<QString, t_bias*> ib(_bias);
100 while (ib.hasNext()) {
101 ib.next();
102 delete ib.value();
103 }
[2035]104}
105
106//
107////////////////////////////////////////////////////////////////////////////
108void bncPPPclient::putNewObs(p_obs pp) {
[2037]109 QMutexLocker locker(&_mutex);
[2051]110
[2037]111 t_obsInternal* obs = &(pp->_o);
[2051]112
[2226]113 if (obs->satSys != 'G' && !_useGlonass) {
[2225]114 return;
115 }
116
[2051]117 t_satData* satData = new t_satData();
118
[2274]119 // Satellite Number
120 // ----------------
121 if (obs->satSys == 'G') {
122 QString prn = QString("G%1").arg(obs->satNum, 2, 10, QChar('0'));
123 satData->prn = prn;
124 }
125 else if (obs->satSys == 'R') {
126 QString prn = QString("R%1").arg(obs->satNum, 2, 10, QChar('0'));
127 satData->prn = prn;
128 }
129
130 // Handle Code Biases
131 // ------------------
132 t_bias* bb = 0;
133 if (_bias.contains(satData->prn)) {
134 bb = _bias.value(satData->prn);
135 }
136
[2051]137 // Set Code Observations
138 // ---------------------
139 if (obs->P1) {
[2317]140 satData->P1 = obs->P1;
[2051]141 satData->codeTypeF1 = t_satData::P_CODE;
142 }
143 else if (obs->C1) {
[2317]144 satData->P1 = obs->C1 + (bb ? bb->p1c1 : 0.0);
[2051]145 satData->codeTypeF1 = t_satData::C_CODE;
146 }
147 else {
148 delete satData;
149 return;
150 }
151
152 if (obs->P2) {
[2317]153 satData->P2 = obs->P2 + (bb ? bb->p1p2 : 0.0);
[2051]154 satData->codeTypeF2 = t_satData::P_CODE;
155 }
156 else if (obs->C2) {
[2317]157 satData->P2 = obs->C2;
[2051]158 satData->codeTypeF2 = t_satData::C_CODE;
159 }
160 else {
161 delete satData;
162 return;
163 }
164
[2228]165 double f1 = t_CST::freq1;
166 double f2 = t_CST::freq2;
167
[2229]168 if (obs->satSys == 'R') {
169 f1 = 1602000000.0 + 562500.0 * obs->slot;
170 f2 = 1246000000.0 + 437500.0 * obs->slot;
171 }
172
[2228]173 // Ionosphere-Free Combination
174 // ---------------------------
175 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
176 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
177
[2051]178 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
179
180 // Set Phase Observations
181 // ----------------------
182 if (obs->L1 && obs->L2) {
[2228]183 satData->L1 = obs->L1 * t_CST::c / f1;
184 satData->L2 = obs->L2 * t_CST::c / f2;
[2051]185 }
186 else {
187 delete satData;
188 return;
189 }
190 satData->L3 = c1 * satData->L1 + c2 * satData->L2;
191
192 // Add new Satellite to the epoch
193 // ------------------------------
[2123]194 bncTime tt(obs->GPSWeek, obs->GPSWeeks);
[2037]195
[2039]196 if (!_epoData) {
197 _epoData = new t_epoData();
198 _epoData->tt = tt;
[2037]199 }
[2039]200 else if (tt != _epoData->tt) {
[2037]201 processEpoch();
[2039]202 delete _epoData;
203 _epoData = new t_epoData();
204 _epoData->tt = tt;
[2037]205 }
[2039]206
[2231]207 if (obs->satSys == 'G') {
[2274]208 _epoData->satDataGPS[satData->prn] = satData;
[2231]209 }
210 else if (obs->satSys == 'R') {
[2274]211 _epoData->satDataGlo[satData->prn] = satData;
[2231]212 }
[2233]213
[2035]214}
215
216//
217////////////////////////////////////////////////////////////////////////////
218void bncPPPclient::slotNewEphGPS(gpsephemeris gpseph) {
219 QMutexLocker locker(&_mutex);
220
221 QString prn = QString("G%1").arg(gpseph.satellite, 2, 10, QChar('0'));
222
223 if (_eph.contains(prn)) {
[2040]224 t_ephGPS* ee = static_cast<t_ephGPS*>(_eph.value(prn));
225 if ( (ee->GPSweek() < gpseph.GPSweek) ||
226 (ee->GPSweek() == gpseph.GPSweek &&
227 ee->TOC() < gpseph.TOC) ) {
228 ee->set(&gpseph);
229 }
[2035]230 }
231 else {
232 t_ephGPS* ee = new t_ephGPS();
233 ee->set(&gpseph);
234 _eph[prn] = ee;
235 }
236}
237
238//
239////////////////////////////////////////////////////////////////////////////
[2224]240void bncPPPclient::slotNewEphGlonass(glonassephemeris gloeph) {
241 QMutexLocker locker(&_mutex);
242
243 QString prn = QString("R%1").arg(gloeph.almanac_number, 2, 10, QChar('0'));
244
245 if (_eph.contains(prn)) {
[2268]246 int ww = gloeph.GPSWeek;
247 int tow = gloeph.GPSTOW;
248 updatetime(&ww, &tow, gloeph.tb*1000, 0); // Moscow -> GPS
[2224]249 t_ephGlo* ee = static_cast<t_ephGlo*>(_eph.value(prn));
[2268]250 if (ee->GPSweek() < ww ||
251 (ee->GPSweek() == ww && ee->GPSweeks() < tow)) {
[2224]252 ee->set(&gloeph);
253 }
254 }
255 else {
256 t_ephGlo* ee = new t_ephGlo();
257 ee->set(&gloeph);
258 _eph[prn] = ee;
259 }
260}
261
262//
263////////////////////////////////////////////////////////////////////////////
[2035]264void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
265 QMutexLocker locker(&_mutex);
[2069]266
267 if (corrList.size() == 0) {
268 return;
269 }
270
271 // Remove All Corrections
272 // ----------------------
273 QMapIterator<QString, t_corr*> ic(_corr);
274 while (ic.hasNext()) {
275 ic.next();
276 delete ic.value();
277 }
278 _corr.clear();
279
[2035]280 QListIterator<QString> it(corrList);
281 while (it.hasNext()) {
282 QTextStream in(it.next().toAscii());
283 int messageType;
284 int updateInterval;
285 int GPSweek;
286 double GPSweeks;
287 QString prn;
288 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
289 if ( messageType == COTYPE_GPSCOMBINED ||
[2207]290 messageType == COTYPE_GLONASSCOMBINED ||
291 messageType == COTYPE_GPSORBIT ||
292 messageType == COTYPE_GPSCLOCK ||
293 messageType == COTYPE_GLONASSORBIT ||
294 messageType == COTYPE_GLONASSCLOCK ) {
295
[2035]296 t_corr* cc = 0;
297 if (_corr.contains(prn)) {
298 cc = _corr.value(prn);
299 }
300 else {
301 cc = new t_corr();
302 _corr[prn] = cc;
303 }
[2207]304
[2035]305 cc->tt.set(GPSweek, GPSweeks);
[2207]306
307 if ( messageType == COTYPE_GPSCOMBINED ||
308 messageType == COTYPE_GLONASSCOMBINED ) {
309 cc->rao.ReSize(3);
[2296]310 cc->dotRao.ReSize(3);
311 cc->dotRao = 0.0;
312 double dummyDotClk;
313 in >> cc->iod >> cc->dClk >> cc->rao[0] >> cc->rao[1] >> cc->rao[2]
314 >> dummyDotClk >> cc->dotRao[0] >> cc->dotRao[1] >> cc->dotRao[2];
[2207]315 cc->dClk /= t_CST::c;
[2208]316 cc->raoSet = true;
317 cc->dClkSet = true;
[2207]318 }
319 else if ( messageType == COTYPE_GPSORBIT ||
320 messageType == COTYPE_GLONASSORBIT ) {
321 cc->rao.ReSize(3);
[2296]322 in >> cc->iod >> cc->rao[0] >> cc->rao[1] >> cc->rao[2]
323 >> cc->dotRao[0] >> cc->dotRao[1] >> cc->dotRao[2];
[2208]324 cc->raoSet = true;
[2207]325 }
326 else if ( messageType == COTYPE_GPSCLOCK ||
327 messageType == COTYPE_GLONASSCLOCK ) {
328 int dummyIOD;
329 in >> dummyIOD >> cc->dClk;
330 cc->dClk /= t_CST::c;
[2208]331 cc->dClkSet = true;
[2207]332 }
[2035]333 }
[2317]334 else if ( messageType == BTYPE_GPS ) {
[2274]335
336 t_bias* bb = 0;
337 if (_bias.contains(prn)) {
[2341]338 bb = _bias.value(prn);
[2274]339 }
340 else {
341 bb = new t_bias();
342 _bias[prn] = bb;
343 }
344
345 bb->tt.set(GPSweek, GPSweeks);
346
347 int numBiases;
348 in >> numBiases;
349 for (int ii = 0; ii < numBiases; ++ii) {
350 int bType;
351 double bValue;
352 in >> bType >> bValue;
[2317]353 if (bType == 0) {
[2342]354 bb->p1c1 = -bValue;
[2274]355 }
[2317]356 else if (bType == 10) {
[2342]357 bb->p1p2 = -bValue;
[2274]358 }
359 }
360 }
[2035]361 }
[2208]362
363 QMutableMapIterator<QString, t_corr*> im(_corr);
364 while (im.hasNext()) {
365 im.next();
366 t_corr* cc = im.value();
367 if (!cc->ready()) {
368 delete cc;
369 im.remove();
370 }
371 }
[2035]372}
373
374// Satellite Position
375////////////////////////////////////////////////////////////////////////////
[2123]376t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
[2041]377 ColumnVector& xc, ColumnVector& vv, bool& corr) {
[2035]378
[2341]379//const bool CORR_REQUIRED = true;
380 bncSettings settings;
[2067]381 const double MAXAGE = 120.0;
[2041]382
[2341]383
[2041]384 corr = false;
385
[2035]386 if (_eph.contains(prn)) {
[2040]387 t_eph* ee = _eph.value(prn);
388 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
389
[2341]390// if (CORR_REQUIRED) {
391 if (settings.value("pppSPP").toString() == "PPP") {
[2068]392 if (_corr.contains(prn)) {
393 t_corr* cc = _corr.value(prn);
394 if (ee->IOD() == cc->iod && (tt - cc->tt) < MAXAGE) {
395 corr = true;
[2296]396 applyCorr(tt, cc, xc, vv);
[2068]397 return success;
398 }
[2041]399 }
[2068]400 return failure;
[2040]401 }
[2068]402
[2035]403 return success;
404 }
405
406 return failure;
407}
408
409//
410////////////////////////////////////////////////////////////////////////////
[2296]411void bncPPPclient::applyCorr(const bncTime& tt, const t_corr* cc,
412 ColumnVector& xc, ColumnVector& vv) {
[2043]413 ColumnVector dx(3);
[2041]414
[2296]415 ColumnVector raoHlp = cc->rao + cc->dotRao * (tt - cc->tt);
416
417 RSW_to_XYZ(xc.Rows(1,3), vv, raoHlp, dx);
418
[2341]419 xc[0] -= dx[0];
420 xc[1] -= dx[1];
421 xc[2] -= dx[2];
422 xc[3] -= cc->dClk;
[2068]423
424 // Relativistic Correction
425 // -----------------------
426 xc[3] -= 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c ;
[2041]427}
428
[2049]429// Correct Time of Transmission
430////////////////////////////////////////////////////////////////////////////
[2240]431t_irc bncPPPclient::cmpToT(t_satData* satData) {
[2049]432
[2051]433 double prange = satData->P3;
[2049]434 if (prange == 0.0) {
435 return failure;
436 }
437
438 double clkSat = 0.0;
439 for (int ii = 1; ii <= 10; ii++) {
440
[2123]441 bncTime ToT = _epoData->tt - prange / t_CST::c - clkSat;
[2049]442
443 ColumnVector xc(4);
444 ColumnVector vv(3);
445 bool corr = false;
[2240]446 if (getSatPos(ToT, satData->prn, xc, vv, corr) != success) {
[2049]447 return failure;
448 }
449
450 double clkSatOld = clkSat;
[2050]451 clkSat = xc(4);
[2049]452
453 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
454 satData->xx = xc.Rows(1,3);
455 satData->vv = vv;
[2050]456 satData->clk = clkSat * t_CST::c;
[2049]457 satData->clkCorr = corr;
458 return success;
459 }
460 }
461
462 return failure;
463}
464
[2041]465//
466////////////////////////////////////////////////////////////////////////////
[2035]467void bncPPPclient::processEpoch() {
468
[2053]469 // Data Pre-Processing
470 // -------------------
[2231]471 QMutableMapIterator<QString, t_satData*> iGPS(_epoData->satDataGPS);
472 while (iGPS.hasNext()) {
473 iGPS.next();
474 QString prn = iGPS.key();
475 t_satData* satData = iGPS.value();
[2049]476
[2240]477 if (cmpToT(satData) != success) {
[2049]478 delete satData;
[2231]479 iGPS.remove();
[2049]480 continue;
481 }
[2053]482 }
[2035]483
[2231]484 QMutableMapIterator<QString, t_satData*> iGlo(_epoData->satDataGlo);
485 while (iGlo.hasNext()) {
486 iGlo.next();
487 QString prn = iGlo.key();
488 t_satData* satData = iGlo.value();
489
[2240]490 if (cmpToT(satData) != success) {
[2231]491 delete satData;
492 iGlo.remove();
493 continue;
494 }
495 }
496
[2060]497 // Filter Solution
498 // ---------------
[2143]499 if (_model->update(_epoData) == success) {
[2145]500 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
[2060]501 }
[2035]502}
[2049]503
Note: See TracBrowser for help on using the repository browser.