-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlentickleEngine.m
206 lines (166 loc) · 6.13 KB
/
lentickleEngine.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
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
% result = lentickleEngine(cucumber, pos, f, sigAC, mMech)
% generate pickle result matrices, TFs, etc.
% sigAC and mMech will be generated if not given as arguments
%
% Syntax: result lentickleEngine(cucumber, pos, f, sigAC, mMech)
%
% cucmber - lentickle control system model (cucumber)
% pos - array of drive offsets (passed directly to tickle)
% f - frequency array
% sigAC - 3D drive->probe matrix, output of tickle (optional)
% mMech - tickle mechanical response modification matrix (optional)
%
% result - result structure containing closed loop matricies
function rslt = lentickleEngine(lentickle, pos, f, sigAC, mMech)
if isfield(lentickle, 'param')
pp = lentickle.param;
else
pp = lentickle;
end
opt = lentickle.opt;
% sizes of things
Nfreq = numel(f);
Nsens = size(pp.probeSens,1);
[Nmirr,Ndof] = size(pp.dofMirr);
Ndrive = opt.Ndrive;
% which drives are used
vMirr = zeros(1,Nmirr);
for jMirr = 1:Nmirr
vMirr(jMirr) = find(pp.mirrDrive(:,jMirr),1);
end
% get loop TFs
hCtrl = pickleMakeFilt(f, pp.ctrlFilt);
hMirr = pickleMakeFilt(f, pp.mirrFilt);
hPend = pickleMakeFilt(f, pp.pendFilt);
% make DOF subtraction matrix
dofMix = repmat(eye(Ndof), [1 1 Nfreq]);
if isfield(pp, 'subPath')
for n = 1:numel(pp.subPath)
% thus subtraction filter has nameFrome, nameTo, filt field
sp = pp.subPath(n);
% find DOF names
nFrom = find(strcmp(sp.nameFrom, pp.dofNames), 1);
nTo = find(strcmp(sp.nameTo, pp.dofNames), 1);
% check for valid names
if isempty(nFrom)
error('Subtraction path %d: nameFrom "%s" is not a DOF', ...
n, sp.nameFrom)
end
if isempty(nTo)
error('Subtraction path %d: nameTo "%s" is not a DOF', ...
n, sp.nameTo)
end
% fill in the TF
dofMix(nTo, nFrom, :) = pickleMakeFilt(f, sp.filt);
end
end
% call tickle to compute fields and TFs
mirrReduce = eye(Ndrive);
if( nargin < 5 )
[fDC,sigDC,sigAC,mMech] = tickle(opt, pos, f, vMirr);
mirrReduce = mirrReduce(vMirr,:);
end
%%%%%%%%%%%%%%%%% initialize result matrices
rslt.f = f;
rslt.Nfreq = Nfreq;
rslt.Nsens = Nsens;
rslt.Ndof = Ndof;
rslt.Nmirr = Nmirr;
rslt.sensCL = zeros(Nsens, Nsens, Nfreq);
rslt.errCL = zeros(Ndof, Ndof, Nfreq);
rslt.ctrlCL = zeros(Ndof, Ndof, Nfreq);
rslt.corrCL = zeros(Nmirr, Nmirr, Nfreq);
rslt.mirrCL = zeros(Nmirr, Nmirr, Nfreq);
rslt.errOL = zeros(Ndof, Ndof, Nfreq);
rslt.sensErr = zeros(Ndof, Nsens, Nfreq);
rslt.errCtrl = zeros(Ndof, Ndof, Nfreq);
rslt.ctrlCorr = zeros(Nmirr, Ndof, Nfreq);
rslt.corrMirr = zeros(Nmirr, Nmirr, Nfreq);
rslt.mirrSens = zeros(Nsens, Nmirr, Nfreq);
rslt.corrSens = zeros(Nsens, Nmirr, Nfreq);
rslt.mMirr = zeros(Nmirr, Nmirr, Nfreq);
% compute result matrices for each frequency
probeSens = pp.probeSens;
sensDof = pp.sensDof;
dofMirr = pp.dofMirr;
mirrDrive = pp.mirrDrive;
% if desired, set UGF directly to desired value for each DOF
gainCorr = ones(Ndof, 1);
if isfield(pp,'setUgfDof')
setUgfDof = pp.setUgfDof;
ugfDof = setUgfDof;
for m = 1:Ndof
if isnan(setUgfDof(m)) %skip NaNs because they mean don't change it
continue
end
% find the nearest f index and use that
[fDelta,fIndex] = min(abs(f - setUgfDof(m)));
ugfDof(m) = f(fIndex);
% calculate the current gain at that frequency
dofGain = sensDof * probeSens * sigAC(:, :, fIndex) * mirrReduce * mirrDrive *...
diag(hPend(fIndex, :)) * diag(hMirr(fIndex, :)) * dofMirr *...
diag(hCtrl(fIndex, :));
dofGain = dofGain(m,m);
dofSign = sign(imag(dofGain));
gainCorr(m) = abs(dofGain) * dofSign;
% divide out the gain to make it 1 at the desired frequency
sensDof(m,:) = pp.sensDof(m,:) / gainCorr(m);
end
% store the true UGFs
rslt.ugfDof = ugfDof;
end
rslt.gainCorr = gainCorr;
eyeSens = eye(Nsens);
eyeDof = eye(Ndof);
eyeMirr = eye(Nmirr);
% prevent scale warnings
sWarn = warning('off', 'MATLAB:nearlySingularMatrix');
for n = 1:Nfreq
% use maps to produce mirrSens
mirrSens = probeSens * sigAC(:, :, n) * mirrReduce * mirrDrive;
% make piecewise TFs
errCtrl = diag(hCtrl(n, :));
ctrlCorr = diag(hMirr(n, :)) * dofMirr * dofMix(:, :, n);
corrMirr = diag(hPend(n, :));
% make half-loop pairs
corrSens = mirrSens * corrMirr;
sensCorr = ctrlCorr * errCtrl * sensDof;
% make open-loop TFs
sensOL = corrSens * sensCorr;
errOL = sensDof * corrSens * ctrlCorr * errCtrl;
ctrlOL = errCtrl * sensDof * corrSens * ctrlCorr;
corrOL = sensCorr * corrSens;
mirrOL = corrMirr * sensCorr * mirrSens;
% store results
rslt.sensErr(:, :, n) = sensDof;
rslt.errCtrl(:, :, n) = errCtrl;
rslt.ctrlCorr(:, :, n) = ctrlCorr;
rslt.corrMirr(:, :, n) = corrMirr;
rslt.mirrSens(:, :, n) = mirrSens;
rslt.corrSens(:, :, n) = corrSens;
rslt.errOL(:, :, n) = errOL;
rslt.sensCL(:, :, n) = inv(eyeSens - sensOL);
rslt.errCL(:, :, n) = inv(eyeDof - errOL);
rslt.ctrlCL(:, :, n) = inv(eyeDof - ctrlOL);
rslt.corrCL(:, :, n) = inv(eyeMirr - corrOL);
rslt.mirrCL(:, :, n) = inv(eyeMirr - mirrOL);
rslt.mMirr(:, :, n) = mirrDrive.' * mirrReduce.' * mMech(:, :, n)...
* mirrReduce * mirrDrive;
end
% reset scale warning state
warning(sWarn.state, sWarn.identifier);
% test point names
rslt.testPoints = {'sens', 'err', 'ctrl', 'corr', 'mirr'};
rslt.Ntp = numel(rslt.testPoints);
% copy names
rslt.mirrNames = pp.mirrNames;
rslt.sensNames = pp.sensNames;
rslt.dofNames = pp.dofNames;
% make capitalized version of names
rslt.testPointsUpper = rslt.testPoints;
for n = 1:rslt.Ntp
nameTmp = rslt.testPoints{n};
nameTmp(1) = upper(nameTmp(1));
rslt.testPointsUpper{n} = nameTmp;
end
end