0% found this document useful (0 votes)
75 views

Matcodes

The document describes code to analyze and visualize the motion of a four-bar linkage mechanism. It defines geometric parameters, calculates the positions and velocities of points on the linkage over time, and animates the motion by plotting the linkage components and relevant values at each time step. The code analyzes both the kinematics and dynamics of the four-bar linkage as it rotates.

Uploaded by

Acet Aldehyde
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Matcodes

The document describes code to analyze and visualize the motion of a four-bar linkage mechanism. It defines geometric parameters, calculates the positions and velocities of points on the linkage over time, and animates the motion by plotting the linkage components and relevant values at each time step. The code analyzes both the kinematics and dynamics of the four-bar linkage as it rotates.

Uploaded by

Acet Aldehyde
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

%% Plot Any Four Bar Linkage

clc;clear;close all
X = [4 12 8 10 40 120];
% X = [180 100 185 220 55 0];
% X = [r1 r2 r3 r4 Cx Cy ];
% r1: Crank (make sure its always the smallest, also r3+r4>=r1+r2)
% r2: Coupler
% r3: Lever (Rocker)
% r4: Frame
% Cu: x coordinate for coupler point wrt crank-coupler point
% Cv: y coordinate for coupler point wrt crank-coupler point

cycles = 2;% number of crank rotations


INCREMENTS = 100;% divide a rotation into this number
%% check the geometry
P = X(1:4);
check = P;
[L locL] = max(check);
check(locL) = [];
[S locS] = min(check);
check(locS) = [];
R = check;
flag = 0;

if S==X(4) & sum(check)>(L+S)


TITLE = 'This is a Double-Crank Mechanism';
elseif (S==X(1)|S==X(3)) & sum(check)>(L+S)
TITLE = 'This is a Rocker-Crank Mechanism';
elseif S==X(2) & sum(check)>(L+S)
TITLE = 'This is a Double-Rocker Mechanism';
flag = 1;
elseif sum(check)==(L+S)
TITLE = 'This is a Change Point Mechanism';
elseif sum(check)<(L+S)
flag = 1;
TITLE = 'This is a Double-Rocker Mechanism';
end
%%
TH1 = linspace(0,2*pi,INCREMENTS);% Input angle theta1
dig = 10;% divide links into this number
R1 = X(1); r1 = linspace(0,R1,dig);
R2 = X(2); r2 = linspace(0,R2,dig);
R3 = X(3); r3 = linspace(0,R3,dig);
R4 = X(4); r4 = linspace(0,R4,dig);
Cu = X(5); cu = linspace(0,Cu,dig);
Cv = X(6); cv = linspace(0,Cv,dig);

%% check valid region


D = sqrt(R1^2 + R4^2 - 2*R1*R4*cos(TH1));% diagonal distance between
% crank-coupler point and rocker-frame point
TH5 = acos((R3^2+D.^2-R2^2)./(2*R3*D));% angle between rocker and diagonal
% link (d)
IMAG = imag(TH5);
[VALUES LOCATION] = find(IMAG==0);
%%
IMAG = imag(TH5);
LOCATION = IMAG==0;
LOCATION1 = find(IMAG==0);
LOC = LOCATION;
n = length(LOCATION);
n1 = length(LOCATION1);
Check = 0;
direction = 1;
for i=1:n-1
if LOC(i+1)~=LOC(i)
if Check==0
direction = LOC(i);
end
Check = Check+1;
end
end
%%
Rotate = 0;
if isempty(LOCATION1)
error('This is not a valid linkage');
elseif direction==0 & Check==2
LOC1 = find(LOCATION==1);
th1 = [TH1(LOC1) TH1(fliplr(LOC1))];
elseif n1==n
th1 = TH1;
elseif direction==1 & Check==2
Rotate = 1;
loc1 = LOC(1:end-1);
loc2 = LOC(2:end);
[Value deadpoint] = find((loc2-loc1)~=0);
deadp = deadpoint + [0 1];
LOC2 = [deadp(2):n 1:deadp(1)];
th1 = [TH1(LOC2) TH1(fliplr(LOC2))];
elseif Check==4
Rotate = 1;
loc1 = LOC(1:end-1);
loc2 = LOC(2:end);
[Value deadpoint] = find((loc2-loc1)~=0);
deadp1 = deadpoint(1:2) + [1 0];
deadp2 = deadpoint(3:4) + [1 0];
fprintf('This mechanism has two disconnected upper and lower regions\n');
DIREC = 1;
DIREC = input('Select [1] for upper, [2] for lower Default = [1] ');
if DIREC == 1
LOC3 = [deadp1(1):deadp1(2)];
else
LOC3 = [deadp2(1):deadp2(2)];
end
th1 = [TH1(LOC3) TH1(fliplr(LOC3))];
end

d = sqrt(R1^2 + R4^2 - 2*R1*R4*cos(th1));


th5 = acos((R3^2+d.^2-R2^2)./(2*R3*d));% angle between rocker and

%%
if Rotate == 1
d = sqrt(R1^2 + R4^2 - 2*R1*R4*cos(th1));
th5 = acos((R3^2+d.^2-R2^2)./(2*R3*d));% angle between rocker and diagonal link
(d)
th5 = [th5(1:end/2) -th5(end/2+1:end)];
end
Ax = R1*cos(th1);% x coordinate for the crank-coupler point
Ay = R1*sin(th1);% y coordinate for the crank-coupler point
a = R4 - R1*cos(th1);% horizontal distance between rocker-frame point and
% projection of crank-coupler point
b = Ay;% vertical projection of crank-coupler point
th6 = atan2(b,a);% angle between frame and diagonal link (d)
th4 = pi - th5 - th6;% angle the rocker makes with horizon
Bx = R3*cos(th4) + R4;% horizontal distance between frame-crank point and
% projection of coupler-rocker point
By = R3*sin(th4);% vertical projection of coupler-rocker point
th2 = atan2((By-Ay),(Bx-Ax));% angle the coupler makes with the horizon
Cx = Ax + Cu*cos(th2) - Cv*sin(th2);% horizontal projection of coupler
% point wrt coupler
Cy = Ay + Cu*sin(th2) + Cv*cos(th2);% vertical projection of coupler
% point wrt coupler
% calculate display (figure) limits
xmin = 1.2*min([min(Cx) -R1 -R3]);
xmax = 1.2*max([max(Cx) R4+max([R3 max(R3*cos(th4))])]);
ymin = 1.2*min([min(Cy) -R1 -R3]);
ymax = 1.2*max([max(Cy) max([R1 R3 R3+Cv])]);
%%
increments = length(th1);
for i=1:increments
link1x(i,:) = r1*cos(th1(i));
link1y(i,:) = r1*sin(th1(i));
link2x(i,:) = linspace(Ax(i),Bx(i),dig);
link2y(i,:) = linspace(Ay(i),By(i),dig);
link3x(i,:) = R4 + r3*cos(th4(i));
link3y(i,:) = r3*sin(th4(i));
Couplx1(i,:) = linspace(Ax(i),Cx(i),dig);
Couply1(i,:) = linspace(Ay(i),Cy(i),dig);
Couplx2(i,:) = linspace(Cx(i),Bx(i),dig);
Couply2(i,:) = linspace(Cy(i),By(i),dig);
end
for k=1:cycles
for i = 1:increments
plot(link1x(i,:),link1y(i,:),'b',link2x(i,:),link2y(i,:),'r',...
link3x(i,:),link3y(i,:),'k',Couplx1(i,:),Couply1(i,:),'r',...
Couplx2(i,:),Couply2(i,:),'r')
hold on
plot([link2x(i,:) ;Couplx1(i,:)],[link2y(i,:);
Couply1(i,:)],'g','linewidth',2)
plot([link2x(i,:) ;Couplx2(i,:)],[link2y(i,:);
Couply2(i,:)],'g','linewidth',2)
plot(0,0,'sk',R4,0,'sk','MarkerSize',12)
plot(0,0,'ok',R4,0,'ok')
plot(Couplx1(i,end),Couply1(i,end),'ok','MarkerSize',6,...
'MarkerFaceColor','g')
axis([xmin xmax ymin ymax])
if Rotate == 1 & i<=increments/2
plot(Couplx1(1:i,end),Couply1(1:i,end),'--g','linewidth',2)
elseif Rotate == 1
plot(Couplx1(1:increments/2,end),Couply1(1:increments/2,end),'--
g','linewidth',2)
plot(Couplx1(increments/2:i,end),Couply1(increments/2:i,end),'--
r','linewidth',2)
else
plot(Couplx1(1:i,end),Couply1(1:i,end),'--g','linewidth',2)
end
clc
title(['\bf',TITLE])
fprintf('Th1 = %5.2f, th5 = %5.2f, D = %7.2f\n',th1(i),th5(i),d(i))
YY = input('Hit Enter ');
hold off
end
end
if Rotate==1
hold on
plot(Couplx1([1 end/2],end),Couply1([1 end/2],end),'hr','MarkerSize',10)

clc; clear all;


% Parameters
A = 2; B = 3; C = 4; D = 5;

t = 0:0.05:10;

ang_speed = 2;
theta = ang_speed*t;

P1 = [0;0];
P4 = D*[1;0];

P2 = A*[cos(theta); sin(theta)];
E = sqrt(A^2 + D^2 - 2*A*D*cos(theta));
alfa = asin(A*sin(theta)./E);
beta = acos((E.^2 + C^2 - B^2)./(2*E*C));
P3 = [D - C*cos(alfa+beta); C*sin(alfa+beta)];

P3_x = P3(1,:);
P3_y = P3(2,:);

P3_vx = diff(P3_x)./diff(t);
P3_vy = diff(P3_y)./diff(t);

P3_v = sqrt(P3_vx.^2 + P3_vy.^2);

for i=1:length(t);

ani = subplot(2,1,1);
P1_circle = viscircles(P1',0.05);
P2_circle = viscircles(P2(:,i)',0.05);
P3_circle = viscircles(P3(:,i)',0.05);
P4_circle = viscircles(P4',0.05);

A_bar = line([P1(1) P2(1,i)],[P1(2) P2(2,i)]);


B_bar = line([P2(1,i) P3(1,i)],[P2(2,i) P3(2,i)]);
C_bar = line([P3(1,i) P4(1)],[P3(2,i) P4(2)]);
axis(ani,'equal');
set(gca,'XLim',[-5 8],'YLim',[-2 7]);

str1 = 'P3';
str2 = ['Time elapsed: ' num2str(t(i)) ' s'];
P3_text = text(P3(1,i),P3(2,i)+0.4,str1);
Time = text(-2,6,str2);
pause(0.005);
if i<length(t)
delete(P1_circle);
delete(P2_circle);
delete(P3_circle);
delete(P4_circle);
delete(A_bar);
delete(B_bar);
delete(C_bar);
delete(P3_text);
delete(Time);
vel = subplot(2,1,2);
plot(vel,t(1:i),P3_v(1:i));
set(vel,'XLim',[0 10],'YLim',[0 10]);
xlabel(vel, 'Time (s)');
ylabel(vel, 'Amplitude (m/s');
title(vel,'Speed of P3');
grid on;
end

end

You might also like