diff --git a/scripts/config.yml b/scripts/config.yml new file mode 100644 index 0000000..65fb7b8 --- /dev/null +++ b/scripts/config.yml @@ -0,0 +1,43 @@ +defaults: + tnom: 25 + rnom: 10000.0 + corr: 4800 + spi: + mode: 0 + hz: 1000000 + pwm: + hz: 25000 + start_c: 0 + hysterese: 2.0 +temp_input: + outside: + channel: 1 + switch: 26 + Rk: 9770.0 + inside: + channel: 0 + switch: 20 +fan_output: + in: + switch: 17 + pwm: 18 + out: + switch: 22 + pwm: 23 +operation: + - greater: 20 + in_pwm: 100 + out_pwm: 100 + - greater: 15 + in_pwm: 66 + out_pwm: 50 + - greater: 10 + in_pwm: 50 + out_pwm: 33 + - greater: 5 + in_pwm: 33 + out_pwm: 0 + - greater: 0 + in_pwm: 0 + out_pwm: 0 + diff --git a/scripts/mvp.py b/scripts/mvp.py new file mode 100755 index 0000000..a515089 --- /dev/null +++ b/scripts/mvp.py @@ -0,0 +1,134 @@ +#! /usr/bin/env python3 +#import RPIO +import RPi.GPIO as GPIO +import time +import sys +import spidev +import yaml +import os +from decimal import * +from datetime import datetime +from datetime import timedelta + + +class MVP: + def __init__(self): + print("Initialization...") + dirname = os.path.dirname(os.path.realpath(__file__)) + self.config = None + with open(os.path.join(dirname, "config.yml"), "r") as stream: + self.config = yaml.safe_load(stream) + self.spi = None + self.fan = None + self.ufan = None + self.spi = spidev.SpiDev() + + self.spi.open(0,0) + self.spi.mode = self.config.get("defaults").get("spi").get("mode") + self.spi.max_speed_hz = self.config.get("defaults").get("spi").get("hz") + GPIO.setmode(GPIO.BCM) + for k, v in self.config.get("fan_output").items(): + if v.get("switch", None): + GPIO.setup(v.get("switch"), GPIO.OUT) + if v.get("pwm", None): + GPIO.setup(v.get("pwm"), GPIO.OUT) + self.fan = GPIO.PWM(self.config["fan_output"]["in"]["pwm"], 25000) + self.ufan = GPIO.PWM(self.config["fan_output"]["out"]["pwm"],25000) + + for k, v in self.config.get("temp_input").items(): + GPIO.setup(v.get("switch"), GPIO.OUT) + GPIO.output(v.get("switch"), False) + self.fan.start(self.config.get("defaults").get("pwm").get("start_c")) + self.ufan.start(self.config.get("defaults").get("pwm").get("start_c")) + print("...done") + + def read_spiADC(self, sensor): + bcm_pin = self.config.get("temp_input").get(sensor).get("switch") + channel = self.config.get("temp_input").get(sensor).get("channel") + GPIO.output(bcm_pin, True) + hchbit=channel >> 2 + lchbits = channel & 0x03 + to_send = [ hchbit | 0x06, lchbits << 6, 0x00 ] + self.spi.cshigh=True + # time.sleep(0.01) + self.spi.cshigh = False + r=self.spi.xfer(to_send) + self.spi.cshigh = True + GPIO.output(bcm_pin, False) + # time.sleep(0.01) + highbyte = r[1] + lowbyte = r[2] + value = (highbyte << 8) | lowbyte + return value + + def calc_temp(self, adc_reading, channel = 0): + rser = self.config.get("temp_input").get(channel).get("Rk", None) + tnom = self.config.get("defaults").get("tnom") + rnom = self.config.get("defaults").get("rnom") + corr = self.config.get("defaults").get("corr") + if not rser: + rser = rnom + r = (rser/(4095/adc_reading - 1)) + r = r - corr + t = r/rnom + if t < 0: + t = t * -1 + t = float(Decimal(t).ln()) / 4050 + 1.0/(tnom + 273.15) + t = 1/t + t = t - 273.15 + return t + + def set_duty(self, fan, percent): + if percent == 0: + GPIO.output(self.config["fan_output"][fan]["switch"], False) + else: + GPIO.output(self.config["fan_output"][fan]["switch"], True) + self.fan.ChangeDutyCycle(100 - percent) + print("Duty: {fan}: {pwm}".format(fan=fan, pwm=percent)) + + def run(self): + print("Starting...") + try: + last_th = -1 + while True: + # read temperatures + inner = self.read_spiADC("inside") + outer = self.read_spiADC("outside") + if inner != 0 and outer != 0: + # inner_R = rnom/(4095/ inner - 1) + inner_T = self.calc_temp(inner, "inside") + # outer_R = rnom/(4095/ outer - 1) + outer_T = self.calc_temp(outer, "outside") + # activate/deactivate and pwm + difference = inner_T - outer_T + operation = self.config.get("operation") + for elem in operation: + greater = elem.get("greater") + if ( + difference > greater and + last_th != greater + ): + hyst = self.config["defaults"]["hysterese"] + in_pwm = elem.get("in_pwm") + out_pwm = elem.get("out_pwm") + if ( + difference + hyst > greater and greater > last_th or + difference - hyst < greater and greater < last_th + ): + self.set_duty("in", in_pwm) + self.set_duty("out", out_pwm) + last_th = greater + print("Inner temp: {:.2f} - outer temp: {:.2f} || difference: {:.2f}".format(inner_T, outer_T, difference)) + break + if int(time.time()) % 10 == 0: + print("Inner temp: {:.2f} - outer temp: {:.2f} || difference: {:.2f}\n".format(inner_T, outer_T, difference)) + time.sleep(0.5) + except Exception as e: + raise e + finally: + GPIO.cleanup() + self.spi.close() + + +mvp = MVP() +mvp.run() diff --git a/scripts/pwm.py b/scripts/pwm.py index 29fc37c..0c0fbee 100755 --- a/scripts/pwm.py +++ b/scripts/pwm.py @@ -23,7 +23,7 @@ upwm = 23 GPIO.setup(uswitch, GPIO.OUT) GPIO.setup(upwm, GPIO.OUT) -ufan = GPIO.PWM(pwm,25000) +ufan = GPIO.PWM(upwm,25000) # GPIO.setup(fan_speed, GPIO.IN, pull_up_down=GPIO.PUD_UP) global last_ts diff --git a/src/service.py b/src/service.py index a1e8a41..b39d29d 100644 --- a/src/service.py +++ b/src/service.py @@ -46,12 +46,12 @@ class Service(object): channel = int(k) if k < 0: raise RuntimeError("fuck.") - for threshold in thresholds: - if results[channel] < threshold["temp"]: - LowLevel().lower_fan(threshold["fan-low"]) - LowLevel().lower_fan_pwm(threshold["pwm-low"]) - LowLevel().upper_fan(threshold["fan-up"]) - LowLevel().upper_fan_pwm(threshold["pwm-up"]) - break +# for threshold in thresholds: +# if results[channel] < threshold["temp"]: +# LowLevel().lower_fan(threshold["fan-low"]) +# LowLevel().lower_fan_pwm(threshold["pwm-low"]) +# LowLevel().upper_fan(threshold["fan-up"]) +# LowLevel().upper_fan_pwm(threshold["pwm-up"]) +# break pass