changeset 271:11cebd6f0bfb

untested fridge.integrator
author Matt Johnston <matt@ucc.asn.au>
date Wed, 11 Nov 2015 00:20:20 +0800
parents 9d0313b685ae
children af924d27140f
files py/fridge.py py/utils.py
diffstat 2 files changed, 15 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/py/fridge.py	Wed Nov 11 00:07:21 2015 +0800
+++ b/py/fridge.py	Wed Nov 11 00:20:20 2015 +0800
@@ -5,6 +5,7 @@
 import config
 
 import gpio
+import utils
 
 class Fridge(object):
 
@@ -13,6 +14,7 @@
     def __init__(self, server, nowait = False):
         self.server = server
         self.gpio = gpio.Gpio(config.FRIDGE_GPIO_PIN, "fridge")
+        self.integrator = utils.StepIntegrator(self.server.now, self.server.params.overshoot_delay)
         self.wort_valid_clock = 0
         self.fridge_on_clock = 0
         self.off()
@@ -21,10 +23,10 @@
 
     def turn(self, value):
         self.gpio.turn(value)
+        self.integrator.turn(value)
 
     def on(self):
         self.turn(True)
-        pass
 
     def off(self):
         self.turn(False)
@@ -61,6 +63,8 @@
         if wort is not None:
             self.wort_valid_clock = self.server.now()
 
+        self.integrator.set_limit(params.overshoot_delay)
+
         # Safety to avoid bad things happening to the fridge motor (?)
         # When it turns off don't start up again for at least FRIDGE_DELAY
         if not self.is_on() and off_time < config.FRIDGE_DELAY:
@@ -90,11 +94,7 @@
             turn_off = False
             on_time = self.server.now() - self.fridge_on_clock
 
-            overshoot = 0
-            if on_time > params.overshoot_delay:
-                overshoot = params.overshoot_factor \
-                    * min(self.OVERSHOOT_MAX_DIV, on_time) \
-                    / self.OVERSHOOT_MAX_DIV
+            overshoot = params.overshoot_factor * self.integrator.integrate()
             D("on_time %(on_time)f, overshoot %(overshoot)f" % locals())
 
             if not params.nowort and wort is not None:
--- a/py/utils.py	Wed Nov 11 00:07:21 2015 +0800
+++ b/py/utils.py	Wed Nov 11 00:20:20 2015 +0800
@@ -174,6 +174,9 @@
 
 class StepIntegrator(object):
     """
+    Takes on/off events and a monotonically increasing timefn. Returns the integral 
+    of (now-limittime, now) over those events.
+
     >>> s = StepIntegrator(lambda: t, 40)
     >>> t = 1
     >>> s.turn(1)
@@ -212,6 +215,12 @@
         self._timefn = timefn
         self._limittime = limittime
 
+    def set_limit(self, limittime):
+        if self._limittime == limittime:
+            return
+        self._limittime = limittime
+        self.trim()
+
     def turn(self, value):
         if not self._on_periods:
             if value: