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

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