annotate Control.py @ 18:ff12367786a7

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