7#include "linearguider.h"
10#include "ekos_guide_debug.h"
12LinearGuider::LinearGuider(
const QString &
id) : m_ID(id)
17void LinearGuider::reset()
24 m_SumTimeOffset = 0.0;
26 m_LastGuideTime = QDateTime();
27 m_GuiderIteration = 0;
29double LinearGuider::guide(
double offset)
35 if (m_LastGuideTime.isValid())
37 constexpr int MAX_GUIDE_LAG = 30;
38 const int interval = m_LastGuideTime.secsTo(now);
39 if (interval < 0 || interval > MAX_GUIDE_LAG)
42 comment = QString(
"Reset: guide lag %1s").
arg(interval);
47 m_LastGuideTime = now;
49 const double time = ++m_GuiderIteration;
50 const Sample s(time, offset);
51 double guideVal = m_Gain * offset;
54 const int size = m_Samples.size();
58 constexpr double MIN_BAD_OFFSET = 2.0;
59 if (fabs(offset) > std::min(MIN_BAD_OFFSET, 4 * m_MinMove))
61 comment = QString(
"%1Reset: offset > 4*minMove %2").
arg(!comment.
isEmpty() ?
", " :
"").
arg(m_MinMove, 0,
'f', 2);
66 const double slope = getSlope();
67 if (std::isfinite(slope))
69 guideVal = slope * size * m_Gain;
71 if (guideVal * offset < 0)
74 comment = QString(
"%1slope %2 opposite to offset").
arg(!comment.
isEmpty() ?
", " :
"").
arg(slope, 0,
'f', 2);
77 comment = QString(
"%1slope %2").
arg(!comment.
isEmpty() ?
", " :
"").
arg(slope, 0,
'f', 2);
80 comment = QString(
"%1bad slope").
arg(!comment.
isEmpty() ?
", " :
"");
84 comment.
append(QString(
"%1Starting").arg(!comment.
isEmpty() ?
", " :
""));
86 if (fabs(guideVal) > fabs(offset))
88 comment.
append(QString(
"%1Guideval %2 > offset").arg(!comment.
isEmpty() ?
", " :
"").arg(guideVal, 0,
'f', 2));
89 guideVal = m_Gain * offset;
91 if (m_NumRejects >= 3)
93 comment.
append(QString(
"%1Reset: rejects %2").arg(!comment.
isEmpty() ?
", " :
"").arg(m_NumRejects));
100 if (fabs(guideVal) > 0 && fabs(guideVal) < m_MinMove)
103 comment.
append(QString(
"%1%2 < minMove %3").arg(!comment.
isEmpty() ?
", " :
"")
104 .arg(guideVal, 0,
'f', 2).arg(m_MinMove, 0,
'f', 2));
108 qCDebug(KSTARS_EKOS_GUIDE) << QString(
"LinearGuide(%1,%2) %3 * %4 --> %5: %6")
109 .
arg(m_ID, 3).
arg(time, 3,
'f', 0).
arg(offset, 6,
'f', 2).
arg(m_Gain, 4,
'f', 2)
110 .
arg(guideVal, 5,
'f', 2).
arg(comment);
114void LinearGuider::addSample(
const Sample &sample)
116 m_Samples.enqueue(sample);
118 if (m_Samples.size() > m_Length)
122void LinearGuider::updateStats(
const Sample &sample)
124 m_SumTime += sample.time;
125 m_SumOffset += sample.offset;
126 m_SumTimeSq += sample.time * sample.time;
127 m_OffsetSq += sample.offset * sample.offset;
128 m_SumTimeOffset += sample.time * sample.offset;
131void LinearGuider::removeLastSample()
133 if (m_Samples.size() <= 0)
135 const Sample &sample = m_Samples.head();
136 m_SumTime -= sample.time;
137 m_SumOffset -= sample.offset;
138 m_SumTimeSq -= sample.time * sample.time;
139 m_OffsetSq -= sample.offset * sample.offset;
140 m_SumTimeOffset -= sample.time * sample.offset;
144double LinearGuider::getSlope()
146 const double sumTime = m_SumTime;
147 const double denom = (m_Samples.size() * m_SumTimeSq) - (sumTime * sumTime);
148 if (denom == 0)
return 0;
149 const double slope = ((m_Samples.size() * m_SumTimeOffset) - (sumTime * m_SumOffset)) / denom;
QDateTime currentDateTime()
QString & append(QChar ch)
QString arg(Args &&... args) const const
bool isEmpty() const const