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
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 _epoData->satDataGPS[prn] = satData;
186 }
187 else if (obs->satSys == 'R') {
188 QString prn = QString("R%1").arg(obs->satNum, 2, 10, QChar('0'));
189 _epoData->satDataGlo[prn] = satData;
190 }
191
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)) {
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 }
208 }
209 else {
210 t_ephGPS* ee = new t_ephGPS();
211 ee->set(&gpseph);
212 _eph[prn] = ee;
213 }
214}
215
216//
217////////////////////////////////////////////////////////////////////////////
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////////////////////////////////////////////////////////////////////////////
240void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
241 QMutexLocker locker(&_mutex);
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
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 ||
266 messageType == COTYPE_GLONASSCOMBINED ||
267 messageType == COTYPE_GPSORBIT ||
268 messageType == COTYPE_GPSCLOCK ||
269 messageType == COTYPE_GLONASSORBIT ||
270 messageType == COTYPE_GLONASSCLOCK ) {
271
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 }
280
281 cc->tt.set(GPSweek, GPSweeks);
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;
288 cc->raoSet = true;
289 cc->dClkSet = true;
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];
295 cc->raoSet = true;
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;
302 cc->dClkSet = true;
303 }
304 }
305 }
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 }
316}
317
318// Satellite Position
319////////////////////////////////////////////////////////////////////////////
320t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
321 ColumnVector& xc, ColumnVector& vv, bool& corr) {
322
323 const bool CORR_REQUIRED = true;
324 const double MAXAGE = 120.0;
325
326 corr = false;
327
328 if (_eph.contains(prn)) {
329 t_eph* ee = _eph.value(prn);
330 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
331
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 }
340 }
341 return failure;
342 }
343
344 return success;
345 }
346
347 return failure;
348}
349
350//
351////////////////////////////////////////////////////////////////////////////
352void bncPPPclient::applyCorr(const t_corr* cc, ColumnVector& xc,
353 ColumnVector& vv) {
354 ColumnVector dx(3);
355 RSW_to_XYZ(xc.Rows(1,3), vv, cc->rao, dx);
356
357 xc[0] -= dx[0];
358 xc[1] -= dx[1];
359 xc[2] -= dx[2];
360 xc[3] -= cc->dClk;
361
362 // Relativistic Correction
363 // -----------------------
364 xc[3] -= 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c ;
365}
366
367// Correct Time of Transmission
368////////////////////////////////////////////////////////////////////////////
369t_irc bncPPPclient::cmpToT(const QString& prn, t_satData* satData) {
370
371 double prange = satData->P3;
372 if (prange == 0.0) {
373 return failure;
374 }
375
376 double clkSat = 0.0;
377 for (int ii = 1; ii <= 10; ii++) {
378
379 bncTime ToT = _epoData->tt - prange / t_CST::c - clkSat;
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;
389 clkSat = xc(4);
390
391 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
392 satData->xx = xc.Rows(1,3);
393 satData->vv = vv;
394 satData->clk = clkSat * t_CST::c;
395 satData->clkCorr = corr;
396 return success;
397 }
398 }
399
400 return failure;
401}
402
403//
404////////////////////////////////////////////////////////////////////////////
405void bncPPPclient::processEpoch() {
406
407 // Data Pre-Processing
408 // -------------------
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();
414
415 if (cmpToT(prn, satData) != success) {
416 delete satData;
417 iGPS.remove();
418 continue;
419 }
420 }
421
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
435 // Filter Solution
436 // ---------------
437 if (_model->update(_epoData) == success) {
438 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
439 }
440}
441
Note: See TracBrowser for help on using the repository browser.