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

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

* empty log message *

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