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

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