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

Last change on this file since 2379 was 2367, checked in by mervart, 15 years ago

* empty log message *

File size: 13.6 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;
148 satData->codeTypeF1 = t_satData::P_CODE;
149 }
150 else if (obs->C1) {
151 satData->P1 = obs->C1 + (bb ? bb->p1c1 : 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->p1p2 : 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
376#define CODETYPEGPS_L1_CA 0
377#define CODETYPEGPS_L1_P 1
378#define CODETYPEGPS_L1_Z 2
379#define CODETYPEGPS_L2_P 10
380#define CODETYPEGPS_L2_Z 11
381
382 if (bType == CODETYPEGPS_L1_CA) {
383 bb->p1c1 = bValue;
384 }
385 else if (bType == CODETYPEGPS_L2_Z) {
386 bb->p1p2 = bValue;
387 }
388 }
389 }
390 }
391
392 QMutableMapIterator<QString, t_corr*> im(_corr);
393 while (im.hasNext()) {
394 im.next();
395 t_corr* cc = im.value();
396 if (!cc->ready()) {
397 delete cc;
398 im.remove();
399 }
400 }
401}
402
403// Satellite Position
404////////////////////////////////////////////////////////////////////////////
405t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
406 ColumnVector& xc, ColumnVector& vv) {
407
408 const double MAXAGE = 120.0;
409
410 if (_eph.contains(prn)) {
411 t_eph* ee = _eph.value(prn);
412 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
413
414 if (_pppMode) {
415 if (_corr.contains(prn)) {
416 t_corr* cc = _corr.value(prn);
417 if (ee->IOD() == cc->iod && (tt - cc->tt) < MAXAGE) {
418 applyCorr(tt, cc, xc, vv);
419 return success;
420 }
421 }
422 return failure;
423 }
424
425 return success;
426 }
427
428 return failure;
429}
430
431//
432////////////////////////////////////////////////////////////////////////////
433void bncPPPclient::applyCorr(const bncTime& tt, const t_corr* cc,
434 ColumnVector& xc, ColumnVector& vv) {
435 ColumnVector dx(3);
436
437 double dt = tt - cc->tt;
438 ColumnVector raoHlp = cc->rao + cc->dotRao * dt + cc->dotDotRao * dt * dt;
439
440 RSW_to_XYZ(xc.Rows(1,3), vv, raoHlp, dx);
441
442 xc[0] -= dx[0];
443 xc[1] -= dx[1];
444 xc[2] -= dx[2];
445 xc[3] -= cc->dClk + cc->dotDClk * dt + cc->dotDotDClk * dt * dt;
446
447 // Relativistic Correction
448 // -----------------------
449 xc[3] -= 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c ;
450}
451
452// Correct Time of Transmission
453////////////////////////////////////////////////////////////////////////////
454t_irc bncPPPclient::cmpToT(t_satData* satData) {
455
456 double prange = satData->P3;
457 if (prange == 0.0) {
458 return failure;
459 }
460
461 double clkSat = 0.0;
462 for (int ii = 1; ii <= 10; ii++) {
463
464 bncTime ToT = _epoData->tt - prange / t_CST::c - clkSat;
465
466 ColumnVector xc(4);
467 ColumnVector vv(3);
468 if (getSatPos(ToT, satData->prn, xc, vv) != success) {
469 return failure;
470 }
471
472 double clkSatOld = clkSat;
473 clkSat = xc(4);
474
475 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
476 satData->xx = xc.Rows(1,3);
477 satData->vv = vv;
478 satData->clk = clkSat * t_CST::c;
479 return success;
480 }
481 }
482
483 return failure;
484}
485
486//
487////////////////////////////////////////////////////////////////////////////
488void bncPPPclient::processEpoch() {
489
490 // Data Pre-Processing
491 // -------------------
492 QMutableMapIterator<QString, t_satData*> iGPS(_epoData->satDataGPS);
493 while (iGPS.hasNext()) {
494 iGPS.next();
495 QString prn = iGPS.key();
496 t_satData* satData = iGPS.value();
497
498 if (cmpToT(satData) != success) {
499 delete satData;
500 iGPS.remove();
501 continue;
502 }
503 }
504
505 QMutableMapIterator<QString, t_satData*> iGlo(_epoData->satDataGlo);
506 while (iGlo.hasNext()) {
507 iGlo.next();
508 QString prn = iGlo.key();
509 t_satData* satData = iGlo.value();
510
511 if (cmpToT(satData) != success) {
512 delete satData;
513 iGlo.remove();
514 continue;
515 }
516 }
517
518 // Filter Solution
519 // ---------------
520 if (_model->update(_epoData) == success) {
521 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
522 }
523}
524
Note: See TracBrowser for help on using the repository browser.