1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-06 21:54:15 +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, PlotData::PlotData(QString objectName, QString fieldName, QString elementName,
QwtPlotCurve *plotCurve, int scaleOrderFactor, int meanSamples, QwtPlotCurve *plotCurve, int scaleOrderFactor, int meanSamples,
QString mathFunction, double plotDataSize) : QString mathFunction, double plotDataSize) :
m_objectName(objectName), m_fieldName(fieldName), m_elementName(elementName), m_scalePower(scaleOrderFactor), m_meanSamples(meanSamples),
m_plotCurve(plotCurve), m_scalePower(scaleOrderFactor), m_meanSamples(meanSamples), m_mathFunction(mathFunction), m_plotDataSize(plotDataSize),
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_meanSum = 0.0f;
m_correctionSum = 0.0f; m_correctionSum = 0.0f;
@ -71,7 +73,7 @@ PlotData::~PlotData()
void PlotData::updatePlotCurveData() void PlotData::updatePlotCurveData()
{ {
m_plotCurve->setSamples(xData, yData); m_plotCurve->setSamples(m_xDataEntries, m_yDataEntries);
} }
bool SequentialPlotData::append(UAVObject *obj) bool SequentialPlotData::append(UAVObject *obj)
@ -86,13 +88,13 @@ bool SequentialPlotData::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 front // Put the new value at the front
yDataHistory.append(currentValue); m_yDataHistory.append(currentValue);
// calculate average value // calculate average value
m_meanSum += currentValue; m_meanSum += currentValue;
if (yDataHistory.size() > m_meanSamples) { if (m_yDataHistory.size() > m_meanSamples) {
m_meanSum -= yDataHistory.first(); m_meanSum -= m_yDataHistory.first();
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
@ -104,26 +106,26 @@ bool SequentialPlotData::append(UAVObject *obj)
m_correctionCount = 0; m_correctionCount = 0;
} }
double boxcarAvg = m_meanSum / 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;
for (int i = 0; i < yDataHistory.size(); i++) { for (int i = 0; i < m_yDataHistory.size(); i++) {
stdSum += pow(yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1); stdSum += pow(m_yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1);
} }
yData.append(sqrt(stdSum)); m_yDataEntries.append(sqrt(stdSum));
} else { } else {
yData.append(boxcarAvg); m_yDataEntries.append(boxcarAvg);
} }
} else { } else {
yData.append(currentValue); m_yDataEntries.append(currentValue);
} }
if (yData.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...
yData.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
xData.insert(xData.size(), xData.size()); m_xDataEntries.insert(m_xDataEntries.size(), m_xDataEntries.size());
} }
return true; return true;
@ -147,13 +149,13 @@ 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 // Put the new value at the back
yDataHistory.append(currentValue); m_yDataHistory.append(currentValue);
// calculate average value // calculate average value
m_meanSum += currentValue; m_meanSum += currentValue;
if (yDataHistory.size() > m_meanSamples) { if (m_yDataHistory.size() > m_meanSamples) {
m_meanSum -= yDataHistory.first(); m_meanSum -= m_yDataHistory.first();
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
@ -164,24 +166,24 @@ bool ChronoPlotData::append(UAVObject *obj)
m_correctionCount = 0; m_correctionCount = 0;
} }
double boxcarAvg = m_meanSum / yDataHistory.size(); double boxcarAvg = m_meanSum / m_yDataHistory.size();
// qDebug()<<mathFunction; // qDebug()<<mathFunction;
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;
for (int i = 0; i < yDataHistory.size(); i++) { for (int i = 0; i < m_yDataHistory.size(); i++) {
stdSum += pow(yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1); stdSum += pow(m_yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1);
} }
yData.append(sqrt(stdSum)); m_yDataEntries.append(sqrt(stdSum));
} else { } else {
yData.append(boxcarAvg); m_yDataEntries.append(boxcarAvg);
} }
} else { } else {
yData.append(currentValue); m_yDataEntries.append(currentValue);
} }
double valueX = NOW.toTime_t() + NOW.time().msec() / 1000.0; 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; // qDebug() << "Data " << uavObject << "." << field->getName() << " X,Y:" << valueX << "," << valueY;
@ -200,16 +202,16 @@ void ChronoPlotData::removeStaleData()
double oldestValue; double oldestValue;
while (1) { while (1) {
if (xData.size() == 0) { if (m_xDataEntries.size() == 0) {
break; break;
} }
newestValue = xData.last(); newestValue = m_xDataEntries.last();
oldestValue = xData.first(); oldestValue = m_xDataEntries.first();
if (newestValue - oldestValue > m_plotDataSize) { if (newestValue - oldestValue > m_plotDataSize) {
yData.pop_front(); m_yDataEntries.pop_front();
xData.pop_front(); m_xDataEntries.pop_front();
} else { } else {
break; break;
} }

View File

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

View File

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