-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfermenterpid.py
42 lines (37 loc) · 1.34 KB
/
fermenterpid.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
class FermenterPID:
def __init__(self, kp=2, ki=0.0001, kd=2):
self.k_p = float(kp)
self.k_i = float(ki)
self.k_d = float(kd)
self.k_p_backup = self.k_p
self.k_i_backup = self.k_i
self.k_d_backup = self.k_d
self.last_error = 0
self.integration = 0
self.last_output = 0
def update(self, temp, setpoint):
"""
temp: float; real-time temperature measured by ds18 sensor
setpoint: float; target temperature to achieve
return: float; temperature correction
"""
error = setpoint - temp
if self.last_error == 0:
self.last_error = error #catch first run error
P_value = self.k_p * error
D_value = -(self.k_d * (error - self.last_error))
self.last_error = error
if -15 < self.last_output < 15:
self.integration = self.integration + error
I_value = self.integration * self.k_i
self.last_output = max(min(P_value + I_value + D_value, 15), -15)
return self.last_output
def reset(self, kp=0, ki=0, kd=0):
if kp or ki or kd:
self.k_p = float(kp)
self.k_i = float(ki)
self.k_d = float(kd)
else:
self.k_p = self.k_p_backup
self.k_i = self.k_i_backup
self.k_d = self.k_d_backup