2013-04-05 23:46:56 +03:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file plotdata.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
|
|
|
* @addtogroup ScopePlugin Scope Gadget Plugin
|
|
|
|
* @{
|
|
|
|
* @brief The scope Gadget, graphically plots the states of UAVObjects
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "plotdata.h"
|
|
|
|
#include <math.h>
|
|
|
|
#include <QDebug>
|
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
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_plotCurve->setSamples(xData, yData);
|
|
|
|
|
|
|
|
m_meanSum = 0.0f;
|
|
|
|
m_correctionSum = 0.0f;
|
|
|
|
m_correctionCount = 0;
|
|
|
|
m_yMin = 0;
|
|
|
|
m_yMax = 0;
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
double PlotData::valueAsDouble(UAVObject *obj, UAVObjectField *field)
|
2013-04-05 23:46:56 +03:00
|
|
|
{
|
|
|
|
Q_UNUSED(obj);
|
|
|
|
QVariant value;
|
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
if (!m_elementName.isEmpty()) {
|
|
|
|
int indexOfSubField = field->getElementNames().indexOf(QRegExp(elementName(), Qt::CaseSensitive, QRegExp::FixedString));
|
2013-04-05 23:46:56 +03:00
|
|
|
value = field->getValue(indexOfSubField);
|
2013-05-19 17:37:30 +03:00
|
|
|
} else {
|
2013-04-05 23:46:56 +03:00
|
|
|
value = field->getValue();
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2013-04-05 23:46:56 +03:00
|
|
|
|
|
|
|
// qDebug() << "Data (" << value.typeName() << ") " << value.toString();
|
|
|
|
|
|
|
|
return value.toDouble();
|
|
|
|
}
|
|
|
|
|
|
|
|
PlotData::~PlotData()
|
|
|
|
{
|
2014-10-22 00:52:39 +02:00
|
|
|
m_plotCurve->detach();
|
|
|
|
delete m_plotCurve;
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
void PlotData::updatePlotCurveData()
|
|
|
|
{
|
|
|
|
m_plotCurve->setSamples(xData, yData);
|
|
|
|
}
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
bool SequentialPlotData::append(UAVObject *obj)
|
2013-04-05 23:46:56 +03:00
|
|
|
{
|
2014-10-22 00:52:39 +02:00
|
|
|
if (objectName() == obj->getName()) {
|
2013-05-19 17:37:30 +03:00
|
|
|
// Get the field of interest
|
2014-10-22 00:52:39 +02:00
|
|
|
UAVObjectField *field = obj->getField(fieldName());
|
2013-04-05 23:46:56 +03:00
|
|
|
|
|
|
|
if (field) {
|
2014-10-22 00:52:39 +02:00
|
|
|
double currentValue = valueAsDouble(obj, field) * pow(10, m_scalePower);
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
// Perform scope math, if necessary
|
2014-10-22 00:52:39 +02:00
|
|
|
if (m_mathFunction == "Boxcar average" || m_mathFunction == "Standard deviation") {
|
2013-05-19 17:37:30 +03:00
|
|
|
// Put the new value at the front
|
2014-10-22 00:52:39 +02:00
|
|
|
yDataHistory.append(currentValue);
|
2013-04-05 23:46:56 +03:00
|
|
|
|
|
|
|
// calculate average value
|
2014-10-22 00:52:39 +02:00
|
|
|
m_meanSum += currentValue;
|
|
|
|
if (yDataHistory.size() > m_meanSamples) {
|
|
|
|
m_meanSum -= yDataHistory.first();
|
|
|
|
yDataHistory.pop_front();
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure to correct the sum every meanSamples steps to prevent it
|
|
|
|
// from running away due to floating point rounding errors
|
2014-10-22 00:52:39 +02:00
|
|
|
m_correctionSum += currentValue;
|
|
|
|
if (++m_correctionCount >= m_meanSamples) {
|
|
|
|
m_meanSum = m_correctionSum;
|
|
|
|
m_correctionSum = 0.0f;
|
|
|
|
m_correctionCount = 0;
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
double boxcarAvg = m_meanSum / yDataHistory.size();
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
if (m_mathFunction == "Standard deviation") {
|
2013-05-19 17:37:30 +03:00
|
|
|
// Calculate square of sample standard deviation, with Bessel's correction
|
|
|
|
double stdSum = 0;
|
2014-10-22 00:52:39 +02:00
|
|
|
for (int i = 0; i < yDataHistory.size(); i++) {
|
|
|
|
stdSum += pow(yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1);
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
2014-10-22 00:52:39 +02:00
|
|
|
yData.append(sqrt(stdSum));
|
2013-05-19 17:37:30 +03:00
|
|
|
} else {
|
2014-10-22 00:52:39 +02:00
|
|
|
yData.append(boxcarAvg);
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
} else {
|
2014-10-22 00:52:39 +02:00
|
|
|
yData.append(currentValue);
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
if (yData.size() > m_plotDataSize) { // If new data overflows the window, remove old data...
|
|
|
|
yData.pop_front();
|
2013-05-19 17:37:30 +03:00
|
|
|
} else { // ...otherwise, add a new y point at position xData
|
2014-10-22 00:52:39 +02:00
|
|
|
xData.insert(xData.size(), xData.size());
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2013-04-05 23:46:56 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
bool ChronoPlotData::append(UAVObject *obj)
|
2013-04-05 23:46:56 +03:00
|
|
|
{
|
2014-10-22 00:52:39 +02:00
|
|
|
if (objectName() == obj->getName()) {
|
2013-05-19 17:37:30 +03:00
|
|
|
// Get the field of interest
|
2014-10-22 00:52:39 +02:00
|
|
|
UAVObjectField *field = obj->getField(fieldName());
|
2013-05-19 17:37:30 +03:00
|
|
|
// qDebug() << "uavObject: " << uavObject << ", uavField: " << uavField;
|
2013-04-05 23:46:56 +03:00
|
|
|
|
|
|
|
if (field) {
|
2013-05-19 17:37:30 +03:00
|
|
|
QDateTime NOW = QDateTime::currentDateTime(); // THINK ABOUT REIMPLEMENTING THIS TO SHOW UAVO TIME, NOT SYSTEM TIME
|
2014-10-22 00:52:39 +02:00
|
|
|
double currentValue = valueAsDouble(obj, field) * pow(10, m_scalePower);
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
// Perform scope math, if necessary
|
2014-10-22 00:52:39 +02:00
|
|
|
if (m_mathFunction == "Boxcar average" || m_mathFunction == "Standard deviation") {
|
2013-05-19 17:37:30 +03:00
|
|
|
// Put the new value at the back
|
2014-10-22 00:52:39 +02:00
|
|
|
yDataHistory.append(currentValue);
|
2013-04-05 23:46:56 +03:00
|
|
|
|
|
|
|
// calculate average value
|
2014-10-22 00:52:39 +02:00
|
|
|
m_meanSum += currentValue;
|
|
|
|
if (yDataHistory.size() > m_meanSamples) {
|
|
|
|
m_meanSum -= yDataHistory.first();
|
|
|
|
yDataHistory.pop_front();
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
// make sure to correct the sum every meanSamples steps to prevent it
|
|
|
|
// from running away due to floating point rounding errors
|
2014-10-22 00:52:39 +02:00
|
|
|
m_correctionSum += currentValue;
|
|
|
|
if (++m_correctionCount >= m_meanSamples) {
|
|
|
|
m_meanSum = m_correctionSum;
|
|
|
|
m_correctionSum = 0.0f;
|
|
|
|
m_correctionCount = 0;
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
double boxcarAvg = m_meanSum / yDataHistory.size();
|
|
|
|
// qDebug()<<mathFunction;
|
|
|
|
if (m_mathFunction == "Standard deviation") {
|
2013-05-19 17:37:30 +03:00
|
|
|
// Calculate square of sample standard deviation, with Bessel's correction
|
|
|
|
double stdSum = 0;
|
2014-10-22 00:52:39 +02:00
|
|
|
for (int i = 0; i < yDataHistory.size(); i++) {
|
|
|
|
stdSum += pow(yDataHistory.at(i) - boxcarAvg, 2) / (m_meanSamples - 1);
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
2014-10-22 00:52:39 +02:00
|
|
|
yData.append(sqrt(stdSum));
|
2013-05-19 17:37:30 +03:00
|
|
|
} else {
|
2014-10-22 00:52:39 +02:00
|
|
|
yData.append(boxcarAvg);
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
} else {
|
2014-10-22 00:52:39 +02:00
|
|
|
yData.append(currentValue);
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double valueX = NOW.toTime_t() + NOW.time().msec() / 1000.0;
|
2014-10-22 00:52:39 +02:00
|
|
|
xData.append(valueX);
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
// qDebug() << "Data " << uavObject << "." << field->getName() << " X,Y:" << valueX << "," << valueY;
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
// Remove stale data
|
2013-04-05 23:46:56 +03:00
|
|
|
removeStaleData();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChronoPlotData::removeStaleData()
|
|
|
|
{
|
|
|
|
double newestValue;
|
|
|
|
double oldestValue;
|
|
|
|
|
|
|
|
while (1) {
|
2014-10-22 00:52:39 +02:00
|
|
|
if (xData.size() == 0) {
|
2013-04-05 23:46:56 +03:00
|
|
|
break;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
newestValue = xData.last();
|
|
|
|
oldestValue = xData.first();
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
if (newestValue - oldestValue > m_plotDataSize) {
|
|
|
|
yData.pop_front();
|
|
|
|
xData.pop_front();
|
2013-05-19 17:37:30 +03:00
|
|
|
} else {
|
2013-04-05 23:46:56 +03:00
|
|
|
break;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
// qDebug() << "removeStaleData ";
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChronoPlotData::removeStaleDataTimeout()
|
|
|
|
{
|
|
|
|
removeStaleData();
|
2013-05-19 17:37:30 +03:00
|
|
|
// qDebug() << "removeStaleDataTimeout";
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|