forked from nebhead/PiFire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc_prototype.py
executable file
·136 lines (111 loc) · 5.51 KB
/
adc_prototype.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
# *****************************************
# PiFire ADC Prototype Interface Library
# *****************************************
#
# Description: This library simulates getting temperature in F from an generic ADC
#
# *****************************************
# *****************************************
# Imported Libraries
# *****************************************
import math
import random
class ReadADC:
def __init__(self, grill_probe1_profile, grill_probe2_profile, probe_01_profile, probe_02_profile, units='F'):
self.grill_probe1_profile = grill_probe1_profile
self.grill_probe2_profile = grill_probe2_profile
self.probe_01_profile = probe_01_profile
self.probe_02_profile = probe_02_profile
self.adc_data = {}
self.units = units
if self.units == 'F':
self.adc_data['Grill1Temp'] = 55 # Fake starting temperature for prototype only
self.adc_data['Grill2Temp'] = 55 # Fake starting temperature for prototype only
self.adc_data['Probe1Temp'] = 32 # Fake starting temperature for prototype only
self.adc_data['Probe2Temp'] = 42 # Fake starting temperature for prototype only
else:
self.adc_data['Grill1Temp'] = 12 # Fake starting temperature for prototype only
self.adc_data['Grill2Temp'] = 12 # Fake starting temperature for prototype only
self.adc_data['Probe1Temp'] = 0 # Fake starting temperature for prototype only
self.adc_data['Probe2Temp'] = 5.5 # Fake starting temperature for prototype only
def set_profiles(self, grill_probe1_profile, grill_probe2_profile, probe_01_profile, probe_02_profile):
self.grill_probe1_profile = grill_probe1_profile
self.grill_probe2_profile = grill_probe2_profile
self.probe_01_profile = probe_01_profile
self.probe_02_profile = probe_02_profile
def __adc_to_temp(self, temp, probe_profile):
# Since this is just a prototype module, and data is simulated, this function is used to determine the
# resistance value
A = probe_profile['A']
B = probe_profile['B']
C = probe_profile['C']
try:
if self.units == 'F':
tempK = ((temp - 32) * (5/9)) + 273.15
else:
tempK = temp + 273.15
# https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
# Inverse of the equation, to determine Tr = Resistance Value of the thermistor
x = (1/(2*C))*(A-(1/tempK))
y = math.sqrt(math.pow((B/(3*C)),3)+math.pow(x,2))
Tr = math.exp(((y-x)**(1/3)) - ((y+x)**(1/3)))
except:
Tr = 0
return Tr
def read_all_ports(self):
# This is my attempt at making a pseudo-random temperature that will generally rise
adc_value = [0,0,0,0] # Using this to populate random numbers from 0-9
for index in range(4):
adc_value[index] = random.randint(0,9)
if self.units == 'F':
maxGrillTemp = 425
minGrillTemp = 50
maxProbeTemp = 220
minProbeTemp = 32
changeFactor = 1
else:
maxGrillTemp = 220
minGrillTemp = 10
maxProbeTemp = 105
minProbeTemp = 0
changeFactor = 0.5
if adc_value[0] > 7 and self.adc_data['Grill1Temp'] < maxGrillTemp:
self.adc_data['Grill1Temp'] += 1 # raise temperature by changeFactor degree
elif adc_value[0] < 1 and self.adc_data['Grill1Temp'] > minGrillTemp:
self.adc_data['Grill1Temp'] -= 1 # reduce temperature by changeFactor degree
if adc_value[3] > 7 and self.adc_data['Grill2Temp'] < maxGrillTemp:
self.adc_data['Grill2Temp'] += changeFactor # raise temperature by changeFactor degree
elif adc_value[3] < 1 and self.adc_data['Grill2Temp'] > minGrillTemp:
self.adc_data['Grill2Temp'] -= changeFactor # reduce temperature by changeFactor degree
if adc_value[1] > 7 and self.adc_data['Probe1Temp'] < maxProbeTemp:
self.adc_data['Probe1Temp'] += changeFactor # raise temperature by changeFactor degree
elif adc_value[1] < 1 and self.adc_data['Probe1Temp'] > minProbeTemp:
self.adc_data['Probe1Temp'] -= changeFactor # reduce temperature by changeFactor degree
if adc_value[2] > 7 and self.adc_data['Probe2Temp'] < maxProbeTemp:
self.adc_data['Probe2Temp'] += changeFactor # raise temperature by changeFactor degree
elif adc_value[2] < 1 and self.adc_data['Probe2Temp'] > minProbeTemp:
self.adc_data['Probe2Temp'] -= changeFactor # reduce temperature by changeFactor degree
# Thermistor data is not useful in prototype mode
# Resistance of Grill 1 Thermistor
self.adc_data['Grill1Tr'] = self.__adc_to_temp(self.adc_data['Grill1Temp'], self.grill_probe1_profile)
# Resistance of Grill 2 Thermistor
self.adc_data['Grill2Tr'] = self.__adc_to_temp(self.adc_data['Grill2Temp'], self.grill_probe2_profile)
# Resistance of Probe Thermistor
self.adc_data['Probe1Tr'] = self.__adc_to_temp(self.adc_data['Probe1Temp'], self.probe_01_profile)
# Resistance of Probe Thermistor
self.adc_data['Probe2Tr'] = self.__adc_to_temp(self.adc_data['Probe2Temp'], self.probe_02_profile)
return (self.adc_data)
def update_units(self, units):
if units == 'C':
self.units = 'C'
self.adc_data['Grill1Temp'] = 12 # Fake starting temperature for prototype only
self.adc_data['Grill2Temp'] = 12 # Fake starting temperature for prototype only
self.adc_data['Probe1Temp'] = 0 # Fake starting temperature for prototype only
self.adc_data['Probe2Temp'] = 5.5 # Fake starting temperature for prototype only
else:
self.units = 'F'
self.adc_data['Grill1Temp'] = 55 # Fake starting temperature for prototype only
self.adc_data['Grill2Temp'] = 55 # Fake starting temperature for prototype only
self.adc_data['Probe1Temp'] = 32 # Fake starting temperature for prototype only
self.adc_data['Probe2Temp'] = 42 # Fake starting temperature for prototype only