/
calibration.py
executable file
·116 lines (100 loc) · 4.22 KB
/
calibration.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
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
#!/usr/bin/python
import sys
import os
import time
import logging
from average import Average
from Atlas.atlas import AtlasDaemon, AtlasError
from Atlas.topic import Topic
from Interfaces.bbio import OutputPin
class CalibrationDaemon(AtlasDaemon):
def _init(self):
self.atlas.register_topic_handler(self._logger)
self.pin = OutputPin("P8_10")
self.pin.set_low()
self.t0 = time.time()
self.stage1 = 6 * 60 * 60 # 6 hours
self.stage2 = 6 * 60 * 60 # 6 hours
self.stage3 = 6 * 60 * 60 # 6 hours
self.t_cycle = self.t0
self.cycle_period = 5 * 60 # 5 minutes
self.power = 0.2 # 20% of full power
self.t_log = self.t0
self.log_period = 20
self.triplets = []
self.topics = []
self.atlas.register_topic_handler(self._logger)
subscriber = self.get_subscriber(Topic("temperature", "sensor1"))
average_temp = Average(subscriber.topic.data, 10)
topic = Topic("temperature", "sensor1_average", average_temp.get_value())
publisher = self.get_publisher(topic)
self.triplets.append((subscriber, average_temp, publisher))
subscriber = self.get_subscriber(Topic("temperature", "sensor2"))
average_temp = Average(subscriber.topic.data, 10)
topic = Topic("temperature", "sensor2_average", average_temp.get_value())
publisher = self.get_publisher(topic)
self.triplets.append((subscriber, average_temp, publisher))
subscriber = self.get_subscriber(Topic("temperature", "sensor3"))
average_temp = Average(subscriber.topic.data, 10)
topic = Topic("temperature", "sensor3_average", average_temp.get_value())
publisher = self.get_publisher(topic)
self.triplets.append((subscriber, average_temp, publisher))
topic = Topic("state", "heater", self.pin.get_state())
self.heater_publisher = self.get_publisher(topic)
topic = Topic("stage", "calibration_stage", 1)
self.stage_publisher = self.get_publisher(topic)
def _loop(self):
if time.time() < self.t0 + self.stage1:
if self.pin.get_state():
self.pin.set_low()
self.stage_publisher.publish(1)
elif time.time() < self.t0 + self.stage1 + self.stage2:
self.stage_publisher.publish(2)
if self.t_cycle + self.cycle_period < time.time():
self.t_cycle = time.time()
self.pin.set_high()
elif self.t_cycle + self.power * self.cycle < time.time():
self.pin.set_low()
elif self.t0 + self.stage1 + self.stage2 < time.time():
if self.pin.get_state():
self.pin.set_low()
self.stage_publisher.publish(3)
for triple in self.triplets:
triple[1].update(triple[0].topic.data)
triple[2].publish(triple[1].get_value())
self.heater_publisher.publish(self.pin.get_state())
time.sleep(1)
def _logger(topic):
if topic not in self.topics:
self.topics.append(topic)
else:
for _topic in self.topics:
if topic == _topic:
_topic.data = topic.data
if self.t_log + self.log_period < time.time():
file = open("data.csv")
for _topic in self.topics:
file.write(str(topic), self.data_file)
file.close()
if __name__ == '__main__':
file_name = os.path.splitext(os.path.basename(__file__))[0]
logging.basicConfig(filename='/var/log/' + file_name + '.log',
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.INFO)
daemon = CalibrationDaemon('/var/run/' + file_name + '.pid')
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
else:
print 'Unknown command: ' + sys.argv[1]
sys.exit(2)
sys.exit(0)
else:
print "usage: %s start|stop|restart" % sys.argv[0]
sys.exit(2)