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

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

* empty log message *

File size: 11.2 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'));
[2231]185 _epoData->satDataGPS[prn] = satData;
186 }
187 else if (obs->satSys == 'R') {
[2232]188 QString prn = QString("R%1").arg(obs->satNum, 2, 10, QChar('0'));
[2231]189 _epoData->satDataGlo[prn] = satData;
190 }
[2233]191
[2035]192}
193
194//
195////////////////////////////////////////////////////////////////////////////
196void bncPPPclient::slotNewEphGPS(gpsephemeris gpseph) {
197 QMutexLocker locker(&_mutex);
198
199 QString prn = QString("G%1").arg(gpseph.satellite, 2, 10, QChar('0'));
200
201 if (_eph.contains(prn)) {
[2040]202 t_ephGPS* ee = static_cast<t_ephGPS*>(_eph.value(prn));
203 if ( (ee->GPSweek() < gpseph.GPSweek) ||
204 (ee->GPSweek() == gpseph.GPSweek &&
205 ee->TOC() < gpseph.TOC) ) {
206 ee->set(&gpseph);
207 }
[2035]208 }
209 else {
210 t_ephGPS* ee = new t_ephGPS();
211 ee->set(&gpseph);
212 _eph[prn] = ee;
213 }
214}
215
216//
217////////////////////////////////////////////////////////////////////////////
[2224]218void bncPPPclient::slotNewEphGlonass(glonassephemeris gloeph) {
219 QMutexLocker locker(&_mutex);
220
221 QString prn = QString("R%1").arg(gloeph.almanac_number, 2, 10, QChar('0'));
222
223 if (_eph.contains(prn)) {
224 t_ephGlo* ee = static_cast<t_ephGlo*>(_eph.value(prn));
225 if ( (ee->GPSweek() < gloeph.GPSWeek) ||
226 (ee->GPSweek() == gloeph.GPSWeek &&
227 ee->GPSweeks() < gloeph.GPSTOW) ) {
228 ee->set(&gloeph);
229 }
230 }
231 else {
232 t_ephGlo* ee = new t_ephGlo();
233 ee->set(&gloeph);
234 _eph[prn] = ee;
235 }
236}
237
238//
239////////////////////////////////////////////////////////////////////////////
[2035]240void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
241 QMutexLocker locker(&_mutex);
[2069]242
243 if (corrList.size() == 0) {
244 return;
245 }
246
247 // Remove All Corrections
248 // ----------------------
249 QMapIterator<QString, t_corr*> ic(_corr);
250 while (ic.hasNext()) {
251 ic.next();
252 delete ic.value();
253 }
254 _corr.clear();
255
[2035]256 QListIterator<QString> it(corrList);
257 while (it.hasNext()) {
258 QTextStream in(it.next().toAscii());
259 int messageType;
260 int updateInterval;
261 int GPSweek;
262 double GPSweeks;
263 QString prn;
264 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
265 if ( messageType == COTYPE_GPSCOMBINED ||
[2207]266 messageType == COTYPE_GLONASSCOMBINED ||
267 messageType == COTYPE_GPSORBIT ||
268 messageType == COTYPE_GPSCLOCK ||
269 messageType == COTYPE_GLONASSORBIT ||
270 messageType == COTYPE_GLONASSCLOCK ) {
271
[2035]272 t_corr* cc = 0;
273 if (_corr.contains(prn)) {
274 cc = _corr.value(prn);
275 }
276 else {
277 cc = new t_corr();
278 _corr[prn] = cc;
279 }
[2207]280
[2035]281 cc->tt.set(GPSweek, GPSweeks);
[2207]282
283 if ( messageType == COTYPE_GPSCOMBINED ||
284 messageType == COTYPE_GLONASSCOMBINED ) {
285 cc->rao.ReSize(3);
286 in >> cc->iod >> cc->dClk >> cc->rao[0] >> cc->rao[1] >> cc->rao[2];
287 cc->dClk /= t_CST::c;
[2208]288 cc->raoSet = true;
289 cc->dClkSet = true;
[2207]290 }
291 else if ( messageType == COTYPE_GPSORBIT ||
292 messageType == COTYPE_GLONASSORBIT ) {
293 cc->rao.ReSize(3);
294 in >> cc->iod >> cc->rao[0] >> cc->rao[1] >> cc->rao[2];
[2208]295 cc->raoSet = true;
[2207]296 }
297 else if ( messageType == COTYPE_GPSCLOCK ||
298 messageType == COTYPE_GLONASSCLOCK ) {
299 int dummyIOD;
300 in >> dummyIOD >> cc->dClk;
301 cc->dClk /= t_CST::c;
[2208]302 cc->dClkSet = true;
[2207]303 }
[2035]304 }
305 }
[2208]306
307 QMutableMapIterator<QString, t_corr*> im(_corr);
308 while (im.hasNext()) {
309 im.next();
310 t_corr* cc = im.value();
311 if (!cc->ready()) {
312 delete cc;
313 im.remove();
314 }
315 }
[2035]316}
317
318// Satellite Position
319////////////////////////////////////////////////////////////////////////////
[2123]320t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
[2041]321 ColumnVector& xc, ColumnVector& vv, bool& corr) {
[2035]322
[2067]323 const bool CORR_REQUIRED = true;
324 const double MAXAGE = 120.0;
[2041]325
326 corr = false;
327
[2035]328 if (_eph.contains(prn)) {
[2040]329 t_eph* ee = _eph.value(prn);
330 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
331
[2068]332 if (CORR_REQUIRED) {
333 if (_corr.contains(prn)) {
334 t_corr* cc = _corr.value(prn);
335 if (ee->IOD() == cc->iod && (tt - cc->tt) < MAXAGE) {
336 corr = true;
337 applyCorr(cc, xc, vv);
338 return success;
339 }
[2041]340 }
[2068]341 return failure;
[2040]342 }
[2068]343
[2035]344 return success;
345 }
346
347 return failure;
348}
349
350//
351////////////////////////////////////////////////////////////////////////////
[2041]352void bncPPPclient::applyCorr(const t_corr* cc, ColumnVector& xc,
353 ColumnVector& vv) {
[2043]354 ColumnVector dx(3);
355 RSW_to_XYZ(xc.Rows(1,3), vv, cc->rao, dx);
[2041]356
[2107]357 xc[0] -= dx[0];
358 xc[1] -= dx[1];
359 xc[2] -= dx[2];
360 xc[3] -= cc->dClk;
[2068]361
362 // Relativistic Correction
363 // -----------------------
364 xc[3] -= 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c ;
[2041]365}
366
[2049]367// Correct Time of Transmission
368////////////////////////////////////////////////////////////////////////////
369t_irc bncPPPclient::cmpToT(const QString& prn, t_satData* satData) {
370
[2051]371 double prange = satData->P3;
[2049]372 if (prange == 0.0) {
373 return failure;
374 }
375
376 double clkSat = 0.0;
377 for (int ii = 1; ii <= 10; ii++) {
378
[2123]379 bncTime ToT = _epoData->tt - prange / t_CST::c - clkSat;
[2049]380
381 ColumnVector xc(4);
382 ColumnVector vv(3);
383 bool corr = false;
384 if (getSatPos(ToT, prn, xc, vv, corr) != success) {
385 return failure;
386 }
387
388 double clkSatOld = clkSat;
[2050]389 clkSat = xc(4);
[2049]390
391 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
392 satData->xx = xc.Rows(1,3);
393 satData->vv = vv;
[2050]394 satData->clk = clkSat * t_CST::c;
[2049]395 satData->clkCorr = corr;
396 return success;
397 }
398 }
399
400 return failure;
401}
402
[2041]403//
404////////////////////////////////////////////////////////////////////////////
[2035]405void bncPPPclient::processEpoch() {
406
[2053]407 // Data Pre-Processing
408 // -------------------
[2231]409 QMutableMapIterator<QString, t_satData*> iGPS(_epoData->satDataGPS);
410 while (iGPS.hasNext()) {
411 iGPS.next();
412 QString prn = iGPS.key();
413 t_satData* satData = iGPS.value();
[2049]414
415 if (cmpToT(prn, satData) != success) {
416 delete satData;
[2231]417 iGPS.remove();
[2049]418 continue;
419 }
[2053]420 }
[2035]421
[2231]422 QMutableMapIterator<QString, t_satData*> iGlo(_epoData->satDataGlo);
423 while (iGlo.hasNext()) {
424 iGlo.next();
425 QString prn = iGlo.key();
426 t_satData* satData = iGlo.value();
427
428 if (cmpToT(prn, satData) != success) {
429 delete satData;
430 iGlo.remove();
431 continue;
432 }
433 }
434
[2060]435 // Filter Solution
436 // ---------------
[2143]437 if (_model->update(_epoData) == success) {
[2145]438 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
[2060]439 }
[2035]440}
[2049]441
Note: See TracBrowser for help on using the repository browser.