Files
maloja/panel_test/temperature_controller/temperature_control_updated.py
T
2026-08-21 21:40:30 +02:00

216 lines
7.3 KiB
Python
Executable File

from collections import deque
import random
import wx
import numpy as np
import epics
from datetime import datetime
import matplotlib.pyplot as plt
#from epics.wx import MotorPanel
from slic.gui.widgets import LabeledMathEntry
from slic.gui.widgets import make_filled_hbox, make_filled_vbox, EXPANDING
from slic.gui.widgets.plotting import PlotPanel
from bstrd import BS, bsstream
from epics import caget, caput, cainfo
from scipy.signal import savgol_filter
from decimal import Decimal
import time
def gauss(axis, amp, center, sigma):
return amp*np.exp( -1*(axis - center)**2/(2*sigma**2))
#nshots = 100
#channel_tof = "SATES21-GES1:A1_VALUES"
#ch_tof = BS(channel_tof)
#temp_tof = ch_tof.get()
#print(temp_tof)
#next(bsstream)
temp1_channel = 'SATES20-CWAG-GEP01:TEMP07'
temp2_channel = 'SATES20-CWAG-GEP01:TEMP08'
voltage1_channel = 'SATES20-CWAG-GEP01:DAC07_VOLTS'
voltage2_channel = 'SATES20-CWAG-GEP01:DAC08_VOLTS'
pressure_channel = 'SATES21-VM-VT1020:PRESSURE'
temp1_PV = epics.PV(temp1_channel)
temp2_PV = epics.PV(temp2_channel)
voltage1_PV = epics.PV(voltage1_channel)
voltage2_PV = epics.PV(voltage2_channel)
pressure_PV = epics.PV(pressure_channel)
iso_format = "%Y-%m-%d %H:%M:%S"
def create_text_entry(parent,text = "", maxlength = 5, hint = 0, setvalue = 0):
text = wx.StaticText(parent, -1, text)
textentry = wx.TextCtrl(parent)
textentry.SetMaxLength(maxlength)
textentry.SetHint('%s'%str(hint))
textentry.SetValue('%s'%str(setvalue))
return textentry, text
class MainPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.last_update_time = time.time()
self.quelength = 6000
self.temp1deque = deque(maxlen=self.quelength)
self.temp2deque = deque(maxlen=self.quelength)
self.pressuredeque = deque(maxlen=self.quelength)
self.set_temp1 = 24.0
self.set_temp2 = 24.0
self.set_tempw1 = 10.0
self.set_tempw2 = 10.0
self.plottemp = plottemp = PlotPanel(self, figsize=(4,4))
plots1 = (plottemp,)
hb_plot1 = make_filled_hbox(plots1)
###### ROI box ######
self.temp1, self.tempt1 = create_text_entry(self, text = "Temperature 1", maxlength = 5, setvalue = self.set_temp1)
temp1box = make_filled_vbox((self.tempt1, self.temp1), border=1)
self.tempw1, self.tempwt1 = create_text_entry(self, text = "Temp window 1", maxlength = 5, setvalue = self.set_tempw1)
tempw1box = make_filled_vbox((self.tempwt1, self.tempw1), border=1)
self.temp2, self.tempt2 = create_text_entry(self, text = "Temperature 2", maxlength = 5, setvalue = self.set_temp2)
temp2box = make_filled_vbox((self.tempt2, self.temp2), border=1)
self.tempw2, self.tempwt2 = create_text_entry(self, text = "Temp window 2", maxlength = 5, setvalue = self.set_tempw2)
tempw2box = make_filled_vbox((self.tempwt2, self.tempw2), border=1)
btn_updatesettemp = wx.Button(self, label="Update Temperatures")
tempbox = make_filled_hbox(( temp1box, tempw1box, temp2box, tempw2box, btn_updatesettemp), border=1)
temp_box = make_filled_vbox((tempbox, hb_plot1))
self.plotpressure = plotpressure = PlotPanel(self, figsize=(4,4))
plots2 = (plotpressure,)
hb_plot2 = make_filled_hbox(plots2)
self.pressure_min_ctrl, self.pressure_min_text = create_text_entry(self, text = "Pressure Y min", maxlength = 15, setvalue = "1e-6")
self.pressure_max_ctrl, self.pressure_max_text = create_text_entry(self, text = "Pressure Y max", maxlength = 15, setvalue = "1e-2")
set_pressure_btn = wx.Button(self, label="Pressure ")
self.pressure_min = Decimal('1e-6')
self.pressure_max = Decimal('1e-2')
press_min_box = make_filled_vbox((self.pressure_min_text, self.pressure_min_ctrl))
press_max_box = make_filled_vbox((self.pressure_max_text, self.pressure_max_ctrl))
pressure_entries_box = make_filled_hbox(( press_min_box, press_max_box, set_pressure_btn), border=1)
pressure_box = make_filled_vbox((hb_plot2, pressure_entries_box))
box = make_filled_vbox((temp_box, pressure_box))
self.SetSizerAndFit(box)
btn_updatesettemp.Bind(wx.EVT_BUTTON, self.on_click_updateTemp) # change function later
set_pressure_btn.Bind(wx.EVT_BUTTON, self.on_click_updatePressure)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_update, self.timer)
self.timer.Start(100)
def on_update(self, _event):
self.time = time.time()
self.currenttemp1 = temp1_PV.get()
self.currenttemp2 = temp2_PV.get()
self.pressure = pressure_PV.get()
if self.time - self.last_update_time>2:### Update voltage settings and append to queue every 2 seconds
self.change_temp()
self.temp1deque.append(self.currenttemp1)
self.temp2deque.append(self.currenttemp2)
self.pressuredeque.append(self.pressure)
self.draw_plot()
wx.GetApp().Yield()
def change_temp(self):
## currently open or close transistor, future, implement PID
self.last_update_time = time.time()
if self.currenttemp1 - self.set_temp1 > self.set_tempw1:
voltage1_PV.put(0)
print('Current temp: %0.1f // Set temp %0.1f // Turn OFF voltage'%(self.currenttemp1,self.set_temp1 ))
self.tempt1.SetForegroundColour(wx.Colour(255,0,0))
else:
voltage1_PV.put(5)
print('Current temp: %0.1f // Set temp %0.1f // Turn ON voltage'%(self.currenttemp1,self.set_temp1 ))
self.tempt1.SetForegroundColour(wx.Colour(0,255,0))
if self.currenttemp2 - self.set_temp2 > self.set_tempw2:
voltage2_PV.put(0)
print('Current temp: %0.1f // Set temp %0.1f // Turn OFF voltage'%(self.currenttemp2,self.set_temp2 ))
self.tempt2.SetForegroundColour(wx.Colour(255,0,0))
else:
voltage2_PV.put(5)
print('Current temp: %0.1f // Set temp %0.1f // Turn ON voltage'%(self.currenttemp2,self.set_temp2 ))
self.tempt2.SetForegroundColour(wx.Colour(0,255,0))
def draw_plot(self):
self.plottemp.clear()
self.plottemp.plot(np.squeeze(self.temp1deque).flatten(), '.-', label = 'Temp 1')
self.plottemp.plot(np.squeeze(self.temp2deque).flatten(), '.-', label = 'Temp 2')
self.plottemp.legend()
self.plottemp.draw()
self.plotpressure.clear()
self.plotpressure.plot(np.squeeze(self.pressuredeque).flatten(), '.-', label = 'Pressure')
self.plotpressure.set_ylim([self.pressure_min, self.pressure_max])
self.plotpressure.set_yscale('log')
self.plotpressure.legend()
self.plotpressure.draw()
def on_click_updateTemp(self, _event):
self.set_temp1 = np.double(self.temp1.GetValue())
self.set_temp2 = np.double(self.temp2.GetValue())
self.set_tempw1 = np.double(self.tempw1.GetValue())
self.set_tempw2 = np.double(self.tempw2.GetValue())
print('Updated temperature setpoints')
def on_click_updatePressure(self, _event):
self.pressure_min = Decimal(self.pressure_min_ctrl.GetValue())
self.pressure_max = Decimal(self.pressure_max_ctrl.GetValue())