-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIDtunningTool_fumbled_about.pde
175 lines (148 loc) · 4.55 KB
/
PIDtunningTool_fumbled_about.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
/* I needs Ramp Response */
int Speed=500; // define for Ramp speed in Steps per Second VERY aproximately as it just changes a delay length
int Steps=250; // define length of Ramp in Steps
boolean to_ramp_or_not_to_ramp=true; // DO A BARREL ROLL...?
// 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
import java.util.*;
import processing.serial.*;
Serial myPort; // The serial port
float kp=2, ki=0.125, kd=0.01;
void setup() {
size(1200, 800);
PFont myFont = createFont(PFont.list()[2], 14);
textFont(myFont);
frameRate(1);
// List all the available serial ports:
println(Serial.list());
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 115200);
}
void draw() {
if(to_ramp_or_not_to_ramp==true){
strokeWeight(1);
background(255,100); stroke(255,0,0);
line(0,800,1000,200);
line(1000,200,1200,200);
strokeWeight(.1);
for(int i=1;i<12;i++) line(i*100,0,i*100,800);
delay(500);
stroke(0,0,255); strokeWeight(2);
while(myPort.available()>0) print(char(myPort.read()));
do_a_Ramp(Steps,Speed);
delay(500);
myPort.write('S');
myPort.write('\r');
myPort.write('\n');
delay(200);
String s="";
while(myPort.available()>0) s+=char(myPort.read());
Scanner sc = new Scanner(s);
float xscale=(600/((float(Steps)/float(Speed))/0.002))*(float(13)/float(9));
int x=0;
while(sc.hasNextInt()) {
point((x++)*xscale, 800-2.4*sc.nextInt());
}
delay(500);
}
else{
strokeWeight(1);
background(255,100); stroke(255,0,0);
line(0,200,1200,200);
strokeWeight(.1);
for(int i=1;i<12;i++) line(i*100,0,i*100,800);
delay(500);
stroke(0,0,255); strokeWeight(2);
while(myPort.available()>0) print(char(myPort.read()));
myPort.write('X');
myPort.write('1');
myPort.write('0');
myPort.write('0');
myPort.write('\r');
myPort.write('\n');
delay(500);
myPort.write('S');
myPort.write('\r');
myPort.write('\n');
delay(200);
String s="";
while(myPort.available()>0) s+=char(myPort.read());
Scanner sc = new Scanner(s);
int x=0;
while(sc.hasNextInt()) {
point((x++)*4, 800-6*sc.nextInt());
}
}
myPort.write('X');
//myPort.write('0');
//myPort.write('0');
myPort.write('0');
myPort.write('\r');
myPort.write('\n');
delay(500);
}
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);
}
}
/* new code */
void do_a_Ramp(int Steps, int Speed){
int delaytime=1000/Speed; // delaytime after each Step according to Speed
myPort.write('N'); // Stets the 'p' couter that monitors the positions in the Controller Firmware to 0
for(int i=0; i<Steps; i++){ // ofc the rest of the code here takes no time to run and my Speed calculation is absolutely precise!
myPort.write('+');
delay(delaytime);
//println("Step="+i);
}
}