-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpid-controller.sim
More file actions
85 lines (75 loc) · 1.82 KB
/
pid-controller.sim
File metadata and controls
85 lines (75 loc) · 1.82 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
////////////////////
//Configuration
////////////////////
timestep 20
codestep 20
////////////////////
//Data
////////////////////
//////////////////
kf
number
0.0119402985074627
//////////////////
kp
number
0
//////////////////
ki
number
0
//////////////////
kd
number
.1
////////////////////
//Init Code
////////////////////
//Input to the motor
#in = 0;
//Other scaling parameters
#km = 60;
#kr = 0.5;
//Friction parameters
#ks = .1;
#kk = .7;
//Mass
#m = .6;
//Angular acceleration and velocity
#a = 0;
#v = 0;
//Pid constants
#kf = Sim.readData('kf');
#kp = Sim.readData('kp');
#ki = Sim.readData('ki');
#kd = Sim.readData('kd');
//Pid variables
#sp = 0;
#ksr = 0;
#pv = 0;
////////////////////
//Step Code
////////////////////
#sp = Sim.number_input('top:0px;left:0px','Set Point', #sp);
#kf = Sim.number_input('top:0px;left:200px','kf', #kf);
#kp = Sim.number_input('top:0px;left:400px','kp', #kp);
#ki = Sim.number_input('top:0px;left:600px','ki', #ki);
#kd = Sim.number_input('top:0px;left:800px','kd', #kd);
#a = (#in * #km + Sim.friction(#v, #ks, #kk) + Sim.nrandom() * #kr)/#m;
#v = Sim.integral(#a);
#pv = #v + Sim.nrandom() * #ksr;
//#in = Sim.pid(#sp, #pv, #kp, #ki, #kd, #kf * #sp, {l: 0, u: 1, il: -.1, iu: .1});
#in = Sim.pidv(#sp, #pv, #kp, #kd, #kf * #sp, {l: 0, u: 1, pl: -.1, pu: .1});
Sim.graph('top:50px;left:0px;width:1000px;height:500px','Graph',
{t_width: 10, t_spacing: 1, maxs: [1, 80, 80], mins: [0, 0, 0], spacings: [.25, 10, 10], colors: ['red', 'blue', 'green']},[#in, #v, #sp]);
Sim.label('top:550px;left:0px','Red: PID output');
Sim.label('top:550px;left:150px','Green: Set Point');
Sim.label('top:550px;left:300px','Blue: Process Variable');
////////////////////
//End Code
////////////////////
//Pid constants
Sim.writeData('kf', #kf);
Sim.writeData('kp', #kp);
Sim.writeData('ki', #ki);
Sim.writeData('kd', #kd);