-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_meter.py
43 lines (36 loc) · 1.12 KB
/
db_meter.py
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
import time
import utime
import statistics
import machine
INTERVAL = 500
class DBMeter():
modbus = None
modbus_psu = None
def __init__(self, modbus, modbus_psu):
self.modbus = modbus
self.modbus_psu = modbus_psu
def open(self):
self.modbus_psu.hold(False)
self.modbus_psu.value(1)
time.sleep(3) # Wait for IC to be ready for results
def close(self):
self.modbus_psu.value(0)
self.modbus_psu.hold(True)
def process_read(self, period):
results = []
now = utime.ticks_ms()
stop = now + period
while now < stop:
now = utime.ticks_ms()
results.append(self.modbus.read_holding_registers(0x01, 0x00, 0x01, True)[0] / 10)
utime.sleep_ms(INTERVAL - (utime.ticks_ms() - now))
mean = statistics.mean(results)
stdev = statistics.stdev(results)
return (mean, stdev)
def read(modbus, modbus_psu, period):
meter = DBMeter(modbus, modbus_psu)
try:
meter.open()
return meter.process_read(period)
finally:
meter.close()