-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logic.py
189 lines (151 loc) · 5.26 KB
/
Logic.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import datetime
import statistics
class PID:
"""
Discrete PID control
"""
def __init__(self, P=1.0, I=1.0, D=1.0, Derivator=0, Integrator=0, Integrator_max=500, Integrator_min=-500):
self.Kp=P
self.Ki=I
self.Kd=D
self.Derivator=Derivator
self.Integrator=Integrator
self.Integrator_max=Integrator_max
self.Integrator_min=Integrator_min
self.set_point=0.0
self.error=0.0
def update(self,current_value):
"""
Calculate PID output value for given reference input and feedback
"""
self.error = self.set_point - current_value
self.P_value = self.Kp * self.error
self.D_value = self.Kd * ( self.error - self.Derivator)
self.Derivator = self.error
self.Integrator = self.Integrator + self.error
if self.Integrator > self.Integrator_max:
self.Integrator = self.Integrator_max
elif self.Integrator < self.Integrator_min:
self.Integrator = self.Integrator_min
self.I_value = self.Integrator * self.Ki
PID = self.P_value + self.I_value + self.D_value
return PID
def setPoint(self,set_point):
"""
Initilize the setpoint of PID
"""
self.set_point = set_point
self.Integrator=0
self.Derivator=0
def setIntegrator(self, Integrator):
self.Integrator = Integrator
def setDerivator(self, Derivator):
self.Derivator = Derivator
def setKp(self,P):
self.Kp=P
def setKi(self,I):
self.Ki=I
def setKd(self,D):
self.Kd=D
def getPoint(self):
return self.set_point
def getError(self):
return self.error
def getIntegrator(self):
return self.Integrator
def getDerivator(self):
return self.Derivator
class DataLogger:
def __init__(self):
pass
class Controls:
def __init__(self):
self.hlt = Keg()
self.mash = Keg()
self.boil = Keg()
self.steps = {1:self.hlt, 2:self.mash, 3:self.hlt, 4:self.boil}
self.currentStep = 1
self.keg = self.steps[1]
self.done = False
self.change = True
self.controller = PID()
self.start_step(1)
self.time = datetime.datetime.now()
self.started = False
def start_step(self, step):
keg = self.steps[step]
self.tempLog = []
keg.stable = False
keg.running = True
def checkTime(self, keg):
now = datetime.datetime.now()
time = now - keg.start_time
min_total = time.seconds/60
min_left = keg.setTime - min_total
return min_left
def update(self):
self.change_temp()
self.change = False
if not(self.keg.stable):
if len(self.tempLog) == 100:
stdev = statistics.stdev(self.tempLog)
if stdev < 1.0:
self.keg.stable = True
elif self.keg.time <= 0:
self.keg.running = False
self.currentStep+=1
if self.currentStep == 5:
self.done = True
else:
self.change = True
self.keg = self.steps[self.currentStep]
self.controller.setPoint(self.keg.setTemp)
self.stupid_bit = True
if self.currentStep == 3:
self.keg.set_temp(self.recipe.sparge_temp)
else:
if self.stupid_bit:
self.keg.start_time = datetime.datetime.now()
self.stupid_bit = False
self.keg.time = self.checkTime(self.keg)
def change_temp(self):
now = datetime.datetime.now()
time = now - self.time
if time.seconds >= 1 and self.started:
pidValue = self.controller.update(self.keg.temp)
self.keg.temp = self.keg.temp + pidValue/28 #temp
self.tempLog.append(self.keg.temp)
self.tempLog = self.tempLog[-100:]
self.time = now
def loadrecipe(self, strike_temp, sparge_temp, mash_temp, mash_time, boil_temp, boil_time):
self.recipe = Recipe(strike_temp, sparge_temp, mash_temp, mash_time, boil_temp, boil_time)
self.mash.set_temp(mash_temp)
self.mash.set_time(mash_time)
self.hlt.set_temp(strike_temp)
self.boil.set_temp(boil_temp)
self.boil.set_time(boil_time)
self.controller.setPoint(self.keg.setTemp)
class Keg:
def __init__(self):
self.temp = 20
self.volume = 0
self.running = False
self.element_on = False
self.stable = False
self.time = 0
def set_temp(self, temp):
self.setTemp = temp
def set_time(self, time):
self.setTime = time
self.time = time
class Recipe:
def __init__(self, strike_temp, sparge_temp, mash_temp, mash_time, boil_temp, boil_time):
self.strike_temp = strike_temp
self.mash_temp = mash_temp
self.boil_temp = boil_temp
self.mash_time = mash_time
self.boil_time = boil_time
self.sparge_temp = sparge_temp
self.run_time = mash_time + boil_time
self.strike_volume = 5
self.sparge_volume = 5