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 |
|
---|
44 | #include "bncpppclient.h"
|
---|
45 | #include "bncapp.h"
|
---|
46 | #include "bncutils.h"
|
---|
47 | #include "bncconst.h"
|
---|
48 | #include "bncmodel.h"
|
---|
49 | #include "bncsettings.h"
|
---|
50 |
|
---|
51 | extern "C" {
|
---|
52 | #include "clock_orbit_rtcm.h"
|
---|
53 | }
|
---|
54 |
|
---|
55 | using namespace std;
|
---|
56 |
|
---|
57 | // Constructor
|
---|
58 | ////////////////////////////////////////////////////////////////////////////
|
---|
59 | bncPPPclient::bncPPPclient(QByteArray staID) {
|
---|
60 |
|
---|
61 | bncSettings settings;
|
---|
62 |
|
---|
63 | if ( Qt::CheckState(settings.value("pppGLONASS").toInt()) == Qt::Checked) {
|
---|
64 | _useGlonass = true;
|
---|
65 | }
|
---|
66 | else {
|
---|
67 | _useGlonass = false;
|
---|
68 | }
|
---|
69 |
|
---|
70 | connect(((bncApp*)qApp), SIGNAL(newEphGPS(gpsephemeris)),
|
---|
71 | this, SLOT(slotNewEphGPS(gpsephemeris)));
|
---|
72 | connect(((bncApp*)qApp), SIGNAL(newEphGlonass(glonassephemeris)),
|
---|
73 | this, SLOT(slotNewEphGlonass(glonassephemeris)));
|
---|
74 | connect(((bncApp*)qApp), SIGNAL(newCorrections(QList<QString>)),
|
---|
75 | this, SLOT(slotNewCorrections(QList<QString>)));
|
---|
76 |
|
---|
77 | _staID = staID;
|
---|
78 | _epoData = 0;
|
---|
79 | _model = new bncModel(staID);
|
---|
80 | connect(_model, SIGNAL(newNMEAstr(QByteArray)),
|
---|
81 | this, SIGNAL(newNMEAstr(QByteArray)));
|
---|
82 | }
|
---|
83 |
|
---|
84 | // Destructor
|
---|
85 | ////////////////////////////////////////////////////////////////////////////
|
---|
86 | bncPPPclient::~bncPPPclient() {
|
---|
87 | delete _model;
|
---|
88 | delete _epoData;
|
---|
89 | QMapIterator<QString, t_eph*> it(_eph);
|
---|
90 | while (it.hasNext()) {
|
---|
91 | it.next();
|
---|
92 | delete it.value();
|
---|
93 | }
|
---|
94 | QMapIterator<QString, t_corr*> ic(_corr);
|
---|
95 | while (ic.hasNext()) {
|
---|
96 | ic.next();
|
---|
97 | delete ic.value();
|
---|
98 | }
|
---|
99 | QMapIterator<QString, t_bias*> ib(_bias);
|
---|
100 | while (ib.hasNext()) {
|
---|
101 | ib.next();
|
---|
102 | delete ib.value();
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | //
|
---|
107 | ////////////////////////////////////////////////////////////////////////////
|
---|
108 | void bncPPPclient::putNewObs(p_obs pp) {
|
---|
109 | QMutexLocker locker(&_mutex);
|
---|
110 |
|
---|
111 | t_obsInternal* obs = &(pp->_o);
|
---|
112 |
|
---|
113 | if (obs->satSys != 'G' && !_useGlonass) {
|
---|
114 | return;
|
---|
115 | }
|
---|
116 |
|
---|
117 | t_satData* satData = new t_satData();
|
---|
118 |
|
---|
119 | // Satellite Number
|
---|
120 | // ----------------
|
---|
121 | if (obs->satSys == 'G') {
|
---|
122 | QString prn = QString("G%1").arg(obs->satNum, 2, 10, QChar('0'));
|
---|
123 | satData->prn = prn;
|
---|
124 | }
|
---|
125 | else if (obs->satSys == 'R') {
|
---|
126 | QString prn = QString("R%1").arg(obs->satNum, 2, 10, QChar('0'));
|
---|
127 | satData->prn = prn;
|
---|
128 | }
|
---|
129 |
|
---|
130 | // Handle Code Biases
|
---|
131 | // ------------------
|
---|
132 | t_bias* bb = 0;
|
---|
133 | if (_bias.contains(satData->prn)) {
|
---|
134 | bb = _bias.value(satData->prn);
|
---|
135 | }
|
---|
136 |
|
---|
137 | // Set Code Observations
|
---|
138 | // ---------------------
|
---|
139 | if (obs->P1) {
|
---|
140 | satData->P1 = obs->P1;
|
---|
141 | satData->codeTypeF1 = t_satData::P_CODE;
|
---|
142 | }
|
---|
143 | else if (obs->C1) {
|
---|
144 | satData->P1 = obs->C1 + (bb ? bb->p1c1 : 0.0);
|
---|
145 | satData->codeTypeF1 = t_satData::C_CODE;
|
---|
146 | }
|
---|
147 | else {
|
---|
148 | delete satData;
|
---|
149 | return;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (obs->P2) {
|
---|
153 | satData->P2 = obs->P2 + (bb ? bb->p1p2 : 0.0);
|
---|
154 | satData->codeTypeF2 = t_satData::P_CODE;
|
---|
155 | }
|
---|
156 | else if (obs->C2) {
|
---|
157 | satData->P2 = obs->C2;
|
---|
158 | satData->codeTypeF2 = t_satData::C_CODE;
|
---|
159 | }
|
---|
160 | else {
|
---|
161 | delete satData;
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | double f1 = t_CST::freq1;
|
---|
166 | double f2 = t_CST::freq2;
|
---|
167 |
|
---|
168 | if (obs->satSys == 'R') {
|
---|
169 | f1 = 1602000000.0 + 562500.0 * obs->slot;
|
---|
170 | f2 = 1246000000.0 + 437500.0 * obs->slot;
|
---|
171 | }
|
---|
172 |
|
---|
173 | // Ionosphere-Free Combination
|
---|
174 | // ---------------------------
|
---|
175 | double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
|
---|
176 | double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
|
---|
177 |
|
---|
178 | satData->P3 = c1 * satData->P1 + c2 * satData->P2;
|
---|
179 |
|
---|
180 | // Set Phase Observations
|
---|
181 | // ----------------------
|
---|
182 | if (obs->L1 && obs->L2) {
|
---|
183 | satData->L1 = obs->L1 * t_CST::c / f1;
|
---|
184 | satData->L2 = obs->L2 * t_CST::c / f2;
|
---|
185 | }
|
---|
186 | else {
|
---|
187 | delete satData;
|
---|
188 | return;
|
---|
189 | }
|
---|
190 | satData->L3 = c1 * satData->L1 + c2 * satData->L2;
|
---|
191 |
|
---|
192 | // Add new Satellite to the epoch
|
---|
193 | // ------------------------------
|
---|
194 | bncTime tt(obs->GPSWeek, obs->GPSWeeks);
|
---|
195 |
|
---|
196 | if (!_epoData) {
|
---|
197 | _epoData = new t_epoData();
|
---|
198 | _epoData->tt = tt;
|
---|
199 | }
|
---|
200 | else if (tt != _epoData->tt) {
|
---|
201 | processEpoch();
|
---|
202 | delete _epoData;
|
---|
203 | _epoData = new t_epoData();
|
---|
204 | _epoData->tt = tt;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (obs->satSys == 'G') {
|
---|
208 | _epoData->satDataGPS[satData->prn] = satData;
|
---|
209 | }
|
---|
210 | else if (obs->satSys == 'R') {
|
---|
211 | _epoData->satDataGlo[satData->prn] = satData;
|
---|
212 | }
|
---|
213 |
|
---|
214 | }
|
---|
215 |
|
---|
216 | //
|
---|
217 | ////////////////////////////////////////////////////////////////////////////
|
---|
218 | void bncPPPclient::slotNewEphGPS(gpsephemeris gpseph) {
|
---|
219 | QMutexLocker locker(&_mutex);
|
---|
220 |
|
---|
221 | QString prn = QString("G%1").arg(gpseph.satellite, 2, 10, QChar('0'));
|
---|
222 |
|
---|
223 | if (_eph.contains(prn)) {
|
---|
224 | t_ephGPS* ee = static_cast<t_ephGPS*>(_eph.value(prn));
|
---|
225 | if ( (ee->GPSweek() < gpseph.GPSweek) ||
|
---|
226 | (ee->GPSweek() == gpseph.GPSweek &&
|
---|
227 | ee->TOC() < gpseph.TOC) ) {
|
---|
228 | ee->set(&gpseph);
|
---|
229 | }
|
---|
230 | }
|
---|
231 | else {
|
---|
232 | t_ephGPS* ee = new t_ephGPS();
|
---|
233 | ee->set(&gpseph);
|
---|
234 | _eph[prn] = ee;
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | //
|
---|
239 | ////////////////////////////////////////////////////////////////////////////
|
---|
240 | void bncPPPclient::slotNewEphGlonass(glonassephemeris gloeph) {
|
---|
241 | QMutexLocker locker(&_mutex);
|
---|
242 |
|
---|
243 | QString prn = QString("R%1").arg(gloeph.almanac_number, 2, 10, QChar('0'));
|
---|
244 |
|
---|
245 | if (_eph.contains(prn)) {
|
---|
246 | int ww = gloeph.GPSWeek;
|
---|
247 | int tow = gloeph.GPSTOW;
|
---|
248 | updatetime(&ww, &tow, gloeph.tb*1000, 0); // Moscow -> GPS
|
---|
249 | t_ephGlo* ee = static_cast<t_ephGlo*>(_eph.value(prn));
|
---|
250 | if (ee->GPSweek() < ww ||
|
---|
251 | (ee->GPSweek() == ww && ee->GPSweeks() < tow)) {
|
---|
252 | ee->set(&gloeph);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | else {
|
---|
256 | t_ephGlo* ee = new t_ephGlo();
|
---|
257 | ee->set(&gloeph);
|
---|
258 | _eph[prn] = ee;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | //
|
---|
263 | ////////////////////////////////////////////////////////////////////////////
|
---|
264 | void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
|
---|
265 | QMutexLocker locker(&_mutex);
|
---|
266 |
|
---|
267 | if (corrList.size() == 0) {
|
---|
268 | return;
|
---|
269 | }
|
---|
270 |
|
---|
271 | // Remove All Corrections
|
---|
272 | // ----------------------
|
---|
273 | QMapIterator<QString, t_corr*> ic(_corr);
|
---|
274 | while (ic.hasNext()) {
|
---|
275 | ic.next();
|
---|
276 | delete ic.value();
|
---|
277 | }
|
---|
278 | _corr.clear();
|
---|
279 |
|
---|
280 | QListIterator<QString> it(corrList);
|
---|
281 | while (it.hasNext()) {
|
---|
282 | QTextStream in(it.next().toAscii());
|
---|
283 | int messageType;
|
---|
284 | int updateInterval;
|
---|
285 | int GPSweek;
|
---|
286 | double GPSweeks;
|
---|
287 | QString prn;
|
---|
288 | in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
|
---|
289 | if ( messageType == COTYPE_GPSCOMBINED ||
|
---|
290 | messageType == COTYPE_GLONASSCOMBINED ||
|
---|
291 | messageType == COTYPE_GPSORBIT ||
|
---|
292 | messageType == COTYPE_GPSCLOCK ||
|
---|
293 | messageType == COTYPE_GLONASSORBIT ||
|
---|
294 | messageType == COTYPE_GLONASSCLOCK ) {
|
---|
295 |
|
---|
296 | t_corr* cc = 0;
|
---|
297 | if (_corr.contains(prn)) {
|
---|
298 | cc = _corr.value(prn);
|
---|
299 | }
|
---|
300 | else {
|
---|
301 | cc = new t_corr();
|
---|
302 | _corr[prn] = cc;
|
---|
303 | }
|
---|
304 |
|
---|
305 | cc->tt.set(GPSweek, GPSweeks);
|
---|
306 |
|
---|
307 | if ( messageType == COTYPE_GPSCOMBINED ||
|
---|
308 | messageType == COTYPE_GLONASSCOMBINED ) {
|
---|
309 | cc->rao.ReSize(3);
|
---|
310 | cc->dotRao.ReSize(3);
|
---|
311 | cc->dotRao = 0.0;
|
---|
312 | double dummyDotClk;
|
---|
313 | in >> cc->iod >> cc->dClk >> cc->rao[0] >> cc->rao[1] >> cc->rao[2]
|
---|
314 | >> dummyDotClk >> cc->dotRao[0] >> cc->dotRao[1] >> cc->dotRao[2];
|
---|
315 | cc->dClk /= t_CST::c;
|
---|
316 | cc->raoSet = true;
|
---|
317 | cc->dClkSet = true;
|
---|
318 | }
|
---|
319 | else if ( messageType == COTYPE_GPSORBIT ||
|
---|
320 | messageType == COTYPE_GLONASSORBIT ) {
|
---|
321 | cc->rao.ReSize(3);
|
---|
322 | in >> cc->iod >> cc->rao[0] >> cc->rao[1] >> cc->rao[2]
|
---|
323 | >> cc->dotRao[0] >> cc->dotRao[1] >> cc->dotRao[2];
|
---|
324 | cc->raoSet = true;
|
---|
325 | }
|
---|
326 | else if ( messageType == COTYPE_GPSCLOCK ||
|
---|
327 | messageType == COTYPE_GLONASSCLOCK ) {
|
---|
328 | int dummyIOD;
|
---|
329 | in >> dummyIOD >> cc->dClk;
|
---|
330 | cc->dClk /= t_CST::c;
|
---|
331 | cc->dClkSet = true;
|
---|
332 | }
|
---|
333 | }
|
---|
334 | else if ( messageType == BTYPE_GPS ) {
|
---|
335 |
|
---|
336 | t_bias* bb = 0;
|
---|
337 | if (_bias.contains(prn)) {
|
---|
338 | bb = _bias.value(prn);
|
---|
339 | }
|
---|
340 | else {
|
---|
341 | bb = new t_bias();
|
---|
342 | _bias[prn] = bb;
|
---|
343 | }
|
---|
344 |
|
---|
345 | bb->tt.set(GPSweek, GPSweeks);
|
---|
346 |
|
---|
347 | int numBiases;
|
---|
348 | in >> numBiases;
|
---|
349 | for (int ii = 0; ii < numBiases; ++ii) {
|
---|
350 | int bType;
|
---|
351 | double bValue;
|
---|
352 | in >> bType >> bValue;
|
---|
353 | if (bType == 0) {
|
---|
354 | bb->p1c1 = -bValue;
|
---|
355 | }
|
---|
356 | else if (bType == 10) {
|
---|
357 | bb->p1p2 = -bValue;
|
---|
358 | }
|
---|
359 | }
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | QMutableMapIterator<QString, t_corr*> im(_corr);
|
---|
364 | while (im.hasNext()) {
|
---|
365 | im.next();
|
---|
366 | t_corr* cc = im.value();
|
---|
367 | if (!cc->ready()) {
|
---|
368 | delete cc;
|
---|
369 | im.remove();
|
---|
370 | }
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | // Satellite Position
|
---|
375 | ////////////////////////////////////////////////////////////////////////////
|
---|
376 | t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
|
---|
377 | ColumnVector& xc, ColumnVector& vv, bool& corr) {
|
---|
378 |
|
---|
379 | const bool CORR_REQUIRED = true;
|
---|
380 | const double MAXAGE = 120.0;
|
---|
381 |
|
---|
382 | corr = false;
|
---|
383 |
|
---|
384 | if (_eph.contains(prn)) {
|
---|
385 | t_eph* ee = _eph.value(prn);
|
---|
386 | ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
|
---|
387 |
|
---|
388 | if (CORR_REQUIRED) {
|
---|
389 | if (_corr.contains(prn)) {
|
---|
390 | t_corr* cc = _corr.value(prn);
|
---|
391 | if (ee->IOD() == cc->iod && (tt - cc->tt) < MAXAGE) {
|
---|
392 | corr = true;
|
---|
393 | applyCorr(tt, cc, xc, vv);
|
---|
394 | return success;
|
---|
395 | }
|
---|
396 | }
|
---|
397 | return failure;
|
---|
398 | }
|
---|
399 |
|
---|
400 | return success;
|
---|
401 | }
|
---|
402 |
|
---|
403 | return failure;
|
---|
404 | }
|
---|
405 |
|
---|
406 | //
|
---|
407 | ////////////////////////////////////////////////////////////////////////////
|
---|
408 | void bncPPPclient::applyCorr(const bncTime& tt, const t_corr* cc,
|
---|
409 | ColumnVector& xc, ColumnVector& vv) {
|
---|
410 | ColumnVector dx(3);
|
---|
411 |
|
---|
412 | ColumnVector raoHlp = cc->rao + cc->dotRao * (tt - cc->tt);
|
---|
413 |
|
---|
414 | RSW_to_XYZ(xc.Rows(1,3), vv, raoHlp, dx);
|
---|
415 |
|
---|
416 | xc[0] -= dx[0];
|
---|
417 | xc[1] -= dx[1];
|
---|
418 | xc[2] -= dx[2];
|
---|
419 | xc[3] -= cc->dClk;
|
---|
420 |
|
---|
421 | // Relativistic Correction
|
---|
422 | // -----------------------
|
---|
423 | xc[3] -= 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c ;
|
---|
424 | }
|
---|
425 |
|
---|
426 | // Correct Time of Transmission
|
---|
427 | ////////////////////////////////////////////////////////////////////////////
|
---|
428 | t_irc bncPPPclient::cmpToT(t_satData* satData) {
|
---|
429 |
|
---|
430 | double prange = satData->P3;
|
---|
431 | if (prange == 0.0) {
|
---|
432 | return failure;
|
---|
433 | }
|
---|
434 |
|
---|
435 | double clkSat = 0.0;
|
---|
436 | for (int ii = 1; ii <= 10; ii++) {
|
---|
437 |
|
---|
438 | bncTime ToT = _epoData->tt - prange / t_CST::c - clkSat;
|
---|
439 |
|
---|
440 | ColumnVector xc(4);
|
---|
441 | ColumnVector vv(3);
|
---|
442 | bool corr = false;
|
---|
443 | if (getSatPos(ToT, satData->prn, xc, vv, corr) != 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 | satData->clkCorr = corr;
|
---|
455 | return success;
|
---|
456 | }
|
---|
457 | }
|
---|
458 |
|
---|
459 | return failure;
|
---|
460 | }
|
---|
461 |
|
---|
462 | //
|
---|
463 | ////////////////////////////////////////////////////////////////////////////
|
---|
464 | void bncPPPclient::processEpoch() {
|
---|
465 |
|
---|
466 | // Data Pre-Processing
|
---|
467 | // -------------------
|
---|
468 | QMutableMapIterator<QString, t_satData*> iGPS(_epoData->satDataGPS);
|
---|
469 | while (iGPS.hasNext()) {
|
---|
470 | iGPS.next();
|
---|
471 | QString prn = iGPS.key();
|
---|
472 | t_satData* satData = iGPS.value();
|
---|
473 |
|
---|
474 | if (cmpToT(satData) != success) {
|
---|
475 | delete satData;
|
---|
476 | iGPS.remove();
|
---|
477 | continue;
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | QMutableMapIterator<QString, t_satData*> iGlo(_epoData->satDataGlo);
|
---|
482 | while (iGlo.hasNext()) {
|
---|
483 | iGlo.next();
|
---|
484 | QString prn = iGlo.key();
|
---|
485 | t_satData* satData = iGlo.value();
|
---|
486 |
|
---|
487 | if (cmpToT(satData) != success) {
|
---|
488 | delete satData;
|
---|
489 | iGlo.remove();
|
---|
490 | continue;
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 | // Filter Solution
|
---|
495 | // ---------------
|
---|
496 | if (_model->update(_epoData) == success) {
|
---|
497 | emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|