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

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

* empty log message *

File size: 11.3 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
[2238]41#include <iomanip>
42
[2035]43#include "bncpppclient.h"
[2113]44#include "bncapp.h"
[2043]45#include "bncutils.h"
[2044]46#include "bncconst.h"
[2058]47#include "bncmodel.h"
[2226]48#include "bncsettings.h"
[2035]49
50extern "C" {
51#include "clock_orbit_rtcm.h"
52}
53
54using namespace std;
55
56// Constructor
57////////////////////////////////////////////////////////////////////////////
58bncPPPclient::bncPPPclient(QByteArray staID) {
[2113]59
[2226]60 bncSettings settings;
61
62 if ( Qt::CheckState(settings.value("pppGLONASS").toInt()) == Qt::Checked) {
63 _useGlonass = true;
64 }
65 else {
66 _useGlonass = false;
67 }
68
[2113]69 connect(((bncApp*)qApp), SIGNAL(newEphGPS(gpsephemeris)),
70 this, SLOT(slotNewEphGPS(gpsephemeris)));
[2224]71 connect(((bncApp*)qApp), SIGNAL(newEphGlonass(glonassephemeris)),
72 this, SLOT(slotNewEphGlonass(glonassephemeris)));
[2113]73 connect(((bncApp*)qApp), SIGNAL(newCorrections(QList<QString>)),
74 this, SLOT(slotNewCorrections(QList<QString>)));
75
[2039]76 _staID = staID;
77 _epoData = 0;
[2113]78 _model = new bncModel(staID);
[2182]79 connect(_model, SIGNAL(newNMEAstr(QByteArray)),
80 this, SIGNAL(newNMEAstr(QByteArray)));
[2035]81}
82
83// Destructor
84////////////////////////////////////////////////////////////////////////////
85bncPPPclient::~bncPPPclient() {
[2058]86 delete _model;
[2039]87 delete _epoData;
[2035]88 QMapIterator<QString, t_eph*> it(_eph);
89 while (it.hasNext()) {
90 it.next();
91 delete it.value();
92 }
93 QMapIterator<QString, t_corr*> ic(_corr);
94 while (ic.hasNext()) {
95 ic.next();
96 delete ic.value();
97 }
98}
99
100//
101////////////////////////////////////////////////////////////////////////////
102void bncPPPclient::putNewObs(p_obs pp) {
[2037]103 QMutexLocker locker(&_mutex);
[2051]104
[2037]105 t_obsInternal* obs = &(pp->_o);
[2051]106
[2226]107 if (obs->satSys != 'G' && !_useGlonass) {
[2225]108 return;
109 }
110
[2051]111 t_satData* satData = new t_satData();
112
113 // Set Code Observations
114 // ---------------------
115 if (obs->P1) {
116 satData->P1 = obs->P1;
117 satData->codeTypeF1 = t_satData::P_CODE;
118 }
119 else if (obs->C1) {
120 satData->P1 = obs->C1;
121 satData->codeTypeF1 = t_satData::C_CODE;
122 }
123 else {
124 delete satData;
125 return;
126 }
127
128 if (obs->P2) {
129 satData->P2 = obs->P2;
130 satData->codeTypeF2 = t_satData::P_CODE;
131 }
132 else if (obs->C2) {
133 satData->P2 = obs->C2;
134 satData->codeTypeF2 = t_satData::C_CODE;
135 }
136 else {
137 delete satData;
138 return;
139 }
140
[2228]141 double f1 = t_CST::freq1;
142 double f2 = t_CST::freq2;
143
[2229]144 if (obs->satSys == 'R') {
145 f1 = 1602000000.0 + 562500.0 * obs->slot;
146 f2 = 1246000000.0 + 437500.0 * obs->slot;
147 }
148
[2228]149 // Ionosphere-Free Combination
150 // ---------------------------
151 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
152 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
153
[2051]154 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
155
156 // Set Phase Observations
157 // ----------------------
158 if (obs->L1 && obs->L2) {
[2228]159 satData->L1 = obs->L1 * t_CST::c / f1;
160 satData->L2 = obs->L2 * t_CST::c / f2;
[2051]161 }
162 else {
163 delete satData;
164 return;
165 }
166 satData->L3 = c1 * satData->L1 + c2 * satData->L2;
167
168 // Add new Satellite to the epoch
169 // ------------------------------
[2123]170 bncTime tt(obs->GPSWeek, obs->GPSWeeks);
[2037]171
[2039]172 if (!_epoData) {
173 _epoData = new t_epoData();
174 _epoData->tt = tt;
[2037]175 }
[2039]176 else if (tt != _epoData->tt) {
[2037]177 processEpoch();
[2039]178 delete _epoData;
179 _epoData = new t_epoData();
180 _epoData->tt = tt;
[2037]181 }
[2039]182
[2231]183 if (obs->satSys == 'G') {
[2232]184 QString prn = QString("G%1").arg(obs->satNum, 2, 10, QChar('0'));
[2240]185 satData->prn = prn;
[2231]186 _epoData->satDataGPS[prn] = satData;
187 }
188 else if (obs->satSys == 'R') {
[2232]189 QString prn = QString("R%1").arg(obs->satNum, 2, 10, QChar('0'));
[2240]190 satData->prn = prn;
[2231]191 _epoData->satDataGlo[prn] = satData;
192 }
[2233]193
[2035]194}
195
196//
197////////////////////////////////////////////////////////////////////////////
198void bncPPPclient::slotNewEphGPS(gpsephemeris gpseph) {
199 QMutexLocker locker(&_mutex);
200
201 QString prn = QString("G%1").arg(gpseph.satellite, 2, 10, QChar('0'));
202
203 if (_eph.contains(prn)) {
[2040]204 t_ephGPS* ee = static_cast<t_ephGPS*>(_eph.value(prn));
205 if ( (ee->GPSweek() < gpseph.GPSweek) ||
206 (ee->GPSweek() == gpseph.GPSweek &&
207 ee->TOC() < gpseph.TOC) ) {
208 ee->set(&gpseph);
209 }
[2035]210 }
211 else {
212 t_ephGPS* ee = new t_ephGPS();
213 ee->set(&gpseph);
214 _eph[prn] = ee;
215 }
216}
217
218//
219////////////////////////////////////////////////////////////////////////////
[2224]220void bncPPPclient::slotNewEphGlonass(glonassephemeris gloeph) {
221 QMutexLocker locker(&_mutex);
222
223 QString prn = QString("R%1").arg(gloeph.almanac_number, 2, 10, QChar('0'));
224
225 if (_eph.contains(prn)) {
226 t_ephGlo* ee = static_cast<t_ephGlo*>(_eph.value(prn));
227 if ( (ee->GPSweek() < gloeph.GPSWeek) ||
228 (ee->GPSweek() == gloeph.GPSWeek &&
229 ee->GPSweeks() < gloeph.GPSTOW) ) {
230 ee->set(&gloeph);
231 }
232 }
233 else {
234 t_ephGlo* ee = new t_ephGlo();
235 ee->set(&gloeph);
236 _eph[prn] = ee;
237 }
238}
239
240//
241////////////////////////////////////////////////////////////////////////////
[2035]242void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
243 QMutexLocker locker(&_mutex);
[2069]244
245 if (corrList.size() == 0) {
246 return;
247 }
248
249 // Remove All Corrections
250 // ----------------------
251 QMapIterator<QString, t_corr*> ic(_corr);
252 while (ic.hasNext()) {
253 ic.next();
254 delete ic.value();
255 }
256 _corr.clear();
257
[2035]258 QListIterator<QString> it(corrList);
259 while (it.hasNext()) {
260 QTextStream in(it.next().toAscii());
261 int messageType;
262 int updateInterval;
263 int GPSweek;
264 double GPSweeks;
265 QString prn;
266 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
267 if ( messageType == COTYPE_GPSCOMBINED ||
[2207]268 messageType == COTYPE_GLONASSCOMBINED ||
269 messageType == COTYPE_GPSORBIT ||
270 messageType == COTYPE_GPSCLOCK ||
271 messageType == COTYPE_GLONASSORBIT ||
272 messageType == COTYPE_GLONASSCLOCK ) {
273
[2035]274 t_corr* cc = 0;
275 if (_corr.contains(prn)) {
276 cc = _corr.value(prn);
277 }
278 else {
279 cc = new t_corr();
280 _corr[prn] = cc;
281 }
[2207]282
[2035]283 cc->tt.set(GPSweek, GPSweeks);
[2207]284
285 if ( messageType == COTYPE_GPSCOMBINED ||
286 messageType == COTYPE_GLONASSCOMBINED ) {
287 cc->rao.ReSize(3);
288 in >> cc->iod >> cc->dClk >> cc->rao[0] >> cc->rao[1] >> cc->rao[2];
289 cc->dClk /= t_CST::c;
[2208]290 cc->raoSet = true;
291 cc->dClkSet = true;
[2207]292 }
293 else if ( messageType == COTYPE_GPSORBIT ||
294 messageType == COTYPE_GLONASSORBIT ) {
295 cc->rao.ReSize(3);
296 in >> cc->iod >> cc->rao[0] >> cc->rao[1] >> cc->rao[2];
[2208]297 cc->raoSet = true;
[2207]298 }
299 else if ( messageType == COTYPE_GPSCLOCK ||
300 messageType == COTYPE_GLONASSCLOCK ) {
301 int dummyIOD;
302 in >> dummyIOD >> cc->dClk;
303 cc->dClk /= t_CST::c;
[2208]304 cc->dClkSet = true;
[2207]305 }
[2035]306 }
307 }
[2208]308
309 QMutableMapIterator<QString, t_corr*> im(_corr);
310 while (im.hasNext()) {
311 im.next();
312 t_corr* cc = im.value();
313 if (!cc->ready()) {
314 delete cc;
315 im.remove();
316 }
317 }
[2035]318}
319
320// Satellite Position
321////////////////////////////////////////////////////////////////////////////
[2123]322t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
[2041]323 ColumnVector& xc, ColumnVector& vv, bool& corr) {
[2035]324
[2240]325 const bool CORR_REQUIRED = false;
[2067]326 const double MAXAGE = 120.0;
[2041]327
328 corr = false;
329
[2035]330 if (_eph.contains(prn)) {
[2040]331 t_eph* ee = _eph.value(prn);
332 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
333
[2068]334 if (CORR_REQUIRED) {
335 if (_corr.contains(prn)) {
336 t_corr* cc = _corr.value(prn);
337 if (ee->IOD() == cc->iod && (tt - cc->tt) < MAXAGE) {
338 corr = true;
339 applyCorr(cc, xc, vv);
340 return success;
341 }
[2041]342 }
[2068]343 return failure;
[2040]344 }
[2068]345
[2035]346 return success;
347 }
348
349 return failure;
350}
351
352//
353////////////////////////////////////////////////////////////////////////////
[2041]354void bncPPPclient::applyCorr(const t_corr* cc, ColumnVector& xc,
355 ColumnVector& vv) {
[2043]356 ColumnVector dx(3);
357 RSW_to_XYZ(xc.Rows(1,3), vv, cc->rao, dx);
[2041]358
[2107]359 xc[0] -= dx[0];
360 xc[1] -= dx[1];
361 xc[2] -= dx[2];
362 xc[3] -= cc->dClk;
[2068]363
364 // Relativistic Correction
365 // -----------------------
366 xc[3] -= 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c ;
[2041]367}
368
[2049]369// Correct Time of Transmission
370////////////////////////////////////////////////////////////////////////////
[2240]371t_irc bncPPPclient::cmpToT(t_satData* satData) {
[2049]372
[2051]373 double prange = satData->P3;
[2049]374 if (prange == 0.0) {
375 return failure;
376 }
377
378 double clkSat = 0.0;
379 for (int ii = 1; ii <= 10; ii++) {
380
[2123]381 bncTime ToT = _epoData->tt - prange / t_CST::c - clkSat;
[2049]382
383 ColumnVector xc(4);
384 ColumnVector vv(3);
385 bool corr = false;
[2240]386 if (getSatPos(ToT, satData->prn, xc, vv, corr) != success) {
[2049]387 return failure;
388 }
389
390 double clkSatOld = clkSat;
[2050]391 clkSat = xc(4);
[2049]392
393 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
394 satData->xx = xc.Rows(1,3);
395 satData->vv = vv;
[2050]396 satData->clk = clkSat * t_CST::c;
[2049]397 satData->clkCorr = corr;
398 return success;
399 }
400 }
401
402 return failure;
403}
404
[2041]405//
406////////////////////////////////////////////////////////////////////////////
[2035]407void bncPPPclient::processEpoch() {
408
[2053]409 // Data Pre-Processing
410 // -------------------
[2231]411 QMutableMapIterator<QString, t_satData*> iGPS(_epoData->satDataGPS);
412 while (iGPS.hasNext()) {
413 iGPS.next();
414 QString prn = iGPS.key();
415 t_satData* satData = iGPS.value();
[2049]416
[2240]417 if (cmpToT(satData) != success) {
[2049]418 delete satData;
[2231]419 iGPS.remove();
[2049]420 continue;
421 }
[2053]422 }
[2035]423
[2231]424 QMutableMapIterator<QString, t_satData*> iGlo(_epoData->satDataGlo);
425 while (iGlo.hasNext()) {
426 iGlo.next();
427 QString prn = iGlo.key();
428 t_satData* satData = iGlo.value();
429
[2240]430 if (cmpToT(satData) != success) {
[2231]431 delete satData;
432 iGlo.remove();
433 continue;
434 }
435 }
436
[2060]437 // Filter Solution
438 // ---------------
[2143]439 if (_model->update(_epoData) == success) {
[2145]440 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
[2060]441 }
[2035]442}
[2049]443
Note: See TracBrowser for help on using the repository browser.