-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpumpControl.m
More file actions
374 lines (304 loc) · 13.1 KB
/
Copy pathpumpControl.m
File metadata and controls
374 lines (304 loc) · 13.1 KB
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
function varargout = pumpControl(varargin)
% PUMPCONTROL MATLAB code for pumpControl.fig
% PUMPCONTROL, by itself, creates a new PUMPCONTROL or raises the existing
% singleton*.
%
% H = PUMPCONTROL returns the handle to a new PUMPCONTROL or the handle to
% the existing singleton*.
%
% PUMPCONTROL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PUMPCONTROL.M with the given input arguments.
%
% PUMPCONTROL('Property','Value',...) creates a new PUMPCONTROL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before pumpControl_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to pumpControl_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help pumpControl
% Last Modified by GUIDE v2.5 23-Oct-2015 12:01:08
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @pumpControl_OpeningFcn, ...
'gui_OutputFcn', @pumpControl_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
function pumpControl_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to pumpControl (see VARARGIN)
% Choose default command line output for pumpControl
handles.output = hObject;
global pumpguihs
COMPORT = 'COM1';
pumpguihs.serial = serial(COMPORT);
fopen(pumpguihs.serial)
set(pumpguihs.serial,'Terminator',13)
% Handshake:
fprintf(pumpguihs.serial,sprintf('1#\n'));
ret = fscanf(pumpguihs.serial);
if pumpguihs.serial.BytesAvailable
dump = char(fread(pumpguihs.serial,pumpguihs.serial.BytesAvailable)'); % Just to get rid ofthe last byte
end
while ~strfind('REGLO DIGITAL',ret)
fclose(pumpguihs.serial);
cpcell = inputdlg({sprintf('The pump can not be found on the serial port, please specify a new port:\nP.S: Maybe you forgot to turn the pump on?')},'Handshake error',1,{COMPORT});
COMPORT = cpcell{1};
pumpguihs.serial = serial(COMPORT);
fopen(pumpguihs.serial)
set(pumpguihs.serial,'Terminator',13)
fprintf(pumpguihs.serial,sprintf('1#\n'));
ret = fscanf(pumpguihs.serial);
if pumpguihs.serial.BytesAvailable
dump = char(fread(pumpguihs.serial,pumpguihs.serial.BytesAvailable)'); % Just to get rid ofthe last byte
end
end
% Find out how the pump is calibrated:
fprintf(pumpguihs.serial,sprintf('1!\n'));
ret = fscanf(pumpguihs.serial);
indmlmin = strfind(ret,'ml/min');
pumpguihs.calib = str2double(ret(1:(indmlmin-1)));
if pumpguihs.serial.BytesAvailable
dump = char(fread(pumpguihs.serial,pumpguihs.serial.BytesAvailable)'); % Just to get rid ofthe last byte
end
% Find out the current speed:
fprintf(pumpguihs.serial,sprintf('1S\n'));
ret = fscanf(pumpguihs.serial);
pumpguihs.speedPer = str2double(ret);
if pumpguihs.serial.BytesAvailable
dump = char(fread(pumpguihs.serial,pumpguihs.serial.BytesAvailable)'); % Just to get rid ofthe last byte
end
pumpguihs.speedFR = (pumpguihs.speedPer/100)*pumpguihs.calib;
% Find out whether the pump is currently running:
fprintf(pumpguihs.serial,sprintf(['1E\n']));
while(~pumpguihs.serial.BytesAvailable) end
ret = fread(pumpguihs.serial,1);
switch ret
case '+' % running
pumpguihs.running = true;
togglebutton(handles);
case '-' % not running
pumpguihs.running = false;
togglebutton(handles);
otherwise % kewa?
pumpguihs.running = false;
togglebutton(handles);
end
% Set the values in teh GUI
set(handles.editPercent,'String',num2str(pumpguihs.speedPer,'% .2f'));
set(handles.mainslider,'Value',pumpguihs.speedPer/100);
set(handles.editFrate,'String',num2str(pumpguihs.speedFR,'% .3f'));
% Start the timer for monitoring the pump:
pumpguihs.timerSerial = timer;
pumpguihs.timerSerial.TimerFcn = {@tmrCLBK,handles};
pumpguihs.timerSerial.ExecutionMode = 'fixedRate';
pumpguihs.timerSerial.Period = 1;
pumpguihs.timerSerial.StartDelay = 1;
start(pumpguihs.timerSerial)
% Create the timer for the flushing events:
pumpguihs.timerFlush = timer;
pumpguihs.timerFlush.ExecutionMode = 'fixedRate';
pumpguihs.timerFlush.StartDelay = 0;
pumpguihs.timerFlush.TimerFcn = {@flushCLBK,handles};
pumpguihs.timerFlush.UserData = struct();
pumpguihs.timerRevert = timer;
pumpguihs.timerRevert.ExecutionMode = 'SingleShot';
% Update handles structure
guidata(hObject, handles);
function varargout = pumpControl_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function tmrCLBK(obj,evt,handles) % This function monitors the speed in the pump.
global pumpguihs
if pumpguihs.serial.BytesAvailable
dump = char(fread(pumpguihs.serial,pumpguihs.serial.BytesAvailable)'); % Just to get rid ofthe first bytes
end
% Find out current speed:
fprintf(pumpguihs.serial,sprintf('1S\n'));
ret = fscanf(pumpguihs.serial);
if (pumpguihs.speedPer~=str2double(ret)) % Speed difference
Coordinate(ret,'%',handles);
end
if pumpguihs.serial.BytesAvailable
dump = char(fread(pumpguihs.serial,pumpguihs.serial.BytesAvailable)'); % Just to get rid ofthe last byte
end
fprintf(pumpguihs.serial,sprintf(['1E\n']));
while(~pumpguihs.serial.BytesAvailable) end
ret = fread(pumpguihs.serial,1);
switch ret
case '+' % running
pumpguihs.running = true;
togglebutton(handles);
case '-' % not running
pumpguihs.running = false;
togglebutton(handles);
otherwise % kewa?
% Do nothing
end
function flushCLBK(obj,evt,handles) % This function monitors the speed in the pump.
global pumpguihs
% Save old speed:
RUD= get(pumpguihs.timerRevert,'UserData');
RUD.oldspeed= pumpguihs.speedPer;
set(pumpguihs.timerRevert,'UserData',RUD);
% Flush at user speed:
UD= get(pumpguihs.timerFlush,'UserData');
Coordinate(num2str(UD.speed),'%',handles);
% setup the timer to revert speed back to normal
start(pumpguihs.timerRevert);
function revertCLBK(obj,~,handles)
UD = get(obj,'UserData');
Coordinate(num2str(UD.oldspeed),'%',handles)
function Coordinate(value,type,handles)
global pumpguihs
switch type
case '%' % The percent text
value = str2double(value);
pumpguihs.speedPer = value;
case '1' % The slider
pumpguihs.speedPer = value*100;
case 'FR' % the flow rate text
value = str2double(value);
pumpguihs.speedPer = (value*100)/pumpguihs.calib;
otherwise
%nothing
end
pumpguihs.speedFR = (pumpguihs.speedPer/100)*pumpguihs.calib;
% Set the values in teh GUI
set(handles.editPercent,'String',num2str(pumpguihs.speedPer,'% .2f'));
set(handles.mainslider,'Value',pumpguihs.speedPer/100);
set(handles.editFrate,'String',num2str(pumpguihs.speedFR,'% .3f'));
% Send the value to the pump
fprintf(pumpguihs.serial,sprintf(['1S' num2str(100*pumpguihs.speedPer,'%06.f') '\n']));
while(pumpguihs.serial.BytesAvailable==0) pause(.05); end
ret = char(fread(pumpguihs.serial,pumpguihs.serial.BytesAvailable)'); % Just to get rid of it...
function mainslider_Callback(hObject, eventdata, handles)
Coordinate(get(hObject,'Value'),'1',handles);
function mainslider_CreateFcn(hObject, eventdata, handles)
% hObject handle to mainslider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function pushbutton1_Callback(hObject, eventdata, handles)
function editflushperC_Callback(hObject, eventdata, handles)
function editflushperC_CreateFcn(hObject, eventdata, handles)
% hObject handle to editflushperC (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function editflushInt_Callback(hObject, eventdata, handles)
function editflushInt_CreateFcn(hObject, eventdata, handles)
% hObject handle to editflushInt (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function editPercent_Callback(hObject, eventdata, handles)
Coordinate(get(hObject,'String'),'%',handles);
function editPercent_CreateFcn(hObject, eventdata, handles)
% hObject handle to editPercent (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function editFrate_Callback(hObject, eventdata, handles)
Coordinate(get(hObject,'String'),'FR',handles);
function editFrate_CreateFcn(hObject, eventdata, handles)
% hObject handle to editFrate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function editflushDur_Callback(hObject, eventdata, handles)
function editflushDur_CreateFcn(hObject, eventdata, handles)
% hObject handle to editflushDur (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function pushbuttonRUNSTOP_Callback(hObject, eventdata, handles)
global pumpguihs
if strcmp(pumpguihs.timerFlush.running,'off') % If not already running, start it
pumpguihs.timerFlush.UserData = struct('speed',str2double(get(handles.editflushperC,'String')));
pumpguihs.timerFlush.Period = str2double(get(handles.editflushInt,'String'))*60;
pumpguihs.timerRevert.StartDelay = str2double(get(handles.editflushDur,'String'));
pumpguihs.timerRevert.TimerFcn = {@revertCLBK,handles};
start(pumpguihs.timerFlush);
set(handles.pushbuttonRUNSTOP,'String','STOP');
else % Otherwise just stop the timer.
stop(pumpguihs.timerFlush);
set(handles.pushbuttonRUNSTOP,'String','RUN');
end
function pushbuttonSTARTSTOP_Callback(hObject, eventdata, handles)
global pumpguihs
if pumpguihs.running
pumpguihs.running = false;
togglebutton(handles);
fprintf(pumpguihs.serial,sprintf('1I\n'));
else
pumpguihs.running = true;
togglebutton(handles);
fprintf(pumpguihs.serial,sprintf('1H\n'));
end
function togglebutton(handles)
global pumpguihs
if pumpguihs.running
set(handles.pushbuttonSTARTSTOP,'BackgroundColor',[1 0 0]);
set(handles.pushbuttonSTARTSTOP,'String','STOP');
else
set(handles.pushbuttonSTARTSTOP,'BackgroundColor',[0 1 0]);
set(handles.pushbuttonSTARTSTOP,'String','START');
end
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
global pumpguihs
fclose(pumpguihs.serial);
delete(pumpguihs.serial);
stop(pumpguihs.timerSerial);
delete(pumpguihs.timerSerial);
if strcmp(pumpguihs.timerFlush.Running,'on')
button = questdlg('The flush timer is still running, do you want to stop it?','Flush timer running','Yes','No','Yes');
if strcmp(button,'Yes')
stop(pumpguihs.timerFlush);
delete(pumpguihs.timerFlush);
end
end
delete(hObject);