-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpidypi.py
131 lines (110 loc) · 4.3 KB
/
pidypi.py
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
import time
class pidpy(object):
ek_1 = 0.0 # e[k-1] = SP[k-1] - PV[k-1] = Tset_hlt[k-1] - Thlt[k-1]
ek_2 = 0.0 # e[k-2] = SP[k-2] - PV[k-2] = Tset_hlt[k-2] - Thlt[k-2]
xk_1 = 0.0 # PV[k-1] = Thlt[k-1]
xk_2 = 0.0 # PV[k-2] = Thlt[k-1]
yk_1 = 0.0 # y[k-1] = Gamma[k-1]
yk_2 = 0.0 # y[k-2] = Gamma[k-1]
lpf_1 = 0.0 # lpf[k-1] = LPF output[k-1]
lpf_2 = 0.0 # lpf[k-2] = LPF output[k-2]
yk = 0.0 # output
GMA_HLIM = 100.0
GMA_LLIM = 0.0
def __init__(self, ts, kc, ti, td):
self.kc = kc
self.ti = ti
self.td = td
self.ts = ts
self.k_lpf = 0.0
self.k0 = 0.0
self.k1 = 0.0
self.k2 = 0.0
self.k3 = 0.0
self.lpf1 = 0.0
self.lpf2 = 0.0
self.ts_ticks = 0
self.pid_model = 3
self.pp = 0.0
self.pi = 0.0
self.pd = 0.0
if (self.ti == 0.0):
self.k0 = 0.0
else:
self.k0 = self.kc * self.ts / self.ti
self.k1 = self.kc * self.td / self.ts
self.lpf1 = (2.0 * self.k_lpf - self.ts) / (2.0 * self.k_lpf + self.ts)
self.lpf2 = self.ts / (2.0 * self.k_lpf + self.ts)
def calcPID_reg3(self, xk, tset, enable):
ek = 0.0
lpf = 0.0
ek = tset - xk # calculate e[k] = SP[k] - PV[k]
#--------------------------------------
# Calculate Lowpass Filter for D-term
#--------------------------------------
lpf = self.lpf1 * pidpy.lpf_1 + self.lpf2 * (ek + pidpy.ek_1);
if (enable):
#-----------------------------------------------------------
# Calculate PID controller:
# y[k] = y[k-1] + kc*(e[k] - e[k-1] +
# Ts*e[k]/Ti +
# Td/Ts*(lpf[k] - 2*lpf[k-1] + lpf[k-2]))
#-----------------------------------------------------------
self.pp = self.kc * (ek - pidpy.ek_1) # y[k] = y[k-1] + Kc*(PV[k-1] - PV[k])
self.pi = self.k0 * ek # + Kc*Ts/Ti * e[k]
self.pd = self.k1 * (lpf - 2.0 * pidpy.lpf_1 + pidpy.lpf_2)
pidpy.yk += self.pp + self.pi + self.pd
else:
pidpy.yk = 0.0
self.pp = 0.0
self.pi = 0.0
self.pd = 0.0
pidpy.ek_1 = ek # e[k-1] = e[k]
pidpy.lpf_2 = pidpy.lpf_1 # update stores for LPF
pidpy.lpf_1 = lpf
# limit y[k] to GMA_HLIM and GMA_LLIM
if (pidpy.yk > pidpy.GMA_HLIM):
pidpy.yk = pidpy.GMA_HLIM
if (pidpy.yk < pidpy.GMA_LLIM):
pidpy.yk = pidpy.GMA_LLIM
return pidpy.yk
def calcPID_reg4(self, xk, tset, enable):
ek = 0.0
ek = tset - xk # calculate e[k] = SP[k] - PV[k]
if (enable):
#-----------------------------------------------------------
# Calculate PID controller:
# y[k] = y[k-1] + kc*(PV[k-1] - PV[k] +
# Ts*e[k]/Ti +
# Td/Ts*(2*PV[k-1] - PV[k] - PV[k-2]))
#-----------------------------------------------------------
self.pp = self.kc * (pidpy.xk_1 - xk) # y[k] = y[k-1] + Kc*(PV[k-1] - PV[k])
self.pi = self.k0 * ek # + Kc*Ts/Ti * e[k]
self.pd = self.k1 * (2.0 * pidpy.xk_1 - xk - pidpy.xk_2)
pidpy.yk += self.pp + self.pi + self.pd
else:
pidpy.yk = 0.0
self.pp = 0.0
self.pi = 0.0
self.pd = 0.0
pidpy.xk_2 = pidpy.xk_1 # PV[k-2] = PV[k-1]
pidpy.xk_1 = xk # PV[k-1] = PV[k]
# limit y[k] to GMA_HLIM and GMA_LLIM
if (pidpy.yk > pidpy.GMA_HLIM):
pidpy.yk = pidpy.GMA_HLIM
if (pidpy.yk < pidpy.GMA_LLIM):
pidpy.yk = pidpy.GMA_LLIM
return pidpy.yk
if __name__=="__main__":
temps = [22.0, 22.5, 23, 23.5, 23.5, 24, 24.3, 25, 25, 25, 25, 25, 25, 24.6, 24, 25]
sampleTime = 2
pid = pidpy(sampleTime,44,165,4)
temp = 5
setpoint = 25
enable = True
for index in range(len(temps)):
print pid.calcPID_reg4(temps[index], setpoint, enable)
time.sleep(sampleTime)
# while True:
# print pid.calcPID_reg4(temp, setpoint, enable)
# time.sleep(2)