-
Notifications
You must be signed in to change notification settings - Fork 46
/
tab_veltest.cpp
266 lines (213 loc) · 6.45 KB
/
tab_veltest.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
#include "tab_veltest.h"
#include <QPushButton>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QCheckBox>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_legend.h>
#include <qwt_symbol.h>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QThread>
#include <QStatusBar>
#include <QTreeWidget>
#include <QTabWidget>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
TabVeltest::TabVeltest(MainWindow* _mw,QWidget *parent): QWidget(parent)
{
mw=_mw;
testing=false;
QVBoxLayout *layout = new QVBoxLayout;
QHBoxLayout *h1box=new QHBoxLayout(this);
QGridLayout *g1=new QGridLayout(this);
g1->addWidget(new QLabel(""),0,0);
g1->addWidget(new QLabel("Vmin"),0,1);
g1->addWidget(new QLabel("Vmax"),0,2);
g1->addWidget(new QLabel("Amax"),0,3);
g1->addWidget(doTest[0]=new QPushButton("X"), 1,0);
g1->addWidget(vmin[0]=new QLineEdit("100"), 1,1);
g1->addWidget(vmax[0]=new QLineEdit("500"), 1,2);
g1->addWidget(amax[0]=new QLineEdit("10000"), 1,3);
g1->addWidget(doTest[1]=new QPushButton("Y"), 2,0);
g1->addWidget(vmin[1]=new QLineEdit("100"), 2,1);
g1->addWidget(vmax[1]=new QLineEdit("500"), 2,2);
g1->addWidget(amax[1]=new QLineEdit("10000"), 2,3);
g1->addWidget(doTest[2]=new QPushButton("Z"), 3,0);
g1->addWidget(vmin[2]=new QLineEdit("5"), 3,1);
g1->addWidget(vmax[2]=new QLineEdit("30"), 3,2);
g1->addWidget(amax[2]=new QLineEdit("10000"), 3,3);
g1->addWidget(new QLabel("Teststeps"), 4,0);
g1->addWidget(vsteps=new QLineEdit("5"),4,2);
g1->addWidget(asteps=new QLineEdit("5"),4,3);
g1->addWidget(new QLabel(""),5,0);
g1->setColumnStretch(5,1);
g1->setColumnStretch(0,0);
connect(doTest[0],SIGNAL(clicked(bool)), this,SLOT(testX()));
connect(doTest[1],SIGNAL(clicked(bool)), this,SLOT(testY()));
connect(doTest[2],SIGNAL(clicked(bool)), this,SLOT(testZ()));
h1box->addLayout(g1);
plot= new QwtPlot( this);
//myPlot->setAxisTitle(QwtPlot::xBottom,"");
plot->setAxisTitle(QwtPlot::yLeft,"a mm/sec^2");
plot->setAxisTitle(QwtPlot::xBottom,"v mm/sec");
QwtPlotGrid *myGrid=new QwtPlotGrid();
myGrid->attach(plot);
plot->insertLegend(new QwtLegend());
//double x[2]={0,0};
//double y[2]={0,0};
curveGood= new QwtPlotCurve("Good");
curveFail = new QwtPlotCurve("Fail");
//curveGood->setSamples(x,y,2);
curveGood->setPen(QPen(Qt::blue,2));
//curveFail->setSamples(x,y,2);
curveFail->setPen(QPen(Qt::red,2));
curveGood->setStyle(QwtPlotCurve::Dots);
curveFail->setStyle(QwtPlotCurve::Dots);
curveGood->attach(plot);
curveFail->attach(plot);
QwtSymbol *s=new QwtSymbol(QwtSymbol::Rect);
s->setSize(10,10);
s->setColor(Qt::blue);
curveGood->setSymbol(s);
QwtSymbol *s2=new QwtSymbol(QwtSymbol::Rect);
s2->setSize(10,10);
s2->setColor(Qt::red);
curveFail->setSymbol(s2);
plot->replot();
h1box->addWidget(plot);
layout->addLayout(h1box);
//setLayout(layout);
}
void TabVeltest::testX()
{
starttest(0);
}
void TabVeltest::testY()
{
starttest(1);
}
void TabVeltest::testZ()
{
starttest(2);
}
void TabVeltest::starttest(uint8_t axis)
{
if(axis==2)
return;
float v,a;
int nrV=vsteps->text().toInt();
int nrA=asteps->text().toInt()+1;
for(int i=0;i<=nrV;i++)
{
v=vmin[axis]->text().toFloat()+(vmax[axis]->text().toFloat()-vmin[axis]->text().toFloat())*i/float(nrV);
for(int j=1;j<=nrA;j++)
{
a=amax[axis]->text().toFloat()*j/float(nrA);
bool result=testSetting(axis,v,a,30,10);
}
}
}
void TabVeltest::startTestCase(TestCase& tc)
{
float t=tc.v/tc.a;
//float x=tc.a*t*t/2.*2; //10mm + full acceleration distance.
float x=200;
qDebug()<<"Test:"<<tc.a<<" "<<tc.v<<endl;
mw->endstopfound=false;
mw->sendGcode(QString("M205 S0 T0"));
mw->sendGcode(QString("M205 S0 T0 %0%1 Z%2").arg(AxisChars[tc.axis]).arg(tc.xyjerk).arg(tc.zjerk));
mw->sendGcode(QString("M202 %00%1 Y%2 ").arg(AxisChars[tc.axis]).arg(tc.a).arg(tc.a));
mw->sendGcode(QString("M204 S%0 T%1 ").arg(tc.a).arg(tc.a));
mw->sendGcode(QString("M203 %00%1 Y%2 ").arg(AxisChars[tc.axis]).arg(tc.v).arg(tc.v));
mw->sendGcode(QString("G28 %000 ").arg(AxisChars[tc.axis]));
mw->sendGcode(QString("G1 %00%1 F%2").arg(AxisChars[tc.axis]).arg(x).arg(tc.v*60));
mw->sendGcode(QString("M400"));
mw->sendGcode(QString("G1 %001 F%1").arg(AxisChars[tc.axis]).arg(tc.v*0.5*60));
mw->sendGcode(QString("M400"));
testing=true;
}
#include <QWaitCondition>
bool TabVeltest::testSetting(uint8_t axis, float v, float a, float xyjerk, float zjerk)
{
struct TestCase tc;
tc.axis=axis;
tc.v=v;
tc.a=a;
tc.xyjerk=xyjerk;
tc.zjerk=zjerk;
float t=tc.v/tc.a;
if(tc.a*t*t/2.*2>200) return false;
bufferedCases.push_back(tc);
qDebug()<<"Adding testcase; "<<(testing?" already testing":"not testing until now")<<" a="<<a<<" v="<<v;
if(!testing)
{
mw->sendGcode(QString("G28 X0 Y0 Z0"));
mw->sendGcode(QString("G1 Z10 F400"));
startTestCase(bufferedCases[0]);
testing=true;
}
}
void TabVeltest::checkDone()
{
if(!testing)
return;
//qDebug()<<"checking"<<endl;
if(mw->sendcodes.size()!=0) //if mainwindow is still sending, not finished
return;
qDebug()<<"no more sending";
if(bufferedCases.size()==0) //if cue is empty, we are finished
{
testing=false;
return;
}
//qDebug()<<"still stuff do do."<<endl;
qDebug()<<"Testcase finished";
//we here know we have at least one buffered case
bool badresult=(mw->endstopfound);
if(!badresult)
{
sucess_x.push_back(bufferedCases[0].v);
sucess_y.push_back(bufferedCases[0].a);
curveGood->setSamples(sucess_x,sucess_y);
}
else
{
fail_x.push_back(bufferedCases[0].v);
fail_y.push_back(bufferedCases[0].a);
curveFail->setSamples(fail_x,fail_y);
}
plot->replot();
float lasta,lastv;
lasta=bufferedCases[0].a;
lastv=bufferedCases[0].v;
bufferedCases.removeFirst();
qDebug()<<"still buffered"<<bufferedCases.size()<<endl;
if(bufferedCases.size())
{
if(!badresult)
startTestCase(bufferedCases[0]);
else
{
while(bufferedCases[0].a>=lasta && bufferedCases[0].v>=lastv)
{
bufferedCases.removeFirst();
if(bufferedCases.size()==0)
{
qDebug()<<"Testing done"<<endl;;
testing=false;
break;
}
}
}
}
else
{
qDebug()<<"Testing done"<<endl;;
testing=false;
}
}