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

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

* empty log message *

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