Sie sind auf Seite 1von 3

from msvcrt import kbhit

import u3
import time
import atexit
import math
d = u3.U3()
'''Constants'''
MAX_THRESH = 120 # Overheat temperature in degrees fahrenheit
V_PER_DEG_F = 0.0052 # Configured volts per degree celsius on IR sensor*
AMB_TEMP = 74 # Set as the average ambient temp for table in degrees fahrenheit
AMB_VOLTAGE = 2.1 # Set to be the voltage reading on thermopile at room temp wit
h no object
READ_INTERVAL = 1 # Number of seconds between reads
ON_OVERHEAT = 30 # Number of seconds to power off on an overheat
CLOCK = 4 # 4MHz/Divisor
DIVISOR = 10 # Configure clock
MODE = 3 # 32bit falling edge
REAL_CLOCK = (CLOCK * 1000000) / DIVISOR # Used for RPM calc
'''Fan RPM Detection'''
d.configIO(TimerCounterPinOffset = 6, NumberOfTimersEnabled = 1, FIOAnalog = 48,
EIOAnalog = 3)
d.configTimerClock(CLOCK, DIVISOR) # Set timer clock, divisor
d.getFeedback(u3.Timer0Config(MODE, Value = 0)) # Set timer mode
'''Digital Output functions
FIO3 H-Bridge control wire #1
FIO2 H-Bridge control wire #2
'''
def hot(d):
d.setFIOState(3,0) # FIO3
d.setFIOState(2,1) # FIO2
def cold(d):
d.setFIOState(3,1) # FIO3
d.setFIOState(2,0) # FIO2
def off(d):
d.setFIOState(3,0) # FIO3
d.setFIOState(2,0) # FIO2

Low
High
High
Low
Low
Low

def convert(reading):
"""
Convert a reading from celsius to fahrenheit.
"""
fahrenheit = 1.8 * reading + 32
reading = fahrenheit
return reading
count = 0
while kbhit() == False:
time.sleep(READ_INTERVAL)
atexit.register(off, d)
'''Analog Inputs'''
Temp_Top_In = d.getAIN(4) # AIN4 Temp sensor on top plate
Temp_Bottom_In = d.getAIN(5) # AIN5 Temp sensor on bottom plate
VthermoIR = d.getAIN(9) # EIN1 Voltage reading on the thermopile

VambIR = d.getAIN(8) # EIN0 Voltage reading on the ambient temp sensor


Temp_u3 = d.getAIN(30) # AIN30 automatic internal temp reading in K
'''Digital Inputs'''
Clock_Cycles, = d.getFeedback(u3.Timer0(UpdateReset = True)) # FIO6 Fan spee
d wire input
Toggle_Cold = d.getFIOState(7) # FIO7 Cold and hot selection switch input
Toggle_Man = d.getFIOState(0) # FIO0 Manual selection switch input
Toggle_Auto = d.getFIOState(1) # FIO1 Automatic selection switch input
'''Calculations'''
Ttop = Temp_Top_In * 100 # Convert mV to V
Tbottom = Temp_Bottom_In * 100 # Convert mV to V
Tu3 = (1.8 * (Temp_u3 - 273.15) + 32) # u3 Internal temp sensor
if Clock_Cycles > 0:
RPM = (REAL_CLOCK / Clock_Cycles) * 60 # RPM
else:
RPM = 0
Difference = AMB_TEMP - Ttop
AdjVthermo = VthermoIR + (Difference * V_PER_DEG_F)
Vdrink = AdjVthermo - AMB_VOLTAGE
Tdrink = AMB_TEMP + (Vdrink * (1/V_PER_DEG_F))
#print 'Vdrink: ', Vdrink, 'Volts'
#print 'VSensor: ', VthermoIR, 'Volts'
print 'Drink: ', math.trunc(Tdrink), 'F'
#print 'IR: ', math.trunc(TambIR), 'F'
#print 'U3-LV: ', math.trunc(Tu3), 'F'
print 'Top: ', math.trunc(Ttop), 'F'
print 'Bottom: ', math.trunc(Tbottom), 'F\n'
print 'Fan Speed: ', RPM, ' RPM\n'
'''Digital input switch states'''
Cold, Man, Auto = (Toggle_Cold, Toggle_Man, Toggle_Auto)
if Ttop > MAX_THRESH:
print 'Too Hot!!'
count+=1
if count == 25:
kbhit() == True # Exit failsafe, if gets stuck in 'hot loop'
if Auto and not Man: # Switch is in Manual position
print 'here1'
if Cold:
cold(d)
else:
off(d)
time.sleep(ON_OVERHEAT)
continue
elif not Auto: # Switch is in Automatic position
off(d)
time.sleep(ON_OVERHEAT)
continue
'''Auto / OFF / Manual Toggle switch operations'''
if not Auto: # Switch is in Automatic position
print 'Automatic'
if Tdrink > (AMB_TEMP + 8): # 8 degrees more than ambient
print 'Hot!\n'

hot(d)
elif Tdrink < (AMB_TEMP - 12): # 8 degrees less than ambient
print 'Cold!\n'
cold(d)
else: # IR temp reading ~ equal to ambient
print 'No drink!'
off(d)
elif Auto and Man: # Switch is in Off position
print 'OFF\n'
off(d)
elif Auto and not Man: # Switch is in Manual position
print 'Manual'
'''Hot / Cold Toggle switch operations'''
if not Cold: # Switch is set to hot
print 'Hot!\n'
hot(d)
elif Cold: # Switch is set to hot
print 'Cold!\n'
cold(d)
else: # Something is broken, nothing happens
off(d)
else: # Something is broken, nothing happens
off(d)
"""
* Note: 0.0052 V per degree F based on linear
calibration using a refrigerated beer and a boiling hot coffee mug
"""

Das könnte Ihnen auch gefallen