-
Notifications
You must be signed in to change notification settings - Fork 0
/
boiler.py
101 lines (89 loc) · 3.43 KB
/
boiler.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
import queue
import signal
import multiprocessing
from gpio import Output
from heater import Heater
from message import Message
from thermo_couple_amp import TCAmp
class Boiler:
def __init__(self):
self.qToWorker = multiprocessing.Queue()
self.qFromWorker = multiprocessing.Queue()
self.worker = BoilerWorker(self.qToWorker, self.qFromWorker)
self.worker.start()
def setTemperatureC(self, temperatureC):
if temperatureC > 99:
raise ValueError('temperature {}°C exeeds limit'.format(temperatureC))
self.qToWorker.put(Message('setTemperatureC', temperatureC))
def getTemperatures(self):
self.qToWorker.put(Message('getTemperatures'))
return self.qFromWorker.get().data
def shutdown(self):
self.qToWorker.put(Message('shutdown'))
self.worker.join()
class BoilerWorker(multiprocessing.Process):
def __init__(self, qToWorker, qFromWorker):
multiprocessing.Process.__init__(self)
self.qToWorker = qToWorker
self.qFromWorker = qFromWorker
self.setPointC = 0
self.hysteresis = 2
self.tempSensor1 = TCAmp(0)
self.tempSensor2 = TCAmp(1)
self.heater = Heater()
self.ledWait = Output(18)
def run(self):
signal.signal(signal.SIGINT, signal.SIG_IGN)
try:
while True:
self.controlHeater()
try:
message = self.qToWorker.get(block=True, timeout=0.01)
except queue.Empty:
continue
if message.command == 'shutdown':
break
elif message.command == 'setTemperatureC':
self.setTemperatureC(message.data)
elif message.command == 'getTemperatures':
self.qFromWorker.put(Message(
'temperatures', [self.tempSensor1.getTemperatureC(),
self.tempSensor2.getTemperatureC()]))
else:
raise ValueError('no command: {}'.format(message))
finally:
self.shutdownHeater()
def controlHeater(self):
temperature1 = self.tempSensor1.getTemperatureC()
temperature2 = self.tempSensor2.getTemperatureC()
maxDelta = 5
if abs(temperature1 - temperature2) > maxDelta:
raise RuntimeError(
'temperatures differ more than {}\n'.format(maxDelta) +
'temp1: {}, temp2: {}'.format(
temperature1,
temperature2))
temperature = (temperature1 + temperature2) / 2
if ((not self.heater.isOn()) and
(temperature < self.setPointC - self.hysteresis)):
self.heater.turnOn()
self.turnLedWaitOn()
if ((self.heater.isOn()) and
(temperature > self.setPointC + self.hysteresis)):
self.heater.turnOff()
self.turnLedWaitOff()
self.heater.resetWatchdog()
def shutdownHeater(self):
self.heater.turnOff()
self.turnLedWaitOff()
def setTemperatureC(self, temperatureC):
self.setPointC = temperatureC
def turnLedWaitOn(self):
self.ledWait.set()
def turnLedWaitOff(self):
self.ledWait.clear()
if __name__ == '__main__':
boiler = Boiler()
boiler.setTemperatureC(50)
input('shutdown? ')
boiler.shutdown()