-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.py
More file actions
126 lines (104 loc) · 4.15 KB
/
main.py
File metadata and controls
126 lines (104 loc) · 4.15 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import machine
import time
from tools import WeightedAverageCalculator
adc = machine.ADC(machine.Pin(26))
def raw() -> None:
while True:
val = adc.read_u16()
print(val)
time.sleep(0.25)
def shifted(baseline:int = 32000) -> None:
# the baseline is what this particular reads with clean air (which would be 400 ppm)
scaler:float = 400 / baseline
while True:
val = adc.read_u16()
co2ppm:int = int(round(val * scaler, 0))
print(co2ppm)
time.sleep(0.25)
def gap(baseline:int = 32000) -> None:
# baseline here is interpretted as the reading it gives with very clean air.
maxv:int = 65025
minv:int = baseline
while True:
val = adc.read_u16()
percentage = (val - minv) / (maxv - minv)
percentage = min(percentage, 1.0)
percentage = max(percentage, 0.0)
percentages = str(round(percentage * 100, 0)) + "%"
print(percentages)
time.sleep(0.25)
def gap2(baseline:int = 32000) -> None:
# baseline here is interpretted as the reading it gives with very clean air.
maxv:int = 65025
minv:int = baseline
wac = WeightedAverageCalculator()
while True:
val = adc.read_u16()
val = wac.feed(val)
print("WAV: " + str(val))
percentage = (val - minv) / (maxv - minv)
percentage = min(percentage, 1.0)
percentage = max(percentage, 0.0)
percentages = str(int(round(percentage * 100, 0))) + "%"
print(percentages)
time.sleep(0.25)
def gap3(baseline:int = 32000) -> None:
# baseline here is interpretted as the reading it gives with very clean air.
maxv:int = 65025
minv:int = baseline
wac = WeightedAverageCalculator(alpha=0.9)
while True:
val = adc.read_u16()
# constrain the val
val = min(max(val, minv), maxv)
# calculate weighted average of the constrained value
val = wac.feed(val)
# transform the weighted average reading into a percentage. Constraining is not necessary because it was already constrained above
percentage = (val - minv) / (maxv - minv)
percentages = str(int(round(percentage * 100, 0))) + "%"
print(percentages)
time.sleep(0.25)
def stest(samples:int, duration_ms:int) -> tuple[int, int, int]:
"""
Returns (min, avg, max) for a series of samples over a sample duration. This function is meant to test the average values in various sample sets of various durations.
Through testing, this has proved that average 10 samples over 1 second should roughly equal sampling even more samples over a longer duration.
"""
duration_in_between_ms:int = int(duration_ms / samples)
minv:int = None
maxv:int = None
allsummed:int = 0
for x in range(0, samples):
val = adc.read_u16()
allsummed = allsummed + val
if minv == None or val < minv:
minv = val
if maxv == None or val > maxv:
maxv = val
time.sleep_ms(duration_in_between_ms)
# calculate avg
avg = int(allsummed / samples)
# return
return (minv, avg, maxv)
def sample(last:float = None) -> float:
"""Returns a reading from the sensor between 0.0 and 1.0, expressing the quality of the air (lower is clean air, higher is dirty air)."""
# settings
samples:int = 10
across_ms:int = 500
baseline:int = 30000 # the observed baseline reading of the sensor in clean or relatively clean air
alpha:float = 0.9 # weighted average calculation (if they provide the last value)
# perform read
delay_ms:int = int(across_ms / samples)
allsummed:int = 0
for _ in range(0, samples):
val = adc.read_u16()
allsummed = allsummed + val
time.sleep_ms(delay_ms)
val:float = allsummed / samples
# constrain within floor (baseline is the floor)
val = max(val, baseline)
# calculate as percentage of gap
percentage:float = (val - baseline) / (65025 - baseline) #65025 is the max value for u16
# if they provided the last value, perform a weighted average calculation
if last != None:
percentage = (last * alpha) + (percentage * (1 - alpha))
return percentage