126 lines
3.6 KiB
Matlab
126 lines
3.6 KiB
Matlab
classdef DC_supply
|
|
% Simple control of DC supply via fixed GBIB address.
|
|
% Agilent E3646A
|
|
% No OVP or OCP implementation
|
|
% Not tested what happens if this script asks for Voltage in higher V
|
|
% Range (8V vs. 22V)
|
|
|
|
properties(Access=public)
|
|
active
|
|
voltage
|
|
end
|
|
|
|
methods (Access=public)
|
|
|
|
function obj = DC_supply(options)
|
|
|
|
|
|
arguments
|
|
options.active logical = true
|
|
options.voltage double = [0,0];
|
|
end
|
|
|
|
%
|
|
fn = fieldnames(options);
|
|
for n = 1:numel(fn)
|
|
try
|
|
obj.(fn{n}) = options.(fn{n});
|
|
end
|
|
end
|
|
|
|
assert(size(obj.voltage,1)==1,'Set Voltage for CH1 and CH2 must be given as vector [V1, V2]');
|
|
assert(size(obj.voltage,2)==2,'Set Voltage for CH1 and CH2 must be given as vector [V1, V2]');
|
|
|
|
end
|
|
|
|
function success = set(obj,options)
|
|
|
|
|
|
arguments
|
|
obj
|
|
options.voltage double = obj.voltage;
|
|
end
|
|
|
|
success = [0,0];
|
|
|
|
try
|
|
%connect to device
|
|
v = visadev("GPIB1::19::INSTR");
|
|
|
|
debug = 0;
|
|
if debug
|
|
disp(['Connected to Instrument: ',char(v.Vendor),' ',char(v.Model),' SerNo:',char(v.SerialNumber)]);
|
|
end
|
|
|
|
cmd = 'INST:SEL?';
|
|
writeline(v, cmd);
|
|
prev_selected_channel = readline(v);
|
|
|
|
for ch = 1:2
|
|
|
|
% choose channel
|
|
cmd = ['INST:SEL OUT',num2str(ch)];
|
|
writeline(v, cmd);
|
|
|
|
% get current voltage level
|
|
cmd = 'VOLT?';
|
|
writeline(v, cmd);
|
|
act_volt = str2num(readline(v));
|
|
|
|
% desired voltage (round to two digits after comma)
|
|
des_volt = round(options.voltage(ch),2);
|
|
|
|
cnt = 0;
|
|
while abs(act_volt-des_volt) > 0
|
|
|
|
selected_channel = ['OUT',num2str(ch)];
|
|
|
|
% get current voltage level
|
|
cmd = 'VOLT?';
|
|
writeline(v, cmd);
|
|
act_volt = str2num(readline(v));
|
|
|
|
% difference
|
|
diff_volt = act_volt-des_volt;
|
|
|
|
% set new voltage
|
|
increment_voltage = -0.01* sign(diff_volt) ;
|
|
cmd = ['VOLT ',num2str(act_volt+increment_voltage)];
|
|
writeline(v, cmd);
|
|
|
|
cnt = cnt+1;
|
|
if mod(cnt,100) == 0
|
|
wait(1);
|
|
end
|
|
|
|
% get current voltage level
|
|
cmd = 'VOLT?';
|
|
writeline(v, cmd);
|
|
act_volt = str2num(readline(v));
|
|
|
|
end
|
|
|
|
% check if voltage is set
|
|
if act_volt ~= des_volt
|
|
hMsgBox = msgbox('An error occurred in dc supply module. Check if voltage is set correctly.');
|
|
uiwait(hMsgBox);
|
|
else
|
|
success(ch) = 1;
|
|
end
|
|
|
|
end
|
|
|
|
% choose channel
|
|
cmd = ['INST:SEL ',char(strtrim(prev_selected_channel))];
|
|
writeline(v, cmd);
|
|
|
|
%disconnect
|
|
delete(v);
|
|
|
|
catch
|
|
|
|
end
|
|
end
|
|
end
|
|
end
|