MATLAB will animate the demonstration and save the process as a gif dynamic picture

Usually when I want to make an animated picture for demonstration, I usually have two methods:
one is to take many pictures and combine them into a gif animation;
the other is to record the screen and then make it into a gif. I usually After recording the video, use QQ Video. The video toolbox inside has a function to create animations.
Both of these methods are relatively cumbersome, especially the first one that requires you to slowly cut out the pictures you need, so here is a method of using code in MATLAB to save gif pictures of the entire animation demonstration process, which is much more convenient.

1. Robot teaching

Still using the p560 six-axis robotic arm robot for demonstration, the first step is to load the robot:

startup_rvc
mdl_puma560

If you have not downloaded MATLAB, you can check out: Download, installation and use of MatLab (valid for personal testing) If
you need to study robots, you need to download the robot toolkit: MATLAB's rvctools toolbox to familiarize yourself with kinematics [Robotic arm robot example]

We use the correct solution of kinematics learned previously to determine the starting pose and target pose, plan a motion trajectory, and then draw it:

t=[0:0.05:2];%两秒完成轨迹,步长0.05
T1 = p560.fkine(qz);%起始位姿,qz零角度
%T2 = p560.fkine(qn)
T2 = p560.fkine([pi/2 pi/3 pi/6 0 0 0]);%目标位姿
J = p560.jtraj(T1,T2,t);%生成轨迹
p560.plot(J)

Next we will save the animation of this demonstration and make it into a gif animation:

%制作动画
filename = 'demo.gif';
for i = 1:length(t)
    pause(0.01)
    p560.plot(J(i,:));
    f = getframe(gcf);  %gcf获取当前图窗的句柄,getframe捕获坐标区或图窗作为影片帧
    imind = frame2im(f); %返回与影片帧关联的图像数据
    [imind,cm] = rgb2ind(imind,256); %将 RGB 图像转换为索引图像
    if i == 1
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',0.1);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',0.1);
    end
end

The process of making animation is to write each frame of the animation into the animated picture GIF. The principle is as follows:
start the animation demonstration, use the gcf  function to obtain the handle of the current figure, and then use the getframe  function to capture the axes or figure as movie frame, then use the frame2im  function to return the image data associated with the movie frame, and the rgb2ind function to convert the RGB image to an indexed image (This MATLAB function uses minimum variance quantization and dithering to convert an RGB image to an indexed image X. map contains at most n colors. n must be less than or equal to 65,536.) Finally, use the imwrite function to write the image data into the graphics file. This is animation, so dynamic image data will be continuously written to the gif file. In addition, the usage of for loop and if here needs to be noted that end is required to match the end.   

2. Power function

Animation demonstration from the first power of x to the sixth power of x:

x = -2:0.01:2;
n = 1:1:6;
filename = 'demo1.gif'
for i = 1:length(n)
    y = x.^(n(i));
    plot(x,y,'r','LineWidth',1)
    title(['y = x^' num2str(n(i))])
    drawnow
    frame = getframe(gcf);
    im{i} = frame2im(frame);
    [A,map] = rgb2ind(im{i},256); 
    if i == 1
        imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',1);%每帧播放时间
    else
        imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',1);
    end
end

 

3. Sine function

filename = 'demo2.gif'
for i = 1:4
    x = 0:0.1:2*pi*i;
    y = sin(x);
    plot(x,y,'r','LineWidth',1)
    title('y = sinx')
    drawnow
    frame = getframe(gcf);
    im{i} = frame2im(frame);
    [A,map] = rgb2ind(im{i},256); 
    if i == 1
        imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',1);%每帧播放时间
    else
        imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',1);
    end
end

 

4. Circle and sine functions

The relationship between circular motion and sine function (a circle from 0 to 2π is also a period of sine function), so trigonometric functions are also called circular functions. They are originally the trajectories formed by the movement of points on the circle.

theta = linspace(0,2*pi,200); 
%圆的坐标
x = cos(theta)-1;
y = sin(theta);
%正弦函数坐标
x1 = theta;
y1 = sin(x1);
 
for i = 1:length(theta)
    set(gcf,'outerposition',get(0,'screensize'));%全屏
    %圆和正弦函数之间的连接线
    connectLineX = linspace(x(i),x1(i),50);
    connectLineY = zeros(1,50)+y(i);

    AxisX = linspace(-2,10,50);
    AxisY = zeros(1,50);
    %圆中的箭头
    arrowX = [-1,x(i)];
    arrowY = [0,y(i)];
    
    %圆周上的点到横轴的垂线
    lineX = zeros(1,20)+x(i);
    lineY = linspace(0,y(i),20);
    %正弦函数上的点到横轴的垂线
    x3 = zeros(1,20)+x1(i);
    y3 = linspace(0,y1(i),20);
    %画图
    plot(x(1:i),y(1:i),'r',x1(1:i),y1(1:i),'g',connectLineX,connectLineY,'--',arrowX,arrowY,'-bo',lineX,lineY,'k',x3,y3,'k','LineWidth',3);
    text(x(i)+0.05,y(i),strcat(num2str(theta(i)/pi),'\pi'),'fontsize',14);
    text(x1(i)+0.05,y1(i),strcat(num2str(theta(i)/pi),'\pi'),'fontsize',14);
    grid on
    axis equal
    axis([-2.2 7 -1.2 1.2])
    set(gca,'XTick',[-2,-1,0:pi/8:2*pi]);
    set(gca,'xtickLabel',{'-2','-1','0','\pi/8','\pi/4','3\pi/8','\pi/2','5\pi/8','3\pi/4','7\pi/8','\pi','9\pi/8','5\pi/4','11\pi/8','3\pi/2','13\pi/8','7\pi/4','15\pi/8','2\pi'});
    title('圆周运动跟正弦函数','fontsize',22,'fontname','微软雅黑')
    drawnow
    f=getframe(gcf);
    imind=frame2im(f);
    [imind,cm] = rgb2ind(imind,256);
    if i == 1
        imwrite(imind,cm,'sin.gif','GIF', 'Loopcount',inf,'DelayTime',0.02);
    else
        imwrite(imind,cm,'sin.gif','GIF','WriteMode','append','DelayTime',0.02);
    end
end

Since the animation exceeds 5M, it cannot be uploaded. Here is a screenshot:

PS draws a circle:

t=0:0.1:2*pi
x = cos(t);
y = sin(t);
plot(x,y)
或者
ezplot('x^2+y^2=1')

Guess you like

Origin blog.csdn.net/weixin_41896770/article/details/134852388