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

Last change on this file since 3979 was 3755, checked in by mervart, 13 years ago
File size: 12.8 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 QListIterator<QString> it(corrList);
274 while (it.hasNext()) {
275 QString line = it.next();
276
277 QTextStream in(&line);
278 int messageType;
279 int updateInterval;
280 int GPSweek;
281 double GPSweeks;
282 QString prn;
283 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
284
285 if ( t_corr::relevantMessageType(messageType) ) {
286 t_corr* cc = 0;
287 if (_corr.contains(prn)) {
288 cc = _corr.value(prn);
289 }
290 else {
291 cc = new t_corr();
292 _corr[prn] = cc;
293 }
294
295 cc->readLine(line);
296 _corr_tt = cc->tClk;
297 }
298 else if ( messageType == BTYPE_GPS ) {
299
300 t_bias* bb = 0;
301 if (_bias.contains(prn)) {
302 bb = _bias.value(prn);
303 }
304 else {
305 bb = new t_bias();
306 _bias[prn] = bb;
307 }
308
309 bb->tt.set(GPSweek, GPSweeks);
310
311 int numBiases;
312 in >> numBiases;
313 for (int ii = 0; ii < numBiases; ++ii) {
314 int bType;
315 double bValue;
316 in >> bType >> bValue;
317 if (bType == CODETYPEGPS_L1_Z) {
318 bb->p1 = bValue;
319 }
320 else if (bType == CODETYPEGPS_L1_CA) {
321 bb->c1 = bValue;
322 }
323 else if (bType == CODETYPEGPS_L2_Z) {
324 bb->p2 = bValue;
325 }
326 }
327 }
328 }
329}
330
331// Satellite Position
332////////////////////////////////////////////////////////////////////////////
333t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
334 ColumnVector& xc, ColumnVector& vv) {
335
336 const double MAXAGE = 120.0;
337
338 if (_eph.contains(prn)) {
339
340 if (_opt->pppMode) {
341 if (_corr.contains(prn)) {
342 t_corr* cc = _corr.value(prn);
343 if (cc->ready() && tt - cc->tClk < MAXAGE) {
344 t_eph* eLast = _eph.value(prn)->last;
345 t_eph* ePrev = _eph.value(prn)->prev;
346 if (eLast && eLast->IOD() == cc->iod) {
347 eLast->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
348 return applyCorr(tt, cc, xc, vv);
349 }
350 else if (ePrev && ePrev->IOD() == cc->iod) {
351 ePrev->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
352 return applyCorr(tt, cc, xc, vv);
353 }
354 }
355 }
356 }
357
358 else {
359 t_eph* ee = _eph.value(prn)->last;
360 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
361 return success;
362 }
363 }
364
365 return failure;
366}
367
368//
369////////////////////////////////////////////////////////////////////////////
370t_irc bncPPPclient::applyCorr(const bncTime& tt, const t_corr* cc,
371 ColumnVector& xc, ColumnVector& vv) {
372
373 double dtRao = tt - cc->tRao;
374 ColumnVector raoHlp = cc->rao + cc->dotRao * dtRao
375 + cc->dotDotRao * dtRao * dtRao;
376
377 if (raoHlp.norm_Frobenius() > 20.0) {
378 return failure;
379 }
380
381 ColumnVector dx(3);
382 RSW_to_XYZ(xc.Rows(1,3), vv, raoHlp, dx);
383 xc[0] -= dx[0];
384 xc[1] -= dx[1];
385 xc[2] -= dx[2];
386
387 double dtClk = tt - cc->tClk;
388
389 xc[3] += cc->dClk + cc->dotDClk * dtClk + cc->dotDotDClk * dtClk * dtClk
390 + cc->hrClk;
391
392 return success;
393}
394
395// Correct Time of Transmission
396////////////////////////////////////////////////////////////////////////////
397t_irc bncPPPclient::cmpToT(t_satData* satData) {
398
399 double prange = satData->P3;
400 if (prange == 0.0) {
401 return failure;
402 }
403
404 double clkSat = 0.0;
405 for (int ii = 1; ii <= 10; ii++) {
406
407 bncTime ToT = satData->tt - prange / t_CST::c - clkSat;
408
409 ColumnVector xc(4);
410 ColumnVector vv(3);
411 if (getSatPos(ToT, satData->prn, xc, vv) != success) {
412 return failure;
413 }
414
415 double clkSatOld = clkSat;
416 clkSat = xc(4);
417
418 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
419 satData->xx = xc.Rows(1,3);
420 satData->vv = vv;
421 satData->clk = clkSat * t_CST::c;
422 return success;
423 }
424 }
425
426 return failure;
427}
428
429//
430////////////////////////////////////////////////////////////////////////////
431void bncPPPclient::processFrontEpoch() {
432
433 // Data Pre-Processing
434 // -------------------
435 QMutableMapIterator<QString, t_satData*> it(_epoData.front()->satData);
436 while (it.hasNext()) {
437 it.next();
438 QString prn = it.key();
439 t_satData* satData = it.value();
440
441 if (cmpToT(satData) != success) {
442 delete satData;
443 it.remove();
444 continue;
445 }
446 }
447
448 // Filter Solution
449 // ---------------
450 if (_model->update(_epoData.front()) == success) {
451 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
452 }
453}
454
455//
456////////////////////////////////////////////////////////////////////////////
457void bncPPPclient::processEpochs() {
458
459 // Make sure the buffer does not grow beyond any limit
460 // ---------------------------------------------------
461 const unsigned MAX_EPODATA_SIZE = 120;
462 if (_epoData.size() > MAX_EPODATA_SIZE) {
463 delete _epoData.front();
464 _epoData.pop();
465 }
466
467 // Loop over all unprocessed epochs
468 // --------------------------------
469 while (!_epoData.empty()) {
470
471 t_epoData* frontEpoData = _epoData.front();
472
473 // No corrections yet, skip the epoch
474 // ----------------------------------
475 if (_opt->corrSync != 0.0 && !_corr_tt.valid()) {
476 return;
477 }
478
479 // Process the front epoch
480 // -----------------------
481 if (_opt->corrSync == 0 || frontEpoData->tt - _corr_tt < _opt->corrSync) {
482 processFrontEpoch();
483 delete _epoData.front();
484 _epoData.pop();
485 }
486 else {
487 return;
488 }
489 }
490}
Note: See TracBrowser for help on using the repository browser.