필터 지우기
필터 지우기

how to use "getframe" commander for making animation video.

조회 수: 2 (최근 30일)
상태 박
상태 박 2020년 11월 28일
답변: Walter Roberson 2020년 11월 29일
hello everybody
i want to make animation video.
but something matter. i think "F(i2) = getframe;" is problem.
but i don't know how to correct.
please help me.
thank you in advance.
d = fopen('displacement.dat','w');
v= fopen('velocity.dat','w');
a=fopen('acceleration.dat','w');
length1 = input('Enter length of link1:');
length2 = input('Enter length of link2:');
n1=input('Enter the number of image:');
t=0.0;
t_step =0.01;
t_end = 1.0;
local_r1 = [length1,0]';
local_r2 = [length2,0]';
while(t<=t_end)
theta_1=t;
theta_2=3*t;
r_d1=trans1(local_r1,theta_1);
r_d2=trans1(local_r2,theta_2);
r_td=addition(r_d1,r_d2);
r_v1=trans_velocity(theta_1,3,local_r1);
r_v2=trans_velocity(theta_2,1,local_r2);
r_tv=addition(r_v1,r_v2);
r_a1=trans_acceleration(3,0,r_d1);
r_a2=trans_acceleration(1,0,r_d2);
r_ta=addition(r_a1,r_a2);
fprintf(d,'%7.5f %15.6e %15.6e',t,r_td(1),r_td(2));
fprintf(d,'%15.6e %15.6e',r_d1(1),r_d1(2));
fprintf(d,'%15.6e %15.6e\n',r_d2(1),r_d2(2));
fprintf(v,'%15.6e %15.6e\n',r_tv(1),r_tv(2));
fprintf(a,'%15.6e %15.6e\n',r_ta(1),r_ta(2));
t=t+t_step;
end
fclose(d);
fclose(v);
fclose(a);
load displacement.dat
load velocity.dat
load acceleration.dat
time=displacement(:,1);
x_p=displacement(:,2); y_p=displacement(:,3);
rd1_xp=displacement(:,4); rd1_yp=displacement(:,5);
rd2_xp=displacement(:,6); rd2_yp=displacement(:,7);
x_p_dot=velocity(:,1); y_p_dot=velocity(:,2);
x_p_ddot=acceleration(:,1); y_p_ddot=acceleration(:,2);
subplot 331
plot(time,x_p,'r')
xlabel('Time(s)','Fontsize',14)
ylabel('x_p','Fontsize',14)
title('end-effector x','Fontsize',16)
legend('x_p&time','Location','SouthEast');
subplot 332
plot(time,y_p,'r')
xlabel('Time(s)','Fontsize',14)
ylabel('y_p','Fontsize',14)
title('end-effector y','Fontsize',16)
legend('y_p&time','Location','SouthEast');
subplot 333
plot(x_p,y_p,'r')
xlabel('x_p','Fontsize',14)
ylabel('y_p','Fontsize',14)
title('x-y relationship','Fontsize',16)
legend('x_p&y_p','Location','SouthEast');
subplot 334
plot(time,x_p_dot,'g')
xlabel('Time(s)','Fontsize',14)
ylabel('x_pdot','Fontsize',14)
title('velocity-x ','Fontsize',16)
legend('x_pdot&time','Location','SouthEast');
subplot 335
plot(time,y_p_dot,'g')
xlabel('Time(s)','Fontsize',14)
ylabel('y_p_dot','Fontsize',14)
title('velocity-y','Fontsize',16)
legend('y_pdot&time','Location','SouthEast');
subplot 336
plot(x_p_dot,y_p_dot,'g')
xlabel('x_p_dot','Fontsize',14)
ylabel('y_p_dot','Fontsize',14)
title('x-y velocity relationship','Fontsize',16)
legend('x_pdot&y_pdot','Location','SouthEast');
subplot 337
plot(time,x_p_ddot,'b')
xlabel('Time(s)','Fontsize',14)
ylabel('x_p_ddot','Fontsize',14)
title('acceleration-x','Fontsize',16)
legend('x_pddot&time','Location','SouthEast');
subplot 338
plot(time,y_p_ddot,'b')
xlabel('Time(s)','Fontsize',14)
ylabel('y_p_ddot','Fontsize',14)
title('acceleration-y','Fontsize',16)
legend('y_pddot&time','Location','SouthEast');
figure(2)
plot(x_p_ddot,y_p_ddot,'b')
xlabel('x_p_ddot)','Fontsize',14)
ylabel('y_p_ddot','Fontsize',14)
title('x-y acceleration relationship','Fontsize',16)
legend('x_pddot&y_pddot','Location','SouthEast')
k=VideoWriter('n2me2tion2.avi');
open(k);
for i2=1:length(time)
n2=fix(length(time)/n1);
figure(3)
hold on
axis equal
plot(x_p(i2),y_p(i2),'o-r','MarkerSize',3);
u1=line([0 rd1_xp(i2)],[0 rd1_yp(i2)],'Color','k','LineWidth',2);
u2=line([rd1_xp(i2) rd1_xp(i2)+rd2_xp(i2)],...
[rd1_yp(i2) rd1_yp(i2)+rd2_yp(i2)],'Color','b','LineWidth',2);
u3=line([x_p(i2) x_p(i2)+x_p_dot(i2)],...
[y_p(i2) y_p(i2)+y_p_dot(i2)],'Color','g','LineWidth',2);
if(i2==length(time))
break;
end
if(0==rem(i2,n2))
else
delete(u1)
delete(u2)
delete(u3)
end
F(i2) = getframe;
end
writeVideo(k,F);
close(k);
function r_prime= trans1(local_r1,theta_1)
A=[cos(theta_1),-sin(theta_1); sin(theta_1),cos(theta_1)];
r_prime=A*local_r1;
end
function r_dot=trans_velocity(theta,theta_dot,r_prime)
B=[-sin(theta),-cos(theta);cos(theta),-sin(theta)];
r_dot=theta_dot*B*r_prime;
end
function r_ddot=trans_acceleration(theta_dot,theta_ddot,r)
R=[0,-1;1,0];
r_ddot=theta_ddot*R*r-(theta_dot)^2*r;
end
function r=addition(r_prime1,r_prime2)
r=r_prime1 + r_prime2;
end
  댓글 수: 9
Image Analyst
Image Analyst 2020년 11월 29일
You keep forgetting to upload the three .dat files, so how can we run your code???
상태 박
상태 박 2020년 11월 29일
i don't know what you say
it doesn't matter.
data file is in my code
open my three .dat file and write information and close and open in my code

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 11월 29일
Each time the XLim or YLim changes, MATLAB recalculates the exact size of the inner window. Sometimes, possibly due to round-off, it can end up with very slightly different widths, such as varying by +/- 1 pixel.
The key is to figure out the limits ahead of time, and enforce them.
I am not sure that changing them every iteration is strictly necessary after I pre-calculated them, but there were steps along the way in my debugging in which setting the limits each time was key to getting the frames the same size.
You will notice in the attached code that I parent every graphic operation -- tell it exactly which figure or axes the graphing pertains to. The code was not careful about which axes it was writing to, with the result that running the code twice could end up with quite a mess.

추가 답변 (2개)

Image Analyst
Image Analyst 2020년 11월 28일
See attached demo.

Image Analyst
Image Analyst 2020년 11월 29일
It looks like your image might change size partway through. Try maximizing the figure in the loop after your last call to plot(), so it's always the same size for each frame (full screen).
g = gcf;
g.WindowState = 'maximized';

카테고리

Help CenterFile Exchange에서 Simulation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by