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

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

* empty log message *

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