-
Notifications
You must be signed in to change notification settings - Fork 0
/
MFAFM.m
251 lines (213 loc) · 8.97 KB
/
MFAFM.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
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
%% ------------------------------ INITIALIZATION --------------------------------------
clear; % close all
global m b o k1 k2 w1 w2 wd D F1 F2 Q1 Q2 Ar
last = @(V) V(end); % function to retrieve last entry in an vector
% cantilever
m = 1.3098e-11; % mass [kg]
c = 2.3455e-7; % damping coefficient [kg/s]
r = 0.9; % restitution coefficient
Nm = input('Operation Mode (1 = monomodal, 2 = bimodal): ');
switch Nm
case 1
%% -------------------------------- USER INPUT 1 ----------------------------------------
fprintf('Sample file to load '); Nf = input('(0 = Flat, 1 = Ramp, 2 = Wave, 3 = DNA, 4 = TiS2, 5 = UO): ');
switch Nf
case 0
load('flatSurface.mat')
Af = 50e-9;
case 1
load('rampSurface.mat')
Af = 50e-9;
case 2
load('sqWave.mat')
Af = 50e-9;
case 3
load('DNA.mat')
Af = 2e-9;
case 4
load('titaniumDisulfide.mat')
Af = 2e-9;
case 5
load('uraniumOxide.mat')
Af = 200e-9;
otherwise
error('File DNE')
end
Nc = size(trueSample,2); % # of columns of selected sample
fprintf('Line no. to be sampled (1 - %u)',Nc); Nl = round(input(': '));
if Nl < 1 || Nl > Nc
error('Line DNE')
end
tT = input('Duration of input signal (sec): ');
wd = input('Driving frequency of dither piezo (rad/s): ');
%% -------------------------------- PARAMETERS 1 ---------------------------------------
% cantilever
k1 = 42; % stiffness coefficient [N/m]
w1 = sqrt(k1/m); % natural frequency [rad/s]
Q1 = m*w1/c; % quality factor []
%% -------------------------------- DITHER PIEZO 1 --------------------------------------
Ar = 0.9*Af; % amplitude reference value
D = Af*abs(w1^2 - wd^2 + (w1/Q1)*(1i*wd)); % driving amplitude of dither piezo input signal
% tA = (2*Q1)/w1; % dither piezo time constant
%% -------------------------------- Z-AXIS PIEZO 1 --------------------------------------
b = [Ar]; % initial base height
figure; mesh(trueSample)
s = trueSample(:,Nl); % line of sample to be scanned chosen by user
figure; h = linspace(0,length(s),length(s)); plot(h,s); xlim([0 length(s)]);
xlabel('distance [m]'); ylabel('height [m]');
title('Sample Topography');
KP = 0.5; KI = 8.5; KD = 0; % PID controller gains
%% ------------------------------ INITIAL CONDITIONS 1 ---------------------------------------
A = b - s(1);
t0 = 0;
y0 = [Ar;0;Ar - A];
refine = 4;
options = odeset('Events',@impact,'OutputFcn',@odeplot,'OutputSel',1,'Refine',refine);
diagram = figure;
axis([0,tT,-inf,inf]);
box on; hold on;
tt = t0; yt = y0.'; tet = []; yet = []; iet = []; % initialize ODE parameters
eAt = []; bt = [b]; st = [s(1)];
%% ---------------------------------- SIMULATION 1 ---------------------------------------
for i = 1:length(s)
o = s(i);
% Solve until the first terminal event.
[t,y,te,ye,ie] = ode15s(@(t,y) cantilever1(t,y,A),[t0 tT],y0,options);
if ~ishold
hold on
end
% Accumulate output. This could be passed out as output arguments.
c = length(t);
tt = [tt; t(2:c)];
yt = [yt; y(2:c,:)];
tet = [tet; te];
yet = [yet; ye];
iet = [iet; ie];
eAt = [eAt; Ar - A];
% PID controller for base height adjustment
b = KP*(Ar - A) + KI*y(c,3) + KD*last(gradient(eAt));
bt = [bt;b];
A = b - o;
if diagram.UserData.stop
break;
end
y0(1) = y(c,1); y0(2) = -r*y(c,2); % Reset Law
% A good guess of a valid first timestep is the length of the last valid
% timestep, so use it for faster computation. 'refine' is 4 by default.
options = odeset(options,'Events',@impact,'InitialStep',t(c)-t(c-refine),'MaxStep',t(c)-t(1));
t0 = t(c);
if t0 == tT
break
end
end
plot(tet,yet(:,1),'ro')
xlabel('time');
ylabel('height');
title('Cantilever Tip Trajectory');
hold off
odeplot([],[],'done');
figure; plot(linspace(0,tT,length(eAt)),eAt); title('Error Signal vs. Time'); xlabel('Time (s)'); ylabel('Amplitude Error (m)');xlim([0 tT]);
case 2
%% -------------------------------- USER INPUT 2 ----------------------------------------
fprintf('Sample file to load '); Nf = input('(0 = Flat, 1 = Ramp, 2 = Wave, 3 = DNA, 4 = TiS2, 5 = UO): ');
switch Nf
case 0
load('flatSurface.mat')
A01 = 10e-9;
case 1
load('rampSurface.mat')
A01 = 6e-9;
case 2
load('sqWave.mat')
A01 = 50e-9;
case 3
load('DNA.mat')
A01 = 2e-9;
case 4
load('titaniumDisulfide.mat')
A01 = 2e-9;
case 5
load('uraniumOxide.mat')
A01 = 200e-9;
otherwise
error('File DNE')
end
Nc = size(trueSample,2); % # of columns of selected sample
fprintf('Line no. to be sampled (1 - %u)',Nc); Nl = round(input(': '));
if Nl < 1 || Nl > Nc
error('Line DNE')
end
tT = input('Duration of input signal (sec): ');
p = input('Free amplitude of 2nd mode (% of 1st mode free amplitude): ');
%% -------------------------------- PARAMETERS 2 ---------------------------------------
% cantilever
A02 = (p/100)*A01; % free amplitude for 2nd mode [m]
f1 = 48.913; f2 = 306.194; % resonant frequencies [kHz]
w1 = 2*pi*f1; w2 = 2*pi*f2; % angular frequencies [rad/s]
k1 = 0.9; k2 = 35.2; % stiffness coefficients [N/m]
Q1 = 255; Q2 = 1000; % quality factor []
%% -------------------------------- DITHER PIEZO 2 --------------------------------------
Ar = 0.9*A01; % amplitude reference value
F1 = k1*A01/Q1; F2 = k2*A02/Q2; % external excitation forces
%% -------------------------------- Z-AXIS PIEZO 2 --------------------------------------
b = [Ar]; % initial base height
figure; mesh(trueSample)
s = trueSample(:,Nl); % line of sample to be scanned chosen by user
figure; h = linspace(0,s(end),length(s)); plot(h,s)
xlabel('distance [m]'); ylabel('height [m]');
title('Sample Topography');
KP = 0.5; KI = 8.5; KD = 0; % PID controller gains
%% ------------------------------ INITIAL CONDITIONS 2 ---------------------------------------
A = b - s(1);
t0 = 0;
y0 = [Ar;0;(0/100)*Ar;0;Ar - A];
refine = 4;
options = odeset('Events',@impact,'OutputFcn',@odeplot,'OutputSel',1,'Refine',refine);
diagram = figure;
axis([0,tT,-inf,inf]);
box on; hold on;
tt = t0; yt = y0.'; tet = []; yet = []; iet = []; % initialize ODE parameters
eAt = []; bt = [b]; st = [s(1)];
%% ---------------------------------- SIMULATION 2 ---------------------------------------
for i = 1:length(s)
o = s(i);
% Solve until the first terminal event.
[t,y,te,ye,ie] = ode15s(@(t,y) cantilever2(t,y,A),[t0 tT],y0,options);
if ~ishold
hold on
end
% Accumulate output. This could be passed out as output arguments.
c = length(t);
tt = [tt; t(2:c)];
yt = [yt; y(2:c,:)];
tet = [tet; te];
yet = [yet; ye];
iet = [iet; ie];
eAt = [eAt; Ar - A];
% PID controller for base height adjustment
b = KP*(Ar - A) + KI*y(c,3) + KD*last(gradient(eAt));
bt = [bt;b];
A = b - o;
if diagram.UserData.stop
break;
end
y0(1) = y(c,1); y0(2) = -r*y(c,2); % Reset Law
% A good guess of a valid first timestep is the length of the last valid
% timestep, so use it for faster computation. 'refine' is 4 by default.
options = odeset(options,'Events',@impact,'InitialStep',t(c)-t(c-refine),'MaxStep',t(c)-t(1));
t0 = t(c);
if t0 == tT
break
end
end
plot(tet,yet(:,1),'ro')
xlabel('time');
ylabel('height');
title('Cantilever Tip Trajectory');
hold off
odeplot([],[],'done');
figure; plot(tt,yt(:,3),'-o'); xlim([0 tT]);
figure; plot(linspace(0,tT,length(eAt)),eAt); title('Error Signal vs. Time'); xlabel('Time (s)'); ylabel('Amplitude Error (m)');xlim([0 tT]);
otherwise
error('Mode DNE')
end