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

Last change on this file since 2429 was 2429, checked in by mervart, 12 years ago

* empty log message *

File size: 13.4 KB
Line 
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
41#include <newmatio.h>
42#include <iomanip>
43
44#include "bncpppclient.h"
45#include "bncapp.h"
46#include "bncutils.h"
47#include "bncconst.h"
48#include "bncmodel.h"
49#include "bncsettings.h"
50
51extern "C" {
52#include "clock_orbit_rtcm.h"
53}
54
55using namespace std;
56
57// Constructor
58////////////////////////////////////////////////////////////////////////////
59bncPPPclient::bncPPPclient(QByteArray staID) {
60
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
70 if (settings.value("pppSPP").toString() == "PPP") {
71 _pppMode = true;
72 }
73 else {
74 _pppMode = false;
75 }
76
77 connect(((bncApp*)qApp), SIGNAL(newEphGPS(gpsephemeris)),
78 this, SLOT(slotNewEphGPS(gpsephemeris)));
79 connect(((bncApp*)qApp), SIGNAL(newEphGlonass(glonassephemeris)),
80 this, SLOT(slotNewEphGlonass(glonassephemeris)));
81 connect(((bncApp*)qApp), SIGNAL(newCorrections(QList<QString>)),
82 this, SLOT(slotNewCorrections(QList<QString>)));
83
84 _staID = staID;
85 _epoData = 0;
86 _model = new bncModel(staID);
87 connect(_model, SIGNAL(newNMEAstr(QByteArray)),
88 this, SIGNAL(newNMEAstr(QByteArray)));
89}
90
91// Destructor
92////////////////////////////////////////////////////////////////////////////
93bncPPPclient::~bncPPPclient() {
94 delete _model;
95 delete _epoData;
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 }
106 QMapIterator<QString, t_bias*> ib(_bias);
107 while (ib.hasNext()) {
108 ib.next();
109 delete ib.value();
110 }
111}
112
113//
114////////////////////////////////////////////////////////////////////////////
115void bncPPPclient::putNewObs(p_obs pp) {
116 QMutexLocker locker(&_mutex);
117
118 t_obsInternal* obs = &(pp->_o);
119
120 if (obs->satSys != 'G' && !_useGlonass) {
121 return;
122 }
123
124 t_satData* satData = new t_satData();
125
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
144 // Set Code Observations
145 // ---------------------
146 if (obs->P1) {
147 satData->P1 = obs->P1 + (bb ? bb->p1 : 0.0);
148 satData->codeTypeF1 = t_satData::P_CODE;
149 }
150 else if (obs->C1) {
151 satData->P1 = obs->C1 + (bb ? bb->c1 : 0.0);
152 satData->codeTypeF1 = t_satData::C_CODE;
153 }
154 else {
155 delete satData;
156 return;
157 }
158
159 if (obs->P2) {
160 satData->P2 = obs->P2 + (bb ? bb->p2 : 0.0);
161 satData->codeTypeF2 = t_satData::P_CODE;
162 }
163 else if (obs->C2) {
164 satData->P2 = obs->C2;
165 satData->codeTypeF2 = t_satData::C_CODE;
166 }
167 else {
168 delete satData;
169 return;
170 }
171
172 double f1 = t_CST::freq1;
173 double f2 = t_CST::freq2;
174
175 if (obs->satSys == 'R') {
176 f1 = 1602000000.0 + 562500.0 * obs->slot;
177 f2 = 1246000000.0 + 437500.0 * obs->slot;
178 }
179
180 // Ionosphere-Free Combination
181 // ---------------------------
182 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
183 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
184
185 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
186
187 // Set Phase Observations
188 // ----------------------
189 if (obs->L1 && obs->L2) {
190 satData->L1 = obs->L1 * t_CST::c / f1;
191 satData->L2 = obs->L2 * t_CST::c / f2;
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 // ------------------------------
201 bncTime tt(obs->GPSWeek, obs->GPSWeeks);
202
203 if (!_epoData) {
204 _epoData = new t_epoData();
205 _epoData->tt = tt;
206 }
207 else if (tt != _epoData->tt) {
208 processEpoch();
209 delete _epoData;
210 _epoData = new t_epoData();
211 _epoData->tt = tt;
212 }
213
214 if (obs->satSys == 'G') {
215 _epoData->satDataGPS[satData->prn] = satData;
216 }
217 else if (obs->satSys == 'R') {
218 _epoData->satDataGlo[satData->prn] = satData;
219 }
220
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)) {
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 }
237 }
238 else {
239 t_ephGPS* ee = new t_ephGPS();
240 ee->set(&gpseph);
241 _eph[prn] = ee;
242 }
243}
244
245//
246////////////////////////////////////////////////////////////////////////////
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)) {
253 int ww = gloeph.GPSWeek;
254 int tow = gloeph.GPSTOW;
255 updatetime(&ww, &tow, gloeph.tb*1000, 0); // Moscow -> GPS
256 t_ephGlo* ee = static_cast<t_ephGlo*>(_eph.value(prn));
257 if (ee->GPSweek() < ww ||
258 (ee->GPSweek() == ww && ee->GPSweeks() < tow)) {
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////////////////////////////////////////////////////////////////////////////
271void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
272 QMutexLocker locker(&_mutex);
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
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 ||
297 messageType == COTYPE_GLONASSCOMBINED ||
298 messageType == COTYPE_GPSORBIT ||
299 messageType == COTYPE_GPSCLOCK ||
300 messageType == COTYPE_GLONASSORBIT ||
301 messageType == COTYPE_GLONASSCLOCK ) {
302
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 }
311
312 cc->tt.set(GPSweek, GPSweeks);
313
314 if ( messageType == COTYPE_GPSCOMBINED ||
315 messageType == COTYPE_GLONASSCOMBINED ) {
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;
329 cc->raoSet = true;
330 cc->dClkSet = true;
331 }
332 else if ( messageType == COTYPE_GPSORBIT ||
333 messageType == COTYPE_GLONASSORBIT ) {
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];
341 cc->raoSet = true;
342 }
343 else if ( messageType == COTYPE_GPSCLOCK ||
344 messageType == COTYPE_GLONASSCLOCK ) {
345 int dummyIOD;
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;
353 cc->dClkSet = true;
354 }
355 }
356 else if ( messageType == BTYPE_GPS ) {
357
358 t_bias* bb = 0;
359 if (_bias.contains(prn)) {
360 bb = _bias.value(prn);
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;
375 if (bType == CODETYPEGPS_L1_Z) {
376 bb->p1 = bValue;
377 }
378 else if (bType == CODETYPEGPS_L1_CA) {
379 bb->c1 = bValue;
380 }
381 else if (bType == CODETYPEGPS_L2_Z) {
382 bb->p2 = bValue;
383 }
384 }
385 }
386 }
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 }
397}
398
399// Satellite Position
400////////////////////////////////////////////////////////////////////////////
401t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
402 ColumnVector& xc, ColumnVector& vv) {
403
404 const double MAXAGE = 120.0;
405
406 if (_eph.contains(prn)) {
407 t_eph* ee = _eph.value(prn);
408 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
409
410 if (_pppMode) {
411 if (_corr.contains(prn)) {
412 t_corr* cc = _corr.value(prn);
413 if (ee->IOD() == cc->iod && (tt - cc->tt) < MAXAGE) {
414 applyCorr(tt, cc, xc, vv);
415 return success;
416 }
417 }
418 return failure;
419 }
420
421 return success;
422 }
423
424 return failure;
425}
426
427//
428////////////////////////////////////////////////////////////////////////////
429void bncPPPclient::applyCorr(const bncTime& tt, const t_corr* cc,
430 ColumnVector& xc, ColumnVector& vv) {
431 ColumnVector dx(3);
432
433 double dt = tt - cc->tt;
434 ColumnVector raoHlp = cc->rao + cc->dotRao * dt + cc->dotDotRao * dt * dt;
435
436 RSW_to_XYZ(xc.Rows(1,3), vv, raoHlp, dx);
437
438 xc[0] -= dx[0];
439 xc[1] -= dx[1];
440 xc[2] -= dx[2];
441 xc[3] += cc->dClk + cc->dotDClk * dt + cc->dotDotDClk * dt * dt;
442}
443
444// Correct Time of Transmission
445////////////////////////////////////////////////////////////////////////////
446t_irc bncPPPclient::cmpToT(t_satData* satData) {
447
448 double prange = satData->P3;
449 if (prange == 0.0) {
450 return failure;
451 }
452
453 double clkSat = 0.0;
454 for (int ii = 1; ii <= 10; ii++) {
455
456 bncTime ToT = _epoData->tt - prange / t_CST::c - clkSat;
457
458 ColumnVector xc(4);
459 ColumnVector vv(3);
460 if (getSatPos(ToT, satData->prn, xc, vv) != success) {
461 return failure;
462 }
463
464 double clkSatOld = clkSat;
465 clkSat = xc(4);
466
467 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
468 satData->xx = xc.Rows(1,3);
469 satData->vv = vv;
470 satData->clk = clkSat * t_CST::c;
471 return success;
472 }
473 }
474
475 return failure;
476}
477
478//
479////////////////////////////////////////////////////////////////////////////
480void bncPPPclient::processEpoch() {
481
482 // Data Pre-Processing
483 // -------------------
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();
489
490 if (cmpToT(satData) != success) {
491 delete satData;
492 iGPS.remove();
493 continue;
494 }
495 }
496
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
503 if (cmpToT(satData) != success) {
504 delete satData;
505 iGlo.remove();
506 continue;
507 }
508 }
509
510 // Filter Solution
511 // ---------------
512 if (_model->update(_epoData) == success) {
513 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
514 }
515}
516
Note: See TracBrowser for help on using the repository browser.