Permalink
Browse files

fix zeroing of average (use target) and add running average (sample s…

…et flushed per pid cycle) to api output
  • Loading branch information...
1 parent 1085097 commit abf356dc0b326349040b13bf39ea3fe4ef28c604 @eastein committed Sep 17, 2012
Showing with 9 additions and 4 deletions.
  1. +9 −4 chillmon
View
@@ -14,8 +14,11 @@ def nv(v) :
else :
return v
-def mean(l) :
- return reduce(lambda x,y: x+y, l) / float(len(l))
+def mean(l, default=0) :
+ try :
+ return reduce(lambda x,y: x+y, l) / float(len(l))
+ except :
+ return default
class PID(object) :
def __init__(self, temp_probe, target) :
@@ -40,6 +43,8 @@ class PID(object) :
self.fPID = None
+ self.fTemp = None
+
# guesstimated PID constants. ratio, not percentage based.
self.fKP = 0.02
self.fKI = 0.002
@@ -60,9 +65,9 @@ class PID(object) :
def step(self) :
now = time.time()
self.samples.append(self.temp_probe.read())
+ self.fTemp = mean(self.samples, default=self.target)
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.
@@ -136,7 +141,7 @@ class ChillmonBot(mediorc.IRC) :
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)]),
+ 'temps' : dict([(k,v.read()) for k,v in self.temp_probes.items()] + [('run_avg', self.pid.fTemp), ('target', self.pid.target)]),
'pidstate' : self.pid.state
})
self.pid.step()

0 comments on commit abf356d

Please sign in to comment.