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

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