-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog-and-switch.py
executable file
·88 lines (77 loc) · 2.71 KB
/
log-and-switch.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
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
#!/usr/bin/env python3
"""
Default program upon startup. Logs data at the sampling period and switches
the vave at the valve period.
"""
import argparse
import datetime
import os
import time
import logging
import logging.handlers
from serialdevices import MCPC, ThreeWayValve
# Default values for environment variables for program operation.
# The MCPC_PORT, VALVE_PORT, and SAVE_FILE are set as environment
# variables on startup.
values = [
('MCPC_PORT', str, None),
('MCPC_BAUD', int, 38400),
('VALVE_PORT', str, None),
('VALVE_BAUD', int, 9600),
('VALVE_PERIOD', int, 10),
('SAMPLING_PERIOD', int, 1),
('SAVE_FILE', str, 'data.csv'),
]
def get_config():
"""
Get the configuration information from the environment variables associated
with the namespace.
"""
ns = argparse.Namespace()
for name, modifier, default in values:
val = os.getenv(name, default)
if val is None:
raise ValueError('Required Environment variable {!r} is not set!'.format(name))
setattr(ns, name.lower(), modifier(val))
return ns
def main():
"""
Set up MCPC, valve, and data logger. Start valve switching at given valve
period while logging data at the given sampling period.
"""
cfg = get_config()
m = MCPC()
m.connect(port=cfg.mcpc_port, baudrate=cfg.mcpc_baud)
valve = ThreeWayValve()
valve.connect(port=cfg.valve_port, baudrate=cfg.valve_baud)
# Configure data logger for writing data to the CSV file.
data_logger = logging.Logger('data')
data_logger.setLevel(logging.INFO)
file_handler = logging.handlers.TimedRotatingFileHandler(cfg.save_file, when='D', interval=1)
data_logger.addHandler(file_handler)
formatter = logging.Formatter('%(message)s')
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.INFO)
# Write header value.
mcpc_data = m.get_reading()
data = {'timestamp': None, 'valve': None}
data.update(mcpc_data)
data_logger.info(','.join(map(str, data.keys())))
# Configure and start valve switching.
valve.open_a()
valve_period = cfg.valve_period # This value is in seconds.
valve_state = 'a'
valve_start = time.time()
while True:
timestamp = datetime.datetime.now().isoformat()
mcpc_data = m.get_reading()
data = {'timestamp': timestamp, 'valve': valve_state}
data.update(mcpc_data)
data_logger.info(','.join(map(str, data.values())))
if time.time() - valve_start > valve_period:
valve_state = 'a' if valve_state != 'a' else 'b'
valve.goto_pos(valve_state + '_open')
valve_start = time.time()
time.sleep(cfg.sampling_period)
if __name__ == '__main__':
main()