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

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