forked from misan/dcservo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIDtunningTool.pde
134 lines (122 loc) · 3.62 KB
/
PIDtunningTool.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
// simple tool for testing the PID response using some of the features of new firmware (S command returns the last encoder positions from last X command
// by misan 2015
// it is a loop of moves X100 .. X0 .. X100 .. X0 ...
// "P", "I" or "D" keys increase existing PID values
// p i d keys decrease respective values
// it draws "almost" in real time motor response
// modified for longer target values than 100
import java.util.*;
import processing.serial.*;
Serial myPort; // The serial port
int targetVal = 220; // use this to set the target value
//please note the warparound effect on th graph if using values > 255 as Arduino only recording 8 bits location values :-(
float kp=19, ki=0.5, kd=0.17;
void setup() {
size(800, 400);
// create a font with the third font available to the system:
PFont myFont = createFont(PFont.list()[2], 14);
textFont(myFont);
frameRate(1);
// List all the available serial ports:
println(Serial.list());
String portName = Serial.list()[3];
myPort = new Serial(this, portName, 115200);
}
void draw() {
strokeWeight(1);
background(255,100); stroke(255,0,0);
line(0,400-333,600,400-333);
strokeWeight(.1);
for(int i=1;i<6;i++) line(i*100,0,i*100,400);
delay(500 + targetVal / 10);
stroke(0,0,255); strokeWeight(2);
while(myPort.available()>0) print(char(myPort.read()));
myPort.write('X');
String out = "" + targetVal;
for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
myPort.write('\r');
myPort.write('\n');
delay(500 + targetVal / 10);
myPort.write('S');
myPort.write('\r');
myPort.write('\n');
delay(500 + targetVal / 10);
String s="";
while(myPort.available()>0) s+=char(myPort.read());
//println(s);
Scanner sc = new Scanner(s);
int x=0;
while(sc.hasNextInt()) {
float y = sc.nextInt();
float c = map (y,0,targetVal<255 ? targetVal : 255,0,300);
point(x++, 400-c);
}
myPort.write('X');
myPort.write('0');
myPort.write('\r');
myPort.write('\n');
delay(500 + targetVal / 10);
}
void keyPressed() {
if(key=='P') {
kp+=1;
myPort.write('P');
String out=""+kp; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
myPort.write('\r');
myPort.write('\n');
println("P="+kp);
}
if(key=='p') {
kp-=1; kp=max(0,kp);
myPort.write('P');
String out=""+kp; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
myPort.write('\r');
myPort.write('\n');
println("P="+kp);
}
if(key=='D')
{
kd+=0.01;
myPort.write('D');
String out=""+kd; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
myPort.write('\r');
myPort.write('\n');
println("D="+kd);
}
if(key=='d') {
kd-=0.01; kd=max(0,kd);
myPort.write('D');
String out=""+kd; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
myPort.write('\r');
myPort.write('\n');
println("D="+kd);
}
if(key=='I') {
ki*=2;
myPort.write('I');
String out=""+ki; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
myPort.write('\r');
myPort.write('\n');
println("I="+ki);
}
if(key=='i') {
ki/=2;
myPort.write('I');
String out=""+ki; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
myPort.write('\r');
myPort.write('\n');
println("I="+ki);
}
if(key=='W') {
myPort.write('W');
myPort.write('\r');
myPort.write('\n');
println("Values stored.");
}
if(key=='Q') {
myPort.write('Q');
myPort.write('\r');
myPort.write('\n');
println("Values requested.");
}
}