-
Notifications
You must be signed in to change notification settings - Fork 1
/
animasi_kubus.m
453 lines (400 loc) · 12.4 KB
/
animasi_kubus.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
function [sys,x0,str,ts] = saeroanim(t,x,u,flag,Config)
% animasi S-Function for displaying 6DoF attitude
% IRMUKa 8 Oktober 2006
% Copyright 1990-2003 The MathWorks, Inc.
% J.Hodgson
% Modified by: S.Gage
% $Revision: 1.2.2.4 $ $Date: 2004/04/06 01:04:21 $
% Last modified w/o permission by: Harapan Rachman
% 8 Oktober 2006
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts]=mdlInitializeSizes(Config);
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case {1 , 3, 9},
sys=[];
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2,
sys = [];
if Config.Animenable
mdlUpdate(t,x,u,Config);
end
%%%%%%%%%%%%%%%%%%%%%%%
% GetTimeOfNextVarHit %
%%%%%%%%%%%%%%%%%%%%%%%
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u,Config);
otherwise
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
error('aeroblks:saeroanim:invalidflag',['Unhandled flag = ',num2str(flag)]);
end
% end sanim
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes(Config)
%
% Set Sizes Matrix
%
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 0;
sizes.NumInputs = 6;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
%
% initialise the initial conditions
%
x0 = [];
%
% str is always an empty matrix
%
str = [];
%
% initialise the array of sample times
%
ts = [-2 0]; % variable sample time
if ~Config.Animenable
return
end
%
% Initialise Figure Window
%
h_f=findobj('type','figure','Tag','6DOF anim');
if isempty(h_f)
h_anim=figure;
else
h_anim=h_f;
end
set(h_anim,'name','Animation Figure', ...
'renderer','z','resize','off', ...
'position',[253 112 546 449],'clipping','off', ...
'Tag','6DOF anim');
if ~isempty(h_anim)
h_del = findobj(h_anim,'type','axes');
delete(h_del);
figure(h_anim);
end
%
% Initialize Axes
%
handle.axes(1)=axes; % Satelit
handle.axes(2)=axes; % tak orbit
handle.axes(3)=axes; % tak benda
set([handle.axes(1) handle.axes(2) handle.axes(3)],...
'visible','off',...
'dataaspectratio',[1 1 1],'projection','pers',...
'units','normal', ...
'position',[0.1 0.1 0.75 0.75], ...
'Color',[0 0 0], ...
'drawmode','fast', ...
'clipping','off');
b = 1.1*Config.craft;
axis([-b b -b b -b b]);
c = Config.craft;
view([c c c]);
% ====================== Menggambar tak orbit =====================
Mo_transform = hgtransform;
panah_satelit = hgtransform;
num=5; % Number of z-y segments to make the circle
% count=1; % Counter for number of individual patches
xtak = cell(19,1); %Preallocate for current number of patches
ytak = cell(19,1); %Preallocate for current number of patches
ztak = cell(19,1); %Preallocate for current number of patches
theta=(360/2/num:360/num:(360+360/2/num))*pi/180;
len = 2.5*Config.craft; % Total Length (no units)
radius = len/100; % Radius of body
s_fore = len/10; % Start of main body (w.r.t. nose)
thr_len = 0; % Length of Motor exit
l_fore =len-s_fore-thr_len; % Length of main body
c_g = len; % Position of c.g. w.r.t nose
% ========================== sumbu x orbit ============================
% Fore Body Shape
%
yc_range = radius*sin(theta);
zc_range = -radius*cos(theta);
for i = 1:num
xtak{i}=[s_fore s_fore s_fore+l_fore s_fore+l_fore ]-c_g;
ytak{i}=[yc_range(i) yc_range(i+1) yc_range(i+1) yc_range(i)];
ztak{i}=[zc_range(i) zc_range(i+1) zc_range(i+1) zc_range(i)];
end
count=num+1;
%
% Nose shape
%
for i = 1:num
xtak{count}=[s_fore s_fore 0 s_fore]-c_g;
ytak{count}=[yc_range(i) yc_range(i+1) 0 yc_range(i)];
ztak{count}=[zc_range(i) zc_range(i+1) 0 zc_range(i)];
count=count+1;
end
% Combine individual objects into a single set of co-ordinates
% and roll through 45 degrees.
%
x=[];y=[];z=[];
roll = [1 0 0;
0 cos(45/180*pi) sin(45/180*pi);
0 -sin(45/180*pi) cos(45/180*pi)];
for i = 1:count
x = [x xtak{i}'];
y = [y ytak{i}'];
z = [z ztak{i}'];
end
for i =1:4
dum = [x(i,:);y(i,:);z(i,:)];
dum = roll*dum;
x(i,:)=-dum(1,:);
y(i,:)=dum(2,:);
z(i,:)=dum(3,:);
end
panahx = patch(x,y,z,[1 0 0]);
panahx_satelit = patch(x,y,z,[1 0 0]);
% ========================== sumbu y orbit ============================
% Fore Body Shape
%
xc_range = radius*sin(theta);
zc_range = -radius*cos(theta);
for i = 1:num
xtak{i}=[xc_range(i) xc_range(i+1) xc_range(i+1) xc_range(i)];
ytak{i}=[s_fore s_fore s_fore+l_fore s_fore+l_fore ]-c_g;
ztak{i}=[zc_range(i) zc_range(i+1) zc_range(i+1) zc_range(i)];
end
count=num+1;
%
% Nose shape
%
for i = 1:num
xtak{count}=[xc_range(i) xc_range(i+1) 0 xc_range(i)];
ytak{count}=[s_fore s_fore 0 s_fore]-c_g;
ztak{count}=[zc_range(i) zc_range(i+1) 0 zc_range(i)];
count=count+1;
end
% =====================================================================
% Combine individual objects into a single set of co-ordinates
% and roll through 45 degrees.
%
x=[];y=[];z=[];
pitch = [cos(45/180*pi) 0 -sin(45/180*pi);
0 1 0;
sin(45/180*pi) 0 cos(45/180*pi)];
for i = 1:count
x = [x xtak{i}'];
y = [y ytak{i}'];
z = [z ztak{i}'];
end
for i =1:4
dum = [x(i,:);y(i,:);z(i,:)];
dum = pitch*dum;
x(i,:)=dum(1,:);
y(i,:)=-dum(2,:);
z(i,:)=dum(3,:);
end
panahy = patch(x,y,z,[0 1 0]);
panahy_satelit = patch(x,y,z,[0 1 0]);
% ========================== sumbu z orbit ============================
% Fore Body Shape
%
xc_range = radius*sin(theta);
yc_range = -radius*cos(theta);
for i = 1:num
xtak{i}=[xc_range(i) xc_range(i+1) xc_range(i+1) xc_range(i)];
ytak{i}=[yc_range(i) yc_range(i+1) yc_range(i+1) yc_range(i)];
ztak{i}=[s_fore s_fore s_fore+l_fore s_fore+l_fore ]-c_g;
end
count=num+1;
%
% Nose shape
%
for i = 1:num
xtak{count}=[xc_range(i) xc_range(i+1) 0 xc_range(i)];
ytak{count}=[yc_range(i) yc_range(i+1) 0 yc_range(i)];
ztak{count}=[s_fore s_fore 0 s_fore]-c_g;
count=count+1;
end
% Combine individual objects into a single set of co-ordinates
% and roll through 45 degrees.
%
x=[];y=[];z=[];
yaw = [cos(45/180*pi) sin(45/180*pi) 0;
-sin(45/180*pi) cos(45/180*pi) 0;
0 0 1];
for i = 1:count
x = [x xtak{i}'];
y = [y ytak{i}'];
z = [z ztak{i}'];
end
for i =1:4
dum = [x(i,:);y(i,:);z(i,:)];
dum = yaw*dum;
x(i,:)=dum(1,:);
y(i,:)=dum(2,:);
z(i,:)=-dum(3,:);
end
panahz = patch(x,y,z,[0 0 1]);
panahz_satelit = patch(x,y,z,[0 0 1]);
set([panahx panahy panahz],'Parent',Mo_transform);
set([panahx_satelit panahy_satelit panahz_satelit],...
'Parent',panah_satelit);
set([panahx_satelit panahy_satelit panahz_satelit
panahx panahy panahz],'erasemode','nor',...
'edgecolor',[0 0 0],'clipping','off');
set(handle.axes(2),'userdata',Mo_transform);
set(handle.axes(3),'userdata',panah_satelit);
% ============= selesai membuat tak orbit =======================
%
% Draw in satellite shape
%
[vertex_matrix, faces_matrix] = satellite_shape;
handle.craft = patch('Vertices',vertex_matrix,...
'Faces',faces_matrix,...
'FaceVertexCData',hsv(6),...
'FaceColor','flat');
vert = Config.craft*get(handle.craft,'vertices');
set(handle.axes(1),'userdata',vert)
set(handle.craft,'erasemode','nor',...
'edgecolor',[0 0 0],'clipping','off');
%
% Set Handles of graphics in Figure UserData
%
set(h_anim,'userdata',handle);
%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
function mdlUpdate(t,x,u,Config)
%
% Obtain Handles of Figure Objects
%
handle = get(findobj('type','figure','Tag','6DOF anim'),'userdata');
if isempty(findobj('type','figure','Tag','6DOF anim'))
%figure has been manually closed
return
end
%
% Form Transformation Matrix
%
cph = cos(u(4)); % Roll
sph = sin(u(4));
cth = cos(u(5)); % Pitch
sth = sin(u(5));
cps = cos(u(6)); % Yaw
sps = sin(u(6));
attitude = [cth*cps sph*sth*cps-cph*sps cph*sth*cps+sph*sps
cth*sps sph*sth*sps+cph*cps cph*sth*sps-sph*cps
-sth sph*cth cph*cth];
%
% Update Craft Object
%
vert = get(handle.axes(1),'userdata');
a=size(vert,1);
dum =attitude*vert'+repmat(u(1:3,1),1,a);
set(handle.craft,'vertices',dum');
rx_angle = u(4);
ry_angle = u(5);
rz_angle = u(6);
Rx = makehgtform('xrotate',rx_angle);
Ry = makehgtform('yrotate',ry_angle);
Rz = makehgtform('zrotate',rz_angle);
Matrik_rotasi = Rz*Ry*Rx; % Rotasi 3-2-1
set(get(handle.axes(3),'userdata'),'Matrix',Matrik_rotasi);
% ================ update tak orbit =========================
% Input sikap tak orbit
G = 6.67E-11;
Me = 5.98E24;
tinggi = 400E3;
Rbumi = 6371E3;
a = tinggi + Rbumi;
%wy = sqrt(G*Me/a^3);
wy = 0;
theta_y = wy*t;
sudut = [0 theta_y 0]';
rx_angle = sudut(1,1);
ry_angle = sudut(2,1);
rz_angle = sudut(3,1);
Rx = makehgtform('xrotate',rx_angle);
Ry = makehgtform('yrotate',ry_angle);
Rz = makehgtform('zrotate',rz_angle);
Sk = makehgtform('Scale',0.8);
Matrik_rotasi = Rz*Ry*Rx*Sk; % Rotasi 1-2-3,
set(get(handle.axes(2),'userdata'),'Matrix',Matrik_rotasi);
% =============================================================
%
% Set position of target view to Target
%
switch Config.camera_view
case 1, % Fixed Observer Position
set(handle.axes(1),'cameraupvector',[0 0 -1], ...
'cameraposition',Config.camera_pos, ...
'cameratarget',u(1:3), ...
'cameraviewangle',Config.view);
case 2, % Cockpit View
% Target= Config.target';
ax = Config.axes;
seeker_dir = sqrt(sum((ax(2)-ax(1))^2+(ax(4)-ax(3))^2+(ax(6)-ax(5))^2))*attitude*[1;0;0];
seeker_pos = attitude*[Config.craft 0 0]';
set(handle.axes(1),'cameraupvector',attitude*[0 0 -1]', ...
'cameraposition',u(1:3)+seeker_pos, ...
'cameratarget',u(1:3)+seeker_dir, ...
'cameraviewangle',Config.view);
case 3, % Relative Position View
set(handle.axes(1),'cameraupvector',[0 0 -1], ...
'cameraposition',u(1:3)+Config.camera_pos', ...
'cameratarget',u(1:3), ...
'cameraviewangle',Config.view);
end
%
% Force MATLAB to Update Drawing
%
drawnow
% end mdlUpdate
%
%=============================================================================
% mdlGetTimeOfNextVarHit
% Return the time of the next hit for this block.
%=============================================================================
%
function sys=mdlGetTimeOfNextVarHit(t,x,u,Config)
sys = ceil(t/Config.update)*Config.update+Config.update;
% end mdlGetTimeOfNextVarHit
%
%=============================================================================
% satellite shape
% Function to draw shape of satellite object
%=============================================================================
%
function [vm,fm]=satellite_shape
% Membangun kubus
% Vertices X Y Z
vm = [-1 -1 -1; % 1st vertex
1 -1 -1; % ...
1 1 -1;
-1 1 -1;
-1 -1 1;
1 -1 1;
1 1 1;
-1 1 1]; % 8th vertex
% Faces
fm = [1 2 6 5; % 1st face
2 3 7 6; % ...
3 4 8 7;
4 1 5 8;
1 2 3 4;
5 6 7 8]; % 6th face
% ==============================================