clear all;

AB = 60; 
BC = 180;
DE = 200;
CD = 120;
EF = 300;
h3 = 80;
h1 = 85;
h2 = 225;
omega1 = 100;
detaTime = 0.002;% 时间间隔s
t = 0:detaTime:(2*pi/omega1);% 时间 s
n = length(t);% 数据个数
theta1 = 0;% 曲柄转角
tem = zeros(1,n);
xSlide = zeros(1,n);% 滑块位移 mm
velocitySlide = zeros(1,n);% 滑块速度 mm/s
accelerationSlide = zeros(1,n);% 滑块加速度 mm/s

gifFilePath = 'E:\CrankSliderGifAnimation\'; %图片保存路径
    syms theta1
    xA = 0;
    yA = 0;
    xB = AB*cos(theta1);
    yB = AB*sin(theta1);
    xD = -h2;
    yD = -h3;
    BD = ((xB - xD).^2 + (yB - yD).^2).^0.5; 
    A0 = 2*CD*(xB - xD);
    B0 = 2*CD*(yB - yD);
    C0 = CD.^2 + BD.^2 -BC.^2;
    theta2 = 2*atan((B0 + sqrt(A0.^2+B0.^2-C0.^2))/(A0+C0));
    xC = xD + CD*cos(theta2);
    yC = yD + CD*sin(theta2);
    xE = xD + DE*cos(theta2);
    yE = yD + DE*sin(theta2);
    A1 = h1 - yE;
    theta3 = pi - asin(A1/EF);
    xF = xE + EF*cos(theta3);
    yF = h1;

    v= diff(xF);

    a = diff(v);

for iterTime = 1:n
    theta1 = omega1*t(iterTime);
    xA = 0;
    yA = 0;
    xB = AB*cos(theta1);
    yB = AB*sin(theta1);
    xD = -h2;
    yD = -h3;
    BD = sqrt((xB - xD)^2 + (yB - yD)^2); 
    A0 = 2*CD*(xB - xD);
    B0 = 2*CD*(yB - yD);
    C0 = CD^2 + BD^2 -BC^2;
    theta2 = 2*atan((B0 + sqrt(A0^2+B0^2-C0^2))/(A0+C0));
    xC = xD + CD*cos(theta2);
    yC = yD + CD*sin(theta2);
    xE = xD + DE*cos(theta2);
    yE = yD + DE*sin(theta2);
    A1 = h1 - yE;
    theta3 = pi - asin(A1/EF);
    xF = xE + EF*cos(theta3);
    yF = h1;

    xSlide(iterTime) = xF;
    velocitySlide(iterTime) = subs(v);
    accelerationSlide(iterTime) = subs(a);


    figure(4);
    subplot(2,3,[1,2,3]);%窗口划分成6分
    plot(0,0,'O','MarkerFaceColor','r','MarkerSize',10); %绘制坐标原点
    hold on;
    plot([0,xB],[0,yB],'r','LineWidth',4);%绘制曲柄AB
    hold on;
    plot(xB,yB,'O','MarkerFaceColor','b','MarkerSize',10);% 绘制B点
    hold on;
    plot([xB,xC],[yB,yC],'b','LineWidth',4);%绘制BC杆
    hold on;
    plot(xC,yC,'O','MarkerFaceColor','c','MarkerSize',10);%绘制C点
    hold on;
    plot(xD,yD,'O','MarkerFaceColor','r','MarkerSIze',10);%绘制D点
    hold on;
    plot([xD,xE],[yD,yE],'g','LineWidth',4);%绘制DE杆
    hold on;
    plot(xE,yE,'O','MarkerFaceColor','b','MarkerSize',10);%绘制E点
    hold on;
    plot([xE,xF],[yE,yF],'y','LineWidth',4);%绘制EF杆
    hold on;
    plot(xF,yF,'s','MarkerFaceColor','m','MarkerSize',10);%绘制滑块F
    hold on;
    plot([-600,-100],[85,85],'k','LineWidth',1);%绘制滑轨
    grid on;
    axis([-600, 200, -200, 200]);
    title('曲柄滑块机构');
    
    % 位置
    subplot(2,3,4);%窗口划分成6分
    plot(t(1:iterTime),xSlide(1:iterTime),'k','LineWidth',1);%绘制位置
    hold on;
    plot(t(iterTime),xSlide(iterTime),'O','MarkerFaceColor','r','MarkerSize',3); %绘制曲线末端点
    grid on;
    axis([-0.01,0.08 , -550, -330]);
    title('滑块位移');   
    
    % 速度
    subplot(2,3,5);%窗口划分成6分
    plot(t(1:iterTime),velocitySlide(1:iterTime),'k','LineWidth',1);%绘制速度
    hold on;
    plot(t(iterTime),velocitySlide(iterTime),'O','MarkerFaceColor','g','MarkerSize',3); %绘制曲线末端点
    grid on;
    axis([-0.01,0.08 , -200, 200]);
    title('滑块速度');   
    
    % 加速度
    subplot(2,3,6);%窗口划分成6分
    plot(t(1:iterTime),accelerationSlide(1:iterTime),'k','LineWidth',1);%绘制速度
    hold on;
    plot(t(iterTime),accelerationSlide(iterTime),'O','MarkerFaceColor','b','MarkerSize',3); %绘制曲线末端点
    grid on;
    axis([-0.01,0.08  , -200, 200]);
    title('滑块加速度');    
    drawnow;
    
        
    if mod(iterTime,1) == 0
        z = gcf;
        fileName = num2str(iterTime);% 文件名
        saveas(z,[gifFilePath,fileName,'.jpg']); %按时间顺序保存图片
    end
    
    pause(0.001);
    if iterTime < n
        clf;
    end
end


figure(1)
plot(t, xSlide,'-*');
title('滑块5位移');
xlabel('时间(s)');
ylabel('位移(mm)');

figure(2)
plot(t, velocitySlide,'-o');
title('滑块5速度');
xlabel('时间(s)');
ylabel('速度(mm/s)');

figure(3)
plot(t, accelerationSlide,'-^');
title('滑块5加速度');
xlabel('时间(s)');
ylabel('加速度(mm/s^2)');

% 图片读入
[name,path] = uigetfile('*.*','Image load','MultiSelect','on'); % name 存放文件名字符串矩阵 path 存放图片的目录  E:\TifAnimation\
% 计算选取的图像数量  name 是行向量
[~,b]=size(name);
% 获取图片信息
if b>1;
    info = imfinfo([path,name{1}]);
    
    im = cell(1,b);
    cmap = cell(1,b);
    for n = 1:b
        [im{n},cmap{n}] = imread([path,name{n}]);
    end
    imind = cell(1,b);
    cm = cell(1,b);
    for n = 1:b
        %Convert images to indexed images
        if strcmp(info.ColorType,'truecolor')==1
            [imind{n},cm{n}] = rgb2ind(im{n},256);
        elseif strcmp(info.ColorType,'grayscale')==1
            [imind{n},cm{n}] =  gray2ind(im{n});
        elseif strcmp(info.ColorType,'indexed')==1
            imind{n}=im{n};
            cm{n}=cmap{n};
        end
    end
    drawnow();%刷新
end

str = 'crankSlider.gif';%gif 文件名
GifFileName = [path,str];
for n = 1:b
    try
        [imind2, cm2] = imresize(imind{n},cm{n}, CR);
    catch err
        imind2 = imind{n};
        cm2=cm{n};
    end
    T = 1;
    if n == 1;
        imwrite(imind2,cm2,GifFileName,'gif', 'Loopcount',inf,'DelayTime',T);
    else
        imwrite(imind2,cm2,GifFileName,'gif','WriteMode','append','DelayTime',T);
    end
end
