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

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