/
chillmon
executable file
·170 lines (136 loc) · 4.42 KB
/
chillmon
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
#!/usr/bin/env python
import RPi.GPIO as GPIO
import mcp3008
import mediorc
import sys
import time
CHILLPIN = 17
def nv(v) :
if v is None :
return 0.0
else :
return v
def mean(l) :
return reduce(lambda x,y: x+y, l) / float(len(l))
class PID(object) :
def __init__(self, temp_probe, target) :
self.temp_probe = temp_probe
self.samples = []
self.target = target
GPIO.setup(CHILLPIN, GPIO.OUT)
self.pid_ts = None
self.events = []
GPIO.output(CHILLPIN, False)
self.fPeriod = 300.0
self.fMinOff = 60.0 * 3
assert self.fPeriod > self.fMinOff
# pid state
self.fError = None
self.fIError = 0.0
self.fDError = None
self.fPriorError = None
self.fPID = None
# guesstimated PID constants. ratio, not percentage based.
self.fKP = 0.02
self.fKI = 0.002
self.fKD = 0.0
@property
def state(self) :
return {
'kp' : nv(self.fKP),
'ki' : nv(self.fKI),
'kd' : nv(self.fKD),
'err' : nv(self.fError),
'ierr' : nv(self.fIError),
'derr' : nv(self.fDError),
'pid' : nv(self.fPID)
}
def step(self) :
now = time.time()
self.samples.append(self.temp_probe.read())
if self.pid_ts is None or self.pid_ts < now - self.fPeriod :
self.pid_ts = now
self.fTemp = mean(self.samples)
self.samples = []
# This is the error we want to reduce to 0. Positive values mean it is too warm.
self.fError = self.fTemp - self.target
print 'err: %f' % self.fError
# figure out proportion to use, start at 0
self.fPID = 0.0
# apply proportional...
self.fPID += self.fKP * self.fError
# to degree-minutes
self.fIError += (self.fError * self.fPeriod) / 60.0
# apply integral...
self.fPID += self.fKI * self.fIError
if self.fPriorError is not None :
self.fDError = ((self.fError - self.fPriorError) * 60.0) / self.fPeriod
self.fPriorError = self.fError
# if we've got a derivative... apply it
if self.fDError is not None :
self.fPID += self.fKD * self.fDError
print time.ctime(), 'PID output: %f' % self.fPID
if self.fPID * self.fPeriod < 30.0 :
# nope, it's less than 1%. just turn it off and leave it that way until next time
self.events.append((now, False))
print time.ctime(), 'no chilling, pid output way low. just turning off'
else :
off_sec = (1.0 - self.fPID) * self.fPeriod
off_sec = max(off_sec, self.fMinOff) # 3 minute minimum to be off
on_sec = self.fPeriod - off_sec
self.events.append((now, True))
self.events.append((now + on_sec, False))
print time.ctime(), 'scheduled on/off events for pid output. on for %f, off for %f' % (on_sec, off_sec)
kept_events = []
for event in self.events :
ts,val = event
if ts <= now :
print time.ctime(), 'set chiller: %f' % val
GPIO.output(CHILLPIN, val)
else :
kept_events.append(event)
self.events = kept_events
class ChillmonBot(mediorc.IRC) :
def __init__(self, server, nick, chan, temp_probes, pid, zmqpub=None) :
self.temp_probes = temp_probes
self.pid = pid
self.zmqpub = zmqpub
mediorc.IRC.__init__(self, server, nick, chan)
def on_pubmsg(self, c, e) :
chan = e.target()
words = e.arguments()[0].split(' ')
msg = None
if words[0] == '!temp' :
msg = 'temperatures: %s' % (', '.join(['%s: %0.1f F' % (name, probe.read()) for name,probe in self.temp_probes.items()]))
if msg :
self.connection.privmsg(chan, msg)
def do_work(self) :
if self.zmqpub :
self.zmqpub.send({
'temps' : dict([(k,v.read()) for k,v in self.temp_probes.items()] + [('target', self.pid.target)]),
'pidstate' : self.pid.state
})
self.pid.step()
class ChillmonBotThread(mediorc.IRCThread) :
def __init__(self, server, nick, chan, temperature_probes, pid, zmqpub=None) :
self.bot_create = lambda: ChillmonBot(server, nick, chan, temperature_probes, pid, zmqpub=zmqpub)
mediorc.IRCThread.__init__(self)
if __name__ == '__main__' :
a2d = mcp3008.MCP3008(3300.0)
beer = mcp3008.TMP36(mcp3008.TMP36.F)
a2d.setup_channel(0, beer)
room = mcp3008.TMP36(mcp3008.TMP36.F)
a2d.setup_channel(1, room)
beertop = mcp3008.TMP36(mcp3008.TMP36.F)
a2d.setup_channel(2, beertop)
zmqpub = None
try :
url = sys.argv[4]
import zmqsub
zmqpub = zmqsub.JSONZMQBindPub(url)
except IndexError :
pass
temp_probes = {'beer' : beer, 'room' : room, 'beertop' : beertop}
pid = PID(beer, 67.0) # TODO configuration system for what temp to stick to
chill = ChillmonBotThread(sys.argv[1], sys.argv[2], sys.argv[3], temp_probes, pid, zmqpub=zmqpub)
chill.run()