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-28 13:33:21 +01:00
|
|
|
PlotData::PlotData(UAVObject *object, UAVObjectField *field, int element,
|
2014-10-28 22:42:49 +01:00
|
|
|
int scaleOrderFactor, int meanSamples, QString mathFunction,
|
|
|
|
double plotDataSize, QPen pen, bool antialiased) :
|
2014-10-28 12:19:54 +01:00
|
|
|
m_scalePower(scaleOrderFactor), m_meanSamples(meanSamples),
|
2014-10-30 16:46:39 +01:00
|
|
|
m_meanSum(0.0f), m_mathFunction(mathFunction), m_correctionSum(0.0f),
|
|
|
|
m_correctionCount(0), m_plotDataSize(plotDataSize),
|
|
|
|
m_object(object), m_field(field), m_element(element),
|
2014-10-31 10:14:46 +01:00
|
|
|
m_plotCurve(NULL), m_isVisible(true), m_pen(pen), m_isEnumPlot(false)
|
2014-10-28 13:33:21 +01:00
|
|
|
{
|
2014-11-07 00:32:38 +01:00
|
|
|
if (m_field->getNumElements() > 1) {
|
2014-10-28 22:42:49 +01:00
|
|
|
m_elementName = m_field->getElementNames().at(m_element);
|
|
|
|
}
|
|
|
|
|
2014-10-29 00:05:53 +01:00
|
|
|
m_plotName.append(QString("%1.%2").arg(m_object->getName()).arg(m_field->getName()));
|
2014-10-28 22:42:49 +01:00
|
|
|
if (!m_elementName.isEmpty()) {
|
2014-10-29 00:05:53 +01:00
|
|
|
m_plotName.append(QString(".%1").arg(m_elementName));
|
2014-10-28 22:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_scalePower == 0) {
|
2014-10-29 00:05:53 +01:00
|
|
|
m_plotName.append(QString(" (%1)").arg(m_field->getUnits()));
|
2014-10-28 22:42:49 +01:00
|
|
|
} else {
|
2014-10-29 00:05:53 +01:00
|
|
|
m_plotName.append(QString(" (x10^%1 %2)").arg(m_scalePower).arg(m_field->getUnits()));
|
2014-10-28 22:42:49 +01:00
|
|
|
}
|
|
|
|
|
2014-10-30 16:46:39 +01:00
|
|
|
// Create the curve
|
2014-10-29 00:05:53 +01:00
|
|
|
m_plotCurve = new QwtPlotCurve(m_plotName);
|
2014-10-22 00:52:39 +02:00
|
|
|
|
2014-10-28 22:42:49 +01:00
|
|
|
if (antialiased) {
|
|
|
|
m_plotCurve->setRenderHint(QwtPlotCurve::RenderAntialiased);
|
2014-10-28 13:33:21 +01:00
|
|
|
}
|
2014-10-28 22:42:49 +01:00
|
|
|
|
2014-10-30 16:46:39 +01:00
|
|
|
m_plotCurve->setPen(m_pen);
|
2014-10-28 22:42:49 +01:00
|
|
|
m_plotCurve->setSamples(m_xDataEntries, m_yDataEntries);
|
2014-10-31 10:14:46 +01:00
|
|
|
m_isEnumPlot = m_field->getType() == UAVObjectField::ENUM;
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PlotData::~PlotData()
|
|
|
|
{
|
2014-10-30 16:46:39 +01:00
|
|
|
while (!m_enumMarkerList.isEmpty()) {
|
|
|
|
QwtPlotMarker *marker = m_enumMarkerList.takeFirst();
|
|
|
|
marker->detach();
|
|
|
|
delete marker;
|
|
|
|
}
|
2014-10-22 00:52:39 +02:00
|
|
|
m_plotCurve->detach();
|
|
|
|
delete m_plotCurve;
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
|
|
|
|
2014-11-03 23:09:26 +01:00
|
|
|
bool PlotData::isVisible() const
|
|
|
|
{
|
2014-10-30 16:46:39 +01:00
|
|
|
return m_plotCurve->isVisible();
|
|
|
|
}
|
|
|
|
|
2014-11-03 23:09:26 +01:00
|
|
|
void PlotData::setVisible(bool visible)
|
|
|
|
{
|
2014-10-30 16:46:39 +01:00
|
|
|
m_plotCurve->setVisible(visible);
|
|
|
|
visibilityChanged(m_plotCurve);
|
|
|
|
}
|
|
|
|
|
2014-10-29 00:05:53 +01:00
|
|
|
void PlotData::updatePlotData()
|
2014-10-22 00:52:39 +02:00
|
|
|
{
|
2014-10-28 12:19:54 +01:00
|
|
|
m_plotCurve->setSamples(m_xDataEntries, m_yDataEntries);
|
2014-10-22 00:52:39 +02:00
|
|
|
}
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2014-11-14 14:18:46 +01:00
|
|
|
void PlotData::clear()
|
|
|
|
{
|
|
|
|
m_meanSum = 0.0f;
|
|
|
|
m_correctionSum = 0.0f;
|
|
|
|
m_correctionCount = 0;
|
|
|
|
m_xDataEntries.clear();
|
|
|
|
m_yDataEntries.clear();
|
|
|
|
while (!m_enumMarkerList.isEmpty()) {
|
|
|
|
QwtPlotMarker *marker = m_enumMarkerList.takeFirst();
|
|
|
|
marker->detach();
|
|
|
|
delete marker;
|
|
|
|
}
|
|
|
|
if (wantsInitialData()) {
|
|
|
|
append(m_object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 23:09:26 +01:00
|
|
|
bool PlotData::hasData() const
|
|
|
|
{
|
2014-10-31 10:14:46 +01:00
|
|
|
if (!m_isEnumPlot) {
|
|
|
|
return !m_xDataEntries.isEmpty();
|
|
|
|
} else {
|
|
|
|
return !m_enumMarkerList.isEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 23:09:26 +01:00
|
|
|
QString PlotData::lastDataAsString()
|
|
|
|
{
|
2014-10-31 10:14:46 +01:00
|
|
|
if (!m_isEnumPlot) {
|
|
|
|
return QString().sprintf("%3.10g", m_yDataEntries.last());
|
|
|
|
} else {
|
|
|
|
return m_enumMarkerList.last()->title().text();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 22:42:49 +01:00
|
|
|
void PlotData::attach(QwtPlot *plot)
|
|
|
|
{
|
|
|
|
m_plotCurve->attach(plot);
|
|
|
|
}
|
|
|
|
|
2014-10-30 16:46:39 +01:00
|
|
|
void PlotData::visibilityChanged(QwtPlotItem *item)
|
|
|
|
{
|
|
|
|
if (m_plotCurve == item) {
|
2014-11-03 23:09:26 +01:00
|
|
|
foreach(QwtPlotMarker * marker, m_enumMarkerList) {
|
2014-10-30 16:46:39 +01:00
|
|
|
m_plotCurve->isVisible() ? marker->attach(m_plotCurve->plot()) : marker->detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 13:44:58 +01:00
|
|
|
void PlotData::calcMathFunction(double currentValue)
|
|
|
|
{
|
|
|
|
// Put the new value at the back
|
|
|
|
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();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-31 10:14:46 +01:00
|
|
|
QwtPlotMarker *PlotData::createMarker(QString value)
|
|
|
|
{
|
|
|
|
QwtPlotMarker *marker = new QwtPlotMarker(value);
|
2014-11-03 23:09:26 +01:00
|
|
|
|
2014-10-31 10:14:46 +01:00
|
|
|
marker->setZ(10);
|
|
|
|
QwtText label(QString(" %1 ").arg(value));
|
|
|
|
label.setColor(QColor(Qt::black));
|
|
|
|
label.setBorderPen(QPen(m_pen.color(), 1));
|
|
|
|
label.setBorderRadius(2);
|
|
|
|
QColor labelBackColor = QColor(Qt::white);
|
|
|
|
labelBackColor.setAlpha(200);
|
|
|
|
label.setBackgroundBrush(labelBackColor);
|
|
|
|
QFont fnt(label.font());
|
|
|
|
fnt.setPointSize(8);
|
|
|
|
label.setFont(fnt);
|
|
|
|
marker->setLabel(label);
|
|
|
|
marker->setTitle(value);
|
|
|
|
marker->setLabelOrientation(Qt::Vertical);
|
|
|
|
marker->setLabelAlignment(Qt::AlignBottom);
|
|
|
|
marker->setLineStyle(QwtPlotMarker::VLine);
|
|
|
|
marker->setLinePen(QPen(m_pen.color(), 1, Qt::DashDotLine));
|
|
|
|
return marker;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SequentialPlotData::append(UAVObject *obj)
|
2013-04-05 23:46:56 +03:00
|
|
|
{
|
2014-11-15 11:04:19 +01:00
|
|
|
if (obj == NULL) {
|
|
|
|
obj = m_object;
|
|
|
|
}
|
|
|
|
|
2014-10-28 18:12:04 +01:00
|
|
|
if (m_object == obj && m_field) {
|
2014-10-31 10:14:46 +01:00
|
|
|
if (!m_isEnumPlot) {
|
2014-10-30 16:46:39 +01:00
|
|
|
double currentValue = m_field->getValue(m_element).toDouble() * pow(10, m_scalePower);
|
|
|
|
|
|
|
|
// Perform scope math, if necessary
|
|
|
|
if (m_mathFunction == "Boxcar average" || m_mathFunction == "Standard deviation") {
|
|
|
|
calcMathFunction(currentValue);
|
|
|
|
} else {
|
|
|
|
m_yDataEntries.append(currentValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
m_xDataEntries.insert(m_xDataEntries.size(), m_xDataEntries.size());
|
|
|
|
}
|
|
|
|
return true;
|
2014-10-28 18:12:04 +01:00
|
|
|
} else {
|
2014-10-31 10:14:46 +01:00
|
|
|
// Enum markers
|
|
|
|
QString value = m_field->getValue(m_element).toString();
|
|
|
|
|
|
|
|
QwtPlotMarker *marker = m_enumMarkerList.isEmpty() ? NULL : m_enumMarkerList.last();
|
|
|
|
if (!marker || marker->title() != value) {
|
|
|
|
marker = createMarker(value);
|
|
|
|
marker->setXValue(m_enumMarkerList.size());
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2014-10-31 10:14:46 +01:00
|
|
|
if (m_plotCurve->isVisible()) {
|
|
|
|
marker->attach(m_plotCurve->plot());
|
|
|
|
}
|
|
|
|
m_enumMarkerList.append(marker);
|
|
|
|
}
|
2014-10-28 18:12:04 +01:00
|
|
|
}
|
|
|
|
}
|
2013-04-05 23:46:56 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-31 10:14:46 +01:00
|
|
|
bool ChronoPlotData::append(UAVObject *obj)
|
2013-04-05 23:46:56 +03:00
|
|
|
{
|
2014-11-15 11:04:19 +01:00
|
|
|
if (obj == NULL) {
|
|
|
|
obj = m_object;
|
|
|
|
}
|
|
|
|
|
2014-10-28 18:12:04 +01:00
|
|
|
if (m_object == obj && m_field) {
|
2013-05-19 17:37:30 +03:00
|
|
|
// Get the field of interest
|
2014-10-31 10:14:46 +01:00
|
|
|
// THINK ABOUT REIMPLEMENTING THIS TO SHOW UAVO TIME, NOT SYSTEM TIME
|
|
|
|
QDateTime NOW = QDateTime::currentDateTime();
|
2014-10-28 18:12:04 +01:00
|
|
|
|
2014-10-30 16:46:39 +01:00
|
|
|
double xValue = NOW.toTime_t() + NOW.time().msec() / 1000.0;
|
2014-10-31 10:14:46 +01:00
|
|
|
if (!m_isEnumPlot) {
|
2014-10-30 16:46:39 +01:00
|
|
|
double currentValue = m_field->getValue(m_element).toDouble() * pow(10, m_scalePower);
|
|
|
|
|
|
|
|
// Perform scope math, if necessary
|
|
|
|
if (m_mathFunction == "Boxcar average" || m_mathFunction == "Standard deviation") {
|
|
|
|
calcMathFunction(currentValue);
|
|
|
|
} else {
|
|
|
|
m_yDataEntries.append(currentValue);
|
|
|
|
}
|
2013-04-05 23:46:56 +03:00
|
|
|
|
2014-10-30 16:46:39 +01:00
|
|
|
m_xDataEntries.append(xValue);
|
|
|
|
} else {
|
|
|
|
// Enum markers
|
|
|
|
QString value = m_field->getValue(m_element).toString();
|
|
|
|
|
|
|
|
QwtPlotMarker *marker = m_enumMarkerList.isEmpty() ? NULL : m_enumMarkerList.last();
|
|
|
|
if (!marker || marker->title() != value) {
|
2014-10-31 10:14:46 +01:00
|
|
|
marker = createMarker(value);
|
2014-10-30 16:46:39 +01:00
|
|
|
marker->setXValue(xValue);
|
2014-10-31 10:14:46 +01:00
|
|
|
|
2014-10-30 16:46:39 +01:00
|
|
|
if (m_plotCurve->isVisible()) {
|
2014-10-31 10:14:46 +01:00
|
|
|
marker->attach(m_plotCurve->plot());
|
2014-10-30 16:46:39 +01:00
|
|
|
}
|
|
|
|
m_enumMarkerList.append(marker);
|
|
|
|
}
|
|
|
|
}
|
2014-10-31 10:14:46 +01:00
|
|
|
removeStaleData();
|
|
|
|
return true;
|
2014-10-28 18:12:04 +01:00
|
|
|
}
|
2013-04-05 23:46:56 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChronoPlotData::removeStaleData()
|
|
|
|
{
|
2014-10-28 18:12:04 +01:00
|
|
|
while (!m_xDataEntries.isEmpty() &&
|
|
|
|
(m_xDataEntries.last() - m_xDataEntries.first()) > m_plotDataSize) {
|
2014-11-03 23:09:26 +01:00
|
|
|
m_yDataEntries.pop_front();
|
|
|
|
m_xDataEntries.pop_front();
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|
2014-10-30 16:46:39 +01:00
|
|
|
while (!m_enumMarkerList.isEmpty() &&
|
|
|
|
(m_enumMarkerList.last()->xValue() - m_enumMarkerList.first()->xValue()) > m_plotDataSize) {
|
2014-11-03 23:09:26 +01:00
|
|
|
QwtPlotMarker *marker = m_enumMarkerList.takeFirst();
|
2014-10-30 16:46:39 +01:00
|
|
|
marker->detach();
|
|
|
|
delete marker;
|
|
|
|
}
|
2013-04-05 23:46:56 +03:00
|
|
|
}
|