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
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 <iomanip>
42
43#include "bncpppclient.h"
44#include "bncapp.h"
45#include "bncutils.h"
46#include "bncconst.h"
47#include "bncmodel.h"
48#include "bncsettings.h"
49
50extern "C" {
51#include "clock_orbit_rtcm.h"
52}
53
54using namespace std;
55
56// Constructor
57////////////////////////////////////////////////////////////////////////////
58bncPPPclient::bncPPPclient(QByteArray staID) {
59
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
69 connect(((bncApp*)qApp), SIGNAL(newEphGPS(gpsephemeris)),
70 this, SLOT(slotNewEphGPS(gpsephemeris)));
71 connect(((bncApp*)qApp), SIGNAL(newEphGlonass(glonassephemeris)),
72 this, SLOT(slotNewEphGlonass(glonassephemeris)));
73 connect(((bncApp*)qApp), SIGNAL(newCorrections(QList<QString>)),
74 this, SLOT(slotNewCorrections(QList<QString>)));
75
76 _staID = staID;
77 _epoData = 0;
78 _model = new bncModel(staID);
79 connect(_model, SIGNAL(newNMEAstr(QByteArray)),
80 this, SIGNAL(newNMEAstr(QByteArray)));
81}
82
83// Destructor
84////////////////////////////////////////////////////////////////////////////
85bncPPPclient::~bncPPPclient() {
86 delete _model;
87 delete _epoData;
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) {
103 QMutexLocker locker(&_mutex);
104
105 t_obsInternal* obs = &(pp->_o);
106
107 if (obs->satSys != 'G' && !_useGlonass) {
108 return;
109 }
110
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
141 double f1 = t_CST::freq1;
142 double f2 = t_CST::freq2;
143
144 if (obs->satSys == 'R') {
145 f1 = 1602000000.0 + 562500.0 * obs->slot;
146 f2 = 1246000000.0 + 437500.0 * obs->slot;
147 }
148
149 // Ionosphere-Free Combination
150 // ---------------------------
151 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
152 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
153
154 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
155
156 // Set Phase Observations
157 // ----------------------
158 if (obs->L1 && obs->L2) {
159 satData->L1 = obs->L1 * t_CST::c / f1;
160 satData->L2 = obs->L2 * t_CST::c / f2;
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 // ------------------------------
170 bncTime tt(obs->GPSWeek, obs->GPSWeeks);
171
172 if (!_epoData) {
173 _epoData = new t_epoData();
174 _epoData->tt = tt;
175 }
176 else if (tt != _epoData->tt) {
177 processEpoch();
178 delete _epoData;
179 _epoData = new t_epoData();
180 _epoData->tt = tt;
181 }
182
183 if (obs->satSys == 'G') {
184 QString prn = QString("G%1").arg(obs->satNum, 2, 10, QChar('0'));
185 satData->prn = prn;
186 _epoData->satDataGPS[prn] = satData;
187 }
188 else if (obs->satSys == 'R') {
189 QString prn = QString("R%1").arg(obs->satNum, 2, 10, QChar('0'));
190 satData->prn = prn;
191 _epoData->satDataGlo[prn] = satData;
192 }
193
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)) {
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 }
210 }
211 else {
212 t_ephGPS* ee = new t_ephGPS();
213 ee->set(&gpseph);
214 _eph[prn] = ee;
215 }
216}
217
218//
219////////////////////////////////////////////////////////////////////////////
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////////////////////////////////////////////////////////////////////////////
242void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
243 QMutexLocker locker(&_mutex);
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
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 ||
268 messageType == COTYPE_GLONASSCOMBINED ||
269 messageType == COTYPE_GPSORBIT ||
270 messageType == COTYPE_GPSCLOCK ||
271 messageType == COTYPE_GLONASSORBIT ||
272 messageType == COTYPE_GLONASSCLOCK ) {
273
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 }
282
283 cc->tt.set(GPSweek, GPSweeks);
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;
290 cc->raoSet = true;
291 cc->dClkSet = true;
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];
297 cc->raoSet = true;
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;
304 cc->dClkSet = true;
305 }
306 }
307 }
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 }
318}
319
320// Satellite Position
321////////////////////////////////////////////////////////////////////////////
322t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
323 ColumnVector& xc, ColumnVector& vv, bool& corr) {
324
325 const bool CORR_REQUIRED = false;
326 const double MAXAGE = 120.0;
327
328 corr = false;
329
330 if (_eph.contains(prn)) {
331 t_eph* ee = _eph.value(prn);
332 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
333
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 }
342 }
343 return failure;
344 }
345
346 return success;
347 }
348
349 return failure;
350}
351
352//
353////////////////////////////////////////////////////////////////////////////
354void bncPPPclient::applyCorr(const t_corr* cc, ColumnVector& xc,
355 ColumnVector& vv) {
356 ColumnVector dx(3);
357 RSW_to_XYZ(xc.Rows(1,3), vv, cc->rao, dx);
358
359 xc[0] -= dx[0];
360 xc[1] -= dx[1];
361 xc[2] -= dx[2];
362 xc[3] -= cc->dClk;
363
364 // Relativistic Correction
365 // -----------------------
366 xc[3] -= 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c ;
367}
368
369// Correct Time of Transmission
370////////////////////////////////////////////////////////////////////////////
371t_irc bncPPPclient::cmpToT(t_satData* satData) {
372
373 double prange = satData->P3;
374 if (prange == 0.0) {
375 return failure;
376 }
377
378 double clkSat = 0.0;
379 for (int ii = 1; ii <= 10; ii++) {
380
381 bncTime ToT = _epoData->tt - prange / t_CST::c - clkSat;
382
383 ColumnVector xc(4);
384 ColumnVector vv(3);
385 bool corr = false;
386 if (getSatPos(ToT, satData->prn, xc, vv, corr) != success) {
387 return failure;
388 }
389
390 double clkSatOld = clkSat;
391 clkSat = xc(4);
392
393 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
394 satData->xx = xc.Rows(1,3);
395 satData->vv = vv;
396 satData->clk = clkSat * t_CST::c;
397 satData->clkCorr = corr;
398 return success;
399 }
400 }
401
402 return failure;
403}
404
405//
406////////////////////////////////////////////////////////////////////////////
407void bncPPPclient::processEpoch() {
408
409 // Data Pre-Processing
410 // -------------------
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();
416
417 if (cmpToT(satData) != success) {
418 delete satData;
419 iGPS.remove();
420 continue;
421 }
422 }
423
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
430 if (cmpToT(satData) != success) {
431 delete satData;
432 iGlo.remove();
433 continue;
434 }
435 }
436
437 // Filter Solution
438 // ---------------
439 if (_model->update(_epoData) == success) {
440 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
441 }
442}
443
Note: See TracBrowser for help on using the repository browser.