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

Last change on this file since 2268 was 2268, checked in by mervart, 14 years ago

* empty log message *

File size: 11.4 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
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
51extern "C" {
52#include "clock_orbit_rtcm.h"
53}
54
55using namespace std;
56
57// Constructor
58////////////////////////////////////////////////////////////////////////////
59bncPPPclient::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////////////////////////////////////////////////////////////////////////////
86bncPPPclient::~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}
100
101//
102////////////////////////////////////////////////////////////////////////////
103void bncPPPclient::putNewObs(p_obs pp) {
104 QMutexLocker locker(&_mutex);
105
106 t_obsInternal* obs = &(pp->_o);
107
108 if (obs->satSys != 'G' && !_useGlonass) {
109 return;
110 }
111
112 t_satData* satData = new t_satData();
113
114 // Set Code Observations
115 // ---------------------
116 if (obs->P1) {
117 satData->P1 = obs->P1;
118 satData->codeTypeF1 = t_satData::P_CODE;
119 }
120 else if (obs->C1) {
121 satData->P1 = obs->C1;
122 satData->codeTypeF1 = t_satData::C_CODE;
123 }
124 else {
125 delete satData;
126 return;
127 }
128
129 if (obs->P2) {
130 satData->P2 = obs->P2;
131 satData->codeTypeF2 = t_satData::P_CODE;
132 }
133 else if (obs->C2) {
134 satData->P2 = obs->C2;
135 satData->codeTypeF2 = t_satData::C_CODE;
136 }
137 else {
138 delete satData;
139 return;
140 }
141
142 double f1 = t_CST::freq1;
143 double f2 = t_CST::freq2;
144
145 if (obs->satSys == 'R') {
146 f1 = 1602000000.0 + 562500.0 * obs->slot;
147 f2 = 1246000000.0 + 437500.0 * obs->slot;
148 }
149
150 // Ionosphere-Free Combination
151 // ---------------------------
152 double c1 = f1 * f1 / (f1 * f1 - f2 * f2);
153 double c2 = - f2 * f2 / (f1 * f1 - f2 * f2);
154
155 satData->P3 = c1 * satData->P1 + c2 * satData->P2;
156
157 // Set Phase Observations
158 // ----------------------
159 if (obs->L1 && obs->L2) {
160 satData->L1 = obs->L1 * t_CST::c / f1;
161 satData->L2 = obs->L2 * t_CST::c / f2;
162 }
163 else {
164 delete satData;
165 return;
166 }
167 satData->L3 = c1 * satData->L1 + c2 * satData->L2;
168
169 // Add new Satellite to the epoch
170 // ------------------------------
171 bncTime tt(obs->GPSWeek, obs->GPSWeeks);
172
173 if (!_epoData) {
174 _epoData = new t_epoData();
175 _epoData->tt = tt;
176 }
177 else if (tt != _epoData->tt) {
178 processEpoch();
179 delete _epoData;
180 _epoData = new t_epoData();
181 _epoData->tt = tt;
182 }
183
184 if (obs->satSys == 'G') {
185 QString prn = QString("G%1").arg(obs->satNum, 2, 10, QChar('0'));
186 satData->prn = prn;
187 _epoData->satDataGPS[prn] = satData;
188 }
189 else if (obs->satSys == 'R') {
190 QString prn = QString("R%1").arg(obs->satNum, 2, 10, QChar('0'));
191 satData->prn = prn;
192 _epoData->satDataGlo[prn] = satData;
193 }
194
195}
196
197//
198////////////////////////////////////////////////////////////////////////////
199void bncPPPclient::slotNewEphGPS(gpsephemeris gpseph) {
200 QMutexLocker locker(&_mutex);
201
202 QString prn = QString("G%1").arg(gpseph.satellite, 2, 10, QChar('0'));
203
204 if (_eph.contains(prn)) {
205 t_ephGPS* ee = static_cast<t_ephGPS*>(_eph.value(prn));
206 if ( (ee->GPSweek() < gpseph.GPSweek) ||
207 (ee->GPSweek() == gpseph.GPSweek &&
208 ee->TOC() < gpseph.TOC) ) {
209 ee->set(&gpseph);
210 }
211 }
212 else {
213 t_ephGPS* ee = new t_ephGPS();
214 ee->set(&gpseph);
215 _eph[prn] = ee;
216 }
217}
218
219//
220////////////////////////////////////////////////////////////////////////////
221void bncPPPclient::slotNewEphGlonass(glonassephemeris gloeph) {
222 QMutexLocker locker(&_mutex);
223
224 QString prn = QString("R%1").arg(gloeph.almanac_number, 2, 10, QChar('0'));
225
226 if (_eph.contains(prn)) {
227 int ww = gloeph.GPSWeek;
228 int tow = gloeph.GPSTOW;
229 updatetime(&ww, &tow, gloeph.tb*1000, 0); // Moscow -> GPS
230 t_ephGlo* ee = static_cast<t_ephGlo*>(_eph.value(prn));
231 if (ee->GPSweek() < ww ||
232 (ee->GPSweek() == ww && ee->GPSweeks() < tow)) {
233 ee->set(&gloeph);
234 }
235 }
236 else {
237 t_ephGlo* ee = new t_ephGlo();
238 ee->set(&gloeph);
239 _eph[prn] = ee;
240 }
241}
242
243//
244////////////////////////////////////////////////////////////////////////////
245void bncPPPclient::slotNewCorrections(QList<QString> corrList) {
246 QMutexLocker locker(&_mutex);
247
248 if (corrList.size() == 0) {
249 return;
250 }
251
252 // Remove All Corrections
253 // ----------------------
254 QMapIterator<QString, t_corr*> ic(_corr);
255 while (ic.hasNext()) {
256 ic.next();
257 delete ic.value();
258 }
259 _corr.clear();
260
261 QListIterator<QString> it(corrList);
262 while (it.hasNext()) {
263 QTextStream in(it.next().toAscii());
264 int messageType;
265 int updateInterval;
266 int GPSweek;
267 double GPSweeks;
268 QString prn;
269 in >> messageType >> updateInterval >> GPSweek >> GPSweeks >> prn;
270 if ( messageType == COTYPE_GPSCOMBINED ||
271 messageType == COTYPE_GLONASSCOMBINED ||
272 messageType == COTYPE_GPSORBIT ||
273 messageType == COTYPE_GPSCLOCK ||
274 messageType == COTYPE_GLONASSORBIT ||
275 messageType == COTYPE_GLONASSCLOCK ) {
276
277 t_corr* cc = 0;
278 if (_corr.contains(prn)) {
279 cc = _corr.value(prn);
280 }
281 else {
282 cc = new t_corr();
283 _corr[prn] = cc;
284 }
285
286 cc->tt.set(GPSweek, GPSweeks);
287
288 if ( messageType == COTYPE_GPSCOMBINED ||
289 messageType == COTYPE_GLONASSCOMBINED ) {
290 cc->rao.ReSize(3);
291 in >> cc->iod >> cc->dClk >> cc->rao[0] >> cc->rao[1] >> cc->rao[2];
292 cc->dClk /= t_CST::c;
293 cc->raoSet = true;
294 cc->dClkSet = true;
295 }
296 else if ( messageType == COTYPE_GPSORBIT ||
297 messageType == COTYPE_GLONASSORBIT ) {
298 cc->rao.ReSize(3);
299 in >> cc->iod >> cc->rao[0] >> cc->rao[1] >> cc->rao[2];
300 cc->raoSet = true;
301 }
302 else if ( messageType == COTYPE_GPSCLOCK ||
303 messageType == COTYPE_GLONASSCLOCK ) {
304 int dummyIOD;
305 in >> dummyIOD >> cc->dClk;
306 cc->dClk /= t_CST::c;
307 cc->dClkSet = true;
308 }
309 }
310 }
311
312 QMutableMapIterator<QString, t_corr*> im(_corr);
313 while (im.hasNext()) {
314 im.next();
315 t_corr* cc = im.value();
316 if (!cc->ready()) {
317 delete cc;
318 im.remove();
319 }
320 }
321}
322
323// Satellite Position
324////////////////////////////////////////////////////////////////////////////
325t_irc bncPPPclient::getSatPos(const bncTime& tt, const QString& prn,
326 ColumnVector& xc, ColumnVector& vv, bool& corr) {
327
328 const bool CORR_REQUIRED = true;
329 const double MAXAGE = 120.0;
330
331 corr = false;
332
333 if (_eph.contains(prn)) {
334 t_eph* ee = _eph.value(prn);
335 ee->position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data());
336
337 if (CORR_REQUIRED) {
338 if (_corr.contains(prn)) {
339 t_corr* cc = _corr.value(prn);
340 if (ee->IOD() == cc->iod && (tt - cc->tt) < MAXAGE) {
341 corr = true;
342 applyCorr(cc, xc, vv);
343 return success;
344 }
345 }
346 return failure;
347 }
348
349 return success;
350 }
351
352 return failure;
353}
354
355//
356////////////////////////////////////////////////////////////////////////////
357void bncPPPclient::applyCorr(const t_corr* cc, ColumnVector& xc,
358 ColumnVector& vv) {
359 ColumnVector dx(3);
360 RSW_to_XYZ(xc.Rows(1,3), vv, cc->rao, dx);
361
362 xc[0] -= dx[0];
363 xc[1] -= dx[1];
364 xc[2] -= dx[2];
365 xc[3] -= cc->dClk;
366
367 // Relativistic Correction
368 // -----------------------
369 xc[3] -= 2.0 * DotProduct(xc.Rows(1,3),vv) / t_CST::c / t_CST::c ;
370}
371
372// Correct Time of Transmission
373////////////////////////////////////////////////////////////////////////////
374t_irc bncPPPclient::cmpToT(t_satData* satData) {
375
376 double prange = satData->P3;
377 if (prange == 0.0) {
378 return failure;
379 }
380
381 double clkSat = 0.0;
382 for (int ii = 1; ii <= 10; ii++) {
383
384 bncTime ToT = _epoData->tt - prange / t_CST::c - clkSat;
385
386 ColumnVector xc(4);
387 ColumnVector vv(3);
388 bool corr = false;
389 if (getSatPos(ToT, satData->prn, xc, vv, corr) != success) {
390 return failure;
391 }
392
393 double clkSatOld = clkSat;
394 clkSat = xc(4);
395
396 if ( fabs(clkSat-clkSatOld) * t_CST::c < 1.e-4 ) {
397 satData->xx = xc.Rows(1,3);
398 satData->vv = vv;
399 satData->clk = clkSat * t_CST::c;
400 satData->clkCorr = corr;
401 return success;
402 }
403 }
404
405 return failure;
406}
407
408//
409////////////////////////////////////////////////////////////////////////////
410void bncPPPclient::processEpoch() {
411
412 // Data Pre-Processing
413 // -------------------
414 QMutableMapIterator<QString, t_satData*> iGPS(_epoData->satDataGPS);
415 while (iGPS.hasNext()) {
416 iGPS.next();
417 QString prn = iGPS.key();
418 t_satData* satData = iGPS.value();
419
420 if (cmpToT(satData) != success) {
421 delete satData;
422 iGPS.remove();
423 continue;
424 }
425 }
426
427 QMutableMapIterator<QString, t_satData*> iGlo(_epoData->satDataGlo);
428 while (iGlo.hasNext()) {
429 iGlo.next();
430 QString prn = iGlo.key();
431 t_satData* satData = iGlo.value();
432
433 if (cmpToT(satData) != success) {
434 delete satData;
435 iGlo.remove();
436 continue;
437 }
438 }
439
440 // Filter Solution
441 // ---------------
442 if (_model->update(_epoData) == success) {
443 emit newPosition(_model->time(), _model->x(), _model->y(), _model->z());
444 }
445}
446
Note: See TracBrowser for help on using the repository browser.