From f9ac760a4374886d3f5db5a55399e755f7adfb0a Mon Sep 17 00:00:00 2001 From: MMO Date: Sun, 15 Oct 2017 14:44:23 +0000 Subject: [PATCH] working on it ... --- src/config.py | 2 ++ src/pwm.py | 73 +++++++++++++++++++++++++++++++++++++++++++ src/spi/spi.py | 36 ++++++++++++++++++++++ src/test.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 src/config.py create mode 100644 src/pwm.py create mode 100644 src/spi/spi.py create mode 100644 src/test.py diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..5a0ce96 --- /dev/null +++ b/src/config.py @@ -0,0 +1,2 @@ +from abc import Singleton + diff --git a/src/pwm.py b/src/pwm.py new file mode 100644 index 0000000..ceb1be4 --- /dev/null +++ b/src/pwm.py @@ -0,0 +1,73 @@ +#import RPIO +import RPi.GPIO as GPIO +import time +from datetime import datetime +from datetime import timedelta +switch = 15 +pwm = 14 +fan_speed =18 +#RPIO.setup(switch, RPIO.OUT) + +#RPIO.output(switch, True) + +# RPIO.PWM.setup() +#fan = RPIO.PWM.Servo(0, 40, 1) +GPIO.setmode(GPIO.BCM) +GPIO.setup(switch, GPIO.OUT) +GPIO.setup(pwm, GPIO.OUT) +fan = GPIO.PWM(pwm, 25000) + +GPIO.setup(fan_speed, GPIO.IN, pull_up_down=GPIO.PUD_UP) +global last_ts +global current_ts +global diff +diff = timedelta() +last_ts = datetime.now() +current_ts = 0 +global counter +counter = 0 +def speed_callback(channel): + global last_ts + global current_ts + global diff + global counter + current_ts = datetime.now() + if current_ts != 0 and last_ts != 0: + diff = current_ts - last_ts + if diff.seconds < 1: + counter+=1 + else: + rpm = (counter/2)*60 + print("{} ||| {} ".format(counter, rpm), end="\r") + last_ts = current_ts + counter = 0 +# print("{} ".format(rpm), end="\r") +# print("{:d} | \t{:.2f} | \t{} | \t{} | \t{}".format(diff.microseconds, rpm, diff, current_ts, last_ts), end="\r") + + +GPIO.add_event_detect(fan_speed, GPIO.FALLING, callback=speed_callback) + +try: + GPIO.output(switch, True) + step = 10 + c = 0 + direction = 1 + diff=timedelta() + fan.start(c) + while True: + if c == 100: + direction = -1 + elif c == 0: + direction = 1 + c += step*direction + fan.ChangeDutyCycle(c) +# print("{} ".format(c)) + time.sleep(5) +except KeyboardInterrupt: + pass +finally: + fan.stop() + GPIO.output(switch, False) + GPIO.cleanup() + + diff --git a/src/spi/spi.py b/src/spi/spi.py new file mode 100644 index 0000000..ae55a49 --- /dev/null +++ b/src/spi/spi.py @@ -0,0 +1,36 @@ +from abc import Singleton +import RPi.GPIO as GPIO +import spidev + + +class LowLevel(metaclass=Singleton) + def __init__(self): + self.__spi = spidev.SpiDev() + self.__spi.open(0,0) + self.__spi.mode = 0 + self.__spi.max_speed_hz = 1000000 + GPIO.setmode(GPIO.BCM) + + + def __activate(self): + self.__spi.cshigh = False + + def __deactivate(self): + self.__spi.cshigh = True + + def retrieve_adc(self, channel): + if channel > 7 or channel < 0: + return None + + GPIO.output(pin, True) + hchbit=channel >> 2 + lchbits = channel & 0x03 + to_send = [ hchbit | 0x06, lchbits << 6, 0x00 ] + self.__activate() + r=spi.xfer(to_send) + self.__deactivate() + GPIO.output(pin, False) + highbyte = r[1] + lowbyte = r[2] + value = (highbyte << 8) | lowbyte + return value diff --git a/src/test.py b/src/test.py new file mode 100644 index 0000000..3a13bc0 --- /dev/null +++ b/src/test.py @@ -0,0 +1,84 @@ +#!/usr/bin/python + +import spidev +import time +import RPi.GPIO as GPIO +from decimal import * + +tnom = 25 +rnom = 10000.0 +corr = 4800 +temp_gpio = { + 0: { + "BCM": 20, + "Rk": 9770.0 #9770, + }, + 1: { + "BCM": 26, + "Rk": 10000.0 + } + } + +GPIO.setmode(GPIO.BCM) +for k in temp_gpio: + GPIO.setup(temp_gpio[k]["BCM"], GPIO.OUT) + GPIO.output(temp_gpio[k]["BCM"], False) + +def read_spiADC(spi, channel): + if channel > 7 or channel < 0: + return [] + GPIO.output(temp_gpio[channel]["BCM"], True) + hchbit=channel >> 2 + lchbits = channel & 0x03 + to_send = [ hchbit | 0x06, lchbits << 6, 0x00 ] + spi.cshigh=True +# time.sleep(0.01) + spi.cshigh = False + r=spi.xfer(to_send) + spi.cshigh = True + GPIO.output(temp_gpio[channel]["BCM"], False) +# time.sleep(0.01) + highbyte = r[1] + lowbyte = r[2] + value = (highbyte << 8) | lowbyte + return value + +def calc_temp(adc_reading, channel = 0): + rser = temp_gpio[channel]["Rk"] + 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 + +#GPIO.output(temp_gpio[1], True) +#while True: +# pass + +spi = spidev.SpiDev() + +spi.open(0,0) +spi.mode = 0 +spi.max_speed_hz = 1000000 +while True: + chan0 = read_spiADC(spi, 0) + chan1 = read_spiADC(spi, 1) + r0 = 0 + r1 = 0 + t0 = 0 + t1 = 0 + if chan0 != 0: + r0 = rnom/(4095/ chan0 - 1) + t0 = calc_temp(chan0, 0) + if chan1 != 0: + r1 = rnom/(4095/ chan1 - 1) + t1 = calc_temp(chan1, 1) + print("{:d} R={:d} T={:.2f} |\t| {:d} R={:d} T={:.2f}".format(chan0,int(r0), t0, chan1, int(r1), t1), end="\r") + time.sleep(0.25) +# print(str(chan0) +" R=" + str(int(r0)) + " T=" + str(t0) +" |\t| "+str(chan1)+" R="+str(int(r1)) + " T=" +str(t1), end="\r") + +spi.close()