-
Notifications
You must be signed in to change notification settings - Fork 1
/
canTab.cpp
326 lines (272 loc) · 12.7 KB
/
canTab.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "canTab.h"
#include "ui_cantab.h"
#include "mainwindow.h"
#include "dataprovider.h"
#include <chrono>
#include <thread>
CanTab::CanTab(QWidget *parent) :
QWidget(parent),
ui(new Ui::CanTab)
{
ui->setupUi(this);
connect(ui->startCANButton, SIGNAL(clicked()), this, SLOT(startCAN()));
connect(ui->stopCANButton, SIGNAL(clicked()), this, SLOT(stopCAN()));
connect(ui->setMotorButton, SIGNAL(clicked()), this, SLOT(setMotor()));
connect(ui->reset1Button, SIGNAL(clicked()), this, SLOT(resetMotor1()));
connect(ui->reset2Button, SIGNAL(clicked()), this, SLOT(resetMotor2()));
connect(ui->stopMotorButton, SIGNAL(clicked()), this, SLOT(stopMotors()));
/*QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
x[i] = i/50.0 - 1; // x goes from -1 to 1
y[i] = x[i]*x[i]; // let's plot a quadratic function
}
// create graph and assign data to it:
ui->canPlot->addGraph();
ui->canPlot->graph(0)->setData(x, y);
// give the axes some labels:
ui->canPlot->xAxis->setLabel("x");
ui->canPlot->yAxis->setLabel("y");
// set axes ranges, so we see all data:
ui->canPlot->xAxis->setRange(-1, 1);
ui->canPlot->yAxis->setRange(0, 1);
ui->canPlot->replot();*/
// include this section to fully disable antialiasing for higher performance:
/*
ui->canPlot->setNotAntialiasedElements(QCP::aeAll);
QFont font;
font.setStyleStrategy(QFont::NoAntialias);
ui->canPlot->xAxis->setTickLabelFont(font);
ui->canPlot->yAxis->setTickLabelFont(font);
ui->canPlot->legend->setFont(font);
*/
/*ui->canPlot->addGraph(); // blue dot
ui->canPlot->graph(2)->setPen(QPen(Qt::blue));
ui->canPlot->graph(2)->setLineStyle(QCPGraph::lsNone);
ui->canPlot->graph(2)->setScatterStyle(QCPScatterStyle::ssDisc);
ui->canPlot->addGraph(); // red dot
ui->canPlot->graph(3)->setPen(QPen(Qt::red));
ui->canPlot->graph(3)->setLineStyle(QCPGraph::lsNone);
ui->canPlot->graph(3)->setScatterStyle(QCPScatterStyle::ssDisc);*/
ui->canPlot->legend->setVisible(true);
//ui->canPlot->legend->setFont(QFont("Helvetica", 9));
//ui->canPlot->legend->setRowSpacing(-3);
ui->canPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
ui->canPlot->xAxis->setDateTimeFormat("mm:ss");
ui->canPlot->xAxis->setAutoTickStep(false);
ui->canPlot->xAxis->setTickStep(1);
ui->canPlot->axisRect()->setupFullAxesBox();
/*ui->canPlot->yAxis->setTickLabelFont(QFont(QFont().family(), 8));
ui->canPlot->yAxis->setAutoTicks(false);
ui->canPlot->yAxis->setAutoTickLabels(false);
ui->canPlot->yAxis->setTickVector(QVector<double>() << 5 << 55);
ui->canPlot->yAxis->setTickVectorLabels(QVector<QString>() << "sdg so\nhigh" << "Very\nhigh");*/
//ui->canPlot->yAxis->setRange(-850, 850); // fixed range of the joint data
//ui->canPlot->yAxis->setLabel("Joint Position");
ui->canPlot->yAxis->setVisible(true);
//ui->canPlot->yAxis->setRange(-10, 500); // fixed range of the spring displacement data
ui->canPlot->yAxis->setRange(-10, 1000);
ui->canPlot->yAxis->setAutoTickCount(10);
ui->canPlot->yAxis->setAutoTickLabels(true);
ui->canPlot->yAxis->setAutoTicks(true);
ui->canPlot->yAxis->setAutoTickStep(true);
ui->canPlot->yAxis->setTicks(true);
ui->canPlot->yAxis->setTickLabels(true);
//ui->canPlot->yAxis2->setSubTickLength(1, 1);
ui->canPlot->yAxis->setLabel("Current / Spring / PWM");
ui->canPlot->yAxis2->setRange(-10, 1000);
ui->canPlot->yAxis2->setAutoTickCount(10);
ui->canPlot->yAxis2->setAutoTickLabels(false);
ui->canPlot->yAxis2->setAutoTicks(true);
ui->canPlot->yAxis2->setAutoTickStep(true);
ui->canPlot->yAxis2->setTicks(true);
ui->canPlot->yAxis2->setTickLabels(false);
//ui->canPlot->yAxis2->setAutoTicks(true);
//ui->canPlot->yAxis2->setAutoTickLabels(true);
//ui->canPlot->yAxis->setAutoTickStep(false);
//ui->canPlot->yAxis->setAutoSubTicks(false);
//ui->canPlot->yAxis->setTickStep(1.0);
//ui->canPlot->yAxis->setSubTickCount(0);
//ui->canPlot->yAxis2->setAutoTickStep(false);
//ui->canPlot->yAxis2->setAutoSubTicks(false);
//ui->canPlot->yAxis2->setTickStep(1.0);
//ui->canPlot->yAxis2->setSubTickCount(0);
ui->canPlot->addGraph(ui->canPlot->xAxis, ui->canPlot->yAxis);
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setPen(QPen(QBrush(QColor(200, 100, 10)), 3));//setPen(QPen(Qt::blue));
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setName("Current (R)");
ui->canPlot->addGraph(ui->canPlot->xAxis, ui->canPlot->yAxis);
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setPen(QPen(QBrush(QColor(180, 0, 0)), 3, Qt::DashLine));//setPen(QPen(Qt::blue));
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setName("Spring (R)");
ui->canPlot->addGraph(ui->canPlot->xAxis, ui->canPlot->yAxis);
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setPen(QPen(QBrush(QColor(250, 150, 50)), 3));//setPen(QPen(Qt::blue));
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setName("Current (L)");
ui->canPlot->addGraph(ui->canPlot->xAxis, ui->canPlot->yAxis);
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setPen(QPen(QBrush(QColor(250, 60, 60)), 3, Qt::DashLine));//setPen(QPen(Qt::blue));
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setName("Spring (L)");
ui->canPlot->addGraph(ui->canPlot->xAxis, ui->canPlot->yAxis);
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setPen(QPen(QBrush(QColor(10, 100, 10)), 3));//setPen(QPen(Qt::blue));
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setName("PWM (R)");
ui->canPlot->addGraph(ui->canPlot->xAxis, ui->canPlot->yAxis);
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setPen(QPen(QBrush(QColor(30, 150, 30)), 3));//setPen(QPen(Qt::blue));
ui->canPlot->graph(ui->canPlot->graphCount()-1)->setName("PWM (L)");
//ui->canPlot->addGraph(ui->canPlot->xAxis, ui->canPlot->yAxis2);
//ui->canPlot->graph(ui->canPlot->graphCount()-1)->setPen(QPen(QBrush(Qt::blue), 5));//setPen(QPen(Qt::blue));
//ui->canPlot->graph(ui->canPlot->graphCount()-1)->setName("Joint Position");
//ui->canPlot->graph(ui->canPlot->graphCount()-1)->setLineStyle(QCPGraph::lsNone);
//ui->canPlot->graph(ui->canPlot->graphCount()-1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDiamond, 5));
ui->canPlot->replot(); // needed?
// make left and bottom axes transfer their ranges to right and top axes:
connect(ui->canPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->canPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->canPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->canPlot->yAxis2, SLOT(setRange(QCPRange)));
DataProvider::getInstance()->setCanPlot(ui->canPlot);
// set these values as you wish!
this->showPastTime = 2.0; // + 4.0 only for video!
this->windowWidth = 10.5;
this->rightBlankTime = 0.5;
// setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
this->updateFrequency = this->windowWidth - this->showPastTime - this->rightBlankTime;
this->timeSpiNNakerStartBefore = DataProvider::getInstance()->getTimeSpiNNakerStartInMs();
this->plotStartTime = std::floor((QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0 - this->timeSpiNNakerStartBefore/1000.0) - this->updateFrequency);
connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
dataTimer.start(1000); // 50! Interval 0 means to refresh as fast as possible
/*
QVector<QCPScatterStyle::ScatterShape> shapes;
shapes << QCPScatterStyle::ssCross;
shapes << QCPScatterStyle::ssPlus;
shapes << QCPScatterStyle::ssCircle;
shapes << QCPScatterStyle::ssDisc;
shapes << QCPScatterStyle::ssSquare;
shapes << QCPScatterStyle::ssDiamond;
shapes << QCPScatterStyle::ssStar;
shapes << QCPScatterStyle::ssTriangle;
shapes << QCPScatterStyle::ssTriangleInverted;
shapes << QCPScatterStyle::ssCrossSquare;
shapes << QCPScatterStyle::ssPlusSquare;
shapes << QCPScatterStyle::ssCrossCircle;
shapes << QCPScatterStyle::ssPlusCircle;
shapes << QCPScatterStyle::ssPeace;
shapes << QCPScatterStyle::ssCustom;
// set scatter style:
if(shapes.at(i) != QCPScatterStyle::ssCustom)
{
customPlot->graph()->setScatterStyle(QCPScatterStyle(shapes.at(i), 10));
}
else
{
QPainterPath customScatterPath;
for(int i=0; i<3; ++i)
customScatterPath.cubicTo(qCos(2*M_PI*i/3.0)*9, qSin(2*M_PI*i/3.0)*9, qCos(2*M_PI*(i+0.9)/3.0)*9, qSin(2*M_PI*(i+0.9)/3.0)*9, 0, 0);
customPlot->graph()->setScatterStyle(QCPScatterStyle(customScatterPath, QPen(), QColor(40, 70, 255, 50), 10));
}*/
}
CanTab::~CanTab()
{
}
void CanTab::setMainWindow(MainWindow* mainWindow_)
{
this->mainWindow = mainWindow_;
}
void CanTab::realtimeDataSlot()
{
qint64 timeSpiNNakerStart = DataProvider::getInstance()->getTimeSpiNNakerStartInMs();
if(timeSpiNNakerStart != this->timeSpiNNakerStartBefore)
{
this->timeSpiNNakerStartBefore = timeSpiNNakerStart;
this->plotStartTime = std::floor((QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0 - this->timeSpiNNakerStartBefore/1000.0) - this->updateFrequency);
for(int i=0; i<ui->canPlot->graphCount(); i++)
ui->canPlot->graph(i)->clearData();
}
double time = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0 - timeSpiNNakerStart/1000.0;
if(time - this->plotStartTime > this->updateFrequency)
{
/*double value0 = 0;//qSin(key); //sin(key*1.6+cos(key*1.7)*2)*10 + sin(key*1.2+0.56)*20 + 26;
double value1 = 1;//qCos(key); //sin(key*1.3+cos(key*1.2)*1.2)*7 + sin(key*0.9+0.26)*24 + 26;
double value2 = 2;
bool spike0 = rand()%100 < 5;
bool spike1 = rand()%100 < 3;
bool spike2 = rand()%100 < 1;*/
// add data to lines:
/*if(spike0)
ui->canPlot->graph(0)->addData(key, value0);
if(spike1)
ui->canPlot->graph(1)->addData(key, value1);
if(spike2)
ui->canPlot->graph(2)->addData(key, value2);*/
// set data of dots:
/*if(spike0)
{
ui->canPlot->graph(2)->clearData();
ui->canPlot->graph(2)->addData(key, value0);
}
if(spike1)
{
ui->canPlot->graph(3)->clearData();
ui->canPlot->graph(3)->addData(key, value1);
}*/
// remove data of lines that's outside visible range
this->plotStartTime += this->updateFrequency;
for(int i=0; i<ui->canPlot->graphCount(); i++)
ui->canPlot->graph(i)->removeDataBefore(this->plotStartTime - this->showPastTime);
// rescale value (vertical) axis to fit the current data
/*if(ui->canPlot->graphCount() >= 1)
ui->canPlot->graph(0)->rescaleValueAxis();
for(int i=1; i<ui->canPlot->graphCount(); i++)
ui->canPlot->graph(i)->rescaleValueAxis(true);*/
ui->canPlot->xAxis->setRange(this->plotStartTime - this->showPastTime, this->windowWidth, Qt::AlignLeft);
}
ui->canPlot->replot();
// calculate frames per second:
/*static double lastFpsKey;
static int frameCount;
++frameCount;
if (key-lastFpsKey > 2) // average fps over 2 seconds
{
int dataCount = 0;
for(int i=0; i<ui->canPlot->graphCount(); i++)
dataCount += ui->canPlot->graph(0)->data()->count();
mainWindow->showMessageStatusBar(
QString("%1 FPS, %2 Graphs, Total Data points: %3")
.arg(frameCount/(key-lastFpsKey), 0, 'f', 0)
.arg(ui->canPlot->graphCount())
.arg(dataCount)
, 0);
lastFpsKey = key;
frameCount = 0;
}*/
}
void CanTab::startCAN()
{
DataProvider::getInstance()->startCan();
}
void CanTab::stopCAN()
{
DataProvider::getInstance()->stopCan();
}
void CanTab::setMotor()
{
DataProvider::getInstance()->canInterface->setReference1(ui->motor1Slider->value());
DataProvider::getInstance()->canInterface->setReference2(ui->motor2Slider->value());
DataProvider::getInstance()->canInterface->start();
//std::this_thread::sleep_for(std::chrono::milliseconds(2000));
//DataProvider::getInstance()->canInterface->stop();
}
void CanTab::stopMotors()
{
DataProvider::getInstance()->canInterface->stop();
}
void CanTab::resetMotor1()
{
ui->motor1Slider->setValue(0);
}
void CanTab::resetMotor2()
{
ui->motor2Slider->setValue(0);
}
void CanTab::savePlot()
{
QString filename = "canPlot";
ui->canPlot->savePdf(filename+".pdf", false, 0, 0);
//ui->canPlot->savePdf(filename+"noCosmetics.pdf", true); // better for editing in Inkscape?
//ui->canPlot->setAntialiasedElements(QCP::aeAll);
ui->canPlot->savePng(filename+".png", 0, 0, 2.0, 100);
}