forked from fwachsmuth/PID_FrontEnd_v04
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPID_FrontEnd_v04.pde
473 lines (401 loc) · 16.4 KB
/
PID_FrontEnd_v04.pde
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/********************************************************
* Arduino PID Tuning Front-End, Version 0.3
* by Brett Beauregard
* License: Creative-Commons Attribution Share-Alike
* April 2011
*
* This application is designed to interface with an
* arduino running the PID Library. From this Control
* Panel you can observe & adjust PID performance in
* real time
*
* The ControlP5 library is required to run this sketch.
* files and install instructions can be found at
* http://www.sojamo.de/libraries/controlP5/
*
********************************************************/
/* Updates /Infos to v_04:
- In line 102, set the index of your tty list to the one your sending Arduino is connected to.
The Processing console lists all Serial ports at the very beginning.
- making Setpoint/Input/Output changeable in the UI without Crashes
- replaced a couple of valueLabel with getValueLabel for latest P5 compatability
*/
import java.nio.ByteBuffer;
import processing.serial.*;
import controlP5.*;
/***********************************************
* User spcification section
**********************************************/
int windowWidth = 1280; // set the size of the
int windowHeight = 1024; // form
float InScaleMin = 0; // set the Y-Axis Min
float InScaleMax = 1024; // and Max for both
float OutScaleMin = 0; // the top and
float OutScaleMax = 255; // bottom trends
int windowSpan = 300000; // number of mS into the past you want to display
int refreshRate = 100; // how often you want the graph to be reDrawn;
//float displayFactor = 1; //display Time as Milliseconds
//float displayFactor = 1000; //display Time as Seconds
float displayFactor = 60000; //display Time as Minutes
String outputFileName = ""; // if you'd like to output data to
// a file, specify the path here
/***********************************************
* end user spec
**********************************************/
int nextRefresh;
int arrayLength = windowSpan / refreshRate+1;
int[] InputData = new int[arrayLength]; //we might not need them this big, but
int[] SetpointData = new int[arrayLength]; // this is worst case
int[] OutputData = new int[arrayLength];
float inputTop = 25;
float inputHeight = (windowHeight-70)*2/3;
float outputTop = inputHeight+50;
float outputHeight = (windowHeight-70)*1/3;
float ioLeft = 150, ioWidth = windowWidth-ioLeft-50;
float ioRight = ioLeft+ioWidth;
float pointWidth= (ioWidth)/float(arrayLength-1);
int vertCount = 10;
int nPoints = 0;
float Input, Setpoint, Output;
boolean madeContact =false;
boolean justSent = true;
Serial myPort;
ControlP5 controlP5;
controlP5.Button AMButton, DRButton;
controlP5.Textlabel AMLabel, AMCurrent, InLabel,
OutLabel, SPLabel, PLabel,
ILabel, DLabel,DRLabel, DRCurrent;
controlP5.Textfield SPField, InField, OutField,
PField, IField, DField;
PrintWriter output;
PFont AxisFont, TitleFont;
void setup()
{
frameRate(30);
size(1280 , 1024);
println(Serial.list()); // * Initialize Serial
myPort = new Serial(this, Serial.list()[3], 9600); // Communication with
myPort.bufferUntil(10); // the Arduino
controlP5 = new ControlP5(this); // * Initialize the various
SPField= controlP5.addTextfield("SetpointSetter",10,100,60,20).setInputFilter(ControlP5.FLOAT); // Buttons, Labels, and
InField = controlP5.addTextfield("InputSetter",10,150,60,20).setInputFilter(ControlP5.FLOAT); // Text Fields we'll be
OutField = controlP5.addTextfield("OutputSetter",10,200,60,20).setInputFilter(ControlP5.FLOAT); // using
PField = controlP5.addTextfield("Kp (Proportional)",10,275,60,20);//
IField = controlP5.addTextfield("Ki (Integral)",10,325,60,20);
DField = controlP5.addTextfield("Kd (Derivative)",10,375,60,20);
AMButton = controlP5.addButton("Toggle_AM",0.0,10,50,60,20); //
AMLabel = controlP5.addTextlabel("AM","Manual",12,72); //
AMCurrent = controlP5.addTextlabel("AMCurrent","Manual",80,65); //
controlP5.addButton("Send_To_Arduino",0.0,10,475,120,20); //
SPLabel=controlP5.addTextlabel("SP","3",80,103); //
InLabel=controlP5.addTextlabel("In","1",80,153); //
OutLabel=controlP5.addTextlabel("Out","2",80,203); //
PLabel=controlP5.addTextlabel("P","4",80,278); //
ILabel=controlP5.addTextlabel("I","5",80,328); //
DLabel=controlP5.addTextlabel("D","6",80,378); //
DRButton = controlP5.addButton("Toggle_DR",0.0,10,425,60,20); //
DRLabel = controlP5.addTextlabel("DR","Direct",12,447); //
DRCurrent = controlP5.addTextlabel("DRCurrent","Direct",80,440); //
AxisFont = loadFont("axis.vlw");
TitleFont = loadFont("Titles.vlw");
nextRefresh=millis();
if (outputFileName!="") output = createWriter(outputFileName);
}
void SetpointSetter(String v) {
Setpoint = float(v);
}
void InputSetter(String v) {
Input = float(v);
}
void OutputSetter(String v) {
Output = float(v);
}
void draw()
{
background(200);
drawGraph();
drawButtonArea();
}
void drawGraph()
{
//draw Base, gridlines
stroke(0);
fill(230);
rect(ioLeft, inputTop,ioWidth-1 , inputHeight);
rect(ioLeft, outputTop, ioWidth-1, outputHeight);
stroke(210);
//Section Titles
textFont(TitleFont);
fill(255);
text("PID Input / Setpoint",(int)ioLeft+10,(int)inputTop-5);
text("PID Output",(int)ioLeft+10,(int)outputTop-5);
//GridLines and Titles
textFont(AxisFont);
//horizontal grid lines
int interval = (int)inputHeight/5;
for(int i=0;i<6;i++)
{
if(i>0&&i<5) line(ioLeft+1,inputTop+i*interval,ioRight-2,inputTop+i*interval);
text(str((InScaleMax-InScaleMin)/5*(float)(5-i)+InScaleMin),ioRight+5,inputTop+i*interval+4);
}
interval = (int)outputHeight/5;
for(int i=0;i<6;i++)
{
if(i>0&&i<5) line(ioLeft+1,outputTop+i*interval,ioRight-2,outputTop+i*interval);
text(str((OutScaleMax-OutScaleMin)/5*(float)(5-i)+OutScaleMin),ioRight+5,outputTop+i*interval+4);
}
//vertical grid lines and TimeStamps
int elapsedTime = millis();
interval = (int)ioWidth/vertCount;
int shift = elapsedTime*(int)ioWidth / windowSpan;
shift %=interval;
int iTimeInterval = windowSpan/vertCount;
float firstDisplay = (float)(iTimeInterval*(elapsedTime/iTimeInterval))/displayFactor;
float timeInterval = (float)(iTimeInterval)/displayFactor;
for(int i=0;i<vertCount;i++)
{
int x = (int)ioRight-shift-2-i*interval;
line(x,inputTop+1,x,inputTop+inputHeight-1);
line(x,outputTop+1,x,outputTop+outputHeight-1);
float t = firstDisplay-(float)i*timeInterval;
if(t>=0) text(str(t),x,outputTop+outputHeight+10);
}
// add the latest data to the data Arrays. the values need
// to be massaged to get them to graph correctly. they
// need to be scaled to fit where they're going, and
// because 0, 0 is the top left, we need to flip the values.
// this is easier than having the user stand on their head
// to read the graph.
if(millis() > nextRefresh && madeContact)
{
nextRefresh += refreshRate;
for(int i=nPoints-1;i>0;i--)
{
InputData[i]=InputData[i-1];
SetpointData[i]=SetpointData[i-1];
OutputData[i]=OutputData[i-1];
}
if (nPoints < arrayLength) nPoints++;
InputData[0] = int(inputHeight)-int(inputHeight*(Input-InScaleMin)/(InScaleMax-InScaleMin));
SetpointData[0] =int( inputHeight)-int(inputHeight*(Setpoint-InScaleMin)/(InScaleMax-InScaleMin));
OutputData[0] = int(outputHeight)-int(outputHeight*(Output-OutScaleMin)/(OutScaleMax-OutScaleMin));
}
//draw lines for the input, setpoint, and output
strokeWeight(2);
for(int i=0; i<nPoints-2; i++)
{
int X1 = int(ioRight-2-float(i)*pointWidth);
int X2 = int(ioRight-2-float(i+1)*pointWidth);
boolean y1Above, y1Below, y2Above, y2Below;
//DRAW THE INPUT
boolean drawLine=true;
stroke(255,0,0);
int Y1 = InputData[i];
int Y2 = InputData[i+1];
y1Above = (Y1>inputHeight); // if both points are outside
y1Below = (Y1<0); // the min or max, don't draw the
y2Above = (Y2>inputHeight); // line. if only one point is
y2Below = (Y2<0); // outside constrain it to the limit,
if(y1Above) // and leave the other one untouched.
{ //
if(y2Above) drawLine=false; //
else if(y2Below) { //
Y1 = (int)inputHeight; //
Y2 = 0; //
} //
else Y1 = (int)inputHeight; //
} //
else if(y1Below) //
{ //
if(y2Below) drawLine=false; //
else if(y2Above) { //
Y1 = 0; //
Y2 = (int)inputHeight; //
} //
else Y1 = 0; //
} //
else //
{ //
if(y2Below) Y2 = 0; //
else if(y2Above) Y2 = (int)inputHeight; //
} //
if(drawLine)
{
line(X1,Y1+inputTop, X2, Y2+inputTop);
}
//DRAW THE SETPOINT
drawLine=true;
stroke(0,255,0);
Y1 = SetpointData[i];
Y2 = SetpointData[i+1];
y1Above = (Y1>(int)inputHeight); // if both points are outside
y1Below = (Y1<0); // the min or max, don't draw the
y2Above = (Y2>(int)inputHeight); // line. if only one point is
y2Below = (Y2<0); // outside constrain it to the limit,
if(y1Above) // and leave the other one untouched.
{ //
if(y2Above) drawLine=false; //
else if(y2Below) { //
Y1 = (int)(inputHeight); //
Y2 = 0; //
} //
else Y1 = (int)(inputHeight); //
} //
else if(y1Below) //
{ //
if(y2Below) drawLine=false; //
else if(y2Above) { //
Y1 = 0; //
Y2 = (int)(inputHeight); //
} //
else Y1 = 0; //
} //
else //
{ //
if(y2Below) Y2 = 0; //
else if(y2Above) Y2 = (int)(inputHeight); //
} //
if(drawLine)
{
line(X1, Y1+inputTop, X2, Y2+inputTop);
}
//DRAW THE OUTPUT
drawLine=true;
stroke(0,0,255);
Y1 = OutputData[i];
Y2 = OutputData[i+1];
y1Above = (Y1>outputHeight); // if both points are outside
y1Below = (Y1<0); // the min or max, don't draw the
y2Above = (Y2>outputHeight); // line. if only one point is
y2Below = (Y2<0); // outside constrain it to the limit,
if(y1Above) // and leave the other one untouched.
{ //
if(y2Above) drawLine=false; //
else if(y2Below) { //
Y1 = (int)outputHeight; //
Y2 = 0; //
} //
else Y1 = (int)outputHeight; //
} //
else if(y1Below) //
{ //
if(y2Below) drawLine=false; //
else if(y2Above) { //
Y1 = 0; //
Y2 = (int)outputHeight; //
} //
else Y1 = 0; //
} //
else //
{ //
if(y2Below) Y2 = 0; //
else if(y2Above) Y2 = (int)outputHeight; //
} //
if(drawLine)
{
line(X1, outputTop + Y1, X2, outputTop + Y2);
}
}
strokeWeight(1);
}
void drawButtonArea()
{
stroke(0);
fill(100);
rect(0, 0, ioLeft, windowHeight);
}
void Toggle_AM() {
if(AMLabel.getValueLabel().getText()=="Manual")
{
AMLabel.setValue("Automatic");
}
else
{
AMLabel.setValue("Manual");
}
}
void Toggle_DR() {
if(DRLabel.getValueLabel().getText()=="Direct")
{
DRLabel.setValue("Reverse");
}
else
{
DRLabel.setValue("Direct");
}
}
// Sending Floating point values to the arduino
// is a huge pain. if anyone knows an easier
// way please let know. the way I'm doing it:
// - Take the 6 floats we need to send and
// put them in a 6 member float array.
// - using the java ByteBuffer class, convert
// that array to a 24 member byte array
// - send those bytes to the arduino
void Send_To_Arduino()
{
float[] toSend = new float[6];
toSend[0] = float(SPField.getText());
toSend[1] = float(InField.getText());
toSend[2] = float(OutField.getText());
toSend[3] = float(PField.getText());
toSend[4] = float(IField.getText());
toSend[5] = float(DField.getText());
Byte a = (AMLabel.getValueLabel().getText()=="Manual")?(byte)0:(byte)1;
Byte d = (DRLabel.getValueLabel().getText()=="Direct")?(byte)0:(byte)1;
myPort.write(a);
myPort.write(d);
myPort.write(floatArrayToByteArray(toSend));
justSent=true;
}
byte[] floatArrayToByteArray(float[] input)
{
int len = 4*input.length;
int index=0;
byte[] b = new byte[4];
byte[] out = new byte[len];
ByteBuffer buf = ByteBuffer.wrap(b);
for(int i=0;i<input.length;i++)
{
buf.position(0);
buf.putFloat(input[i]);
for(int j=0;j<4;j++) out[j+i*4]=b[3-j];
}
return out;
}
//take the string the arduino sends us and parse it
void serialEvent(Serial myPort)
{
String read = myPort.readStringUntil(10);
if(outputFileName!="") output.print(str(millis())+ " "+read);
String[] s = split(read, " ");
if (s.length ==9)
{
Setpoint = float(s[1]); // * pull the information
Input = float(s[2]); // we need out of the
Output = float(s[3]); // string and put it
SPLabel.setValue(s[1]); // where it's needed
InLabel.setValue(s[2]); //
OutLabel.setValue(trim(s[3])); //
PLabel.setValue(trim(s[4])); //
ILabel.setValue(trim(s[5])); //
DLabel.setValue(trim(s[6])); //
AMCurrent.setValue(trim(s[7])); //
DRCurrent.setValue(trim(s[8]));
if(justSent) // * if this is the first read
{ // since we sent values to
SPField.setText(trim(s[1])); // the arduino, take the
InField.setText(trim(s[2])); // current values and put
OutField.setText(trim(s[3])); // them into the input fields
PField.setText(trim(s[4])); //
IField.setText(trim(s[5])); //
DField.setText(trim(s[6])); //
// mode = trim(s[7]); //
AMLabel.setValue(trim(s[7])); //
//dr = trim(s[8]); //
DRCurrent.setValue(trim(s[8])); //
justSent=false; //
} //
if(!madeContact) madeContact=true;
}
}