class ControllerDaemon(AtlasDaemon): def _init(self): self.logger.info("Initializing controller daemon.") self.temp_target = 19 self.update_time = 0 self.period = 5 * 60 self.duty_cycle = 0 self.pid = PID() self.pid.setPoint(self.temp_target) self.pin = OutputPin("P8_10") self.pin.set_low() self.heater = State.off self.subscriber = self.get_subscriber(Topic("temperature", "ferm1_wort")) self.wort_temp = Average(self.subscriber.topic.data) topic = Topic("temperature", "ferm1_wort_average", self.wort_temp.get_value()) self.publisher_average_temp = self.get_publisher(topic) topic = Topic("pid", "ferm1_pid", self.pid.update(self.wort_temp.get_value())) self.publisher_pid = self.get_publisher(topic) topic = Topic("state", "ferm1_heating", self.heater) self.publisher_heater = self.get_publisher(topic) topic = Topic("temperature", "ferm1_target", self.temp_target) self.publisher_target = self.get_publisher(topic) topic = Topic("percentage", "ferm1_duty_cycle", self.duty_cycle) self.publisher_duty_cycle = self.get_publisher(topic) self.logger.info("Controller initialized.") def _loop(self): self.wort_temp.update(self.subscriber.topic.data) self.publisher_average_temp.publish(self.wort_temp.get_value()) pid = self.pid.update(self.wort_temp.get_value()) self.publisher_pid.publish(pid) self.publisher_target.publish(self.temp_target) if pid < 1: self.duty_cycle = pid else: self.duty_cycle = 1 self.publisher_duty_cycle.publish(self.duty_cycle) if self.update_time + self.period < time.time(): self.update_time = time.time() if 0 < self.duty_cycle: self.heater = State.on self.pin.set_high() elif self.update_time + self.duty_cycle * self.period < time.time(): if self.heater == State.on: self.heater = State.off self.pin.set_low() self.publisher_heater.publish(self.heater) time.sleep(1)
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()