-
Notifications
You must be signed in to change notification settings - Fork 0
/
fin_kine.m
362 lines (273 loc) · 10.5 KB
/
fin_kine.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
function [lift,torque,drag,drag_theta] = fin_kine(s,hd_ang,hd_vel,...
pitch,heave,p_prime,h_prime,x_vel,y_vel,cLift)
% Calculate the forces acting on the body and generated by the fin
%% Fin kinematics in body FOR
% % Current position of fin quarter-chord point (w.r.t. inertial FOR)
% s.finPosG(:,1) = x_fish - 0.7*s.bodyL*cos(hd_ang) ...
% - s.pedL*cos(hd_ang+heave) ...
% - 0.25*s.finL*cos(hd_ang+heave+pitch);
%
% s.finPosG(:,2) = y_fish - 0.7*s.bodyL*sin(hd_ang) ...
% - s.pedL*sin(hd_ang+heave) ...
% - 0.25*s.finL*sin(hd_ang+heave+pitch);
% Distance from COM to posterior margin of trunk
tr_len = 0.7*s.bodyL;
% Length of peduncle
pd_len = s.pedL;
% Distance of COP along chord length
tl_len = s.finL/4;
% Coordinates of trail edge of trunk
tr_pos(:,1) = -tr_len.*cos(hd_ang);
tr_pos(:,2) = -tr_len.*sin(hd_ang);
% Coordinates of peduncle
pd_pos(:,1) = [tr_pos(:,1) - pd_len.*cos(hd_ang+heave)];
pd_pos(:,2) = [tr_pos(:,2) - pd_len.*sin(hd_ang+heave)];
pd_pos(:,3) = 0.*pd_pos(:,2);
% Coordinates of qtr-chord point
fin_pos(:,1) = pd_pos(:,1) - tl_len.*cos(hd_ang+heave+pitch);
fin_pos(:,2) = pd_pos(:,2) - tl_len.*sin(hd_ang+heave+pitch);
fin_pos(:,3) = 0.* fin_pos(:,2);
% Speed of trunk
tr_spd(:,1) = x_vel + tr_len*sin(hd_ang).*hd_vel;
tr_spd(:,2) = y_vel - tr_len*cos(hd_ang).*hd_vel;
% Speed of peduncle
pd_spd(:,1) = tr_spd(:,1) + pd_len*sin(hd_ang+heave).*(hd_vel+h_prime);
pd_spd(:,2) = tr_spd(:,2) - pd_len*cos(hd_ang+heave).*(hd_vel+h_prime);
% Speed of tail fin
tl_spd(:,1) = pd_spd(:,1) + ...
tl_len*sin(hd_ang+heave+pitch).*(hd_vel+h_prime+p_prime);
tl_spd(:,2) = pd_spd(:,2) ...
- tl_len*cos(hd_ang+heave+pitch).*(hd_vel+h_prime+p_prime);
tl_spd(:,3) = 0.*tl_spd(:,2);
% Unit vector pointed in direction of fin leading edge
unit_f = [cos(hd_ang+heave+pitch), sin(hd_ang+heave+pitch), 0.*heave];
% Unit vector normal to fin
unit_f_perp = [-unit_f(:,2) unit_f(:,1) 0.*heave];
% Velocity normal to the fin
norm_vel = unit_f_perp .* repmat(dot(tl_spd,unit_f_perp,2),1,3);
% Lift
lift = - 0.5 * s.rho * s.finA .* norm_vel .*abs(norm_vel);
% Torque due to lift force (cross product of fin position and lift vector)
fin_pos_cross_lift = cross(fin_pos,lift,2);
torque = (fin_pos_cross_lift(:,3));
% Ditch third dimension
lift = lift(:,1:2);
if 0%max(abs(heave)>pi/8)
%figure
plot([0 tr_pos(1) pd_pos(1) fin_pos(1)],...
[0 tr_pos(2) pd_pos(2) fin_pos(2)],'o-',...
'LineWidth',3);
hold on
n = 2*tl_len .* norm_vel./hypot(norm_vel(1),norm_vel(2));
plot([fin_pos(1) fin_pos(1)+n(1)],[fin_pos(2) fin_pos(2)+n(2)],'-r')
axis equal
grid on
hold off
end
%% Body forces
% Components of drag on body
drag(:,1) = - 0.5 * s.cDrag * s.rho * s.SA .* abs(x_vel) .* x_vel;
drag(:,2) = - 0.5 * s.cDrag * s.rho * s.SA .* abs(y_vel) .* y_vel;
% Viscous drag that resists rotation
drag_theta = - s.cDrag_rot * (s.bodyL/2)^3 * s.visc * hd_vel;
return
% % Velocity of peduncle (body FOR)
% pd_vel = [pd_len*h_prime.*sin(heave) pd_len*h_prime.*cos(heave)];
%
% % Coordinates of tail (body FOR)
% tl_pos(:,1) = pd_pos(:,1) - tl_len.*cos(heave+pitch);
% tl_pos(:,2) = pd_pos(:,2) + tl_len.*sin(heave+pitch);
%
% % Velocity of tail (body FOR)
% tl_vel(:,1) = pd_vel(:,1) + tl_len.*p_prime.*sin(heave+pitch);
% tl_vel(:,2) = pd_vel(:,2) + tl_len.*p_prime.*cos(heave+pitch);
%%
% Current position of fin quarter-chord point (w.r.t. body COM)
finPosB(:,1) = - 0.7*s.bodyL*cos(hd_ang) ...
- s.pedL*cos(hd_ang+heave) ...
- 0.25*s.finL*cos(hd_ang+heave+pitch);
finPosB(:,2) = - 0.7*s.bodyL*sin(hd_ang) ...
- s.pedL*sin(hd_ang+heave) ...
- 0.25*s.finL*sin(hd_ang+heave+pitch);
% Speed of fin in x-direction (interial frame)
u_parl = x_vel + 0.7*s.bodyL*sin(hd_ang).*hd_vel + ...
s.pedL*sin(hd_ang+heave).*(hd_vel+h_prime) + ...
0.25*s.finL*sin(hd_ang+heave+pitch).*(hd_vel+h_prime+p_prime);
% Speed of fin in y-direction (interial frame)
u_perp = y_vel - 0.7*s.bodyL*cos(hd_ang).*hd_vel - ...
s.pedL*cos(hd_ang+heave).*(hd_vel+h_prime) - ...
0.25*s.finL*cos(hd_ang+heave+pitch).*(hd_vel+h_prime+p_prime);
% Unit vector pointed in direction of fin leading edge (w.r.t. fixed axes)
unit_f = [cos(hd_ang+heave+pitch), sin(hd_ang+heave+pitch),...
zeros(length(heave),1)];
% Construct velocity vector for cross product calculation
vel_vec = [u_parl, u_perp, zeros(length(u_parl),1)];
% Cross products of velocity with unit vectors (may be size Nx3)
x_prod = cross(vel_vec,unit_f,2);
% Lift
tmp = 0.5 *s.rho*s.finA .* (cross(x_prod,vel_vec,2));
fin_L(:,1) = cLift .* tmp(:,1);
fin_L(:,2) = cLift .* tmp(:,2);
fin_L(:,3) = cLift .* tmp(:,3);
%fin_L = 0.5*(2*pi) *s.rho*s.finA .* (cross(x_prod,vel_vec,2));
% Drag
% fin_D = (0.5*s.rho*s.cD_perp).*abs((u_parl)).*(u_parl);
% Forward thrust (aligned with long axis of fish)
thrust_fwd = fin_L(:,1) .* cos(hd_ang) - fin_L(:,2) .* sin(hd_ang);
% Lateral thrust (perpendicular to long axis of fish), not correct
% thrust_lat = -fin_L(1) * sin(y(5)) + fin_L(2) * cos(y(5));
% x-component of lift along body axis
lift(:,1) = thrust_fwd.*cos(hd_ang);
% y-component of lift along body axis
lift(:,2) = thrust_fwd.*sin(hd_ang);
% Fin speed, x-y components
%finVel = vel_vec(:,1:2);
% Torque due to lift force (cross product of fin position and lift vector)
fin_pos = [finPosB zeros(length(thrust_fwd),1)];
lift_vec = [fin_L(:,1:2) zeros(length(thrust_fwd),1)];
fin_pos_cross_lift = cross(fin_pos,lift_vec,2);
torque = (fin_pos_cross_lift(:,3));
%% Code for testing and visualizing
if 0
% Plots to verify velocity (of fin) and lift are orthogonal
figure
% Plot the orientation of the fin starting at the quarter chord point
%quiver(fp(:,1),fp(:,2),unit_f(:,1),unit_f(:,2),0.5)
quiver(fp_body(:,1),fp_body(:,2),unit_f(:,1),unit_f(:,2),0.5)
hold on
% Plot the lift force vectors
quiver(fp_body(:,1),fp_body(:,2),fin_L(:,1),fin_L(:,2))
% Plot the quarter chord point velocity vector
quiver(fp_body(:,1),fp_body(:,2),u_parl(:),u_perp(:),0.5)
axis equal
% % Norm of unit vectors (should be 1)
% unit_norm = sqrt(sum(unit_f.^2,2));
%
% % Norm of velocity vector
% vel_norm = sqrt(sum(vel_vec.^2,2));
%
% % Dot product between orientation of fin and velocity
% orient_dot_vel = dot(unit_f,vel_vec,2);
%
% % Cross product between orientation of fin and velocity
% orient_x_vel = cross(unit_f,vel_vec,2);
%
% % Norm of cross products
% cross_norm = sqrt(sum(orient_x_vel.^2,2));
%
% % Angle between fin orientation and velocity vector
% atck_ang = atan2(cross_norm,orient_dot_vel);
%
% figure, plot(t,atck_ang*180/pi)
ttt=3;
end
% function [u_parl,u_perp] = getSpeed(u_fish,pitch,heave,h_prime,fp,t)
%
% % Rigid Body transformation matrix for current time
% rot_mat = [cos(pitch) sin(pitch) -u_fish*t;...
% -sin(pitch) cos(pitch) heave;...
% 0 0 1];
%
% warning off
% % Inverse of transformation
% % rot_inv = inv(rot_mat);
%
% warning on
%
% % Time derivative of transformation
% rot_prime = [-sin(pitch) cos(pitch) -u_fish;...
% -cos(pitch) -sin(pitch) h_prime;...
% 0 0 0];
%
% % Angular velocity matrix
% omega_mat = rot_prime / rot_mat;
%
% % Velocity of center of mass of fin
% u_parl = omega_mat(1,3) + fp(2);
% u_perp = omega_mat(2,3) + fp(1);
%% For test simulations
if 0
% Define airfoil geometry
x_foil = linspace(0,s.finL,500) - s.fp(1,1);
y_foil = 0.6*(0.2969*sqrt(x_foil) - 0.1260*x_foil ...
- 0.3516*x_foil.^2 + 0.2843*x_foil.^3 - 0.1036*x_foil.^4);
x_foil = -x_foil;
% Close al figures
close all
% Create figure
h = figure;
% Set figure size
h.Position = [100, 100, 1080, 900];
% Set figure size for saving
h.PaperUnits = 'inches';
h.PaperPosition = [0 0 6 6];
% Plot foil (before motion)
h1 = subplot(2,2,[1,2]);
plot(x_foil,y_foil,'k-','LineWidth',2)
hold on
plot(x_foil,-y_foil,'k-','LineWidth',2)
h1.XLim = ([-1.05 1.05]);
h1.YLim = ([-0.75 0.75]);
hold off
xlabel('x-position (cm)','FontSize',16)
ylabel('y-position (cm)','FontSize',16)
% Plot heave curve
subplot(2,2,3);
plot(t,heave,'LineWidth',2)
hold on
ax = gca;
h_line = line([t(1),t(1)],[ax.YLim(1),ax.YLim(2)],'Color','k');
hold off
xlabel('time (s)','FontSize',16)
ylabel('Heave Amplitude (cm)','FontSize',16)
% Plot pitch curve
subplot(2,2,4);
plot(t,pitch*180/pi,'LineWidth',2)
hold on
ax = gca;
p_line = line([t(1),t(1)],[ax.YLim(1),ax.YLim(2)],'Color','k');
hold off
xlabel('time (s)','FontSize',16)
ylabel('Pitch Angle (deg)','FontSize',16)
drawnow
% Set path for saving movie stills
% [~, pathMovie] = uiputfile;
% Save first frame
% print(h,[pathMovie filesep 'airfoilMovie' num2str(0)],'-djpeg')
for j=1:length(t)
% current heave value
heave_curr = heave(j);
% current pitch angle
pitch_curr = pitch(j);
% rotation + translation matrix (homogeneous coordinates)
rot_mat = [cos(pitch_curr) sin(pitch_curr) u_fish(j)*t(j);...
-sin(pitch_curr) cos(pitch_curr) heave_curr;...
0 0 1];
% Transformed x & y values
top_foil = rot_mat * [x_foil ; y_foil; ones(size(x_foil))];
bot_foil = rot_mat * [x_foil ; -y_foil; ones(size(x_foil))];
x_new = top_foil(1,:);
y_top = top_foil(2,:);
y_bot = bot_foil(2,:);
fin_l = sqrt((x_new(1) - x_new(end))^2 + (y_top(1) - y_top(end))^2);
% Update foil position
plot(h1,x_new,y_top,'k','LineWidth',2)
hold(h1,'on')
plot(h1,x_new,y_bot,'k','LineWidth',2)
h1.XLim = ([-1.05 1.05]);
h1.YLim = ([-0.75 0.75]);
hold(h1,'off')
xlabel(h1,'x-position (cm)','FontSize',16)
ylabel(h1,'y-position (cm)','FontSize',16)
% Update vertical line position for heave
h_line.XData = [t(j),t(j)];
% Update vertical line position for pitch
p_line.XData = [t(j),t(j)];
drawnow
% pause
if 0%makeMovie
% Save figure window as jpeg
print(h,[pathMovie filesep 'airfoilMovie' num2str(j)],'-djpeg')
end
end
end