-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifaceSpectrometer.m
159 lines (135 loc) · 4.38 KB
/
ifaceSpectrometer.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
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
%% ifaceSpectrometer.m MN 2023-09-08
% Interface with spectrometers
%
% Requirements:
% - VISA interface functions in path
% - Equipment is connected
%
% Usage: [[L, P], measurement] = ifaceSpectrometer(specID[, option, value])
% Returns:
% L: Measured wavelength
% P: Measured power
% measurement: Selected additional measurement, if requested
%
% Parameters:
% specID: Spectrometer ID - see mapSpec('list') for available spectrometers
%
% Options:
% 'reset': Reset/initialize to standard values
% 'span' | 'range', [%f, %f]: Set range/span [nm]
% 'mode', %i: Set mode; varies by spectrometer
% 'res' | 'resolution', %f: Set resolution [nm]
% 'avg', %i: Set averaging number
% 'points' | 'N', %i: Set number of points
% 'notrig': Don't trigger new measurement
% 'nomeas': Don't return measurement
%
% TODO:
% - Verify units
% - Test and debug
function [LP, measurement] = ifaceSpectrometer(specID, varargin)
%% Defaults and magic numbers
avg = NaN; span = [NaN, NaN]; reset = 0; mode = ""; trig = 1; N = NaN; res = NaN; meas = 1;
%% Helper functions
inrange = @(x, lims) (x >= min(lims)) & (x <= max(lims));
function arg = nextarg(strExpected)
if isempty(strExpected); strExpected = ''; end
if ~isempty(varargin)
arg = varargin{1}; varargin(1) = [];
else
error('Expected next argument "%s", but no more arguments present!', strExpected);
end
end
%% Argument parsing
% Accept a struct.option = value structure
if numel(varargin) > 0 && isstruct(varargin{1})
paramStruct = varargin{1}; varargin(1) = [];
varargin = [reshape([fieldnames(paramStruct) struct2cell(paramStruct)]', 1, []), varargin];
end
% Parameter parsing
while ~isempty(varargin)
arg = lower(varargin{1}); varargin(1) = [];
switch arg
case 'reset'
reset = 1;
case 'notrig'
trig = 0;
case {'nomeas', 'skip'}
meas = 0;
case {'range', 'span'}
span = double(nextarg('wavelength span'));
if numel(span) < 2
warning('"span" was passed with only one value, assuming current span %f', span(1));
span = [span(1) NaN];
end
case 'avg'
avg = round(nextarg('# traces for averaging'));
case 'mode'
mode = upper(string(nextarg('measurement mode')));
case {'res', 'resolution'}
res = nextarg('resolution');
case {'points', 'N'}
N = round(nextarg('# points'));
otherwise
if ~isempty(arg)
warning('Unexpected option "%s"', num2str(arg));
if ~strcmpi(input('Continue? [y/N]: ', 's'), 'y')
disp('Aborted.');
return;
end
end
end
end
%% Dependent parameters or defaults
span = sort(span);
%% Look up power meter and set appropriate interface
spec = mapSpectrometer(specID);
specInterface = spec.interface;
if isempty(specInterface)
error('Unsupported spectrometer type "%s" for spectrometer "%s"!', spec.type, dmmID);
end
%% Set options as requested
% Reset/initialize
if reset == 1
specInterface(spec.visaAddr, 'reset');
end
% Set span
if ~isnan(span(1))
if ~inrange(span(1), spec.span)
error('Specified span start %.5g out of valid range [%.5g %.5g]!', span(1), spec.span(1), spec.span(2));
end
if ~inrange(span(2), spec.span)
error('Specified span end %.5g out of valid range [%.5g %.5g]!', span(2), spec.span(1), spec.span(2));
end
specInterface(spec.visaAddr, 'span', span);
end
% Set resolution
if ~isnan(res)
res = spec.res(find(spec.res-res<=0, 1, 'last'));
specInterface(spec.visaAddr, 'res', res );
end
% Set points
if ~isnan(N)
specInterface(spec.visaAddr, 'N', max([round(N) 101]) );
end
% Set measurement mode
if strlength(string(mode)) > 0
if ~ismember(mode, spec.mode)
error('Specified mode "%s" is invalid; available modes "[%s]"!', mode, strjoin(spec.mode, ","));
end
specInterface(spec.visaAddr, 'mode', mode );
end
% Set trace averaging
if ~isnan(avg)
specInterface(spec.visaAddr, 'avg', max([round(avg) 1]) );
end
%% Take measurements
if meas < 1 && trig > 1
specInterface(spec.visaAddr, 'trig');
end
if meas > 0
LP = specInterface(spec.visaAddr, 'meas', trig);
else
LP = [NaN NaN];
end
end