How to upgrade code for simulating a crank-shaper mechanism from a crank slider mechanism. Failing to add the crank/input disk.
4 views (last 30 days)
Show older comments
This matlab code is for a simple crank slider, and i am failing to add another link to make a crank shaper mechanism(kinetic digram).
%This file calculates the position of various points on the
% basic slider-crank mechanism and animates the linkage
clear all;
close all;
clc;
r2 = 20;
r3 = 55;
theta2 = 0:2*pi/360:2*pi; %this line increments theta2 (the crank angle)
n = length(theta2);
x = r2*cos(theta2)+r3.*sqrt(1-(r2*sin(theta2)/r3).^2);
xO2=0;
yO2=0;
xA = r2*cos(theta2);
yA = r2*sin(theta2);
xB = x;
yB = 0;
X = [xO2,xA(1),xB(1)]; % here we store all the x coordinates
Y = [yO2,yA(1),0]; % here we stor all the y coordinate
Xground = [0,2*r2+r3]; % x coordinates of two pts on the ground
Yground = [0,0]; % y coordinates of the same two pts on the ground
Linkage_dwg = plot(xA,yA,X,Y,Xground,Yground, 'black',xB(1),0,'-rO');
%including xA and yA in the plot will draw a circle
axis ([-2*r2-r3 2*r2+r3 -2*r2 2*r2]); %setting limits for the plot
for i=1:n %we loop here to draw at incremented theta2
delete (Linkage_dwg); %delete and redraw at every theta2
X = [xO2,xA(i),xB(i)]; %Updated x coordinates
Y = [yO2,yA(i),0]; %Updated y coordinates
Linkage_dwg = plot(xA,yA,X,Y,Xground,Yground,'black', xB(i),0,'-rO',...
'MarkerSize',12); %drawing again at the updated position
axis ([-2*r2-r3 2*r2+r3 -2*r2 2*r2]); %chosen limits of the plot
axis equal; %i.e. scale such that circles do not look like ellipses
F(i) = getframe; %saving a frame at every theta2
pause (1/30); %a small pause to allow us to see the linkage
% befor theta2 changes
end
delete (Linkage_dwg); %deleting the last drawing before running
% the movie
movie(F,n) %running a movie of the stored frames
% movie2avi(F,'test.avi','fps',10,'compression','None');
%tried here to save to a file but the avi file didn't run
0 Comments
Answers (1)
Umang Pandey
on 28 Nov 2023
Edited: Umang Pandey
on 28 Nov 2023
Hi Sydney,
I understand that you want to add an additional link to your crank-rocker mechanism to extend it to a crank-shaper mechanism.
To modify the provided MATLAB code for a basic slider-crank mechanism into a crank-shaper mechanism, we need to add an additional link and a point that represents the shaper.
The following code includes a new link (r4) and a point C that moves along a vertical line, representing the shaper motion:
% This file calculates the position of various points on the
% crank-shaper mechanism and animates the linkage
clear all;
close all;
clc;
% Define lengths of links
r2 = 20; % Length of crank
r3 = 55; % Length of connecting rod
r4 = 40; % Length of shaper link
% Define the crank angle theta2
theta2 = 0:2*pi/360:2*pi; % Increment theta2 (the crank angle)
n = length(theta2);
% Preallocate arrays for speed
xA = zeros(1, n);
yA = zeros(1, n);
xB = zeros(1, n);
yB = zeros(1, n);
xC = zeros(1, n);
yC = zeros(1, n);
% Calculate positions of points A, B, and C
for i = 1:n
xA(i) = r2 * cos(theta2(i));
yA(i) = r2 * sin(theta2(i));
xB(i) = xA(i) + r3 * sqrt(1 - (yA(i)/r3)^2);
yB(i) = 0; % Slider B moves horizontally
xC(i) = xB(i); % Point C is directly above point B
yC(i) = r4; % Shaper link is vertical
end
% Ground coordinates
xO2 = 0;
yO2 = 0;
Xground = [0, 2*r2 + r3];
Yground = [0, 0];
% Set up the animation figure
fig = figure;
axis ([-2*r2-r3 2*r2+r3 -r4 2*r2]);
axis equal;
hold on;
xlabel('X');
ylabel('Y');
title('Crank-Shaper Mechanism Animation');
% Animate the linkage
for i = 1:n
% Check if the figure has been closed
if ~ishandle(fig)
break; % Exit the loop if the figure is closed
end
% Update the coordinates of the linkage
X = [xO2, xA(i), xB(i), xC(i)];
Y = [yO2, yA(i), yB(i), yC(i)];
% Plot the crank, connecting rod, and shaper link
Linkage_dwg = plot(X, Y, 'black-o', 'MarkerFaceColor', 'red', 'MarkerSize', 6);
% Plot the ground
ground_dwg = plot(Xground, Yground, 'black', 'LineWidth', 2);
% Draw the path of point A (circle)
path_dwg = plot(xA, yA, 'b:');
% Draw now
drawnow;
% Capture the frame for the animation
F(i) = getframe;
% Pause for a brief moment to make the animation visible
pause(1/30);
% Delete the linkage drawing (but keep the ground and path)
if i < n
delete(Linkage_dwg);
end
end
% Play back the captured animation frames
if ishandle(fig)
movie(fig, F, n);
end
As for saving the animation, “movie2avi” has been removed since MATLAB R2016b version. You can use the “VideoWriter” class to create video files from MATLAB animations and figures.
Here is the link to the documentation:
Hope this helps.
Best,
Umang
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!