Week 2- 2R Robotic Arm Challenge
Program to do forward kinematics of a 2R Robotic Arm
clear all
close alL
clc
%forwardkinematics animation
%inputs
l1 = 1;
l2 = 0.8;
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);
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',3)
axis([-0.2 2 0 2])
pause(0.1)
M(ct) = getframe(gcf);
ct = ct+1;
end
end
movie(M)
videofile = VideoWriter('forward_kinematics.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Output / Results of coding:-
Google drive upload link:
https://drive.google.com/file/d/1xJx5xOo5al98oQKes9eXstmmqJIU9G58/view?usp=sharing
Kindly open with a video player.
Explanation:-
Input values are l1,l2,theta1,theta2. Theta values are given with linspace command(to get 10 values in-between range)
for loop is been used to repeat the task until the value of the variable provided is been true.
Ex:- 
Initial value for x0,y0 is defined as 0.
x1,y1 having the equation
Also x2,y2 have the equation defined.
To draw a line initial and Final point is been defined:
1) line1 (x0,x1),(y0,y1)
2) line2 (x1,x2),(y1,y2)
Why use plot rather than line command:-
Ex; line([x0 x1], [y0 y1],[x1 x2], [y1 y2],'linewidth',3, 'color','c') is incorrect
plot([x0 x1],[y0 y1], [x1 x2], [y1 y2],LineWidth=3) is correct command.
axis([xstart xend ystart yend]) command is used to specify the limits on the x,y axis
pause() command is used to provide intervals in between plot execution.
M is the image variable to get the output of the program in a frame (getframe). Then counter operator (ct=1,ct=ct+1) is used to store all the frame in image variable.
Then, the movie command play this image M in an array
A file is been created on a hard disk with 'Forward_kinematics.avi'.
The file is been open with open(), written with WriteVideo(), & closed with close() command.