/
pyBath_SINGLESTAGE.py
416 lines (345 loc) · 16.2 KB
/
pyBath_SINGLESTAGE.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# standard library
import sys
import os
import time
import logging
# third party
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import uic
# local library
import gr # TESTING shell
import qtgr
from qtgr.backend import QtCore, QtGui
from qtgr.events import GUIConnector, MouseEvent, PickEvent, LegendEvent
from gr.pygr import Plot, PlotAxes, PlotCurve, ErrorBar
class MainWindow(QtGui.QMainWindow):
def __init__(self, *args, **kwargs):
while not shared_memory['data_fresh']:
pass
super(MainWindow, self).__init__(*args, **kwargs)
uic.loadUi(os.path.join(os.path.dirname(os.path.realpath(__file__)),
"second.ui"), self)
self.monitor_mode = False
self.state = 'Connecting...'
self.prev_time = time.time()
self._heat_capacity_spin.valueChanged.connect(self.updateHeatCapacity)
self._heat_capacity_spin.setDecimals(4)
self._heat_capacity_spin.setMaximum(15.00) # Will we ever heat hydrogen?
self._heat_capacity_spin.setSingleStep(0.0001)
self._mass_spin.valueChanged.connect(self.updateMass)
self._mass_spin.setDecimals(4)
self._mass_spin.setMaximum(1000.00)
self._mass_spin.setSingleStep(0.0001)
self._emissivity_spin.valueChanged.connect(self.updateEmissivity)
self._emissivity_spin.setDecimals(4)
self._emissivity_spin.setMaximum(1.00)
self._emissivity_spin.setSingleStep(0.0001)
self._area_spin.valueChanged.connect(self.updateArea)
self._area_spin.setDecimals(4)
self._area_spin.setMaximum(1000.00)
self._area_spin.setSingleStep(0.0001)
self._resistance_spin.valueChanged.connect(self.updateResistance)
self._resistance_spin.setDecimals(1)
self._resistance_spin.setMaximum(1000.0)
self._resistance_spin.setSingleStep(0.1)
self._voltage_spin.valueChanged.connect(self.updateVoltage)
self._voltage_spin.setDecimals(2)
self._voltage_spin.setMaximum(1000.00)
self._voltage_spin.setSingleStep(0.01)
self._p_spin.valueChanged.connect(self.updateP)
self._p_spin.setDecimals(4)
self._p_spin.setMaximum(2.00)
self._p_spin.setSingleStep(0.0001)
self._i_spin.valueChanged.connect(self.updateI)
self._i_spin.setDecimals(4)
self._i_spin.setMaximum(2.00)
self._i_spin.setSingleStep(0.0001)
self._d_spin.valueChanged.connect(self.updateD)
self._d_spin.setDecimals(4)
self._d_spin.setMaximum(2.00)
self._d_spin.setSingleStep(0.0001)
self._target_spin.valueChanged.connect(self.updateTarget)
self._target_spin.setDecimals(2)
self._target_spin.setMaximum(500.00)
self._target_spin.setSingleStep(0.01)
self._monitor_check.stateChanged.connect(self.updateMonitor)
self._btn_start.clicked.connect(self.startProcess)
self._btn_stop.clicked.connect(self.stopProcess)
self._state_out_label.setText(self.state)
try:
self._heat_capacity_spin.setValue(shared_memory['heatCapacity'])
self._mass_spin.setValue(shared_memory['mass'])
self._emissivity_spin.setValue(shared_memory['emissivity'])
self._area_spin.setValue(shared_memory['area'])
self._resistance_spin.setValue(shared_memory['resistance'])
self._voltage_spin.setValue(shared_memory['voltage'])
self._p_spin.setValue(shared_memory['p'])
self._i_spin.setValue(shared_memory['i'])
self._d_spin.setValue(shared_memory['d'])
except:
pass
# palette = self._ambient_lcd.palette()
# # foreground color
# palette.setColor(palette.WindowText, QtGui.QColor(0, 0, 0))
# # background color
# palette.setColor(palette.Background, QtGui.QColor(0, 0, 0))
# # "light" border
# # palette.setColor(palette.Light, QtGui.QColor(255, 0, 0))
# # "dark" border
# palette.setColor(palette.Dark, QtGui.QColor(0, 0, 0))
# # set the palette
# self._ambient_lcd.setPalette(palette)
# self._bath_lcd.setPalette(palette)
# self._ambient_lcd.display(0.0)
# self._bath_lcd.display(0.0)
x = [1.0]
y = [shared_memory['bath_temp']]
xe = [1.0]
ye = [shared_memory['env_temp']]
viewport = [0.1, 0.95, 0.1, 0.95]
self.env_curve = PlotCurve(xe, ye, legend = "Environment")
self.env_curve.linetype = gr.LINETYPE_DASHED
self.env_curve.linecolor = 7
self.bath_curve = PlotCurve(x, y, legend = "Bath Medium")
axes = PlotAxes(viewport)
# axes.addCurves(self.env_curve)
axes.addCurves(self.bath_curve)
self._plot = Plot(viewport).addAxes(axes)
self._plot.title = "System Temperatures"
#self._plot.subTitle = "live data"
self._plot.xlabel = "Seconds"
self._plot.ylabel = "Celsius"
self._plot.setLegend(True)
self._plot.autoscale = PlotAxes.SCALE_X | PlotAxes.SCALE_Y
self._stage.addPlot(self._plot)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.updateData)
timer.start(5000)
def _reset_curves(self):
self.bath_curve.x = [0.0]
self.env_curve.x = [0.0]
self.bath_curve.y = [shared_memory['bath_temp']]
self.env_curve.y = [shared_memory['env_temp']]
def updateData(self):
if shared_memory['data_fresh']:
t = time.time()
if self.state != 'Connected!':
self.state = 'Connected!'
self._state_out_label.setText(self.state)
self._env_label.setText(str(shared_memory['env_temp']))
self._bath_label_2.setText(str(shared_memory['bath_temp']))
if self.monitor_mode or shared_memory['start']:
self.env_curve.x.append(t - self.prev_time)#self.env_curve.x[-1]+5)
self.bath_curve.x.append(t - self.prev_time)#self.bath_curve.x[-1]+5)
self.env_curve.y.append(shared_memory['env_temp'])
self.bath_curve.y.append(shared_memory['bath_temp'])
else:
self.state = 'Connecting...'
self._state_out_label.setText(self.state)
self._env_label.setText('0.0')
self._bath_label_2.setText('0.0')
self.update()
def updateHeatCapacity(self):
shared_memory['heatCapacity'] = float(self._heat_capacity_spin.cleanText())
def updateMass(self):
shared_memory['mass'] = float(self._mass_spin.cleanText())
def updateEmissivity(self):
shared_memory['emissivity'] = float(self._emissivity_spin.cleanText())
def updateArea(self):
shared_memory['area'] = float(self._area_spin.cleanText())
def updateResistance(self):
shared_memory['resistance'] = float(self._resistance_spin.cleanText())
def updateVoltage(self):
shared_memory['voltage'] = float(self._voltage_spin.cleanText())
def updateP(self):
shared_memory['p'] = float(self._p_spin.cleanText())
def updateI(self):
shared_memory['i'] = float(self._i_spin.cleanText())
def updateD(self):
shared_memory['d'] = float(self._d_spin.cleanText())
def updateMonitor(self):
if not self.monitor_mode:
self._reset_curves()
self._stage.update()
self.monitor_mode = True
else:
self.monitor_mode = False
def updateTarget(self):
shared_memory['target'] = float(self._target_spin.cleanText())
def startProcess(self):
if shared_memory['target'] is not None:
self._monitor_check.setCheckState(False)
self._monitor_check.setEnabled(False)
self._heat_capacity_spin.setEnabled(False)
self._mass_spin.setEnabled(False)
self._emissivity_spin.setEnabled(False)
self._area_spin.setEnabled(False)
self._resistance_spin.setEnabled(False)
self._voltage_spin.setEnabled(False)
self._p_spin.setEnabled(False)
self._i_spin.setEnabled(False)
self._d_spin.setEnabled(False)
self._target_spin.setEnabled(False)
self._btn_start.setEnabled(False)
self._reset_curves()
self._stage.update()
shared_memory['start'] = True
def stopProcess(self):
self._monitor_check.setEnabled(True)
self._heat_capacity_spin.setEnabled(True)
self._mass_spin.setEnabled(True)
self._emissivity_spin.setEnabled(True)
self._area_spin.setEnabled(True)
self._resistance_spin.setEnabled(True)
self._voltage_spin.setEnabled(True)
self._p_spin.setEnabled(True)
self._i_spin.setEnabled(True)
self._d_spin.setEnabled(True)
self._target_spin.setEnabled(True)
self._btn_start.setEnabled(True)
shared_memory['start'] = False
def get_temp( send=False, on_time=0 ):
if send: controller.set_element_time(on_time)
# If there is a problem with packet
t = controller.get_temperatures()
if t is None or t[0] == 0.0 or t[1] == 0.0:
shared_memory['data_fresh'] = False
return
shared_memory['env_temp'] = t[0]
shared_memory['bath_temp'] = t[1]
shared_memory['data_fresh'] = True
def window_main(*args):
app = QtGui.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
if __name__ == "__main__":
import PID
from BathController import BathController
from multiprocessing import Process, Manager, Lock
from ConfigParser import SafeConfigParser
def resistance_to_watts(resistance, voltage):
""" Calculate element wattage from resitance and voltage """
return (voltage**2)/resistance
def joules_to_watt_seconds(joules, element_wattage):
""" Calculate element time from energy in joules """
return float(joules/element_wattage)
def temperature_to_joules(temp_obj, temp_amb, mass_kg, heat_capacity):
""" Calculate energy contained within the bath medium """
# 4.184 is cal -> joule, specific heat is cal/g/c, we want joule/g/k
return float((heat_capacity * (mass_kg*1000) * (temp_obj - temp_amb))/4.184)
def boltzmann_loss(emissivity, area, temp_obj, temp_amb):
""" Calculate the energy radiated at a given temperature """
temp_amb += 273.15
temp_obj += 273.15
boltzmann_const = 5.670373e-8# W * m^-2 * K^-4
a = area*((temp_obj**4) - (temp_amb**4))# A(T^4 - Tc^4)
return emissivity*boltzmann_const*a
manager = Manager()
config = SafeConfigParser()
config.read('config.conf')
shared_memory = manager.dict({
'env_temp':None,
'bath_temp':None,
'heatCapacity':float(config.get('Tuning', 'Heat_Capacity')),
'mass':float(config.get('Tuning', 'Mass')),
'emissivity':float(config.get('Tuning', 'Emissivity')),
'area':float(config.get('Tuning', 'Area')),
'resistance':float(config.get('Tuning', 'Resistance')),
'voltage':float(config.get('Tuning', 'Voltage')),
'p':float(config.get('Tuning', 'P')),
'i':float(config.get('Tuning', 'I')),
'd':float(config.get('Tuning', 'D')),
'target':None,
'start':False,
'data_fresh':False
})
controller = BathController(config.get('Connection', 'Port'),
config.get('Connection', 'Baud'))
time.sleep(3)
print "\rLift off!"
get_temp()
# Start main window thread
window_thread = Process(target = window_main, args = (sys.argv, shared_memory))
window_thread.daemon = True
window_thread.start()
# Initialise PID object
pid = PID.control(shared_memory['p']/10000.0, shared_memory['i']/10000.0, shared_memory['d']/10000.0)
# Whilst the UI is open
while window_thread.is_alive():
# Kick over
get_temp()
#get_temp()
if shared_memory['start'] and shared_memory['data_fresh']:
cycleStart = time.time()
dist = None
target_reached = False
while shared_memory['start'] and window_thread.is_alive():
cycleStart = time.time()
To = shared_memory['target']
Ta = shared_memory['env_temp']
Tb = shared_memory['bath_temp']
m = shared_memory['mass']
h = shared_memory['heatCapacity']
e = shared_memory['emissivity']
a = shared_memory['area']
w = resistance_to_watts(shared_memory['resistance'], shared_memory['voltage'])
if Tb >= To: target_reached = True
# Calculate desired energy within medium
set_point = temperature_to_joules(To, Ta, m, h)
# print "Set point is {0} Joules".format(set_point)
# Add boltzmann radiated loss for that moment
#set_point += boltzmann_loss(e, a, To, Ta)
# Assess the current energy within medium
current_energy = temperature_to_joules(Tb, Ta, m, h)
print "\nCurrent stored energy is {0} Joules".format(current_energy)
bltz = boltzmann_loss(e,a, Tb, Ta)
# Add boltzamn
energy_deficit = set_point - current_energy #+ bltz
# First loop? save total error for adaptive tuning
if dist is None: dist = energy_deficit
print "Energy deficit is {0} Joules".format(set_point - current_energy)#+ bltz)
if energy_deficit > 0:
# Adaptive tuning
# Below 33% and more than 20Kj, agressive tuning
# if energy_deficit >= (dist/3)*2 and energy_deficit >= 20000:
# print 'Mode: Agrressive '
# pid.setKp(shared_memory['p'] / 100.00)
# pid.setKi(shared_memory['i'] / 100.00)
# pid.setKd(shared_memory['d'] / 100.00)
# Within 25%, use a more conservative tuning
# if energy_deficit <= (dist/10):
# print 'Mode: Conservative '
# pid.setKp(shared_memory['p'] / 20000.00)
# pid.setKi(shared_memory['i'] / 20000.00)
# pid.setKd(shared_memory['d'] / 20000.00)
# # Within 25%, use a more conservative tuning
# elif energy_deficit <= (dist/4):
# print 'Mode: Conservative '
# pid.setKp(shared_memory['p'] / 10000.00)
# pid.setKi(shared_memory['i'] / 10000.00)
# pid.setKd(shared_memory['d'] / 10000.00)
# # Else normal tuning
# else:
print 'Mode: SINGLE STAGE '
pid.setKp(shared_memory['p'] / 1000.00)
pid.setKi(shared_memory['i'] / 1000.00)
pid.setKd(shared_memory['d'] / 1000.00)
# send the PID controller set_energy - stored energy (error)
energy_output = pid.genOut(set_point - current_energy)
# pid returns energy to put in, convert to watt seconds
element_time = joules_to_watt_seconds(energy_output, w)
print "PID wants {0} Joules".format(energy_output)
if element_time > 0.0 and element_time < 10.0:
print 'Element time = ', element_time
get_temp(send=True, on_time=int(element_time*1000))
elif element_time > 10.0:
print 'Element time = 10.0 [CLAMPED]'
get_temp(send=True, on_time=10000)
# Update temps
get_temp()
# dt
time.sleep(10 - (time.time()-cycleStart))
else:
time.sleep(5)