-
Notifications
You must be signed in to change notification settings - Fork 0
/
instPiezo_Prototype.m
96 lines (84 loc) · 3.52 KB
/
instPiezo_Prototype.m
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
%% instPiezo_Prototype.m MN 2020-06-26
% Template for piezo interface
%
% Requirements:
% - VISA interface functions in path
% - Equipment is connected on specified VISA address
%
% Usage: meas = instPiezo_Prototype(visaAddr, option[, value])
% Returns:
% meas: Returns results of command
%
% Parameters:
% visaAddr: Valid VISA address with a diode controller connected
% 'option': One of available options below
% 'value': Value for options that require it
%
% Options:
% 'read' | 'measure', %i: Returns voltage of channel %i
% 'readall' | 'all', %i: Read all available channels
% 'set' | 'v', %i, %f: Sets channel %i to voltage %f
% 'setall', %f: Set all available channels to voltage %f
% If multiple channels, accepts %f as array
% 'limit' | 'maxv': Returns system maximum voltage
% 'serial': Returns device serial
%
% TODO:
% - Add more options
function meas = instPiezo_Prototype(visaAddr, option, varargin)
%% Helper functions
% TODO: replace with `serialport`
piezoWrite = @(x) visaWrite(visaAddr, x);
piezoRead = @(x) replace(visaRead(visaAddr, x), {'>',char([13 13])}, '');
%% Defaults and magic numbers
chs = 'xyz'; % For MDT693B
%% Set appropriate properties for this abnormal type of connection
visaObj = visaConn(visaAddr);
visaObj.BaudRate = 115200;
visaObj.FlowControl = 'none';
visaObj.Parity = 'none';
configureTerminator(visaObj, {'>', 'LF'});
if (~contains(visaObj.Status, 'open')) || ...
(visaObj.InputBufferSize < 4096)
fclose(visaObj);
visaObj.InputBufferSize = 4096;
fopen(visaObj);
end
%% Execute selected command
switch lower(option)
case 'serial'
% Get serial
meas = piezoRead('friendly?');
case {'limit', 'maxv'}
% Get and format voltage limit
meas = str2num(piezoRead('vlimit?'));
case {'read', 'measure'}
if isempty(varargin); error('Value not provided for channel!'); end
if round(varargin{1}) > length(chs); error('Specified channel %i exceeds available channels (%i)', varargin{1}, length(chs)); end
% Get and format channel voltage
meas = str2num( piezoRead(sprintf('%cvoltage?', chs(round(varargin{1})))) );
case {'readall', 'all'}
% MDT693B modern firmware actually has a 'xyzvoltage' command
meas = str2num( piezoRead(sprintf('%svoltage?', chs)) );
case {'set', 'v'}
if size(varargin) < 2; error('Insufficient parameters to set voltage: only %i provided', numel(varargin)); end
if round(varargin{1}) > length(chs); error('Specified channel %i exceeds available channels (%i)', varargin{1}, length(chs)); end
% Set channel voltage
piezoWrite( sprintf('%cvoltage=%.2f', chs(round(varargin{1})), double(varargin{2}) ) );
meas = str2num( piezoRead(sprintf('%cvoltage?', chs(round(varargin{1})))) );
case {'setall', 'vall'}
if (numel(varargin{1}) > 1) && (numel(varargin{1}) ~= length(chs))
warning('Voltage array %s does not match number of channels (%i); trimming to first entry (%f).', num2str(varargin{1}, '%f,'), length(chs), varargin{1}(1));
varargin{1} = double(varargin{1}(1));
end
% Build voltage to correct size
if numel(varargin{1}) ~= numel(chs)
varargin{1} = varargin{1}(1)*ones(size(chs));
end
% Set channel voltage
outQ = sprintf('%svoltage=%s', chs, num2str(varargin{1}, '%.2f,'));
piezoWrite( outQ(1:end-1) );
meas = str2num( piezoRead( sprintf('%svoltage?', chs) ) );
end
%% Clean up
end