Week 2- 2R Robotic Arm Challenge
Question :
Write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm, as shown in the video.
Create an animation file of the plot.
Answer :
Program:
clear all
close all
clc
%Inputs
%lENGTH OF ARM 1 & ARM 2 IN METER
L1=1;
L2=0.5;
%ANGLE OF ARM 1 & ARM 2 WITH RESPECT TO X-AXIS
Theta1=linspace(0,90,10);
Theta2=linspace(0,90,10);
ct=1;
for i=1:length(Theta1)
THETA1=Theta1(i);
for j=1:length(Theta2)
THETA2=Theta2(j);
%CO-ORDINATE CALCULATION
%STARTING POINT OF ARM1
x0=0;
y0=0;
%END POINT AND STARTING POIN T OF ARM 2
x1= L1*cosd(THETA1);
y1= L1*sind(THETA1);
%END POINT OF ARM 2
x2= x1+L2*cosd(THETA2);
y2= y1+L2*sind(THETA2);
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'LineWidth',3);
axis([-0.1 1.5 0 1.5]);
xlabel('x-axis');
ylabel('y-axis');
pause(0.1)
M(ct)= getframe(gcf);
ct=ct+1;
end
end
movie(M)
videofile=VideoWriter('forward_kinematics2R_avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Code explaination :
1. linspace- used to define angles of arm at diffrent position.
2. for - used to repeat set of actions again & again.
3. plot- used to plot co ordinates of arms at different positions .
Output Screenshots:

Error faced while programing:
Error 1:
Unrecognized function or variable 'L1'.
Error in forward_kinematics2R_avi (line 21)
x1= L1*cosd(THETA1);
Reason: Variable L1 & L2 were not defined.
Solution : Defined L1- Length of arm 1=1
Defined L2- Length of arm 2=0.5
Screen shot of error :

Link for animation file :
https://drive.google.com/file/d/1nRk76QqKRCnMjjvuZizipV5ci1-QDxbB/view?usp=sharing