annotate Control.py @ 16:f1832dec26e3 default tip

Move OWReadError to MonitorDev since that's where it's raised.
author darius
date Tue, 23 Oct 2007 01:05:21 +0000
parents 9d5b291cfd01
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
1 #!/usr/bin/env python
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
2
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
3 ############################################################################
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
4 # Control class for beermon
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
5 #
16
f1832dec26e3 Move OWReadError to MonitorDev since that's where it's raised.
darius
parents: 12
diff changeset
6 # $Id: Control.py,v 1.3 2007/10/23 01:05:21 darius Exp $
8
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
7 #
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
8 # Depends on: Python 2.3 (I think)
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
9 #
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
10 ############################################################################
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
11 #
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
12 # Copyright (C) 2007 Daniel O'Connor. All rights reserved.
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
13 #
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
14 # Redistribution and use in source and binary forms, with or without
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
15 # modification, are permitted provided that the following conditions
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
16 # are met:
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
17 # 1. Redistributions of source code must retain the above copyright
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
18 # notice, this list of conditions and the following disclaimer.
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
19 # 2. Redistributions in binary form must reproduce the above copyright
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
20 # notice, this list of conditions and the following disclaimer in the
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
21 # documentation and/or other materials provided with the distribution.
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
22 #
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
23 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
25 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
26 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
27 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
28 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
29 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
30 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
32 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
33 # SUCH DAMAGE.
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
34 #
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
35 ############################################################################
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
36
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
37 import threading, ConfigParser, time
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
38
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
39 class ThreadDied(Exception):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
40 """Raised when the monitor thread has died for some reason"""
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
41 pass
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
42
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
43 class Control():
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
44 """This class is responsible for controlling the temperature of
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
45 the fermenter by switching the heater & cooler."""
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
46 staleDataTime = 30
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
47
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
48 def __init__(self, _log, m, conf):
12
9d5b291cfd01 Add/correct docstrings & comments.
darius
parents: 8
diff changeset
49 """m is a MonitorDev object, _log is a logging object, conf is a ConfigParser object"""
8
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
50 global log
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
51 log = _log
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
52
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
53 self.m = m
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
54
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
55 self.targetTemp = conf.getfloat('control', 'targetTemp')
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
56 self.hysteresis = conf.getfloat('control', 'hysteresis')
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
57 self.pollInterval = conf.getfloat('control', 'pollInterval')
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
58
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
59 log.debug("target temperature - %3.2f" % (self.targetTemp))
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
60 log.debug("hysteresis - %3.2f" % (self.hysteresis))
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
61 log.debug("pollInterval - %d" % (self.pollInterval))
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
62
12
9d5b291cfd01 Add/correct docstrings & comments.
darius
parents: 8
diff changeset
63 # We sleep on this, using time.sleep() doesn't work well in a
9d5b291cfd01 Add/correct docstrings & comments.
darius
parents: 8
diff changeset
64 # threaded environment (eg ctrl-c will be blocked until the
9d5b291cfd01 Add/correct docstrings & comments.
darius
parents: 8
diff changeset
65 # timeout expires.)
8
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
66 self.cv = threading.Condition()
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
67 self.cv.acquire()
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
68
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
69 def doit(self):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
70 """Runs forever controlling the temperature until something breaks or interrupted"""
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
71 log.debug("=== Starting ===")
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
72 log.debug("Fermenter Fridge Ambient State New State")
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
73 while True:
12
9d5b291cfd01 Add/correct docstrings & comments.
darius
parents: 8
diff changeset
74 # Check if the monitor thread has died
8
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
75 if (not self.m.isAlive()):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
76 raise ThreadDied, "Monitor thread has died"
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
77
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
78 # Check for stale data
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
79 if (self.m.lastUpdate[self.m.fermenterId] + self.staleDataTime < time.time()):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
80 log.debug("Stale data")
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
81 self.cv.wait(self.pollInterval)
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
82 self.m.setState('idle')
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
83 continue
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
84
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
85 # Work out what state we should go into
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
86 nextState = "-"
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
87 # Temperature diff, -ve => too cold, +ve => too warm
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
88 diff = self.m.temps[self.m.fermenterId] - self.targetTemp
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
89 if (self.m.currState == 'idle'):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
90 # If we're idle then only heat or cool if the temperate difference is out of the
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
91 # hysteresis band
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
92 if (abs(diff) > self.hysteresis):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
93 if (diff < 0 and self.m.minHeatOffTime + self.m.lastHeatOff < time.time()):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
94 nextState = 'heat'
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
95 elif (diff > 0 and self.m.minHeatOffTime + self.m.lastHeatOff < time.time()):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
96 nextState = 'cool'
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
97 elif (self.m.currState == 'cool'):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
98 # Work out if we should go idle (based on min on time & overshoot)
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
99 if (diff + self.m.minCoolOvershoot < 0 and self.m.minCoolOnTime + self.m.lastCoolOn < time.time()):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
100 nextState = 'idle'
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
101 elif (self.m.currState == 'heat'):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
102 # Ditto
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
103 if (diff - self.m.minHeatOvershoot > 0 and self.m.minHeatOnTime + self.m.lastHeatOn < time.time()):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
104 nextState = 'idle'
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
105 else:
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
106 # Not possible..
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
107 raise KeyError
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
108
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
109 log.debug("%3.2f %3.2f %3.2f %s %s" %
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
110 (self.m.temps[self.m.fermenterId],
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
111 self.m.temps[self.m.fridgeId], self.m.temps[self.m.ambientId],
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
112 self.m.currState, nextState))
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
113
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
114 if (nextState != "-"):
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
115 self.m.setState(nextState)
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
116
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
117 self.cv.wait(self.pollInterval)
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
118
483375ca5d10 Split into seperate files.
darius
parents:
diff changeset
119