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

Last change on this file since 2919 was 2911, checked in by mervart, 14 years ago
File size: 13.7 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#include <sstream>
44
45#include "bncpppclient.h"
46#include "bncapp.h"
47#include "bncutils.h"
48#include "bncconst.h"
49#include "bncmodel.h"
50#include "bncsettings.h"
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 if ( Qt::CheckState(settings.value("pppGalileo").toInt()) == Qt::Checked) {
68 _useGalileo = true;
69 }
70 else {
71 _useGalileo = false;
72 }
73
74 if (settings.value("pppSPP").toString() == "PPP") {
75 _pppMode = true;
76 }
77 else {
78 _pppMode = false;
79 }
80
81 connect(this, SIGNAL(newMessage(QByteArray,bool)),
82 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
83
84 connect(((bncApp*)qApp), SIGNAL(newCorrections(QList<QString>)),
85 this, SLOT(slotNewCorrections(QList<QString>)));
86
87 _staID = staID;
88 _model = new bncModel(staID);
89 connect(_model, SIGNAL(newNMEAstr(QByteArray)),
90 this, SIGNAL(newNMEAstr(QByteArray)));
91}
92
93// Destructor
94////////////////////////////////////////////////////////////////////////////
95bncPPPclient::~bncPPPclient() {
96 delete _model;
97 while (!_epoData.empty()) {
98 delete _epoData.front();
99 _epoData.pop();
100 }
101 QMapIterator<QString, t_corr*> ic(_corr);
102 while (ic.hasNext()) {
103 ic.next();
104 delete ic.value();
105 }
106 QMapIterator<QString, t_bias*> ib(_bias);
107 while (ib.hasNext()) {
108 ib.next();
109 delete ib.value();
110 }
111}
112
113//
114////////////////////////////////////////////////////////////////////////////
115void bncPPPclient::putNewObs(const t_obs& obs) {
116 QMutexLocker locker(&_mutex);
117
118 if (obs.satSys == 'R') {
119 if (!_useGlonass) return;
120 }
121 else if (obs.satSys == 'E') {
122 if (!_useGalileo) return;
123 }
124 else if (obs.satSys != 'G') {
125 return;
126 }
127
128 t_satData* satData = new t_satData();
129 satData->tt = bncTime(obs.GPSWeek, obs.GPSWeeks);
130
131 // Satellite Number
132 // ----------------
133 satData->prn = QString("%1%2").arg(obs.satSys).arg(obs.satNum,2,10,QChar('0'));
134
135 // Check Slips
136 // -----------
137 slipInfo& sInfo = _slips[satData->prn];
138 if ( sInfo.slipCntL1 == obs.slip_cnt_L1 &&
139 sInfo.slipCntL2 == obs.slip_cnt_L2 &&
140 sInfo.slipCntL5 == obs.slip_cnt_L5 ) {
141 satData->slipFlag = false;
142 }
143 else {
144 satData->slipFlag = true;
145 }
146 sInfo.slipCntL1 = obs.slip_cnt_L1;
147 sInfo.slipCntL2 = obs.slip_cnt_L2;
148
149 // Handle Code Biases
150 // ------------------
151 t_bias* bb = 0;
152 if (_bias.contains(satData->prn)) {
153 bb = _bias.value(satData->prn);
154 }
155
156 // Add new epoch, process the older ones
157 // -------------------------------------
158 if (_epoData.size() == 0) {
159 _epoData.push(new t_epoData());
160 _epoData.back()->tt = satData->tt;
161 }
162 else if (satData->tt != _epoData.back()->tt) {
163 processEpochs();
164 _epoData.push(new t_epoData());
165 _epoData.back()->tt = satData->tt;
166 }
167
168 // Set Observations GPS
169 // --------------------
170 if (obs.satSys == 'G') {
171 if ( (obs.P1 || obs.C1) && (obs.P2 || obs.C2) && obs.L1() && obs.L2() ) {
172 double f1 = t_CST::freq1;
173 double f2 = t_CST::freq2;
174 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
175 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
176 if (obs.P1) {
177 satData->P1 = obs.P1 + (bb ? bb->p1 : 0.0);
178 }
179 else {
180 satData->P1 = obs.C1 + (bb ? bb->c1 : 0.0);
181 }
182 if (obs.P2) {
183 satData->P2 = obs.P2 + (bb ? bb->p2 : 0.0);
184 }
185 else {
186 satData->P2 = obs.C2;
187 }
188 satData->L1 = obs.L1() * t_CST::c / f1;
189 satData->L2 = obs.L2() * t_CST::c / f2;
190 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
191 satData->L3 = c1 * satData->L1 + c2 * satData->L2;
192 satData->lambda3 = c1 * t_CST::c / f1 + c2 * t_CST::c / f2;
193
194 _epoData.back()->satDataGPS[satData->prn] = satData;
195 }
196 else {
197 delete satData;
198 }
199 }
200
201 // Set Observations GLONASS
202 // ------------------------
203 else if (obs.satSys == 'R') {
204 if ( (obs.P1 || obs.C1) && (obs.P2 || obs.C2) && obs.L1() && obs.L2() ) {
205 double f1 = 1602000000.0 + 562500.0 * obs.slotNum;
206 double f2 = 1246000000.0 + 437500.0 * obs.slotNum;
207 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
208 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
209 if (obs.P1) {
210 satData->P1 = obs.P1 + (bb ? bb->p1 : 0.0);
211 }
212 else {
213 satData->P1 = obs.C1 + (bb ? bb->c1 : 0.0);
214 }
215 if (obs.P2) {
216 satData->P2 = obs.P2 + (bb ? bb->p2 : 0.0);
217 }
218 else {
219 satData->P2 = obs.C2;
220 }
221 satData->L1 = obs.L1() * t_CST::c / f1;
222 satData->L2 = obs.L2() * t_CST::c / f2;
223 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
224 satData->L3 = c1 * satData->L1 + c2 * satData->L2;
225 satData->lambda3 = c1 * t_CST::c / f1 + c2 * t_CST::c / f2;
226
227 _epoData.back()->satDataGlo[satData->prn] = satData;
228 }
229 else {
230 delete satData;
231 }
232 }
233
234 // Set Observations Galileo
235 // ------------------------
236 else if (obs.satSys == 'E') {
237 if ( obs.C1 && obs.C5 && obs.L1() && obs.L5) {
238 double f1 = t_CST::freq1;
239 double f5 = t_CST::freq5;
240 double c1 = f1 * f1 / (f1 * f1 - f5 * f5);
241 double c5 = - f5 * f5 / (f1 * f1 - f5 * f5);
242
243 satData->P1 = obs.C1;
244 satData->P5 = obs.C5;
245 satData->L1 = obs.L1() * t_CST::c / f1;
246 satData->L5 = obs.L5 * t_CST::c / f5;
247 satData->P3 = c1 * satData->P1 + c5 * satData->P5;
248 satData->L3 = c1 * satData->L1 + c5 * satData->L5;
249 satData->lambda3 = c1 * t_CST::c / f1 + c5 * t_CST::c / f5;
250 _epoData.back()->satDataGal[satData->prn] = satData;
251 }
252 else {
253 delete satData;
254 }
255 }
256}
257
258//
259////////////////////////////////////////////////////////////////////////////
260void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
261 QMutexLocker locker(&_mutex);
262
263 if (corrList.size() == 0) {
264 return;
265 }
266
267 // Remove All Corrections
268 // ----------------------
269 QMapIterator<QString, t_corr*> ic(_corr);
270 while (ic.hasNext()) {
271 ic.next();
272 delete ic.value();
273 }
274 _corr.clear();
275
276 QListIterator<QString> it(corrList);
277 while (it.hasNext()) {
278 QString line = it.next();
279
280 QTextStream in(&line);
281 int messageType;
282 int updateInterval;
283 int GPSweek;
284 double GPSweeks;
285 QString prn;
286 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
287
288 if ( t_corr::relevantMessageType(messageType) ) {
289 t_corr* cc = 0;
290 if (_corr.contains(prn)) {
291 cc = _corr.value(prn);
292 }
293 else {
294 cc = new t_corr();
295 _corr[prn] = cc;
296 }
297
298 cc->readLine(line);
299 _corr_tt = cc->tt;
300 }
301 else if ( messageType == BTYPE_GPS ) {
302
303 t_bias* bb = 0;
304 if (_bias.contains(prn)) {
305 bb = _bias.value(prn);
306 }
307 else {
308 bb = new t_bias();
309 _bias[prn] = bb;
310 }
311
312 bb->tt.set(GPSweek, GPSweeks);
313
314 int numBiases;
315 in >> numBiases;
316 for (int ii = 0; ii < numBiases; ++ii) {
317 int bType;
318 double bValue;
319 in >> bType >> bValue;
320 if (bType == CODETYPEGPS_L1_Z) {
321 bb->p1 = bValue;
322 }
323 else if (bType == CODETYPEGPS_L1_CA) {
324 bb->c1 = bValue;
325 }
326 else if (bType == CODETYPEGPS_L2_Z) {
327 bb->p2 = bValue;
328 }
329 }
330 }
331 }
332
333 QMutableMapIterator<QString, t_corr*> im(_corr);
334 while (im.hasNext()) {
335 im.next();
336 t_corr* cc = im.value();
337 if (!cc->ready()) {
338 delete cc;
339 im.remove();
340 }
341 }
342}
343
344// Satellite Position
345////////////////////////////////////////////////////////////////////////////
346t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
347 ColumnVector& xc, ColumnVector& vv) {
348
349 const double MAXAGE = 120.0;
350
351 if (_eph.contains(prn)) {
352
353 if (_pppMode) {
354 if (_corr.contains(prn)) {
355 t_corr* cc = _corr.value(prn);
356 if (tt - cc->tt < MAXAGE) {
357 t_eph* eLast = _eph.value(prn)->last;
358 t_eph* ePrev = _eph.value(prn)->prev;
359 if (eLast && eLast->IOD() == cc->iod) {
360 eLast->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
361 applyCorr(tt, cc, xc, vv);
362 return success;
363 }
364 else if (ePrev && ePrev->IOD() == cc->iod) {
365 ePrev->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
366 applyCorr(tt, cc, xc, vv);
367 return success;
368 }
369 }
370 }
371 }
372
373 else {
374 t_eph* ee = _eph.value(prn)->last;
375 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
376 return success;
377 }
378 }
379
380 return failure;
381}
382
383//
384////////////////////////////////////////////////////////////////////////////
385void bncPPPclient::applyCorr(const bncTime& tt, const t_corr* cc,
386 ColumnVector& xc, ColumnVector& vv) {
387 ColumnVector dx(3);
388
389 double dt = tt - cc->tt;
390 ColumnVector raoHlp = cc->rao + cc->dotRao * dt + cc->dotDotRao * dt * dt;
391
392 RSW_to_XYZ(xc.Rows(1,3), vv, raoHlp, dx);
393
394 xc[0] -= dx[0];
395 xc[1] -= dx[1];
396 xc[2] -= dx[2];
397 xc[3] += cc->dClk + cc->dotDClk * dt + cc->dotDotDClk * dt * dt;
398}
399
400// Correct Time of Transmission
401////////////////////////////////////////////////////////////////////////////
402t_irc bncPPPclient::cmpToT(t_satData* satData) {
403
404 double prange = satData->P3;
405 if (prange == 0.0) {
406 return failure;
407 }
408
409 double clkSat = 0.0;
410 for (int ii = 1; ii <= 10; ii++) {
411
412 bncTime ToT = satData->tt - prange / t_CST::c - clkSat;
413
414 ColumnVector xc(4);
415 ColumnVector vv(3);
416 if (getSatPos(ToT, satData->prn, xc, vv) != success) {
417 return failure;
418 }
419
420 double clkSatOld = clkSat;
421 clkSat = xc(4);
422
423 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
424 satData->xx = xc.Rows(1,3);
425 satData->vv = vv;
426 satData->clk = clkSat * t_CST::c;
427 return success;
428 }
429 }
430
431 return failure;
432}
433
434//
435////////////////////////////////////////////////////////////////////////////
436void bncPPPclient::processFrontEpoch() {
437
438 // Data Pre-Processing
439 // -------------------
440 QMutableMapIterator<QString, t_satData*> iGPS(_epoData.front()->satDataGPS);
441 while (iGPS.hasNext()) {
442 iGPS.next();
443 QString prn = iGPS.key();
444 t_satData* satData = iGPS.value();
445
446 if (cmpToT(satData) != success) {
447 delete satData;
448 iGPS.remove();
449 continue;
450 }
451 }
452
453 QMutableMapIterator<QString, t_satData*> iGlo(_epoData.front()->satDataGlo);
454 while (iGlo.hasNext()) {
455 iGlo.next();
456 QString prn = iGlo.key();
457 t_satData* satData = iGlo.value();
458
459 if (cmpToT(satData) != success) {
460 delete satData;
461 iGlo.remove();
462 continue;
463 }
464 }
465
466 QMutableMapIterator<QString, t_satData*> iGal(_epoData.front()->satDataGal);
467 while (iGal.hasNext()) {
468 iGal.next();
469 QString prn = iGal.key();
470 t_satData* satData = iGal.value();
471
472 if (cmpToT(satData) != success) {
473 delete satData;
474 iGal.remove();
475 continue;
476 }
477 }
478
479 // Filter Solution
480 // ---------------
481 if (_model->update(_epoData.front()) == success) {
482 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
483 }
484}
485
486//
487////////////////////////////////////////////////////////////////////////////
488void bncPPPclient::processEpochs() {
489
490 // Synchronization threshold (not used in SPP mode)
491 // ------------------------------------------------
492 bncSettings settings;
493 double maxDt = settings.value("pppSync").toDouble();
494 if (!_pppMode) {
495 maxDt = 0.0;
496 }
497
498 // Make sure the buffer does not grow beyond any limit
499 // ---------------------------------------------------
500 const unsigned MAX_EPODATA_SIZE = 120;
501 if (_epoData.size() > MAX_EPODATA_SIZE) {
502 delete _epoData.front();
503 _epoData.pop();
504 }
505
506 // Loop over all unprocessed epochs
507 // --------------------------------
508 while (!_epoData.empty()) {
509
510 t_epoData* frontEpoData = _epoData.front();
511
512 // No corrections yet, skip the epoch
513 // ----------------------------------
514 if (maxDt != 0.0 && !_corr_tt.valid()) {
515 return;
516 }
517
518 // Process the front epoch
519 // -----------------------
520 if (maxDt == 0 || frontEpoData->tt - _corr_tt < maxDt) {
521 processFrontEpoch();
522 delete _epoData.front();
523 _epoData.pop();
524 }
525 else {
526 return;
527 }
528 }
529}
Note: See TracBrowser for help on using the repository browser.