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

Last change on this file since 2426 was 2426, checked in by mervart, 14 years ago

* empty log message *

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