-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathexample_pid_vs_discretePID.py
More file actions
141 lines (109 loc) · 3.29 KB
/
example_pid_vs_discretePID.py
File metadata and controls
141 lines (109 loc) · 3.29 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
#########################################################################################
##
## PathSim Example for PID-controller VS Discrete PID
##
#########################################################################################
# IMPORTS ===============================================================================
import numpy as np
import matplotlib.pyplot as plt
from pathsim import Simulation, Connection
from pathsim.blocks import Source, Integrator, Amplifier, Adder, Scope, PID, Wrapper
from pathsim.solvers import RKCK54
class DiscretePID(Wrapper):
"""
Discrete PID controller
Parameters
----------
T : float
sampling period for the PID controller
tau : float
delay on Schedule event (see Schedule class)
Kp : float
proportional gain
Ki : float
integral gain
Kd : float
derivative gain
Attributes
----------
Kp : float
proportional gain
Ki : float
integral gain
Kd : float
derivative gain
integral : float
integral value for the PID controller
prev_error : float
previous error value for derivative part in the PID controller
"""
def __init__(self, T=1, tau=0, Kp=1, Ki=1, Kd=1):
super().__init__(T=T, tau=tau)
self.Kp = Kp
self.Ki = Ki
self.Kd = Kd
self.integral = 0
self.prev_error = 0
def func(self, error):
"""
Run the PID controller
Parameters
----------
error : float
error signal
Returns
-------
output : float
output of PID controller to correct the system
"""
self.integral += error * self.T
derivative = (error - self.prev_error) / self.T if self.T != 0 else 0
output = self.Kp * error + self.Ki * self.integral + self.Kd * derivative
self.prev_error = error
return output
# SYSTEM SETUP AND SIMULATION ===========================================================
#plant gain
K = 0.4
#pid parameters
Kp, Ki, Kd = 1.5, 0.5, 0.1
#source function
def f_s(t):
if t>60: return 0.5
elif t>20: return 1
else: return 0
#blocks
spt = Source(f_s)
err = Adder("+-")
pid = PID(Kp, Ki, Kd, f_max=10)
pnt = Integrator()
pgn = Amplifier(K)
sco = Scope(labels=[r"$\epsilon(t)$", r"$\epsilon _d(t)$"])
spt2 = Source(f_s)
err2 = Adder("+-")
dis_pid = DiscretePID(T=1,tau=0, Kp=Kp, Ki=Ki, Kd=Kd)
pnt2 = Integrator()
pgn2 = Amplifier(K)
sco2 = Scope(labels=["u(t)", "u_dis(t)"])
blocks = [spt, err, pid, pnt, pgn, sco, dis_pid, sco2, spt2, err2, pnt2, pgn2]
connections = [
Connection(spt, err),
Connection(pgn, err[1]),
Connection(err, pid, sco[0]),
Connection(pid, pnt, sco2[0]),
Connection(pnt, pgn),
Connection(spt2, err2),
Connection(pgn2, err2[1]),
Connection(err2, dis_pid, sco[1]),
Connection(dis_pid, pnt2, sco2[1]),
Connection(pnt2, pgn2),
]
#simulation initialization
Sim = Simulation(blocks, connections, Solver=RKCK54)
# Run Example ===========================================================================
if __name__ == "__main__":
#run the simulation for some time
Sim.run(100)
sco.plot(lw=2)
sco2.plot(lw=2)
#plot sensitivities
plt.show()