source: ntrip/trunk/BNC/combination/bnccomb.cpp@ 3025

Last change on this file since 3025 was 3025, checked in by mervart, 13 years ago
File size: 14.4 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncComb
6 *
7 * Purpose: Combinations of Orbit/Clock Corrections
8 *
9 * Author: L. Mervart
10 *
11 * Created: 22-Jan-2011
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <newmatio.h>
18#include <iomanip>
19
20#include "bnccomb.h"
21#include "bncapp.h"
22#include "cmbcaster.h"
23#include "bncsettings.h"
24#include "bncmodel.h"
25#include "bncutils.h"
26
27using namespace std;
28
29// Constructor
30////////////////////////////////////////////////////////////////////////////
31cmbParam::cmbParam(cmbParam::parType typeIn, int indexIn,
32 const QString& acIn, const QString& prnIn) {
33 type = typeIn;
34 index = indexIn;
35 AC = acIn;
36 prn = prnIn;
37 xx = 0.0;
38}
39
40// Destructor
41////////////////////////////////////////////////////////////////////////////
42cmbParam::~cmbParam() {
43}
44
45// Partial
46////////////////////////////////////////////////////////////////////////////
47double cmbParam::partial(const QString& acIn, t_corr* corr) {
48
49 if (type == AC_offset) {
50 if (AC == acIn) {
51 return 1.0;
52 }
53 }
54 else if (type == Sat_offset) {
55 if (AC == acIn && prn == corr->prn) {
56 return 1.0;
57 }
58 }
59 else if (type == clk) {
60 if (prn == corr->prn) {
61 return 1.0;
62 }
63 }
64
65 return 0.0;
66}
67
68//
69////////////////////////////////////////////////////////////////////////////
70QString cmbParam::toString() const {
71
72 QString outStr;
73
74 if (type == AC_offset) {
75 outStr = "AC offset " + AC;
76 }
77 else if (type == Sat_offset) {
78 outStr = "Sat Offset " + AC + " " + prn;
79 }
80 else if (type == clk) {
81 outStr = "Clk Corr " + prn;
82 }
83
84 return outStr;
85}
86
87// Constructor
88////////////////////////////////////////////////////////////////////////////
89bncComb::bncComb() {
90
91 bncSettings settings;
92
93 QStringList combineStreams = settings.value("combineStreams").toStringList();
94
95 if (combineStreams.size() >= 2) {
96 QListIterator<QString> it(combineStreams);
97 while (it.hasNext()) {
98 QStringList hlp = it.next().split(" ");
99 cmbAC* newAC = new cmbAC();
100 newAC->mountPoint = hlp[0];
101 newAC->name = hlp[1];
102 newAC->weight = hlp[2].toDouble();
103
104 _ACs[newAC->mountPoint] = newAC;
105 }
106 }
107
108 _caster = new cmbCaster();
109 connect(this, SIGNAL(newMessage(QByteArray,bool)),
110 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
111
112 // Initialize Parameters
113 // ---------------------
114 int nextPar = 0;
115 QMapIterator<QString, cmbAC*> it(_ACs);
116 while (it.hasNext()) {
117 it.next();
118 cmbAC* AC = it.value();
119 _params.push_back(new cmbParam(cmbParam::AC_offset, ++nextPar, AC->name, ""));
120 }
121 it.toFront();
122 while (it.hasNext()) {
123 it.next();
124 cmbAC* AC = it.value();
125 for (int iGps = 1; iGps <= 32; iGps++) {
126 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
127 _params.push_back(new cmbParam(cmbParam::Sat_offset, ++nextPar, AC->name, prn));
128 }
129 }
130 for (int iGps = 1; iGps <= 32; iGps++) {
131 QString prn = QString("G%1").arg(iGps, 2, 10, QChar('0'));
132 _params.push_back(new cmbParam(cmbParam::clk, ++nextPar, "", prn));
133 }
134
135 unsigned nPar = _params.size();
136 _QQ.ReSize(nPar);
137 _QQ = 0.0;
138
139 // Clk_Corr = AC_Offset + Sat_Offset + Clk
140 // ---------------------------------------
141 _sigACOff = 100.0; // per analysis center stream and epoch, sigma in meters
142 _sigSatOff = 100.0; // per analysis center stream and satellite, sigma in meters
143 _sigClk = 100.0; // per satellite and epoch, sigma in meters
144
145 for (int iPar = 1; iPar <= _params.size(); iPar++) {
146 cmbParam* pp = _params[iPar-1];
147 if (pp->type == cmbParam::AC_offset) {
148 _QQ(iPar,iPar) = _sigACOff * _sigACOff;
149 }
150 else if (pp->type == cmbParam::Sat_offset) {
151 _QQ(iPar,iPar) = _sigSatOff * _sigSatOff;
152 }
153 else if (pp->type == cmbParam::clk) {
154 _QQ(iPar,iPar) = _sigClk * _sigClk;
155 }
156 }
157}
158
159// Destructor
160////////////////////////////////////////////////////////////////////////////
161bncComb::~bncComb() {
162 QMapIterator<QString, cmbAC*> it(_ACs);
163 while (it.hasNext()) {
164 it.next();
165 delete it.value();
166 }
167 delete _caster;
168}
169
170// Read and store one correction line
171////////////////////////////////////////////////////////////////////////////
172void bncComb::processCorrLine(const QString& staID, const QString& line) {
173 QMutexLocker locker(&_mutex);
174
175 // Find the relevant instance of cmbAC class
176 // -----------------------------------------
177 if (_ACs.find(staID) == _ACs.end()) {
178 return;
179 }
180 cmbAC* AC = _ACs[staID];
181
182 // Read the Correction
183 // -------------------
184 t_corr* newCorr = new t_corr();
185 if (!newCorr->readLine(line) == success) {
186 delete newCorr;
187 return;
188 }
189
190 // Reject delayed corrections
191 // --------------------------
192 if (_processedBeforeTime.valid() && newCorr->tt < _processedBeforeTime) {
193 delete newCorr;
194 return;
195 }
196
197 // Check the IOD
198 //--------------
199 if (_eph.find(newCorr->prn) == _eph.end()) {
200 delete newCorr;
201 return;
202 }
203 else {
204 t_eph* lastEph = _eph[newCorr->prn]->last;
205 t_eph* prevEph = _eph[newCorr->prn]->prev;
206 if (prevEph && prevEph->IOD() == newCorr->iod) {
207 switchToLastEph(lastEph, prevEph, newCorr);
208 }
209 else if (!lastEph || lastEph->IOD() != newCorr->iod) {
210 delete newCorr;
211 return;
212 }
213 newCorr->eph = lastEph;
214 }
215
216 // Process all older Epochs (if there are any)
217 // -------------------------------------------
218 const double waitTime = 5.0; // wait 5 sec
219 _processedBeforeTime = newCorr->tt - waitTime;
220
221 QList<cmbEpoch*> epochsToProcess;
222
223 QMapIterator<QString, cmbAC*> itAC(_ACs);
224 while (itAC.hasNext()) {
225 itAC.next();
226 cmbAC* AC = itAC.value();
227
228 QMutableListIterator<cmbEpoch*> itEpo(AC->epochs);
229 while (itEpo.hasNext()) {
230 cmbEpoch* epoch = itEpo.next();
231 if (epoch->time < _processedBeforeTime) {
232 epochsToProcess.append(epoch);
233 itEpo.remove();
234 }
235 }
236 }
237
238 if (epochsToProcess.size()) {
239 processEpochs(epochsToProcess);
240 }
241
242 // Check Modulo Time
243 // -----------------
244 const int moduloTime = 10;
245 if (int(newCorr->tt.gpssec()) % moduloTime != 0.0) {
246 delete newCorr;
247 return;
248 }
249
250 // Find/Create the instance of cmbEpoch class
251 // ------------------------------------------
252 cmbEpoch* newEpoch = 0;
253 QListIterator<cmbEpoch*> it(AC->epochs);
254 while (it.hasNext()) {
255 cmbEpoch* hlpEpoch = it.next();
256 if (hlpEpoch->time == newCorr->tt) {
257 newEpoch = hlpEpoch;
258 break;
259 }
260 }
261 if (newEpoch == 0) {
262 newEpoch = new cmbEpoch(AC->name);
263 newEpoch->time = newCorr->tt;
264 AC->epochs.append(newEpoch);
265 }
266
267 // Merge or add the correction
268 // ---------------------------
269 if (newEpoch->corr.find(newCorr->prn) != newEpoch->corr.end()) {
270 newEpoch->corr[newCorr->prn]->readLine(line); // merge (multiple messages)
271 }
272 else {
273 newEpoch->corr[newCorr->prn] = newCorr;
274 }
275}
276
277// Send results to caster
278////////////////////////////////////////////////////////////////////////////
279void bncComb::dumpResults(const bncTime& resTime,
280 const QMap<QString, t_corr*>& resCorr) {
281
282 _caster->open();
283
284 unsigned year, month, day;
285 resTime.civil_date (year, month, day);
286 double GPSweeks = resTime.gpssec();
287
288 struct ClockOrbit co;
289 memset(&co, 0, sizeof(co));
290 co.GPSEpochTime = (int)GPSweeks;
291 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0)
292 + 3 * 3600 - gnumleap(year, month, day);
293 co.ClockDataSupplied = 1;
294 co.OrbitDataSupplied = 1;
295 co.SatRefDatum = DATUM_ITRF;
296
297 QMapIterator<QString, t_corr*> it(resCorr);
298 while (it.hasNext()) {
299 it.next();
300 t_corr* corr = it.value();
301
302 struct ClockOrbit::SatData* sd = 0;
303 if (corr->prn[0] == 'G') {
304 sd = co.Sat + co.NumberOfGPSSat;
305 ++co.NumberOfGPSSat;
306 }
307 else if (corr->prn[0] == 'R') {
308 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
309 ++co.NumberOfGLONASSSat;
310 }
311
312 if (sd != 0) {
313 sd->ID = corr->prn.mid(1).toInt();
314 sd->IOD = corr->iod;
315 sd->Clock.DeltaA0 = corr->dClk * t_CST::c;
316 sd->Orbit.DeltaRadial = corr->rao(1);
317 sd->Orbit.DeltaAlongTrack = corr->rao(2);
318 sd->Orbit.DeltaCrossTrack = corr->rao(3);
319 sd->Orbit.DotDeltaRadial = corr->dotRao(1);
320 sd->Orbit.DotDeltaAlongTrack = corr->dotRao(2);
321 sd->Orbit.DotDeltaCrossTrack = corr->dotRao(3);
322 }
323
324 delete corr;
325 }
326
327 // Send Corrections to Caster
328 // --------------------------
329 if ( _caster->usedSocket() &&
330 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
331 char obuffer[CLOCKORBIT_BUFFERSIZE];
332 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
333 if (len > 0) {
334 _caster->write(obuffer, len);
335 }
336 }
337
338 // Emit new Corrections
339 // --------------------
340 co.messageType = COTYPE_GPSCOMBINED;
341 QStringList asciiLines =
342 RTCM3coDecoder::corrsToASCIIlines(resTime.gpsw(), resTime.gpssec(), co, 0);
343
344 long coTime = resTime.gpsw() * 7*24*3600 + long(floor(resTime.gpssec()+0.5));
345 QStringListIterator il(asciiLines);
346 while (il.hasNext()) {
347 QString line = il.next();
348 emit newCorrLine(line, _caster->mountpoint(), coTime);
349 }
350}
351
352// Change the correction so that it refers to last received ephemeris
353////////////////////////////////////////////////////////////////////////////
354void bncComb::switchToLastEph(const t_eph* lastEph, const t_eph* prevEph,
355 t_corr* newCorr) {
356 ColumnVector oldXC(4);
357 ColumnVector oldVV(3);
358 prevEph->position(newCorr->tt.gpsw(), newCorr->tt.gpssec(),
359 oldXC.data(), oldVV.data());
360
361 ColumnVector newXC(4);
362 ColumnVector newVV(3);
363 lastEph->position(newCorr->tt.gpsw(), newCorr->tt.gpssec(),
364 newXC.data(), newVV.data());
365
366 ColumnVector dX = newXC.Rows(1,3) - oldXC.Rows(1,3);
367 ColumnVector dV = newVV - oldVV;
368 double dC = newXC(4) - oldXC(4);
369
370 ColumnVector dRAO(3);
371 XYZ_to_RSW(newXC.Rows(1,3), newVV, dX, dRAO);
372
373 ColumnVector dDotRAO(3);
374 XYZ_to_RSW(newXC.Rows(1,3), newVV, dV, dDotRAO);
375
376 newCorr->iod = lastEph->IOD();
377 newCorr->rao -= dRAO;
378 newCorr->dotRao -= dDotRAO;
379 newCorr->dClk -= dC;
380
381 QString msg = "switch " + newCorr->prn
382 + QString(" %1 -> %2 %3").arg(prevEph->IOD(),3)
383 .arg(lastEph->IOD(),3).arg(dC*t_CST::c, 8, 'f', 4);
384
385 emit newMessage(msg.toAscii(), false);
386}
387
388// Process Epochs
389////////////////////////////////////////////////////////////////////////////
390void bncComb::processEpochs(const QList<cmbEpoch*>& epochs) {
391
392 _log.clear();
393
394 QTextStream out(&_log, QIODevice::WriteOnly);
395
396 out << "Combination:" << endl
397 << "------------------------------" << endl;
398
399 // Predict Parameters Values, Add White Noise
400 // ------------------------------------------
401 for (int iPar = 1; iPar <= _params.size(); iPar++) {
402 cmbParam* pp = _params[iPar-1];
403 if (pp->type == cmbParam::AC_offset || pp->type == cmbParam::clk) {
404 pp->xx = 0.0;
405 for (int jj = 1; jj <= _params.size(); jj++) {
406 _QQ(iPar, jj) = 0.0;
407 }
408 }
409 if (pp->type == cmbParam::AC_offset) {
410 _QQ(iPar,iPar) = _sigACOff * _sigACOff;
411 }
412 else if (pp->type == cmbParam::clk) {
413 _QQ(iPar,iPar) = _sigClk * _sigClk;
414 }
415 }
416
417 bncTime resTime = epochs.first()->time;
418 QMap<QString, t_corr*> resCorr;
419
420 int nPar = _params.size();
421 int nObs = 0;
422
423 ColumnVector x0(nPar);
424 for (int iPar = 1; iPar <= _params.size(); iPar++) {
425 cmbParam* pp = _params[iPar-1];
426 x0(iPar) = pp->xx;
427 }
428
429 // Count Observations
430 // ------------------
431 QListIterator<cmbEpoch*> itEpo(epochs);
432 while (itEpo.hasNext()) {
433 cmbEpoch* epo = itEpo.next();
434 QMapIterator<QString, t_corr*> itCorr(epo->corr);
435 while (itCorr.hasNext()) {
436 itCorr.next();
437 ++nObs;
438 }
439 }
440
441 if (nObs > 0) {
442 Matrix AA(nObs, nPar);
443 ColumnVector ll(nObs);
444 DiagonalMatrix PP(nObs); PP = 1.0;
445
446 int iObs = 0;
447 QListIterator<cmbEpoch*> itEpo(epochs);
448 while (itEpo.hasNext()) {
449 cmbEpoch* epo = itEpo.next();
450 QMapIterator<QString, t_corr*> itCorr(epo->corr);
451
452 while (itCorr.hasNext()) {
453 itCorr.next();
454 ++iObs;
455 t_corr* corr = itCorr.value();
456
457 //// beg test
458 if (epo->acName == "BKG") {
459 resCorr[corr->prn] = new t_corr(*corr);
460 }
461 //// end test
462
463 for (int iPar = 1; iPar <= _params.size(); iPar++) {
464 cmbParam* pp = _params[iPar-1];
465 AA(iObs, iPar) = pp->partial(epo->acName, corr);
466 }
467
468 ll(iObs) = corr->dClk * t_CST::c - DotProduct(AA.Row(iObs), x0);
469
470 delete corr;
471 }
472 }
473
474 ColumnVector dx;
475 bncModel::kalman(AA, ll, PP, _QQ, dx);
476 ColumnVector vv = ll - AA * dx;
477
478 for (int iPar = 1; iPar <= _params.size(); iPar++) {
479 cmbParam* pp = _params[iPar-1];
480 pp->xx += dx(iPar);
481 if (pp->type == cmbParam::clk) {
482 if (resCorr.find(pp->prn) != resCorr.end()) {
483 resCorr[pp->prn]->dClk = pp->xx / t_CST::c;
484 }
485 }
486 out << currentDateAndTimeGPS().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
487 out.setRealNumberNotation(QTextStream::FixedNotation);
488 out.setFieldWidth(8);
489 out.setRealNumberPrecision(4);
490 out << pp->toString() << " "
491 << pp->xx << " +- " << sqrt(_QQ(pp->index,pp->index)) << endl;
492 }
493 }
494
495 printResults(out, resTime, resCorr);
496 dumpResults(resTime, resCorr);
497
498 emit newMessage(_log, false);
499}
500
501// Print results to caster
502////////////////////////////////////////////////////////////////////////////
503void bncComb::printResults(QTextStream& out, const bncTime& resTime,
504 const QMap<QString, t_corr*>& resCorr) {
505
506 QMapIterator<QString, t_corr*> it(resCorr);
507 while (it.hasNext()) {
508 it.next();
509 t_corr* corr = it.value();
510 t_eph* eph = corr->eph;
511 if (eph) {
512 double xx, yy, zz, cc;
513 eph->position(resTime.gpsw(), resTime.gpssec(), xx, yy, zz, cc);
514
515 out << currentDateAndTimeGPS().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
516 out.setFieldWidth(3);
517 out << "Full Clock " << corr->prn << " " << corr->iod << " ";
518 out.setFieldWidth(14);
519 out << (cc + corr->dClk) * t_CST::c << endl;
520 }
521 else {
522 out << "bncComb::printResuls bug" << endl;
523 }
524 }
525}
Note: See TracBrowser for help on using the repository browser.