Changeset 11248 in ntrip for trunk/BNC/src/bncephuser.cpp


Ignore:
Timestamp:
Dec 3, 2026, 10:30:57 PM (less than one hour ago)
Author:
stuerze
Message:

updates regarding merging of eph data sets

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BNC/src/bncephuser.cpp

    r11218 r11248  
    5050// Constructor
    5151////////////////////////////////////////////////////////////////////////////
    52 bncEphUser::bncEphUser(bool connectSlots, unsigned maxQueueSize) : _maxQueueSize(maxQueueSize), _voting(false) {
     52bncEphUser::bncEphUser(bool connectSlots, unsigned maxQueueSize) : _maxQueueSize(maxQueueSize), _voting(false),
     53                                                                     _logMajority(!connectSlots) {
    5354  if (connectSlots) {
    5455    connect(BNC_CORE, SIGNAL(newGPSEph(t_ephGPS)),
    … …  
    9293  }
    9394  qDeleteAll(_votes);
     95  for (unsigned ii = 0; ii < _removedEph.size(); ii++) {
     96    delete _removedEph[ii];
     97  }
     98  QMapIterator<QString, QMap<QString, t_streamEph> > itSat(_streamEph);
     99  while (itSat.hasNext()) {
     100    itSat.next();
     101    QMapIterator<QString, t_streamEph> itStream(itSat.value());
     102    while (itStream.hasNext()) {
     103      delete itStream.next().value()._eph;
     104    }
     105  }
    94106}
    95107
    … …  
    159171
    160172  QString prn(newEph->prn().toInternalString().c_str());
     173
     174  // majority check of the streams (real-time input only)
     175  const bool majority = realTime && !eph->receptStaID().isEmpty();
    161176
    162177  // data set received before, e.g. from a slower stream after a newer one
    … …  
    169184          vote(prn, ii, eph);
    170185        }
     186        if (majority) {
     187          t_eph* copy = clone(eph);
     188          if (basicChecks(copy, realTime) || copy->checkState() == t_eph::unhealthy) {
     189            if (updateStreamEph(prn, copy)) {
     190              removeOutvotedLast(prn);
     191            }
     192          }
     193          delete copy;
     194        }
    171195        return failure;
    172196      }
     197    }
     198  }
     199
     200  // checks of the data set itself
     201  if (!basicChecks(eph, realTime)) {
     202    if (eph->checkState() == t_eph::unhealthy) {
     203      if (majority) {
     204        updateStreamEph(prn, eph);  // the stream abstains
     205      }
     206    }
     207    else {
     208      delete newEph;
     209      return failure;
     210    }
     211  }
     212
     213  // majority of the streams
     214  bool majorityOk = false;
     215  if (majority && eph->checkState() != t_eph::unhealthy) {
     216    bool contradictsLast = updateStreamEph(prn, eph);
     217    int numFor = 0;
     218    int numAgainst = 0;
     219    QStringList against;
     220    majorityCount(prn, eph, numFor, numAgainst, &against);
     221    if (numAgainst > numFor) {
     222      if (_logMajority) BNC_CORE->message(QString("ephemeris majority %1 %2 %3 from %4: inconsistent with %5 of %6 streams (%7), rejected")
     223                        .arg(eph->prn().toString().c_str())
     224                        .arg(eph->TOC().datestr().c_str())
     225                        .arg(eph->TOC().timestr(0).c_str())
     226                        .arg(eph->receptStaID())
     227                        .arg(numAgainst).arg(numFor + numAgainst)
     228                        .arg(against.join(" ")).toLatin1(),
     229                        false, "bce", t_logRecord::warning);
     230      eph->setCheckState(t_eph::bad);
     231      delete newEph;
     232      return failure;
     233    }
     234    majorityOk = (numFor >= 2);
     235    if (contradictsLast) {
     236      removeOutvotedLast(prn);
    173237    }
    174238  }
    … …  
    183247
    184248  if ( (ephOld == 0) || eph->isNewerThan(ephOld)) {
    185     checkEphemeris(eph, realTime);
     249    if (eph->checkState() != t_eph::unhealthy) {
     250      checkEphemeris(eph, majorityOk);
     251    }
    186252  }
    187253  else {
    … …  
    558624}
    559625
     626const double bncEphUser::MAJ_WINDOW  = 7200.0;
     627const double bncEphUser::MAJ_MAXDIFF = 1000.0;
     628
     629// Consistency of two data sets of a satellite (majority check)
     630////////////////////////////////////////////////////////////////////////////
     631bncEphUser::e_consistency bncEphUser::compareDataSets(const t_eph* eph, const t_eph* other) {
     632  if (!eph || !other || eph->prn().system() != other->prn().system() ||
     633      eph->prn().number() != other->prn().number()) {
     634    return notComparable;
     635  }
     636  if (eph->system() == t_eph::SBAS || eph->system() == t_eph::NavIC) {
     637    return notComparable;
     638  }
     639  if (std::fabs(other->TOC() - eph->TOC()) > MAJ_WINDOW) {
     640    return notComparable;
     641  }
     642  if (eph->isUnhealthy() || other->isUnhealthy()) {
     643    return notComparable;
     644  }
     645  ColumnVector x1(6), v1(3), x2(6), v2(3);
     646  if (eph  ->getCrd(eph->TOC(), x1, v1, false) != success ||
     647      other->getCrd(eph->TOC(), x2, v2, false) != success) {
     648    return notComparable;
     649  }
     650  double dPos = (x1.Rows(1,3) - x2.Rows(1,3)).NormFrobenius();
     651  double dClk = std::fabs(x1(4) - x2(4)) * t_CST::c;
     652  if (std::isnan(dPos) || std::isnan(dClk)) {
     653    return notComparable;
     654  }
     655  return (dPos > MAJ_MAXDIFF || dClk > MAJ_MAXDIFF) ? inconsistent : consistent;
     656}
     657
    560658//
    561659////////////////////////////////////////////////////////////////////////////
    … …  
    578676//
    579677////////////////////////////////////////////////////////////////////////////
    580 void bncEphUser::checkEphemeris(t_eph* eph, bool realTime) {
     678bool bncEphUser::basicChecks(t_eph* eph, bool realTime) {
    581679
    582680  if (!eph) {
    583     return;
     681    return false;
    584682  }
    585683
    … …  
    591689    if (outDatedBcep(eph, currentTime)) {
    592690      eph->setCheckState(t_eph::outdated);
    593       return;
     691      return false;
    594692    }
    595693  }
    … …  
    599697  if (eph->isUnhealthy()) {
    600698    eph->setCheckState(t_eph::unhealthy);
    601     return;
     699    return false;
    602700  }
    603701
    … …  
    607705  if (ephGPS && !ephGPS->iodeIodcConsistent()) {
    608706    eph->setCheckState(t_eph::bad);
    609     return;
     707    return false;
     708  }
     709
     710  // GPS LNAV: signature of non-genuine data sets
     711  // --------------------------------------------
     712  if (ephGPS && ephGPS->nineZeroPerturbations()) {
     713    eph->setCheckState(t_eph::bad);
     714    return false;
    610715  }
    611716
    … …  
    614719  if (!radialDistanceOk(eph)) {
    615720    eph->setCheckState(t_eph::bad);
     721    return false;
     722  }
     723  return true;
     724}
     725
     726// Consistency with the last data set of the satellite; with majorityOk
     727// (at least 2 streams and more streams consistent with the data set than
     728// inconsistent) an inconsistency with the last data set does not reject it
     729////////////////////////////////////////////////////////////////////////////
     730void bncEphUser::checkEphemeris(t_eph* eph, bool majorityOk) {
     731
     732  if (!eph) {
    616733    return;
    617734  }
    … …  
    705822    }
    706823    else {
    707       if (ephL->checkState() == t_eph::ok) {
     824      if (majorityOk) {
     825        eph->setCheckState(t_eph::ok);
     826      }
     827      else if (ephL->checkState() == t_eph::ok) {
    708828        eph->setCheckState(t_eph::bad);
    709829      }
    710830    }
    711831  }
    712 }
    713 
     832  if (majorityOk && eph->checkState() == t_eph::unchecked) {
     833    eph->setCheckState(t_eph::ok);
     834  }
     835}
     836
     837// Latest data set of a stream for a satellite (majority check); true if
     838// it contradicts the last data set of the satellite (only then the removal
     839// of the last one has to be checked: the votes of the other streams did not
     840// change)
     841////////////////////////////////////////////////////////////////////////////
     842bool bncEphUser::updateStreamEph(const QString& prn, const t_eph* eph) {
     843  QMap<QString, t_streamEph>& streams = _streamEph[prn];
     844  if (streams.contains(eph->receptStaID())) {
     845    delete streams[eph->receptStaID()]._eph;
     846  }
     847  t_streamEph& streamEph = streams[eph->receptStaID()];
     848  streamEph._eph = clone(eph);
     849  streamEph._key = dataSetKey(eph);
     850  const t_eph* last = ephLast(prn);
     851  return last && compareDataSets(last, eph) == inconsistent;
     852}
     853
     854// Identification of identical data sets: TOC, IOD and type, satellite
     855// position and clock at TOC (mm, ps)
     856////////////////////////////////////////////////////////////////////////////
     857QByteArray bncEphUser::dataSetKey(const t_eph* eph) {
     858  ColumnVector xc(6), vv(3);
     859  if (eph->isUnhealthy() || eph->getCrd(eph->TOC(), xc, vv, false) != success) {
     860    return QByteArray();
     861  }
     862  return QString("%1 %2 %3 %4 %5 %6 %7 %8")
     863           .arg(std::string(eph->TOC()).c_str()).arg(eph->IOD()).arg(int(eph->type()))
     864           .arg(qRound64(xc(1) * 1000.0)).arg(qRound64(xc(2) * 1000.0)).arg(qRound64(xc(3) * 1000.0))
     865           .arg(qRound64(xc(4) * 1.e12)).arg(eph->isUnhealthy()).toLatin1();
     866}
     867
     868// Votes of the streams on a data set: consistent or inconsistent latest
     869// data set of each stream (not comparable ones abstain); identical data sets
     870// of several streams are compared once
     871////////////////////////////////////////////////////////////////////////////
     872void bncEphUser::majorityCount(const QString& prn, const t_eph* eph, int& numFor,
     873                               int& numAgainst, QStringList* against) const {
     874  numFor     = 0;
     875  numAgainst = 0;
     876  QHash<QByteArray, e_consistency> known;
     877  QMapIterator<QString, t_streamEph> it(_streamEph.value(prn));
     878  while (it.hasNext()) {
     879    it.next();
     880    const t_streamEph& streamEph = it.value();
     881    e_consistency cons;
     882    if (!streamEph._key.isEmpty() && known.contains(streamEph._key)) {
     883      cons = known.value(streamEph._key);
     884    }
     885    else {
     886      cons = compareDataSets(eph, streamEph._eph);
     887      if (!streamEph._key.isEmpty()) {
     888        known.insert(streamEph._key, cons);
     889      }
     890    }
     891    if (cons == consistent) {
     892      ++numFor;
     893    }
     894    else if (cons == inconsistent) {
     895      ++numAgainst;
     896      if (against) {
     897        *against << it.key();
     898      }
     899    }
     900  }
     901}
     902
     903// Removal of the last data set of a satellite if the majority of the
     904// streams (at least 2) contradicts it, e.g. a non-genuine first data set
     905// after the start
     906////////////////////////////////////////////////////////////////////////////
     907void bncEphUser::removeOutvotedLast(const QString& prn) {
     908  while (_eph.contains(prn) && !_eph[prn].empty()) {
     909    deque<t_eph*>& qq = _eph[prn];
     910    t_eph* last = qq.back();
     911    int numFor = 0;
     912    int numAgainst = 0;
     913    QStringList against;
     914    majorityCount(prn, last, numFor, numAgainst, &against);
     915    if (numAgainst < 2 || numAgainst <= numFor) {
     916      return;
     917    }
     918    if (_logMajority) BNC_CORE->message(QString("ephemeris majority %1 %2 %3 from %4: inconsistent with %5 of %6 streams (%7), removed")
     919                      .arg(last->prn().toString().c_str())
     920                      .arg(last->TOC().datestr().c_str())
     921                      .arg(last->TOC().timestr(0).c_str())
     922                      .arg(last->receptStaID())
     923                      .arg(numAgainst).arg(numFor + numAgainst)
     924                      .arg(against.join(" ")).toLatin1(),
     925                      false, "bce", t_logRecord::warning);
     926    delete _votes.take(last);
     927    // not deleted at once: users may still hold the pointer (e.g. the
     928    // combination for the corrections of the current epoch)
     929    last->setCheckState(t_eph::bad);
     930    _removedEph.push_back(last);
     931    if (_removedEph.size() > 100) {
     932      delete _removedEph.front();
     933      _removedEph.pop_front();
     934    }
     935    qq.pop_back();
     936    if (qq.empty()) {
     937      _eph.remove(prn);
     938    }
     939    ephBufferChanged();
     940  }
     941}
     942
Note: See TracChangeset for help on using the changeset viewer.