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

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

* empty log message *

File size: 12.9 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 connect(((bncApp*)qApp), SIGNAL(newEphGPS(gpsephemeris)),
71 this, SLOT(slotNewEphGPS(gpsephemeris)));
72 connect(((bncApp*)qApp), SIGNAL(newEphGlonass(glonassephemeris)),
73 this, SLOT(slotNewEphGlonass(glonassephemeris)));
74 connect(((bncApp*)qApp), SIGNAL(newCorrections(QList<QString>)),
75 this, SLOT(slotNewCorrections(QList<QString>)));
76
77 _staID = staID;
78 _epoData = 0;
79 _model = new bncModel(staID);
80 connect(_model, SIGNAL(newNMEAstr(QByteArray)),
81 this, SIGNAL(newNMEAstr(QByteArray)));
82}
83
84// Destructor
85////////////////////////////////////////////////////////////////////////////
86bncPPPclient::~bncPPPclient() {
87 delete _model;
88 delete _epoData;
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 }
99 QMapIterator<QString, t_bias*> ib(_bias);
100 while (ib.hasNext()) {
101 ib.next();
102 delete ib.value();
103 }
104}
105
106//
107////////////////////////////////////////////////////////////////////////////
108void bncPPPclient::putNewObs(p_obs pp) {
109 QMutexLocker locker(&_mutex);
110
111 t_obsInternal* obs = &(pp->_o);
112
113 if (obs->satSys != 'G' && !_useGlonass) {
114 return;
115 }
116
117 t_satData* satData = new t_satData();
118
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
137 // Set Code Observations
138 // ---------------------
139 if (obs->P1) {
140 satData->P1 = obs->P1 + (bb ? bb->p1 : 0.0);
141 satData->codeTypeF1 = t_satData::P_CODE;
142 }
143 else if (obs->C1) {
144 satData->P1 = obs->C1 + (bb ? bb->c1 : 0.0);
145 satData->codeTypeF1 = t_satData::C_CODE;
146 }
147 else {
148 delete satData;
149 return;
150 }
151
152 if (obs->P2) {
153 satData->P2 = obs->P2 + (bb ? bb->p2 : 0.0);
154 satData->codeTypeF2 = t_satData::P_CODE;
155 }
156 else if (obs->C2) {
157 satData->P2 = obs->C2 + (bb ? bb->c2 : 0.0);
158 satData->codeTypeF2 = t_satData::C_CODE;
159 }
160 else {
161 delete satData;
162 return;
163 }
164
165 double f1 = t_CST::freq1;
166 double f2 = t_CST::freq2;
167
168 if (obs->satSys == 'R') {
169 f1 = 1602000000.0 + 562500.0 * obs->slot;
170 f2 = 1246000000.0 + 437500.0 * obs->slot;
171 }
172
173 // Ionosphere-Free Combination
174 // ---------------------------
175 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
176 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
177
178 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
179
180 // Set Phase Observations
181 // ----------------------
182 if (obs->L1 && obs->L2) {
183 satData->L1 = obs->L1 * t_CST::c / f1;
184 satData->L2 = obs->L2 * t_CST::c / f2;
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 // ------------------------------
194 bncTime tt(obs->GPSWeek, obs->GPSWeeks);
195
196 if (!_epoData) {
197 _epoData = new t_epoData();
198 _epoData->tt = tt;
199 }
200 else if (tt != _epoData->tt) {
201 processEpoch();
202 delete _epoData;
203 _epoData = new t_epoData();
204 _epoData->tt = tt;
205 }
206
207 if (obs->satSys == 'G') {
208 _epoData->satDataGPS[satData->prn] = satData;
209 }
210 else if (obs->satSys == 'R') {
211 _epoData->satDataGlo[satData->prn] = satData;
212 }
213
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)) {
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 }
230 }
231 else {
232 t_ephGPS* ee = new t_ephGPS();
233 ee->set(&gpseph);
234 _eph[prn] = ee;
235 }
236}
237
238//
239////////////////////////////////////////////////////////////////////////////
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)) {
246 int ww = gloeph.GPSWeek;
247 int tow = gloeph.GPSTOW;
248 updatetime(&ww, &tow, gloeph.tb*1000, 0); // Moscow -> GPS
249 t_ephGlo* ee = static_cast<t_ephGlo*>(_eph.value(prn));
250 if (ee->GPSweek() < ww ||
251 (ee->GPSweek() == ww && ee->GPSweeks() < tow)) {
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////////////////////////////////////////////////////////////////////////////
264void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
265 QMutexLocker locker(&_mutex);
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
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 ||
290 messageType == COTYPE_GLONASSCOMBINED ||
291 messageType == COTYPE_GPSORBIT ||
292 messageType == COTYPE_GPSCLOCK ||
293 messageType == COTYPE_GLONASSORBIT ||
294 messageType == COTYPE_GLONASSCLOCK ) {
295
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 }
304
305 cc->tt.set(GPSweek, GPSweeks);
306
307 if ( messageType == COTYPE_GPSCOMBINED ||
308 messageType == COTYPE_GLONASSCOMBINED ) {
309 cc->rao.ReSize(3);
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];
315 cc->dClk /= t_CST::c;
316 cc->raoSet = true;
317 cc->dClkSet = true;
318 }
319 else if ( messageType == COTYPE_GPSORBIT ||
320 messageType == COTYPE_GLONASSORBIT ) {
321 cc->rao.ReSize(3);
322 in >> cc->iod >> cc->rao[0] >> cc->rao[1] >> cc->rao[2]
323 >> cc->dotRao[0] >> cc->dotRao[1] >> cc->dotRao[2];
324 cc->raoSet = true;
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;
331 cc->dClkSet = true;
332 }
333 }
334 else if ( messageType == BTYPE_GPS ||
335 messageType == BTYPE_GLONASS ) {
336
337 t_bias* bb = 0;
338 if (_bias.contains(prn)) {
339 bb = _bias.value(prn);
340 }
341 else {
342 bb = new t_bias();
343 _bias[prn] = bb;
344 }
345
346 bb->tt.set(GPSweek, GPSweeks);
347
348 int numBiases;
349 in >> numBiases;
350 for (int ii = 0; ii < numBiases; ++ii) {
351 int bType;
352 double bValue;
353 in >> bType >> bValue;
354 if (bType == 0) {
355 bb->c1 = bValue;
356 }
357 else if (bType >= 1 && bType <= 4) {
358 bb->p1 = bValue;
359 }
360 else if (bType == 5) {
361 bb->c2 = bValue;
362 }
363 else if (bType >= 6 && bType <= 13) {
364 bb->p2 = bValue;
365 }
366 }
367 }
368 }
369
370 QMutableMapIterator<QString, t_corr*> im(_corr);
371 while (im.hasNext()) {
372 im.next();
373 t_corr* cc = im.value();
374 if (!cc->ready()) {
375 delete cc;
376 im.remove();
377 }
378 }
379}
380
381// Satellite Position
382////////////////////////////////////////////////////////////////////////////
383t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
384 ColumnVector& xc, ColumnVector& vv, bool& corr) {
385
386 const bool CORR_REQUIRED = true;
387 const double MAXAGE = 120.0;
388
389 corr = false;
390
391 if (_eph.contains(prn)) {
392 t_eph* ee = _eph.value(prn);
393 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
394
395 if (CORR_REQUIRED) {
396 if (_corr.contains(prn)) {
397 t_corr* cc = _corr.value(prn);
398 if (ee->IOD() == cc->iod && (tt - cc->tt) < MAXAGE) {
399 corr = true;
400 applyCorr(tt, cc, xc, vv);
401 return success;
402 }
403 }
404 return failure;
405 }
406
407 return success;
408 }
409
410 return failure;
411}
412
413//
414////////////////////////////////////////////////////////////////////////////
415void bncPPPclient::applyCorr(const bncTime& tt, const t_corr* cc,
416 ColumnVector& xc, ColumnVector& vv) {
417 ColumnVector dx(3);
418
419 ColumnVector raoHlp = cc->rao + cc->dotRao * (tt - cc->tt);
420
421 RSW_to_XYZ(xc.Rows(1,3), vv, raoHlp, dx);
422
423 xc[0] -= dx[0];
424 xc[1] -= dx[1];
425 xc[2] -= dx[2];
426 xc[3] -= cc->dClk;
427
428 // Relativistic Correction
429 // -----------------------
430 xc[3] -= 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c ;
431}
432
433// Correct Time of Transmission
434////////////////////////////////////////////////////////////////////////////
435t_irc bncPPPclient::cmpToT(t_satData* satData) {
436
437 double prange = satData->P3;
438 if (prange == 0.0) {
439 return failure;
440 }
441
442 double clkSat = 0.0;
443 for (int ii = 1; ii <= 10; ii++) {
444
445 bncTime ToT = _epoData->tt - prange / t_CST::c - clkSat;
446
447 ColumnVector xc(4);
448 ColumnVector vv(3);
449 bool corr = false;
450 if (getSatPos(ToT, satData->prn, xc, vv, corr) != success) {
451 return failure;
452 }
453
454 double clkSatOld = clkSat;
455 clkSat = xc(4);
456
457 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
458 satData->xx = xc.Rows(1,3);
459 satData->vv = vv;
460 satData->clk = clkSat * t_CST::c;
461 satData->clkCorr = corr;
462 return success;
463 }
464 }
465
466 return failure;
467}
468
469//
470////////////////////////////////////////////////////////////////////////////
471void bncPPPclient::processEpoch() {
472
473 // Data Pre-Processing
474 // -------------------
475 QMutableMapIterator<QString, t_satData*> iGPS(_epoData->satDataGPS);
476 while (iGPS.hasNext()) {
477 iGPS.next();
478 QString prn = iGPS.key();
479 t_satData* satData = iGPS.value();
480
481 if (cmpToT(satData) != success) {
482 delete satData;
483 iGPS.remove();
484 continue;
485 }
486 }
487
488 QMutableMapIterator<QString, t_satData*> iGlo(_epoData->satDataGlo);
489 while (iGlo.hasNext()) {
490 iGlo.next();
491 QString prn = iGlo.key();
492 t_satData* satData = iGlo.value();
493
494 if (cmpToT(satData) != success) {
495 delete satData;
496 iGlo.remove();
497 continue;
498 }
499 }
500
501 // Filter Solution
502 // ---------------
503 if (_model->update(_epoData) == success) {
504 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
505 }
506}
507
Note: See TracBrowser for help on using the repository browser.