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

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

* empty log message *

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