Graphing of canbus data now works. It is very preliminary but it works.

This commit is contained in:
Collin Kidder
2015-05-03 19:47:17 -04:00
parent ee66165cfa
commit 9381c957b1
8 changed files with 227 additions and 28 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.4.0, 2015-05-02T22:09:54. -->
<!-- Written by QtCreator 3.4.0, 2015-05-02T23:26:55. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
+120 -2
View File
@@ -1,16 +1,35 @@
#include "graphingwindow.h"
#include "ui_graphingwindow.h"
#include "newgraphdialog.h"
#include <QDebug>
GraphingWindow::GraphingWindow(QWidget *parent) :
GraphingWindow::GraphingWindow(QList<CANFrame> *frames, QWidget *parent) :
QDialog(parent),
ui(new Ui::GraphingWindow)
{
ui->setupUi(this);
modelFrames = frames;
ui->graphingView->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
QCP::iSelectLegend | QCP::iSelectPlottables);
ui->graphingView->xAxis->setRange(-8, 8);
ui->graphingView->yAxis->setRange(-5, 5);
ui->graphingView->axisRect()->setupFullAxesBox();
//ui->graphingView->plotLayout()->insertRow(0);
//ui->graphingView->plotLayout()->addElement(0, 0, new QCPPlotTitle(ui->graphingView, "Data Graphing"));
ui->graphingView->xAxis->setLabel("Time Axis");
ui->graphingView->yAxis->setLabel("Value Axis");
ui->graphingView->legend->setVisible(true);
QFont legendFont = font();
legendFont.setPointSize(10);
ui->graphingView->legend->setFont(legendFont);
ui->graphingView->legend->setSelectedFont(legendFont);
ui->graphingView->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items
connect(ui->graphingView, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));
connect(ui->graphingView, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));
@@ -191,8 +210,107 @@ void GraphingWindow::contextMenuRequest(QPoint pos)
void GraphingWindow::addNewGraph()
{
int tempVal;
NewGraphDialog *thisDialog = new NewGraphDialog();
thisDialog->show();
if (thisDialog->exec() == QDialog::Accepted)
{
GraphParams params;
thisDialog->getParams(params);
qDebug() << "Returned ID: " << params.ID;
qDebug() << "Start byte: " << params.startByte;
qDebug() << "End Byte: " << params.endByte;
qDebug() << "Mask: " << params.mask;
frameCache.clear();
for (int i = 0; i < modelFrames->count(); i++)
{
CANFrame thisFrame = modelFrames->at(i);
if (thisFrame.ID == params.ID) frameCache.append(thisFrame);
}
int numEntries = frameCache.count() / params.stride;
QVector<double> x(numEntries), y(numEntries);
if (params.endByte == -1 || params.startByte == params.endByte)
{
for (int j = 0; j < numEntries; j++)
{
tempVal = (frameCache[j * params.stride].data[params.startByte] & params.mask);
if (params.isSigned && tempVal > 127)
{
tempVal = tempVal - 256;
}
x[j] = j;
y[j] = (tempVal + params.bias) * params.scale;
}
}
else if (params.endByte > params.startByte) //big endian
{
float tempValue;
int tempValInt;
int numBytes = (params.endByte - params.startByte) + 1;
int shiftRef = 1 << (numBytes * 8);
for (int j = 0; j < numEntries; j++)
{
tempValInt = 0;
int expon = 1;
for (int c = 0; c < numBytes; c++)
{
tempValInt += (frameCache[j * params.stride].data[params.endByte - c] * expon);
expon *= 256;
}
tempValInt &= params.mask;
if (params.isSigned && tempValInt > ((shiftRef / 2) - 1))
{
tempValInt = tempValInt - shiftRef;
}
tempValue = (float)tempValInt;
x[j] = j;
y[j] = (tempValue + params.bias) * params.scale;
}
}
else //little endian
{
float tempValue;
int tempValInt;
int numBytes = (params.startByte - params.endByte) + 1;
int shiftRef = 1 << (numBytes * 8);
for (int j = 0; j < numEntries; j++)
{
tempValInt = 0;
int expon = 1;
for (int c = 0; c < numBytes; c++)
{
tempValInt += frameCache[j * params.stride].data[params.endByte + c] * expon;
expon *= 256;
}
tempValInt &= params.mask;
if (params.isSigned && tempValInt > ((shiftRef / 2) - 1))
{
tempValInt = tempValInt - shiftRef;
}
tempValue = (float)tempValInt;
x[j] = j;
y[j] = (tempValue + params.bias) * params.scale;
}
}
ui->graphingView->addGraph();
ui->graphingView->graph()->setName(QString("Graph %1").arg(ui->graphingView->graphCount()-1));
ui->graphingView->graph()->setData(x,y);
ui->graphingView->graph()->setLineStyle(QCPGraph::lsLine); //connect points with lines
QPen graphPen;
graphPen.setColor(params.color);
graphPen.setWidth(1);
ui->graphingView->graph()->setPen(graphPen);
ui->graphingView->replot();
}
delete thisDialog;
}
void GraphingWindow::moveLegend()
+17 -1
View File
@@ -2,6 +2,7 @@
#define GRAPHINGWINDOW_H
#include "qcustomplot.h"
#include "can_structs.h"
#include <QDialog>
@@ -14,7 +15,7 @@ class GraphingWindow : public QDialog
Q_OBJECT
public:
explicit GraphingWindow(QWidget *parent = 0);
explicit GraphingWindow(QList<CANFrame> *, QWidget *parent = 0);
~GraphingWindow();
private slots:
@@ -32,6 +33,21 @@ private slots:
private:
Ui::GraphingWindow *ui;
QList<CANFrame> frameCache;
QList<CANFrame> *modelFrames;
};
class GraphParams
{
public:
int32_t ID;
int startByte, endByte;
bool isSigned;
int32_t mask;
float bias;
float scale;
int stride;
QColor color;
};
#endif // GRAPHINGWINDOW_H
+1 -1
View File
@@ -245,7 +245,7 @@ void MainWindow::connButtonPress()
void MainWindow::showGraphingWindow()
{
if (!graphingWindow) graphingWindow = new GraphingWindow();
if (!graphingWindow) graphingWindow = new GraphingWindow(model->getListReference());
graphingWindow->show();
}
+58
View File
@@ -1,14 +1,72 @@
#include "newgraphdialog.h"
#include "ui_newgraphdialog.h"
#include <QColorDialog>
#include "utility.h"
NewGraphDialog::NewGraphDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::NewGraphDialog)
{
ui->setupUi(this);
connect(ui->colorSwatch, SIGNAL(clicked(bool)), this, SLOT(colorSwatchClick()));
connect(ui->btnAddGraph, SIGNAL(clicked(bool)), this, SLOT(addButtonClicked()));
// Seed the random generator with current time
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
QPalette p = ui->colorSwatch->palette();
p.setColor(QPalette::Button, QColor(qrand() % 255,qrand() % 255,qrand() % 255));
ui->colorSwatch->setPalette(p);
}
NewGraphDialog::~NewGraphDialog()
{
delete ui;
}
void NewGraphDialog::addButtonClicked()
{
accept();
}
void NewGraphDialog::colorSwatchClick()
{
QColor newColor = QColorDialog::getColor(ui->colorSwatch->palette().button().color());
QPalette p = ui->colorSwatch->palette();
p.setColor(QPalette::Button, newColor);
ui->colorSwatch->setPalette(p);
}
void NewGraphDialog::getParams(GraphParams &params)
{
params.ID = Utility::ParseStringToNum(ui->txtID->text());
params.bias = ui->txtBias->text().toFloat();
params.color = ui->colorSwatch->palette().button().color();
params.isSigned = ui->cbSigned->isChecked();
params.mask = Utility::ParseStringToNum(ui->txtMask->text());
params.scale = Utility::ParseStringToNum(ui->txtScale->text());
params.stride = Utility::ParseStringToNum(ui->txtStride->text());
QStringList values = ui->txtData->text().split('-');
params.startByte = -1;
params.endByte = -1;
if (values.count() > 0)
{
params.startByte = values[0].toInt();
if (values.count() > 1)
{
params.endByte = values[1].toInt();
}
}
//now catch stupidity and bring it to defaults
if (params.mask == 0) params.mask = 0xFFFFFFFF;
if (fabs(params.scale) < 0.0001f) params.scale = 1.0f;
if (params.stride < 1) params.stride = 1;
}
+6
View File
@@ -2,6 +2,7 @@
#define NEWGRAPHDIALOG_H
#include <QDialog>
#include "graphingwindow.h"
namespace Ui {
class NewGraphDialog;
@@ -14,6 +15,11 @@ class NewGraphDialog : public QDialog
public:
explicit NewGraphDialog(QWidget *parent = 0);
~NewGraphDialog();
void getParams(GraphParams &);
private slots:
void addButtonClicked();
void colorSwatchClick();
private:
Ui::NewGraphDialog *ui;
+20 -23
View File
@@ -76,50 +76,47 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit"/>
<widget class="QLineEdit" name="txtID"/>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineEdit_2"/>
<widget class="QLineEdit" name="txtData"/>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="checkBox">
<widget class="QCheckBox" name="cbSigned">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="lineEdit_3"/>
<widget class="QLineEdit" name="txtMask"/>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="lineEdit_4"/>
<widget class="QLineEdit" name="txtBias"/>
</item>
<item row="5" column="1">
<widget class="QLineEdit" name="lineEdit_5"/>
<widget class="QLineEdit" name="txtScale"/>
</item>
<item row="6" column="1">
<widget class="QLineEdit" name="lineEdit_6"/>
<widget class="QLineEdit" name="txtStride"/>
</item>
<item row="7" column="1">
<widget class="QGraphicsView" name="graphicsView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>40</height>
</size>
<item row="8" column="1">
<widget class="QPushButton" name="btnAddGraph">
<property name="text">
<string>Add this graph</string>
</property>
</widget>
</item>
<item row="8" column="1">
<widget class="QPushButton" name="pushButton">
<item row="7" column="1">
<widget class="QPushButton" name="colorSwatch">
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="text">
<string>Add this graph</string>
<string>CHANGE</string>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
+4
View File
@@ -32,7 +32,11 @@ public:
}
return temp;
}
static int ParseStringToNum(QString input)
{
return ParseStringToNum(input.toUtf8());
}
static long GetTimeMS()