Write a Matlab to animate the motion of Grashof 4-bar mechanism (s+l < p+q); s = 5 cm, l =
13 cm, p = 10 cm, q = 10 cm
function fourbar(r1,r2,r3,r4)
% fourbar simulates the kinematics of a four-bar linkage.
%
% fourbar(r1,r2,r3,r4) simulates the trajectories of a four-bar
likanges of
% dimensions r1 (ground), r2 (driver), r3 (couples), and r4
(follower).
%
%
% Example
% fourbar(2,1,3,3.5)
%
% input arguments
if nargin < 4
r1 = 2;
r2 = 1;
r3 = 3;
r4 = 3.5;
end
s = min([r1 r2 r3 r4]);
l = max([r1 r2 r3 r4]);
if (s+l)>(sum([r1 r2 r3 r4])-(s+l))
disp('Not a Grashoff mechanism.')
return
end
% user-defind orientation of the ground
t1 = 0;
% user-defind delta of the angle of the driver
deltat2 = 2.5;
% loop for Theta2 (angle of the driver)
for t2 = 0:deltat2:360
% Finding Theta 4 (angle of the follower)
A = 2*r1*r4*cosd(t1) - 2*r2*r4*cosd(t2);
B = 2*r1*r4*sind(t1) - 2*r2*r4*sind(t2);
C =
r1^2+r2^2+r4^2-r3^2-2*r1*r2*(cosd(t1)*cosd(t2)+sind(t1)*sind(t2));
% Assembly mode
s = -1;
t = (-B+s*sqrt(B^2-C^2+A^2))/(C-A);
t4 = 2*atand(t);
% Finding Theta 3 (angle of the coupler)
t3 = atan2((r1*sind(t1)+r4*sind(t4)-r2*sind(t2)),...
(r1*cosd(t1)+r4*cosd(t4)-r2*cosd(t2)))*180/pi;
% Position vectors
R1 = r1*[cosd(t1), sind(t1)];
R2 = r2*[cosd(t2), sind(t2)];
R3 = r3*[cosd(t3), sind(t3)];
R4 = r4*[cosd(t4), sind(t4)];
% Connections
O = [0,0]; % ground-driver
Q = O+R2; % driver-coupler
P = O+R2+R3; % coupler-follower
PP = O+R1+R4; % follower-coupler
R = O+R1; % ground-follower
if norm(P-PP) > 1e-1
disp('the closure equation is not satisfied')
else
% feel free to comment anything you don't need to plot
hold on
% Plot connections
plot(O(1),O(2),'co') % ground-driver
plot(Q(1),Q(2),'co') % driver-coupler
plot(P(1),P(2),'c.') % coupler-follower
plot(PP(1),PP(2),'co') % follower-coupler
plot(R(1),R(2),'co') % ground-follower
% Links
plot([O(1),R(1)],[O(2),R(2)],'g-') % ground
plot([O(1),Q(1)],[O(2),Q(2)],'r-') % driver
plot([Q(1),P(1)],[Q(2),P(2)],'b-') % coupler
plot([R(1),PP(1)],[R(2),PP(2)],'y-')% follower
axis equal, hold off
pause(0.1)
end
end
Type fourbar(5,10,13,10) in Matlab command prompt
Get Answers For Free
Most questions answered within 1 hours.