1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-19 04:52:12 +01:00

OP-1554 More re-factoring.

This commit is contained in:
m_thread 2014-10-28 12:19:54 +01:00
parent 4fc3bc4d3f
commit 13881dee59
3 changed files with 48 additions and 49 deletions

View File

@ -33,11 +33,13 @@
PlotData::PlotData(QString objectName, QString fieldName, QString elementName,
QwtPlotCurve *plotCurve, int scaleOrderFactor, int meanSamples,
QString mathFunction, double plotDataSize) :
m_objectName(objectName), m_fieldName(fieldName), m_elementName(elementName),
m_plotCurve(plotCurve), m_scalePower(scaleOrderFactor), m_meanSamples(meanSamples),
m_mathFunction(mathFunction), m_plotDataSize(plotDataSize)
m_scalePower(scaleOrderFactor), m_meanSamples(meanSamples),
m_mathFunction(mathFunction), m_plotDataSize(plotDataSize),
m_objectName(objectName), m_fieldName(fieldName),
m_elementName(elementName), m_plotCurve(plotCurve)
{
m_plotCurve->setSamples(xData, yData);
m_plotCurve->setSamples(m_xDataEntries, m_yDataEntries);
m_meanSum = 0.0f;
m_correctionSum = 0.0f;
@ -71,7 +73,7 @@ PlotData::~PlotData()
void PlotData::updatePlotCurveData()
{
m_plotCurve->setSamples(xData, yData);
m_plotCurve->setSamples(m_xDataEntries, m_yDataEntries);
}
bool SequentialPlotData::append(UAVObject *obj)
@ -86,13 +88,13 @@ bool SequentialPlotData::append(UAVObject *obj)
// Perform scope math, if necessary
if (m_mathFunction == "Boxcar average" || m_mathFunction == "Standard deviation") {
// Put the new value at the front
yDataHistory.append(currentValue);
m_yDataHistory.append(currentValue);
// calculate average value
m_meanSum += currentValue;
if (yDataHistory.size() > m_meanSamples) {
m_meanSum -= yDataHistory.first();
yDataHistory.pop_front();
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
@ -104,26 +106,26 @@ bool SequentialPlotData::append(UAVObject *obj)
m_correctionCount = 0;
}
double boxcarAvg = m_meanSum / yDataHistory.size();
double boxcarAvg = m_meanSum / m_yDataHistory.size();
if (m_mathFunction == "Standard deviation") {
// Calculate square of sample standard deviation, with Bessel's correction
double stdSum = 0;
for (int i = 0; i < yDataHistory.size(); i++) {
stdSum += pow(yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1);
for (int i = 0; i < m_yDataHistory.size(); i++) {
stdSum += pow(m_yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1);
}
yData.append(sqrt(stdSum));
m_yDataEntries.append(sqrt(stdSum));
} else {
yData.append(boxcarAvg);
m_yDataEntries.append(boxcarAvg);
}
} else {
yData.append(currentValue);
m_yDataEntries.append(currentValue);
}
if (yData.size() > m_plotDataSize) { // If new data overflows the window, remove old data...
yData.pop_front();
if (m_yDataEntries.size() > m_plotDataSize) { // If new data overflows the window, remove old data...
m_yDataEntries.pop_front();
} else { // ...otherwise, add a new y point at position xData
xData.insert(xData.size(), xData.size());
m_xDataEntries.insert(m_xDataEntries.size(), m_xDataEntries.size());
}
return true;
@ -147,13 +149,13 @@ bool ChronoPlotData::append(UAVObject *obj)
// Perform scope math, if necessary
if (m_mathFunction == "Boxcar average" || m_mathFunction == "Standard deviation") {
// Put the new value at the back
yDataHistory.append(currentValue);
m_yDataHistory.append(currentValue);
// calculate average value
m_meanSum += currentValue;
if (yDataHistory.size() > m_meanSamples) {
m_meanSum -= yDataHistory.first();
yDataHistory.pop_front();
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
@ -164,24 +166,24 @@ bool ChronoPlotData::append(UAVObject *obj)
m_correctionCount = 0;
}
double boxcarAvg = m_meanSum / yDataHistory.size();
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 < yDataHistory.size(); i++) {
stdSum += pow(yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1);
for (int i = 0; i < m_yDataHistory.size(); i++) {
stdSum += pow(m_yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1);
}
yData.append(sqrt(stdSum));
m_yDataEntries.append(sqrt(stdSum));
} else {
yData.append(boxcarAvg);
m_yDataEntries.append(boxcarAvg);
}
} else {
yData.append(currentValue);
m_yDataEntries.append(currentValue);
}
double valueX = NOW.toTime_t() + NOW.time().msec() / 1000.0;
xData.append(valueX);
m_xDataEntries.append(valueX);
// qDebug() << "Data " << uavObject << "." << field->getName() << " X,Y:" << valueX << "," << valueY;
@ -200,16 +202,16 @@ void ChronoPlotData::removeStaleData()
double oldestValue;
while (1) {
if (xData.size() == 0) {
if (m_xDataEntries.size() == 0) {
break;
}
newestValue = xData.last();
oldestValue = xData.first();
newestValue = m_xDataEntries.last();
oldestValue = m_xDataEntries.first();
if (newestValue - oldestValue > m_plotDataSize) {
yData.pop_front();
xData.pop_front();
m_yDataEntries.pop_front();
m_xDataEntries.pop_front();
} else {
break;
}

View File

@ -57,10 +57,6 @@ public:
QString mathFunction, double plotDataSize);
~PlotData();
QVector<double> xData;
QVector<double> yData;
QVector<double> yDataHistory;
QString objectName() const { return m_objectName; }
QString fieldName() const { return m_fieldName; }
QString elementName() const { return m_elementName; }
@ -76,6 +72,9 @@ public:
void updatePlotCurveData();
bool hasData() { return !m_xDataEntries.isEmpty(); }
double lastData() { return m_yDataEntries.last(); }
protected:
double valueAsDouble(UAVObject *obj, UAVObjectField *field);
@ -87,6 +86,10 @@ protected:
int m_correctionCount;
double m_plotDataSize;
QVector<double> m_xDataEntries;
QVector<double> m_yDataEntries;
QVector<double> m_yDataHistory;
private:
QString m_objectName;
QString m_fieldName;

View File

@ -625,7 +625,7 @@ int ScopeGadgetWidget::csvLoggingAddData()
if (!m_csvLoggingStarted) {
return -1;
}
m_csvLoggingDataValid = 0;
m_csvLoggingDataValid = false;
QDateTime NOW = QDateTime::currentDateTime();
QString tempString;
@ -638,19 +638,13 @@ int ScopeGadgetWidget::csvLoggingAddData()
ss << (NOW.toTime_t() - m_csvLoggingStartTime.toTime_t());
#endif
ss << ", " << m_csvLoggingConnected << ", " << m_csvLoggingDataUpdated;
m_csvLoggingDataUpdated = 0;
m_csvLoggingDataUpdated = false;
foreach(PlotData * plotData2, m_curvesData.values()) {
ss << ", ";
if (plotData2->xData.isEmpty()) {
ss << ", ";
if (plotData2->xData.isEmpty()) {} else {
ss << QString().sprintf("%3.10g", plotData2->yData.last());
m_csvLoggingDataValid = 1;
}
} else {
ss << QString().sprintf("%3.10g", plotData2->yData.last());
m_csvLoggingDataValid = 1;
if (plotData2->hasData()) {
ss << QString().sprintf("%3.10g", plotData2->lastData());
m_csvLoggingDataValid = true;
}
}
ss << endl;