-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPID_Visualizer.java
More file actions
294 lines (268 loc) · 7.28 KB
/
Copy pathPID_Visualizer.java
File metadata and controls
294 lines (268 loc) · 7.28 KB
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
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class PID_Visualizer extends PApplet {
float middleLineY;
int middleLineWidth = 15;
boolean moving1, moving2, moving3, moving4;
float screenSpeed = 10;
float carSpeed = 5;
float x = 0;
PImage car;
int carHeight = 100;
int carWidth = 200;
float wheelAngle;
float carAngle;
float x_car = -400;
float y_car;
float x_car_speed;
float y_car_speed;
float a = 0;
final float MAX_WHEEL_ANGLE = 0.5f;
final float MAX_ERROR = 900;
float error, err;
float Kp = 10;
float Ki = 0.1f;
float Kd = 0.1f;
final float MAX_KP = 50;
final float MAX_KI = 1;
final float MAX_KD = 1;
final float MIN_KP = 0;
final float MIN_KI = 0;
final float MIN_KD = 0;
float kpy, kiy, kdy;
float work;
float lastTime, errSum, lastErr, dErr;
float MAX_SLIDER = height-155;
float MIN_SLIDER = height-25;
boolean manual = true;
boolean reset = false;
int count1 = 0;
int count2 = 0;
float auto_speed = 0.01f;
float randNum;
public void setup() {
middleLineY = height/2;
y_car = height/2;
MAX_SLIDER = height-155;
MIN_SLIDER = height-25;
//y_car=height;
a = 0;
background(51);
drawDashedLine(x, middleLineY+10);
drawDashedLine(x, middleLineY-10);
car = loadImage("blue_car.png");
imageMode(CENTER);
kpy = map(Kp, MIN_KP, MAX_KP, MIN_SLIDER, MAX_SLIDER);
kiy = map(Ki, MIN_KI, MAX_KI, MIN_SLIDER, MAX_SLIDER);
kdy = map(Kd, MIN_KI, MAX_KI, MIN_SLIDER, MAX_SLIDER);
randNum = random(100, height-500);
}
public void draw() {
if(manual){
if(moving1 || (mouseY >= middleLineY - 40 && mouseY <= middleLineY + 40)) {
if(mousePressed){
moving1 = true;
middleLineY = pmouseY;
}
}
}
else{
middleLineY = map(a, -4, 4, 100, height-100);
a += auto_speed*(sin(random(0, 2*PI)) + sin(random(0, 2*PI)) + sin(random(0, 2*PI)) + sin(random(0, 2*PI)));
}
if(moving2 || (mouseX>=width-310 && mouseX<=width-280 && mouseY >= kpy-5 && mouseY <= kpy+25)){
if(mousePressed){
moving2 = true;
if(pmouseY <= MIN_SLIDER && pmouseY >= MAX_SLIDER) {
kpy = pmouseY; }
}
}
if(moving3 || (mouseX>=width-190 && mouseX<=width-160 && mouseY >= kiy-5 && mouseY <= kiy+25)){
if(mousePressed){
moving3 = true;
if(pmouseY <= MIN_SLIDER && pmouseY >= MAX_SLIDER) {
kiy = pmouseY; }
}
}
if(moving4 || (mouseX>=width-70 && mouseX<=width-40 && mouseY >= kdy-5 && mouseY <= kdy+25)){
if(mousePressed) {
moving4 = true;
if(pmouseY <= MIN_SLIDER && pmouseY >= MAX_SLIDER) {
kdy = pmouseY;
}
}
}
if(mousePressed && (mouseX>=20 && mouseX<=105 && mouseY >= height -100 && mouseY <= height - 10)) count1++;
if(mousePressed && (mouseX>=135 && mouseX<=220 && mouseY >= height -100 && mouseY <= height - 10)) {
count2++;
reset = true;
}
background(51);
drawDashedLine(x, middleLineY+75);
drawDashedLine(x, middleLineY-75);
carSpeed = ((width*3)/7-x_car)*0.01f+10;
drawCar(x_car, y_car, carAngle);
PID_Sliders(kpy, kiy, kdy);
drawErrorLine();
error = middleLineY - y_car;
cornerInfo((int) error, (int) carSpeed);
bottomButtons(manual,reset);
x -= screenSpeed;
err = map(error, -MAX_ERROR, MAX_ERROR, -MAX_WHEEL_ANGLE, MAX_WHEEL_ANGLE);
work = PIDwork(Kp, Ki, Kd, err);
wheelAngle = work;
carAngle = wheelAngle/2;
x_car -= vX_car(carAngle, screenSpeed, carSpeed);
y_car += vY_car(carAngle, screenSpeed, carSpeed);
}
public void PID_Sliders(float Kpy, float Kiy, float Kdy) {
fill(255);
rect(width-410, height-190, width, height);
fill(20,20,20);
rect(width-400, height-180, width, height);
textSize(25);
fill(255);
text("Kp:", width-380, height-145);
text("Ki:", width-260, height-145);
text("Kd:", width-140, height-145);
textSize(18);
text(Kp, width-380, height-100);
text(Ki, width-260, height-100);
text(Kd, width-140, height-100);
fill(160);
rect(width-310, height-160, 10, 140, 5);
rect(width-190, height-160, 10, 140, 5);
rect(width-70, height-160, 10, 140, 5);
ellipseMode(CENTER);
fill(0,0,255);
ellipse(width-305, Kpy, 20, 20);
Kp = map(Kpy, MIN_SLIDER, MAX_SLIDER, MIN_KP, MAX_KP);
fill(0,255,0);
ellipse(width-185, Kiy, 20, 20);
Ki = map(Kiy, MIN_SLIDER, MAX_SLIDER, MIN_KI, MAX_KI);
fill(255,0,0);
ellipse(width-65, Kdy, 20, 20);
Kd = map(Kdy, MIN_SLIDER, MAX_SLIDER, MIN_KD, MAX_KD);
}
public void bottomButtons(boolean manual, boolean reset){
fill(255);
int h = 150;
rect(0,height-h, 250, h);
fill(31);
h -= 10;
rect(0,height-h, 240, h);
fill(255);
text("Random", 15, height -110);
text("Reset", 145, height - 110);
h -= 40;
if(manual) fill(60);
else fill(190);
rect(20, height-h, 85,85);
if(reset) fill(60);
else fill(190);
rect(135, height-h, 85,85);
}
public void cornerInfo(int error, int carSpeed){
fill(255);
rect(0,0,210,100);
fill(30);
rect(0,0,200,90);
typeError(error);
typeCarSpeed(carSpeed);
}
public void typeError(float error){
fill(255,0,0);
textSize(25);
String erStr = "Error: " + (int)error;
text(erStr, 10, 30);
}
public void typeCarSpeed(float speed){
fill(255,0,0);
textSize(25);
String erStr = "Car Speed: " + (int)speed;
text(erStr, 10, 60);
}
public void drawErrorLine(){
if(abs(middleLineY - y_car)>1){
fill(255,0,0);
if(middleLineY < y_car) rect(x_car-2, middleLineY, 4, abs(middleLineY - y_car));
if(y_car < middleLineY) rect(x_car-2, y_car, 4, abs(middleLineY - y_car));
}
}
public float PIDwork(float Kp, float Ki, float Kd, float error){
long now = millis();
float timeChange = (float)(now - lastTime);
errSum += (error * timeChange);
dErr = (error - lastErr) / timeChange;
lastErr = error;
lastTime = now;
return Kp * error + Ki * errSum + Kd * dErr;
}
public float vX_car(float a, float v1, float v2) {
return v1 - cos(a)*v2;
}
public float vY_car(float a, float v1, float v2) {
return sin(a)*v1;
}
public void drawCar(float x_pos, float y_pos, float rot) {
pushMatrix();
translate(x_pos, y_pos);
rotate(rot);
image(car, 0, 0, carWidth, carHeight);
popMatrix();
}
public void mouseReleased() {
moving1 = false;
moving2 = false;
moving3 = false;
moving4 = false;
if(count1 > 0){
manual = !manual;
count1 = 0;
}
if(count2 > 0){
count2 = 0;
reset();
reset = false;
}
}
public void reset()
{
Kp = 10;
Ki = 0.1f;
Kd = 0.1f;
kpy = map(Kp, MIN_KP, MAX_KP, MIN_SLIDER, MAX_SLIDER);
kiy = map(Ki, MIN_KI, MAX_KI, MIN_SLIDER, MAX_SLIDER);
kdy = map(Kd, MIN_KI, MAX_KI, MIN_SLIDER, MAX_SLIDER);
PID_Sliders(kpy, kiy, kdy);
middleLineY = height/2;
y_car = height/2;
x_car = -400;
manual = true;
}
public void drawDashedLine(float x,float y) {
for(float i = x; i < 10000; i += 90) {
fill(255);
rect(i, y - middleLineWidth/2, 40, middleLineWidth);
}
}
public void settings() { size(1800, 900); }
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "PID_Visualizer" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}