source: ntrip/trunk/BNC/src/bncephuser.cpp@ 11218

Last change on this file since 11218 was 11218, checked in by stuerze, 10 hours ago

improvements regarding ssrqc ephemerides upload and combination

File size: 24.2 KB
Line 
1// converting GNSS data streams from NTRIP broadcasters.
2//
3// Copyright (C) 2007
4// German Federal Agency for Cartography and Geodesy (BKG)
5// http://bkg.bund.de
6// Czech Technical University Prague, Department of Geodesy
7// http://www.fsv.cvut.cz
8//
9// Email: euref-ip@bkg.bund.de
10//
11// This program is free software; you can redistribute it and/or
12// modify it under the terms of the GNU General Public License
13// as published by the Free Software Foundation, version 2.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24/* -------------------------------------------------------------------------
25 * BKG NTRIP Client
26 * -------------------------------------------------------------------------
27 *
28 * Class: bncEphUser
29 *
30 * Purpose: Base for Classes that use Ephemerides
31 *
32 * Author: L. Mervart
33 *
34 * Created: 27-Jan-2011
35 *
36 * Changes:
37 *
38 * -----------------------------------------------------------------------*/
39
40#include <cmath>
41#include <iostream>
42
43#include "bncephuser.h"
44#include "bnccore.h"
45#include "bncsettings.h"
46#include "RTCM3/ephEncoder.h"
47
48using namespace std;
49
50// Constructor
51////////////////////////////////////////////////////////////////////////////
52bncEphUser::bncEphUser(bool connectSlots, unsigned maxQueueSize) : _maxQueueSize(maxQueueSize), _voting(false) {
53 if (connectSlots) {
54 connect(BNC_CORE, SIGNAL(newGPSEph(t_ephGPS)),
55 this, SLOT(slotNewGPSEph(t_ephGPS)), Qt::DirectConnection);
56
57 connect(BNC_CORE, SIGNAL(newGlonassEph(t_ephGlo)),
58 this, SLOT(slotNewGlonassEph(t_ephGlo)), Qt::DirectConnection);
59
60 connect(BNC_CORE, SIGNAL(newGalileoEph(t_ephGal)),
61 this, SLOT(slotNewGalileoEph(t_ephGal)), Qt::DirectConnection);
62
63 connect(BNC_CORE, SIGNAL(newSBASEph(t_ephSBAS)),
64 this, SLOT(slotNewSBASEph(t_ephSBAS)), Qt::DirectConnection);
65
66 connect(BNC_CORE, SIGNAL(newBDSEph(t_ephBDS)),
67 this, SLOT(slotNewBDSEph(t_ephBDS)), Qt::DirectConnection);
68 }
69}
70
71// Ephemerides of the streams in the general option 'Ephemeris streams' only
72////////////////////////////////////////////////////////////////////////////
73void bncEphUser::useEphMounts(const QString& user) {
74 bncSettings settings;
75 _ephMounts = settings.value("ephMount").toString().split(QRegularExpression("[,\\s]+"),
76 Qt::SkipEmptyParts);
77 if (!_ephMounts.isEmpty()) {
78 BNC_CORE->message((user.trimmed() + ": ephemerides of " + _ephMounts.join(", ") + " only").toLatin1(), true, "bce", t_logRecord::info);
79 }
80}
81
82// Destructor
83////////////////////////////////////////////////////////////////////////////
84bncEphUser::~bncEphUser() {
85 QMapIterator<QString, deque<t_eph*> > it(_eph);
86 while (it.hasNext()) {
87 it.next();
88 const deque<t_eph*>& qq = it.value();
89 for (unsigned ii = 0; ii < qq.size(); ii++) {
90 delete qq[ii];
91 }
92 }
93 qDeleteAll(_votes);
94}
95
96// New GPS Ephemeris
97////////////////////////////////////////////////////////////////////////////
98void bncEphUser::slotNewGPSEph(t_ephGPS eph) {
99 if (fromEphMount(eph)) {
100 putNewEph(&eph, true);
101 }
102}
103
104// New Glonass Ephemeris
105////////////////////////////////////////////////////////////////////////////
106void bncEphUser::slotNewGlonassEph(t_ephGlo eph) {
107 if (fromEphMount(eph)) {
108 putNewEph(&eph, true);
109 }
110}
111
112// New Galileo Ephemeris
113////////////////////////////////////////////////////////////////////////////
114void bncEphUser::slotNewGalileoEph(t_ephGal eph) {
115 if (fromEphMount(eph)) {
116 putNewEph(&eph, true);
117 }
118}
119
120// New SBAS Ephemeris
121////////////////////////////////////////////////////////////////////////////
122void bncEphUser::slotNewSBASEph(t_ephSBAS eph) {
123 if (fromEphMount(eph)) {
124 putNewEph(&eph, true);
125 }
126}
127
128// New BDS Ephemeris
129////////////////////////////////////////////////////////////////////////////
130void bncEphUser::slotNewBDSEph(t_ephBDS eph) {
131 if (fromEphMount(eph)) {
132 putNewEph(&eph, true);
133 }
134}
135
136//
137////////////////////////////////////////////////////////////////////////////
138t_irc bncEphUser::putNewEph(t_eph* eph, bool realTime) {
139
140 QMutexLocker locker(&_mutex);
141
142 if (eph == 0) {
143 return failure;
144 }
145
146 if (_voting && realTime) {
147 releaseHeld();
148 }
149
150 // satellite outside the range supported by BNC (e.g. G39)
151 if (eph->prn().toInt() == 0) {
152 return failure;
153 }
154
155 t_eph* newEph = clone(eph);
156 if (newEph == 0) {
157 return failure;
158 }
159
160 QString prn(newEph->prn().toInternalString().c_str());
161
162 // data set received before, e.g. from a slower stream after a newer one
163 if (_eph.contains(prn)) {
164 const deque<t_eph*>& qq = _eph[prn];
165 for (unsigned ii = 0; ii < qq.size(); ii++) {
166 if (qq[ii]->sameDataSet(eph)) {
167 delete newEph;
168 if (_voting && realTime) {
169 vote(prn, ii, eph);
170 }
171 return failure;
172 }
173 }
174 }
175
176 const t_eph* ephOld = ephLast(prn);
177
178 if (ephOld &&
179 (ephOld->checkState() == t_eph::bad ||
180 ephOld->checkState() == t_eph::outdated )) {
181 ephOld = 0;
182 }
183
184 if ( (ephOld == 0) || eph->isNewerThan(ephOld)) {
185 checkEphemeris(eph, realTime);
186 }
187 else {
188 delete newEph;
189 return failure;
190 }
191
192 if (eph->checkState() != t_eph::bad &&
193 eph->checkState() != t_eph::outdated) {
194 deque<t_eph*>& qq = _eph[prn];
195 newEph->setCheckState(eph->checkState());
196 qq.push_back(newEph);
197 if (_voting && realTime) {
198 t_votes* votes = new t_votes;
199 votes->_content.append(votingContent(newEph));
200 votes->_eph.append(clone(newEph));
201 votes->_station[newEph->receptStaID()] = 0;
202 votes->_first = currentTime();
203 if (qq.size() > 1) {
204 const t_votes* prev = _votes.value(qq[qq.size()-2], 0);
205 votes->_released = !(prev && prev->_station.size() >= VOTE_AGREE);
206 }
207 _votes[newEph] = votes;
208 }
209 if (qq.size() > _maxQueueSize) {
210 delete _votes.take(qq.front());
211 delete qq.front();
212 qq.pop_front();
213 }
214 ephBufferChanged();
215 return success;
216 }
217 else {
218 delete newEph;
219 return failure;
220 }
221}
222
223// Copy of an ephemeris of any system
224////////////////////////////////////////////////////////////////////////////
225t_eph* bncEphUser::clone(const t_eph* eph) {
226 if (const t_ephGPS* e = dynamic_cast<const t_ephGPS*>(eph)) return new t_ephGPS(*e);
227 if (const t_ephGlo* e = dynamic_cast<const t_ephGlo*>(eph)) return new t_ephGlo(*e);
228 if (const t_ephGal* e = dynamic_cast<const t_ephGal*>(eph)) return new t_ephGal(*e);
229 if (const t_ephSBAS* e = dynamic_cast<const t_ephSBAS*>(eph)) return new t_ephSBAS(*e);
230 if (const t_ephBDS* e = dynamic_cast<const t_ephBDS*>(eph)) return new t_ephBDS(*e);
231 return 0;
232}
233
234// Content compared in the vote: the uploaded RTCM3 message without CRC and,
235// for GLONASS, without the fields differing legitimately between receivers
236////////////////////////////////////////////////////////////////////////////
237QByteArray bncEphUser::votingContent(const t_eph* eph) {
238 unsigned char buffer[80];
239 int size = 0;
240 if (const t_ephGPS* e = dynamic_cast<const t_ephGPS*>(eph)) size = t_ephEncoder::RTCM3(*e, buffer);
241 else if (const t_ephGlo* e = dynamic_cast<const t_ephGlo*>(eph)) size = t_ephEncoder::RTCM3(*e, buffer);
242 else if (const t_ephGal* e = dynamic_cast<const t_ephGal*>(eph)) size = t_ephEncoder::RTCM3(*e, buffer);
243 else if (const t_ephSBAS* e = dynamic_cast<const t_ephSBAS*>(eph)) size = t_ephEncoder::RTCM3(*e, buffer);
244 else if (const t_ephBDS* e = dynamic_cast<const t_ephBDS*>(eph)) size = t_ephEncoder::RTCM3(*e, buffer);
245 if (size <= 6) {
246 return QByteArray();
247 }
248 if (eph->system() == t_eph::GLONASS) {
249 // message bits (after the 3 header bytes) of tk and of the system time
250 // offsets tau_c and tau_GPS: receivers take the latter from the string 5
251 // of any satellite, they differ within the same data set
252 const int field[3][2] = {{27, 12}, {293, 32}, {330, 22}};
253 for (int ii = 0; ii < 3; ii++) {
254 for (int bit = 24 + field[ii][0]; bit < 24 + field[ii][0] + field[ii][1]; bit++) {
255 buffer[bit / 8] &= ~(0x80 >> (bit % 8));
256 }
257 }
258 }
259 return QByteArray((const char*) buffer, size - 3);
260}
261
262// Names of the fields in which two vote contents (RTCM3 messages) differ
263////////////////////////////////////////////////////////////////////////////
264namespace {
265struct t_field {const char* name; int nbits;};
266const t_field fields1019[] = {{"type",12},{"sv",6},{"week",10},{"URA",4},{"L2code",2},{"IDOT",14},{"IODE",8},
267 {"toc",16},{"af2",8},{"af1",16},{"af0",22},{"IODC",10},{"Crs",16},{"dn",16},{"M0",32},{"Cuc",16},{"e",32},
268 {"Cus",16},{"sqrtA",32},{"toe",16},{"Cic",16},{"Omega0",32},{"Cis",16},{"i0",32},{"Crc",16},{"omega",32},
269 {"OmegaDot",24},{"TGD",8},{"health",6},{"L2P",1},{"fit",1},{0,0}};
270const t_field fields1020[] = {{"type",12},{"sv",6},{"fc",5},{"Cn",1},{"A",1},{"P1",2},{"tk",12},{"Bn",1},{"P2",1},
271 {"tb",7},{"xv",24},{"x",27},{"xa",5},{"yv",24},{"y",27},{"ya",5},{"zv",24},{"z",27},{"za",5},{"P3",1},
272 {"gamma",11},{"P",2},{"ln3",1},{"tau",22},{"dtau",5},{"En",5},{"P4",1},{"FT",4},{"NT",11},{"M",2},{"add",1},
273 {"NA",11},{"tauc",32},{"N4",5},{"tauGPS",22},{"ln5",1},{0,0}};
274const t_field fields1042[] = {{"type",12},{"sv",6},{"week",13},{"URAI",4},{"IDOT",14},{"AODE",5},{"toc",17},
275 {"a2",11},{"a1",22},{"a0",24},{"AODC",5},{"Crs",18},{"dn",16},{"M0",32},{"Cuc",18},{"e",32},{"Cus",18},
276 {"sqrtA",32},{"toe",17},{"Cic",18},{"Omega0",32},{"Cis",18},{"i0",32},{"Crc",18},{"omega",32},{"OmegaDot",24},
277 {"TGD1",10},{"TGD2",10},{"SatH1",1},{0,0}};
278const t_field fields1044[] = {{"type",12},{"sv",4},{"toc",16},{"af2",8},{"af1",16},{"af0",22},{"IODE",8},
279 {"Crs",16},{"dn",16},{"M0",32},{"Cuc",16},{"e",32},{"Cus",16},{"sqrtA",32},{"toe",16},{"Cic",16},{"Omega0",32},
280 {"Cis",16},{"i0",32},{"Crc",16},{"omega",32},{"OmegaDot",24},{"IDOT",14},{"L2code",2},{"week",10},{"URA",4},
281 {"health",6},{"TGD",8},{"IODC",10},{"fit",1},{0,0}};
282#define GAL_COMMON {"type",12},{"sv",6},{"week",12},{"IODnav",10},{"SISA",8},{"IDOT",14},{"toc",14},{"af2",6}, \
283 {"af1",21},{"af0",31},{"Crs",16},{"dn",16},{"M0",32},{"Cuc",16},{"e",32},{"Cus",16},{"sqrtA",32},{"toe",14}, \
284 {"Cic",16},{"Omega0",32},{"Cis",16},{"i0",32},{"Crc",16},{"omega",32},{"OmegaDot",24},{"BGDa",10}
285const t_field fields1045[] = {GAL_COMMON,{"E5aHS",2},{"E5aDVS",1},{0,0}};
286const t_field fields1046[] = {GAL_COMMON,{"BGDb",10},{"E5bHS",2},{"E5bDVS",1},{"E1BHS",2},{"E1BDVS",1},{0,0}};
287
288unsigned long long getBits(const QByteArray& buf, int pos, int nbits) {
289 unsigned long long val = 0;
290 for (int bit = pos; bit < pos + nbits; bit++) {
291 val = (val << 1) | ((static_cast<unsigned char>(buf[bit / 8]) >> (7 - bit % 8)) & 1);
292 }
293 return val;
294}
295
296// Fields differing legitimately between the copies of the same data set
297// (receiver-dependent or informational): accuracy indices, GPS/QZSS L2 code
298// and fit flags, GLONASS almanac flags and day numbers, BDS AODE/AODC
299const char* minorFields1019[] = {"URA", "L2code", "L2P", "fit", 0};
300const char* minorFields1020[] = {"Cn", "A", "P", "P1", "P3", "FT", "NA", "N4", 0};
301const char* minorFields1042[] = {"URAI", "AODE", "AODC", 0};
302const char* minorFields1045[] = {"SISA", 0};
303
304// Names of the differing fields; empty: not decodable or no difference
305QStringList fieldDifferences(const QByteArray& c1, const QByteArray& c2, const char* const** minor) {
306 QStringList names;
307 if (minor) {
308 *minor = 0;
309 }
310 if (c1.size() != c2.size() || c1.size() < 5) {
311 return names;
312 }
313 const t_field* fields = 0;
314 const char* const* minorFields = 0;
315 switch (getBits(c1, 24, 12)) {
316 case 1019: fields = fields1019; minorFields = minorFields1019; break;
317 case 1020: fields = fields1020; minorFields = minorFields1020; break;
318 case 1042: fields = fields1042; minorFields = minorFields1042; break;
319 case 1044: fields = fields1044; minorFields = minorFields1019; break;
320 case 1045: fields = fields1045; minorFields = minorFields1045; break;
321 case 1046: fields = fields1046; minorFields = minorFields1045; break;
322 }
323 if (!fields) {
324 return names;
325 }
326 if (minor) {
327 *minor = minorFields;
328 }
329 int pos = 24;
330 for (int ii = 0; fields[ii].name; ii++) {
331 if (pos + fields[ii].nbits > 8 * c1.size()) {
332 break;
333 }
334 if (getBits(c1, pos, fields[ii].nbits) != getBits(c2, pos, fields[ii].nbits)) {
335 names << fields[ii].name;
336 }
337 pos += fields[ii].nbits;
338 }
339 return names;
340}
341}
342
343// Two vote contents differ only in fields differing legitimately between
344// receivers (see minorFields...)
345////////////////////////////////////////////////////////////////////////////
346bool bncEphUser::minorDifference(const QByteArray& c1, const QByteArray& c2) {
347 const char* const* minor = 0;
348 QStringList names = fieldDifferences(c1, c2, &minor);
349 if (names.isEmpty() || !minor) {
350 return false;
351 }
352 for (const QString& name : names) {
353 bool found = false;
354 for (int ii = 0; minor[ii]; ii++) {
355 found = found || name == minor[ii];
356 }
357 if (!found) {
358 return false;
359 }
360 }
361 return true;
362}
363
364QString bncEphUser::differingFields(const QByteArray& c1, const QByteArray& c2) {
365 QStringList names = fieldDifferences(c1, c2, 0);
366 return names.isEmpty() ? QString("content") : names.join(" ");
367}
368
369// Satellite in the vote messages, for Galileo with the navigation message
370// (I/NAV and F/NAV data sets are buffered and voted separately)
371////////////////////////////////////////////////////////////////////////////
372static QString voteSat(const t_eph* eph) {
373 QString sat(QString(eph->prn().toInternalString().c_str()).left(3));
374 if (eph->type() == t_eph::INAV) sat += " INAV";
375 else if (eph->type() == t_eph::FNAV) sat += " FNAV";
376 return sat;
377}
378
379const double bncEphUser::VOTE_HOLD = 40.0; // SSR providers switch to a new IOD after 60 s
380const int bncEphUser::VOTE_AGREE = 3;
381
382// Current GPS time (time of the input data when replaying a file)
383////////////////////////////////////////////////////////////////////////////
384bncTime bncEphUser::currentTime() {
385 return bncTime(currentDateAndTimeGPS().toString(Qt::ISODate).toStdString());
386}
387
388// Release the data sets held back longer than VOTE_HOLD seconds
389////////////////////////////////////////////////////////////////////////////
390void bncEphUser::releaseHeld() {
391 bncTime now = currentTime();
392 bool changed = false;
393 QStringList msgs; // sorted: the QHash order differs between runs
394 QHashIterator<const t_eph*, t_votes*> it(_votes);
395 while (it.hasNext()) {
396 it.next();
397 t_votes* votes = it.value();
398 if (!votes->_released && now - votes->_first >= VOTE_HOLD) {
399 votes->_released = true;
400 changed = true;
401 const t_eph* eph = it.key();
402 QVector<int> count(votes->_content.size(), 0);
403 QMapIterator<QString, int> is(votes->_station);
404 while (is.hasNext()) {
405 is.next();
406 count[is.value()]++;
407 }
408 msgs << QString("ephemeris vote %1 %2 %3: copy of %4 uploaded after %5 s without %6 agreeing streams (%7 of %8 streams)")
409 .arg(voteSat(eph))
410 .arg(eph->TOC().datestr().c_str())
411 .arg(eph->TOC().timestr(0).c_str())
412 .arg(eph->receptStaID())
413 .arg(int(VOTE_HOLD))
414 .arg(VOTE_AGREE)
415 .arg(count[votes->_current])
416 .arg(votes->_station.size());
417 }
418 }
419 msgs.sort();
420 for (const QString& msg : msgs) {
421 BNC_CORE->message(msg.toLatin1(), false, "bce", t_logRecord::info);
422 }
423 if (changed) {
424 ephBufferChanged();
425 }
426}
427
428// Vote over the copies of a data set, check of the hold every second
429////////////////////////////////////////////////////////////////////////////
430void bncEphUser::enableVoting() {
431 _voting = true;
432 QTimer* timer = new QTimer(this);
433 connect(timer, SIGNAL(timeout()), this, SLOT(slotVoteTimer()));
434 timer->start(1000);
435}
436
437// Release of held data sets without new ephemerides; not when replaying a
438// file, where the time of the input data decides (reproducible results)
439////////////////////////////////////////////////////////////////////////////
440void bncEphUser::slotVoteTimer() {
441 if (BNC_CORE->dateAndTimeGPSSet()) {
442 return;
443 }
444 QMutexLocker locker(&_mutex);
445 releaseHeld();
446}
447
448// Data set to be uploaded
449////////////////////////////////////////////////////////////////////////////
450t_eph* bncEphUser::ephUpload(const QString& prn) {
451 if (!_eph.contains(prn)) {
452 return 0;
453 }
454 const deque<t_eph*>& qq = _eph[prn];
455 const t_votes* votes = _votes.value(qq.back(), 0);
456 if (votes && !votes->_released && qq.size() > 1) {
457 return qq[qq.size()-2];
458 }
459 return qq.back();
460}
461
462// Further copy of the buffered data set qq[index] received from a stream
463////////////////////////////////////////////////////////////////////////////
464void bncEphUser::vote(const QString& prn, unsigned index, const t_eph* eph) {
465 deque<t_eph*>& qq = _eph[prn];
466 t_votes* votes = _votes.value(qq[index], 0);
467 if (!votes) {
468 return;
469 }
470 QByteArray content = votingContent(eph);
471 int iVar = votes->_content.indexOf(content);
472 if (iVar < 0) {
473 iVar = votes->_content.size();
474 votes->_content.append(content);
475 votes->_eph.append(clone(eph));
476 }
477 votes->_station[eph->receptStaID()] = iVar;
478
479 QVector<int> count(votes->_content.size(), 0);
480 QMapIterator<QString, int> it(votes->_station);
481 while (it.hasNext()) {
482 it.next();
483 count[it.value()]++;
484 }
485 int iBest = votes->_current;
486 for (int ii = 0; ii < count.size(); ii++) {
487 if (count[ii] > count[iBest]) {
488 iBest = ii;
489 }
490 }
491 bool wasReleased = votes->_released;
492 bool released = false;
493 if (!votes->_released && count[iBest] >= VOTE_AGREE) {
494 votes->_released = true;
495 released = true;
496 }
497 // after the upload, a copy differing only in receiver-dependent fields
498 // replaces it no more: receivers keep the first copy of a data set anyway
499 if (iBest == votes->_current ||
500 (wasReleased && minorDifference(votes->_content[votes->_current], votes->_content[iBest]))) {
501 if (released) {
502 ephBufferChanged();
503 }
504 return;
505 }
506
507 t_eph* oldEph = qq[index];
508 t_eph* newEph = clone(votes->_eph[iBest]);
509 if (!radialDistanceOk(newEph)) {
510 delete newEph;
511 if (released) {
512 ephBufferChanged();
513 }
514 return;
515 }
516 if (newEph->isUnhealthy()) {
517 newEph->setCheckState(t_eph::unhealthy);
518 }
519 else if (oldEph->checkState() == t_eph::unhealthy) {
520 newEph->setCheckState(t_eph::ok);
521 }
522 else {
523 newEph->setCheckState(oldEph->checkState());
524 }
525
526 QStringList stations;
527 for (int ii = 0; ii < count.size(); ii++) {
528 QStringList hlp;
529 QMapIterator<QString, int> is(votes->_station);
530 while (is.hasNext()) {
531 is.next();
532 if (is.value() == ii) {
533 hlp << is.key();
534 }
535 }
536 if (!hlp.isEmpty()) {
537 stations << (ii == iBest ? "+ " : "- ") + hlp.join(" ");
538 }
539 }
540 BNC_CORE->message(QString("ephemeris vote %1 %2 %3: copy of %4 replaced by that of %5 (%6 of %7 streams; %8; differing: %9)")
541 .arg(voteSat(oldEph))
542 .arg(oldEph->TOC().datestr().c_str())
543 .arg(oldEph->TOC().timestr(0).c_str())
544 .arg(oldEph->receptStaID())
545 .arg(newEph->receptStaID())
546 .arg(count[iBest])
547 .arg(votes->_station.size())
548 .arg(stations.join(", "))
549 .arg(differingFields(votes->_content[votes->_current], votes->_content[iBest])).toLatin1(),
550 false, "bce", t_logRecord::info);
551
552 _votes.remove(oldEph);
553 delete oldEph;
554 qq[index] = newEph;
555 votes->_current = iBest;
556 _votes[newEph] = votes;
557 ephBufferChanged();
558}
559
560//
561////////////////////////////////////////////////////////////////////////////
562bool bncEphUser::radialDistanceOk(const t_eph* eph) {
563 ColumnVector xc(6);
564 ColumnVector vv(3);
565 if (eph->getCrd(eph->TOC(), xc, vv, false) != success) {
566 return false;
567 }
568 double rr = xc.Rows(1,3).NormFrobenius();
569
570 const double MINDIST = 2.e7;
571 const double MAXDIST = 6.e7;
572 if (rr < MINDIST || rr > MAXDIST || std::isnan(rr)) {
573 return false;
574 }
575 return true;
576}
577
578//
579////////////////////////////////////////////////////////////////////////////
580void bncEphUser::checkEphemeris(t_eph* eph, bool realTime) {
581
582 if (!eph) {
583 return;
584 }
585
586 // Check whether the epoch is too far away the current time
587 // --------------------------------------------------------
588 if (realTime) {
589 QDateTime now = currentDateAndTimeGPS();
590 bncTime currentTime(now.toString(Qt::ISODate).toStdString());
591 if (outDatedBcep(eph, currentTime)) {
592 eph->setCheckState(t_eph::outdated);
593 return;
594 }
595 }
596
597 // Check health status
598 // -------------------
599 if (eph->isUnhealthy()) {
600 eph->setCheckState(t_eph::unhealthy);
601 return;
602 }
603
604 // GPS/QZSS: IODE and IODC of the same upload
605 // -------------------------------------------
606 const t_ephGPS* ephGPS = dynamic_cast<const t_ephGPS*>(eph);
607 if (ephGPS && !ephGPS->iodeIodcConsistent()) {
608 eph->setCheckState(t_eph::bad);
609 return;
610 }
611
612 // Simple Check - check satellite radial distance
613 // ----------------------------------------------
614 if (!radialDistanceOk(eph)) {
615 eph->setCheckState(t_eph::bad);
616 return;
617 }
618 ColumnVector xc(6);
619 ColumnVector vv(3);
620 eph->getCrd(eph->TOC(), xc, vv, false);
621
622 // Check consistency with older ephemeris
623 // --------------------------------------
624 const double MAXDIFF_INIT = 10.0;
625 const double MINDIFF = 0.005;
626 QString prn = QString(eph->prn().toInternalString().c_str());
627 t_eph* ephL = ephLast(prn);
628
629 // maxdiff adapted to individual systems
630 char sys = eph->prn().system();
631 double MAXDIFF = MAXDIFF_INIT;
632 if (sys == 'C' ||
633 sys == 'S' ) {
634 MAXDIFF *= 10.0;
635 }
636
637 if (ephL && ephL->checkState() != t_eph::bad) {
638 // Update of ephemeris data sets after an outage: if the time since the
639 // last data set exceeds the system's update interval, the last one is
640 // outdated and a consistency check against it (extrapolated over the
641 // whole gap) is meaningless - it would reject the first new data set
642 // after every gap. The new data set stays unchecked and serves as
643 // reference for the next one.
644 // -----------------------------------------------------------------
645 double dt = eph->TOC() - ephL->TOC();
646
647 // update interval: 2h
648 if (eph->system() == t_eph::GPS && dt > 7200.0) {
649 ephL->setCheckState(t_eph::outdated);
650 return;
651 }
652 // update interval: 3h
653 else if (eph->system() == t_eph::Galileo && dt > 10800.0) {
654 ephL->setCheckState(t_eph::outdated);
655 return;
656 }
657 // updated every 30 minutes + 5 min
658 else if (eph->system() == t_eph::GLONASS && dt > 2100.0) {
659 ephL->setCheckState(t_eph::outdated);
660 return;
661 }
662 // updated every ?
663 else if (eph->system() == t_eph::QZSS && dt > 3600.0) {
664 ephL->setCheckState(t_eph::outdated);
665 return;
666 }
667 // maximum update interval: 300 sec
668 else if (eph->system() == t_eph::SBAS && dt > 600.0) {
669 ephL->setCheckState(t_eph::outdated);
670 return;
671 }
672 // updates 1h + 5 min
673 else if (eph->system() == t_eph::BDS && dt > 3900.0) {
674 ephL->setCheckState(t_eph::outdated);
675 return;
676 }
677 // update interval: up to 24 hours
678 else if (eph->system() == t_eph::NavIC && dt > 86400.0) {
679 ephL->setCheckState(t_eph::outdated);
680 return;
681 }
682
683 ColumnVector xcL(6);
684 ColumnVector vvL(3);
685 if (ephL->getCrd(eph->TOC(), xcL, vvL, false) != success) {
686 ephL->setCheckState(t_eph::bad);
687 return;
688 }
689
690 double diff = (xc.Rows(1,3) - xcL.Rows(1,3)).NormFrobenius();
691 double diffC = fabs(xc(4) - xcL(4)) * t_CST::c;
692
693 if (diff < MAXDIFF &&
694 diffC < MAXDIFF) {
695 if (diff < MINDIFF &&
696 diffC < MINDIFF &&
697 ephL->checkState() == t_eph::ok) {
698 // to prevent the same data sets with different TOC values
699 eph->setCheckState(t_eph::bad);
700 }
701 else {
702 eph->setCheckState(t_eph::ok);
703 ephL->setCheckState(t_eph::ok);
704 }
705 }
706 else {
707 if (ephL->checkState() == t_eph::ok) {
708 eph->setCheckState(t_eph::bad);
709 }
710 }
711 }
712}
713
Note: See TracBrowser for help on using the repository browser.