Repository navigation
Expand file tree
/
Copy pathpid.py
More file actions
68 lines (47 loc) · 1.54 KB
/
Copy pathpid.py
File metadata and controls
68 lines (47 loc) · 1.54 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
"""
A simple PID controller
"""
#PID time constant
dt = 0.01
class pid_controller(object):
def __init__(self, set_point,
k_p=0.1, k_i=0.1,k_d = 0.05,
limits=(None, None)):
self.set_point = set_point
self.k_p, self.k_i, self.k_d = k_p, k_i, k_d
self.min_output, self.max_output = limits
self.pv_last = None
self.proportional = 0.
self.integral = 0.
self.derivative = 0.
def clamp(self, value):
if self.max_output is not None and value > self.max_output:
return self.max_output
elif self.min_output is not None and value < self.min_output:
return self.min_output
else:
return value
def __call__(self, pv):
#error
error = self.set_point - pv
if self.pv_last is not None:
d_pv = pv - self.pv_last
else:
d_pv = 0.
#proportional term
self.proportional = self.k_p * error
#integral term
self.integral += self.k_i * error *dt
#below to prevent integral windup
self.integral = self.clamp(self.integral)
#derivative term
self.derivative = -self.k_d * d_pv / dt
#output
output = self.proportional + self.integral + self.derivative
output = self.clamp(output)
self.pv_last = pv
return output
def tunings(self):
return self.k_p, self.k_i, self.k_d
def components(self):
return self.proportional, self.integral, self.derivative