1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-31 16:52:10 +01:00

OP-1554 Re-factoring

This commit is contained in:
m_thread 2014-10-28 13:44:58 +01:00
parent 77e876297e
commit 482f8e3b29
2 changed files with 41 additions and 66 deletions

View File

@ -68,15 +68,9 @@ void PlotData::updatePlotCurveData()
m_plotCurve->setSamples(m_xDataEntries, m_yDataEntries); m_plotCurve->setSamples(m_xDataEntries, m_yDataEntries);
} }
bool SequentialPlotData::append(UAVObject *obj) void PlotData::calcMathFunction(double currentValue)
{ {
if (m_object == obj) { // Put the new value at the back
if (m_field) {
double currentValue = valueAsDouble(m_object, m_field) * pow(10, m_scalePower);
// Perform scope math, if necessary
if (m_mathFunction == "Boxcar average" || m_mathFunction == "Standard deviation") {
// Put the new value at the front
m_yDataHistory.append(currentValue); m_yDataHistory.append(currentValue);
// calculate average value // calculate average value
@ -85,7 +79,6 @@ bool SequentialPlotData::append(UAVObject *obj)
m_meanSum -= m_yDataHistory.first(); m_meanSum -= m_yDataHistory.first();
m_yDataHistory.pop_front(); m_yDataHistory.pop_front();
} }
// make sure to correct the sum every meanSamples steps to prevent it // make sure to correct the sum every meanSamples steps to prevent it
// from running away due to floating point rounding errors // from running away due to floating point rounding errors
m_correctionSum += currentValue; m_correctionSum += currentValue;
@ -96,7 +89,6 @@ bool SequentialPlotData::append(UAVObject *obj)
} }
double boxcarAvg = m_meanSum / m_yDataHistory.size(); double boxcarAvg = m_meanSum / m_yDataHistory.size();
if (m_mathFunction == "Standard deviation") { if (m_mathFunction == "Standard deviation") {
// Calculate square of sample standard deviation, with Bessel's correction // Calculate square of sample standard deviation, with Bessel's correction
double stdSum = 0; double stdSum = 0;
@ -107,13 +99,26 @@ bool SequentialPlotData::append(UAVObject *obj)
} else { } else {
m_yDataEntries.append(boxcarAvg); m_yDataEntries.append(boxcarAvg);
} }
}
bool SequentialPlotData::append(UAVObject *obj)
{
if (m_object == obj) {
if (m_field) {
double currentValue = valueAsDouble(m_object, m_field) * pow(10, m_scalePower);
// Perform scope math, if necessary
if (m_mathFunction == "Boxcar average" || m_mathFunction == "Standard deviation") {
calcMathFunction(currentValue);
} else { } else {
m_yDataEntries.append(currentValue); m_yDataEntries.append(currentValue);
} }
if (m_yDataEntries.size() > m_plotDataSize) { // If new data overflows the window, remove old data... if (m_yDataEntries.size() > m_plotDataSize) {
// If new data overflows the window, remove old data...
m_yDataEntries.pop_front(); m_yDataEntries.pop_front();
} else { // ...otherwise, add a new y point at position xData } else {
// ...otherwise, add a new y point at position xData
m_xDataEntries.insert(m_xDataEntries.size(), m_xDataEntries.size()); m_xDataEntries.insert(m_xDataEntries.size(), m_xDataEntries.size());
} }
@ -134,36 +139,7 @@ bool ChronoPlotData::append(UAVObject *obj)
// Perform scope math, if necessary // Perform scope math, if necessary
if (m_mathFunction == "Boxcar average" || m_mathFunction == "Standard deviation") { if (m_mathFunction == "Boxcar average" || m_mathFunction == "Standard deviation") {
// Put the new value at the back calcMathFunction(currentValue);
m_yDataHistory.append(currentValue);
// calculate average value
m_meanSum += currentValue;
if (m_yDataHistory.size() > m_meanSamples) {
m_meanSum -= m_yDataHistory.first();
m_yDataHistory.pop_front();
}
// make sure to correct the sum every meanSamples steps to prevent it
// from running away due to floating point rounding errors
m_correctionSum += currentValue;
if (++m_correctionCount >= m_meanSamples) {
m_meanSum = m_correctionSum;
m_correctionSum = 0.0f;
m_correctionCount = 0;
}
double boxcarAvg = m_meanSum / m_yDataHistory.size();
// qDebug()<<mathFunction;
if (m_mathFunction == "Standard deviation") {
// Calculate square of sample standard deviation, with Bessel's correction
double stdSum = 0;
for (int i = 0; i < m_yDataHistory.size(); i++) {
stdSum += pow(m_yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1);
}
m_yDataEntries.append(sqrt(stdSum));
} else {
m_yDataEntries.append(boxcarAvg);
}
} else { } else {
m_yDataEntries.append(currentValue); m_yDataEntries.append(currentValue);
} }
@ -171,8 +147,6 @@ bool ChronoPlotData::append(UAVObject *obj)
double valueX = NOW.toTime_t() + NOW.time().msec() / 1000.0; double valueX = NOW.toTime_t() + NOW.time().msec() / 1000.0;
m_xDataEntries.append(valueX); m_xDataEntries.append(valueX);
// qDebug() << "Data " << uavObject << "." << field->getName() << " X,Y:" << valueX << "," << valueY;
// Remove stale data // Remove stale data
removeStaleData(); removeStaleData();
return true; return true;

View File

@ -97,11 +97,12 @@ protected:
int m_element; int m_element;
QString m_elementName; QString m_elementName;
virtual void calcMathFunction(double currentValue);
private: private:
double m_yMin; double m_yMin;
double m_yMax; double m_yMax;
QwtPlotCurve *m_plotCurve; QwtPlotCurve *m_plotCurve;
}; };
/*! /*!