source: ntrip/trunk/BNC/src/bncpppclient.cpp@ 4784

Last change on this file since 4784 was 4784, checked in by mervart, 12 years ago
File size: 13.6 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 and Glonass
160 // --------------------------------
161 if (obs.satSys == 'G' || obs.satSys == 'R') {
162 const QByteArray preferedTypes("WPC");
163 for (int ii = preferedTypes.length()-1; ii >= 0; ii--) {
164 double p1 = obs.measdata("C1" + preferedTypes[ii], 3.0);
165 if (p1 != 0.0) {
166 satData->P1 = p1;
167 if (bb) {
168 satData->P1 += bb->value("1" + preferedTypes[ii]);
169 }
170 }
171 double p2 = obs.measdata("C2" + preferedTypes[ii], 3.0);
172 if (p2 != 0.0) {
173 satData->P2 = p2;
174 if (bb) {
175 satData->P1 += bb->value("2" + preferedTypes[ii]);
176 }
177 }
178 double l1 = obs.measdata("L1" + preferedTypes[ii], 3.0);
179 if (l1 != 0.0) {
180 satData->L1 = l1;
181 }
182 double l2 = obs.measdata("L2" + preferedTypes[ii], 3.0);
183 if (l2 != 0.0) {
184 satData->L2 = l2;
185 }
186 }
187 if (satData->P1 != 0.0 && satData->P2 != 0.0 &&
188 satData->L1 != 0.0 && satData->L2 != 0.0 ) {
189 double f1 = t_CST::f1(obs.satSys, obs.slotNum);
190 double f2 = t_CST::f2(obs.satSys, obs.slotNum);
191 double a1 = f1 * f1 / (f1 * f1 - f2 * f2);
192 double a2 = - f2 * f2 / (f1 * f1 - f2 * f2);
193 satData->L1 = satData->L1 * t_CST::c / f1;
194 satData->L2 = satData->L2 * t_CST::c / f2;
195 satData->P3 = a1 * satData->P1 + a2 * satData->P2;
196 satData->L3 = a1 * satData->L1 + a2 * satData->L2;
197 satData->lambda3 = a1 * t_CST::c / f1 + a2 * t_CST::c / f2;
198 _epoData.back()->satData[satData->prn] = satData;
199 }
200 else {
201 delete satData;
202 }
203 }
204
205 // Set Observations Galileo
206 // ------------------------
207 else if (obs.satSys == 'E') {
208 satData->P1 = obs.measdata("C1", 3.0);
209 satData->L1 = obs.measdata("L1", 3.0);
210 satData->P5 = obs.measdata("C5", 3.0);
211 satData->L5 = obs.measdata("L5", 3.0);
212 if (satData->P1 != 0.0 && satData->P5 != 0.0 &&
213 satData->L1 != 0.0 && satData->L5 != 0.0 ) {
214 double f1 = t_CST::freq1;
215 double f5 = t_CST::freq5;
216 double a1 = f1 * f1 / (f1 * f1 - f5 * f5);
217 double a5 = - f5 * f5 / (f1 * f1 - f5 * f5);
218 satData->L1 = satData->L1 * t_CST::c / f1;
219 satData->L5 = satData->L5 * t_CST::c / f5;
220 satData->P3 = a1 * satData->P1 + a5 * satData->P5;
221 satData->L3 = a1 * satData->L1 + a5 * satData->L5;
222 satData->lambda3 = a1 * t_CST::c / f1 + a5 * t_CST::c / f5;
223 _epoData.back()->satData[satData->prn] = satData;
224 }
225 else {
226 delete satData;
227 }
228 }
229}
230
231//
232////////////////////////////////////////////////////////////////////////////
233void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
234 QMutexLocker locker(&_mutex);
235
236 // Check the Mountpoint (source of corrections)
237 // --------------------------------------------
238 if (!_opt->pppCorrMount.isEmpty()) {
239 QMutableListIterator<QString> itm(corrList);
240 while (itm.hasNext()) {
241 QStringList hlp = itm.next().split(" ");
242 if (hlp.size() > 0) {
243 QString mountpoint = hlp[hlp.size()-1];
244 if (mountpoint != _opt->pppCorrMount) {
245 itm.remove();
246 }
247 }
248 }
249 }
250
251 if (corrList.size() == 0) {
252 return;
253 }
254
255 QListIterator<QString> it(corrList);
256 while (it.hasNext()) {
257 QString line = it.next();
258
259 QTextStream in(&line);
260 int messageType;
261 int updateInterval;
262 int GPSweek;
263 double GPSweeks;
264 QString prn;
265 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
266
267 if ( t_corr::relevantMessageType(messageType) ) {
268 t_corr* cc = 0;
269 if (_corr.contains(prn)) {
270 cc = _corr.value(prn);
271 }
272 else {
273 cc = new t_corr();
274 _corr[prn] = cc;
275 }
276 cc->readLine(line);
277 _corr_tt = cc->tClk;
278 }
279 else if ( messageType == BTYPE_GPS || messageType == BTYPE_GLONASS ) {
280 t_bias* bb = 0;
281 if (_bias.contains(prn)) {
282 bb = _bias.value(prn);
283 }
284 else {
285 bb = new t_bias();
286 _bias[prn] = bb;
287 }
288 bb->readLine(line);
289 }
290 }
291}
292
293// Satellite Position
294////////////////////////////////////////////////////////////////////////////
295t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
296 ColumnVector& xc, ColumnVector& vv) {
297
298 const double MAXAGE = 120.0;
299
300 if (_eph.contains(prn)) {
301
302 if (_opt->pppMode) {
303 if (_corr.contains(prn)) {
304 t_corr* cc = _corr.value(prn);
305 if (cc->ready() && tt - cc->tClk < MAXAGE) {
306 t_eph* eLast = _eph.value(prn)->last;
307 t_eph* ePrev = _eph.value(prn)->prev;
308 if (eLast && eLast->IOD() == cc->iod) {
309 eLast->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
310 return applyCorr(tt, cc, xc, vv);
311 }
312 else if (ePrev && ePrev->IOD() == cc->iod) {
313 ePrev->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
314 return applyCorr(tt, cc, xc, vv);
315 }
316 }
317 }
318 }
319
320 else {
321 t_eph* ee = _eph.value(prn)->last;
322 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
323 return success;
324 }
325 }
326
327 return failure;
328}
329
330//
331////////////////////////////////////////////////////////////////////////////
332t_irc bncPPPclient::applyCorr(const bncTime& tt, const t_corr* cc,
333 ColumnVector& xc, ColumnVector& vv) {
334
335 double dtRao = tt - cc->tRao;
336 ColumnVector raoHlp = cc->rao + cc->dotRao * dtRao
337 + 0.5 * cc->dotDotRao * dtRao * dtRao;
338
339 if (raoHlp.norm_Frobenius() > 20.0) {
340 return failure;
341 }
342
343 ColumnVector dx(3);
344 RSW_to_XYZ(xc.Rows(1,3), vv, raoHlp, dx);
345 xc[0] -= dx[0];
346 xc[1] -= dx[1];
347 xc[2] -= dx[2];
348
349 double dtClk = tt - cc->tClk;
350
351 xc[3] += cc->dClk + cc->dotDClk * dtClk + cc->dotDotDClk * dtClk * dtClk
352 + cc->hrClk;
353
354 return success;
355}
356
357// Correct Time of Transmission
358////////////////////////////////////////////////////////////////////////////
359t_irc bncPPPclient::cmpToT(t_satData* satData) {
360
361 double prange = satData->P3;
362 if (prange == 0.0) {
363 return failure;
364 }
365
366 double clkSat = 0.0;
367 for (int ii = 1; ii <= 10; ii++) {
368
369 bncTime ToT = satData->tt - prange / t_CST::c - clkSat;
370
371 ColumnVector xc(4);
372 ColumnVector vv(3);
373 if (getSatPos(ToT, satData->prn, xc, vv) != success) {
374 return failure;
375 }
376
377 double clkSatOld = clkSat;
378 clkSat = xc(4);
379
380 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
381 satData->xx = xc.Rows(1,3);
382 satData->vv = vv;
383 satData->clk = clkSat * t_CST::c;
384 return success;
385 }
386 }
387
388 return failure;
389}
390
391//
392////////////////////////////////////////////////////////////////////////////
393void bncPPPclient::processFrontEpoch() {
394
395#ifdef BNC_DEBUG
396 QString msg = "List of Corrections\n";
397 QMapIterator<QString, t_corr*> itC(_corr);
398 while (itC.hasNext()) {
399 itC.next();
400 QString src = itC.key();
401 const t_corr* corr = itC.value();
402 msg += QString("%1 %2 %3 %4\n")
403 .arg(corr->prn)
404 .arg(corr->iod)
405 .arg(QString(corr->tClk.datestr().c_str()) + "_" + QString(corr->tClk.timestr().c_str()))
406 .arg(QString(corr->tRao.datestr().c_str()) + "_" + QString(corr->tRao.timestr().c_str()));
407 }
408
409 msg += "List of Ephemeris\n";
410 QMapIterator<QString, t_ephPair*> itE(_eph);
411 while (itE.hasNext()) {
412 itE.next();
413 QString prn = itE.key();
414 const t_ephPair* ephPair = itE.value();
415 if (ephPair->prev) {
416 msg += QString("%1 %2 %3 %4 %5\n")
417 .arg(prn)
418 .arg(ephPair->last->IOD())
419 .arg(QString(ephPair->last->TOC().datestr().c_str()) + "_" +
420 QString(ephPair->last->TOC().timestr().c_str()))
421 .arg(ephPair->prev->IOD())
422 .arg(QString(ephPair->prev->TOC().datestr().c_str()) + "_" +
423 QString(ephPair->prev->TOC().timestr().c_str()));
424 }
425 else {
426 msg += QString("%1 %2 %3\n")
427 .arg(prn)
428 .arg(ephPair->last->IOD())
429 .arg(QString(ephPair->last->TOC().datestr().c_str()) + "_" +
430 QString(ephPair->last->TOC().timestr().c_str()));
431 }
432 }
433
434 emit newMessage(msg.toAscii(), false);
435#endif // BNC_DEBUG
436
437 // Data Pre-Processing
438 // -------------------
439 QMutableMapIterator<QString, t_satData*> it(_epoData.front()->satData);
440 while (it.hasNext()) {
441 it.next();
442 QString prn = it.key();
443 t_satData* satData = it.value();
444
445 if (cmpToT(satData) != success) {
446 delete satData;
447 it.remove();
448 continue;
449 }
450 }
451
452 // Filter Solution
453 // ---------------
454 if (_model->update(_epoData.front()) == success) {
455 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
456 }
457}
458
459//
460////////////////////////////////////////////////////////////////////////////
461void bncPPPclient::processEpochs() {
462
463 // Make sure the buffer does not grow beyond any limit
464 // ---------------------------------------------------
465 const unsigned MAX_EPODATA_SIZE = 120;
466 if (_epoData.size() > MAX_EPODATA_SIZE) {
467 delete _epoData.front();
468 _epoData.pop();
469 }
470
471 // Loop over all unprocessed epochs
472 // --------------------------------
473 while (!_epoData.empty()) {
474
475 t_epoData* frontEpoData = _epoData.front();
476
477 // No corrections yet, skip the epoch
478 // ----------------------------------
479 if (_opt->corrSync != 0.0 && !_corr_tt.valid()) {
480 return;
481 }
482
483 // Process the front epoch
484 // -----------------------
485 if (_opt->corrSync == 0 || frontEpoData->tt - _corr_tt < _opt->corrSync) {
486 processFrontEpoch();
487 delete _epoData.front();
488 _epoData.pop();
489 }
490 else {
491 return;
492 }
493 }
494}
Note: See TracBrowser for help on using the repository browser.