annotate py/fridge.py @ 143:0895f5ad7731

copied fridge logic from main.c
author Matt Johnston <matt@ucc.asn.au>
date Tue, 20 Nov 2012 22:03:10 +0800
parents 4755e6f9a5b8
children 482d7852b511
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
1 # -*- coding: utf-8 -*-
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 from utils import L,W,E
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import config
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 class Fridge(object):
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
6
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
7 OVERSHOOT_MAX_DIV = 1800.0 # 30 mins
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
8 FRIDGE_AIR_MIN_RANGE = 4 # ÂșC
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
9 FRIDGE_AIR_MAX_RANGE = 4
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
10
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 def __init__(self):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 self.setup_gpio()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 self.wort_valid_clock = 0
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
14 self.fridge_on_clock = 0
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
15 self.fridge_off_clock = 0
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 def setup_gpio(self):
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
18 dir_fn = '%s/direction' % config.FRIDGE_GPIO
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
19 with f = open(dir_fn, 'w'):
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
20 f.write('low')
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
21 val_fn = '%s/value' % config.FRIDGE_GPIO
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
22 self.value_file = f.open(val_fn, 'r+')
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 def turn(self, value):
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
25 self.value_file.seek(0)
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 if value:
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
27 self.value_file.write('1')
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 else:
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
29 self.value_file.write('0')
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
30 self.value_file.flush()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 def on(self):
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
33 self.turn(True)
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 def off(self):
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
36 self.turn(False)
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
38 def is_on(self):
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
39 self.value_file.seek(0)
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
40 buf = self.value_file.read().strip()
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
41 if buf == '0':
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
42 return False
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
43 if buf != '1':
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
44 E("Bad value read from gpio '%s': '%s'"
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
45 % (self.value_file.name, buf))
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
46 return True
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
47
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
48 def run(self, server):
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
49
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
50 while True:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
51 self.do(server)
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
52 gevent.sleep(config.FRIDGE_SLEEP)
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
53
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
54 def do(self, server):
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
55 """ this is the main fridge control logic """
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 wort, fridge, ambient = server.current_temps()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
58 fridge_min = params.fridge_setpoint - self.FRIDGE_AIR_MIN_RANGE
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
59 fridge_max = params.fridge_setpoint + self.FRIDGE_AIR_MAX_RANGE
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
60
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
61 wort_max = params.fridge_setpoint + params.fridge_difference
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
62
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
63 off_time = server.now() - self.fridge_off_clock
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
64
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
65 if off_time < config.FRIDGE_DELAY:
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 L("fridge skipping, too early")
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 return
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 # handle broken wort sensor
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 if wort is not None:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 self.wort_valid_clock = server.now()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 else:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 W("Invalid wort sensor")
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 invalid_time = server.now() - self.wort_valid_clock
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 if invalid_time < config.FRIDGE_WORT_INVALID_TIME:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 W("Has only been invalid for %d, waiting" % invalid_time)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 return
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 if fridge is None:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 W("Invalid fridge sensor")
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
82 if self.is_on():
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
83 turn_off = False
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
84 on_time = server.now() - self.fridge_on_clock
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
86 overshoot = 0
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
87 if on_time > params.overshoot_delay:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
88 overshoot = params.overshoot_factor \
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
89 * min(self.OVERSHOOT_MAX_DIV, on_time) \
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
90 / self.OVERSHOOT_MAX_DIV
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
91 L("on_time %(on_time)f, overshoot %(overshoot)f" % locals())
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
93 if wort is not None:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
94 if (wort - overshoot) < params.fridge_setpoint:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
95 L("wort has cooled enough")
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
96 turn_off = True
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
97 else:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
98 # wort sensor is broken
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
99 if fridge is not None and last_fridge < fridge_min:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
100 W("fridge off fallback")
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
101 turn_off = True
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
103 if turn_off:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
104 L("Turning fridge off")
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
105 self.off()
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
106 self.fridge_off_clock = server.now()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107
143
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
108 else:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
109 # fridge is off
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
110 turn_on = False
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
111 if wort is not None:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
112 if wort >= wort_max:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
113 L("Wort is too hot")
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
114 turn_on = True
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
115 else:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
116 # wort sensor is broken
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
117 if fridge is not None and fridge >= fridge_max:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
118 W("frdge on fallback")
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
119 turn_on = True
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
120
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
121 if turn_on:
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
122 L("Turning fridge on")
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
123 self.on()
0895f5ad7731 copied fridge logic from main.c
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
124 fridge_on_clock = server.now()