forked from nebhead/PiFire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_queue.py
More file actions
59 lines (51 loc) · 1.72 KB
/
temp_queue.py
File metadata and controls
59 lines (51 loc) · 1.72 KB
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
import statistics
'''
Class to track temperature averages coming from the ADC and
handle errors gracefully (hopefully).
'''
class TempQueue():
def __init__(self, qlength=10, units='F'):
self.queue = []
self.units = units
if qlength < 2:
self.qlength = 2 # Set minimum qlength to 2
else:
self.qlength = qlength
if units == 'F':
self.stdev_max = 4.75 # Standard Deviation Maximum for degrees F
else:
self.stdev_max = 2.25 # Standard Deviation Maximum for degrees C
self.last_average = 0
def enqueue(self, value):
while len(self.queue) < (self.qlength + 1):
self.queue.insert(0, value)
self.queue.pop()
return(self.average())
def average(self):
if len(self.queue) < self.qlength:
# Handle case if queue isn't full
self.last_average = 0
return(0)
elif self.last_average == 0:
# Handle case if lastaverage isn't initialized
average = (sum(self.queue) / self.qlength)
self.last_average = average
if self.units == 'F':
return(int(average)) # Give integer for F units
else:
return(round(average, 1)) # Give one digit of decimal for C units
else:
# Handle normal case
# Get standard deviation from temperatures in the queue
stdev = statistics.stdev(self.queue)
if stdev < self.stdev_max:
# If the standard deviation is less than the max deviation, calculate the average temperature as normal
average = (sum(self.queue) / self.qlength)
self.last_average = average
else:
# If the standard deviation exceeds the max deviation, keep the last average value
average = self.last_average
if self.units == 'F':
return(int(average)) # Give integer for F units
else:
return(round(average, 1)) # Give one digit of decimal for C units