-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
335 lines (293 loc) · 11.2 KB
/
gui.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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 11 09:47:49 2019
@author: JOANRR
"""
import datetime
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_daq as daq
from dash.dependencies import Input, Output, State
from pyInstruments.pid import TemperatureController
from threading import Thread
from collections import deque
from pyvisa import ResourceManager
import getopt
import sys
global N_CLICK_PREVIOUS, MAX_LENGTH
N_CLICK_PREVIOUS = 0
MAX_LENGTH = 500
# Listing the available resources
lresources = ResourceManager().list_resources()
# Initialize the pid task
p = TemperatureController()
# Preparing the plot
plot_layout = dict(margin = {'l': 60, 'r': 60, 'b': 60, 't': 20},\
legend = {'x': 0, 'y': 1, 'xanchor': 'left'},\
xaxis = dict(title = "Timestamp", font = dict(size = 24)),\
yaxis = dict( title = "Temperature (°C)", font = dict(size = 24))
)
calc_status = lambda x: bool(abs(int((1j**x).real)))
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets = external_stylesheets)
app.title = 'PID'
app.layout = html.Div(children = [
html.Div(className = 'row', children = [
html.Div(className = 'column left', children = [
daq.Indicator(id='my-daq-indicator',
value=True,
color="#FF6633",
# className="two columns",
size = 25, style = {'width': '50px', 'display': 'inline-block', 'vertical-align':'middle'}
),
html.H4('Temperature stage PID setup', style = {'display': 'inline-block','vertical-align':'middle'}),
dcc.Graph(id='live-update-graph',
figure= {"layout": plot_layout
}
),
dcc.Interval(
id='interval-component',
interval = 1000, # in milliseconds
n_intervals = 0,
disabled = True
),
html.Span('Multimeter address:'),
dcc.Dropdown(id = 'dropdown-multimeter',
options = [{'label' : name, 'value': name} for name in lresources],
value = 'GPIB0::23::INSTR' if 'GPIB0::23::INSTR' in lresources else None,
placeholder = 'Multimeter address',
style = {'width' : '80%', 'min-width' : '150px'},
searchable = False
),
html.Span('Power source address:'),
dcc.Dropdown(id = 'dropdown-sourcemeter',
options = [{'label' : name, 'value': name} for name in lresources],
value ='GPIB0::5::INSTR' if 'GPIB0::5::INSTR' in lresources else None,
placeholder = 'Sourcemeter address',
style = {'width' : '80%', 'min-width' : '150px'},
searchable = False
)
]),
html.Div(className = 'column middle', children = [
daq.PowerButton(
id='power-button',
on = None,
color = "#FF5E5E",
),
daq.StopButton(id='my-daq-startbutton',
buttonText = 'Start',
n_clicks = 0,
),
daq.StopButton(id='my-daq-clearbutton',
n_clicks = 0,
buttonText = 'Clear',
),
daq.StopButton(id='refresh-button',
n_clicks = 0,
buttonText = 'Refresh',
),
html.P(['Sensor type:',
dcc.RadioItems(
id = 'rtd-type',
options=[
{'label': 'Pt100', 'value': 100},
{'label': 'Pt1000', 'value': 1000}
],
value = 100
),
daq.BooleanSwitch(
id='cooling-switch',
label = 'Cooling?',
on = False,
disabled = True,
),
daq.NumericInput(
id='max-power',
label = 'Max. action (V)',
min = 0.0,
max = 21.00,
value = 5.00,
)
]),
]),
html.Div( className = 'column right', children = [
html.Div([
html.P(id = 'label-setpoint', children = 'Temperature setpoint is 20.00 °C'),
dcc.Input(
id='set-setpoint',
type = 'number',
min = 0,
max = 100,
value = 20.00,
debounce = True,
size = '10'
),
html.Br(),
html.Br(),
html.P('Current temperature (°C)'),
daq.LEDDisplay(
id = 'my-curent-T',
labelPosition = 'top',
value = f'{0.00:05.2f}',
color= "#FF5E5E",
),
], style ={'padding-top' : '25px', 'text-align' : 'center'})
]),
])
])
# Multiple components can update everytime interval gets fired.
@app.callback([Output('live-update-graph', 'figure'),
Output('my-curent-T', 'value')],
[Input('interval-component', 'n_intervals'),
Input('my-daq-clearbutton', 'n_clicks')],
[State('live-update-graph', 'figure')])
def update_graph_live(n, n_clear,figure):
# Determine which button has been clicked
ctx = dash.callback_context
if not ctx.triggered:
button_id = 'No clicks yet'
else:
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
if button_id == 'interval-component':
tstamp = datetime.datetime.now() #.strftime("%d-%m-%YT %H:%M:%S.%f")
x = deque(figure['data'][0]['x'], MAX_LENGTH)
y = deque(figure['data'][0]['y'], MAX_LENGTH)
y2 = deque(figure['data'][1]['y'], MAX_LENGTH)
x.append(tstamp)
y.append( p.current_T)
y2.append(p.setpoint)
elif button_id == 'my-daq-clearbutton':
print('Clearing')
x = []
y = []
y2 = []
else:
x = y = y2 = []
figure['data'] = [{
'x': list(x),
'y': list(y),
'name': 'Current T',
'mode': 'lines+markers',
'type': 'scatter'
},
{
'x': list(x),
'y': list(y2),
'name': 'Setpoint T',
'mode': 'lines+markers',
'type': 'scatter'
}]
if len(x) == 0: Tstring = '00.00'
else: Tstring = '{0:05.2f}'.format(y[-1])
return figure, Tstring
@app.callback([Output('my-daq-startbutton', 'buttonText'),
Output('my-daq-indicator', 'color'),
Output('interval-component', 'disabled')],
[Input('my-daq-startbutton', 'n_clicks')],
prevent_initial_call = True)
def start_measurement(N):
color = ["#00cc96", '#FF6633']
label = 'Start'
status = calc_status(N + 1)
try:
if status is True:
print('INFO: Instrument started...')
p.pid_on()
t = Thread(target = p.run)
t.daemon = True
t.start()
label = 'Stop'
elif status is False:
print('INFO: Instrument configured and ready!')
p.pid_off()
label = 'Start'
else:
pass
return label, color[int(not status)], not status
except Exception as e:
print('ERROR: An error occured in starting the instrument')
print(e)
return label, color[1], True
@app.callback([Output('label-setpoint', 'children')],
[Input('set-setpoint', 'value')])
def write_setpoint(value):
p.setpoint = value
label = f'Temperature setpoint is {p.setpoint:5.2f} °C'
return [label]
@app.callback([Output('my-daq-startbutton', 'disabled'),
Output('my-daq-startbutton', 'n_clicks'),
Output('cooling-switch', 'disabled')],
[Input('power-button', 'on')],
[State('my-daq-startbutton', 'n_clicks'),
State('set-setpoint', 'value'),
State('cooling-switch', 'on'),
State('max-power', 'value'),
State('dropdown-multimeter', 'value'),
State('dropdown-sourcemeter', 'value'),
State('rtd-type', 'value')])
def start_instrument(on, N, setpoint, cooling, max_power, mult_addr, source_addr, R0):
"""The cooling flag needs to be denied, as the TemperatureController.config() ask if HEATING, just the opposite"""
if on is None:
return True, 0, False
try:
if on:
p.configurate(setpoint, not cooling, max_power, mult_addr, source_addr, R0)
return False, N, True
else:
return True, 0, False
except Exception as e:
print('ERROR: An error occured in starting the instrument')
print(e)
return True, 0, False
@app.callback([Output('max-power', 'label')],
[Input('max-power', 'value')])
def max_power(value):
p.max_poutput = value
label = 'Max. action (V)'
return [label]
@app.callback([Output('dropdown-multimeter', 'value'),
Output('dropdown-multimeter', 'options'),
Output('dropdown-sourcemeter', 'value'),
Output('dropdown-sourcemeter', 'options')],
[Input('refresh-button', 'n_clicks')])
def refresh_resources(n):
list_of_resources = ResourceManager().list_resources()
default_resources = [s for s in list_of_resources if 'GPIB' in s]
if len(default_resources) > 1:
sourcemeter_resource = default_resources[0]
multimeter_resource = default_resources[1]
else:
sourcemeter_resource = None
multimeter_resource = None
options = [{'label' : name, 'value': name} for name in list_of_resources]
return multimeter_resource, options, sourcemeter_resource, options
if __name__ == '__main__':
# Default values
debug = True
port = 8052
user_reloader = False
argv = sys.argv[1:]
try:
options, args = getopt.getopt(argv, "p:d:r:",
["port =",
"debug =",
"user_reloader = "])
for name, value in options:
if name in ['-d', '--debug']:
if value.lower() in ['true', '1']:
debug = True
else:
debug = False
elif name in ['-p', '--port']:
port = value
elif name in ['-r', '--user_reloader']:
if value.lower() in ['true', '1']:
user_reloader = True
else:
user_reloader = False
app.run_server(debug = debug, port = port, use_reloader = user_reloader)
except KeyboardInterrupt:
print("Program terminated.")
except Exception as e:
print(e)