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

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