Challenge

Week 2- 2R Robotic Arm Challenge

AIM - 2R Robotic Arm simulation.

Objective - Write a MATLAB code in order to simulate a robotic arm for the changing angles for link 1 and 2 using forward kinematics.

  • And make a animation of robotic arm simulation.

Theory - 2R Robotic Arm is a machine which helps to move the objects from one place to another place and to assemble parts in industries. It has two links and it has to rotational joints which allows to freely rotate itself in space.

  • In this program we are going to simulate a 2R robotic arm for the changing angles by using Forward Kinematics.

kinematics - Its the branch of mechanics that deals with the motion of the objects without concern about the forces that causes the motion.

  • There are two types of Kinematics.
  • 1. Forward kinematics
  • 2. Inverse kinematics.

Forward kinematics - It refers to the kinematics equation which is used to find the position of end effector from the known joint parameters.

Kinematics of robotic arm - As per the trigonometric relations,

X0=0 Y0=0

X1=L1*cosd(theta1)

Y1=L2*cosd(theta1)

X2=X1+L2*cosd(theta2)

Y2=Y1+L2*cosd(theta2)

Robotic Arm - The length of arm has to be defined depending on the workspace.

  • Workspace is the space upto which the manipulator can move.
  • here we saying both the links can move from 0 to 90 degree.
  • The linspace command is used to make 10 interval between 0 to 90.

Nested for-loop - Using one for-loop inside another for-loop, is known as nested for-loop.

  • In the robotic arm simulation, for a single movement of link-1, we are computing all possible moves of the link-2.
  • Since two links are involved here 'nested for loop' is used to capture the movement of two links.
  • Nested for loop runs a piece of code for predetermined number of times containing one loop changed with other.
  • One for loop will be placed as a body of other for loop.
  • It executes the 'embedded for loop' for every iteration of parent for loop.

Animation - Following terminology are used for creating Animation.

M(ct)=getframe(gcf);

  • getframe - It capture the figure as frame.
  • gcf - It refers to the current figure handle in MATLAB where it captures and in turn give the current figure to capture it as a frame for movie creation.
  • Counter variable(ct) - It helps in proper indexing in which the capture frame will be indexed sequentially.which follows the proper order when comes to move creation.
  • movie(M) = plays the movie frames in array 'M' once.
  • VideoWriter= This command will creat a video file of specified name and format.
  • open= Opens the specified file (video file).
  • writeVideo= writes the created video file with the frames captured in a the 'M' array.
  • close= Close the specified file.

And finally the video file will be saved in the program path.

Procedure -

  • While start any program its better to close all window and erase previous calculations, so we use these three commands-

1. clear - this command clear all previous data stored in the workspace window.

2. close - this command close all the previous figure and plot.

3. clc - this command clear all data in command window.

  • Now we give all the input variables which required.

i. L1(Link 1)=2

ii. L2(Link 2)=1

iii. theta1(Angle's between link 1 move)=linspace(0,90,10)

iv. theta2(Angle's between link 2 move)=linspace(0,90,10)

v. ct(counter variable)=1

  • Now we use nested for loop

for i = s_v : e_v

for j = s_v1 : e_v1

statements;

  • now we use plot command and plotting the graph between ([x0 x1],[y0 y1],[x1 x2],[y1 y2]).
  • now for creating Animation we use

M(ct) = getframe(gcf);

ct = ct+1

  • now we end the nested for loop

end

end

  • now writing video use basic terminologies

And finally the video file will be saved in the program path.

Code -

% 2R robotic arm arm for the changing angles for link 1 and link 2
clear
close
clc
% Using forward kinematics
% Defining links
l1 = 2
l2 = 1
% defining 10 intervals where links can moves by using linspace
theta1 = linspace (0,90,10);
theta2 = linspace (0,90,10);
% defining counter variable
ct = 1
% use nested for loop
for i = 1:length (theta1);
Theta1 = theta1(i)
for j = 1:length (theta2);
Theta2 = theta2(j)
%statements
x0 = 0
y0 = 0
x1 = l1*cosd(Theta1);
y1 = l1*sind(Theta1);
x2 = x1 + l2*cosd(Theta2);
y2 = y1 + l2*sind(Theta2);
% Plotting
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',4);
axis([0 3 0 3]);
pause(0.03)
M(ct) =getframe(gcf);
ct = ct+1
end
end
% video writing
movie(M)
videofile=VideoWriter('robotic_arm.avi','Uncompressed avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Result -

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.