Week 2- 2R Robotic Arm Challenge
%forward kinematics of a 2R robotic arm MANIPULATOR
clear all
close all
clc
%inputs
l1 = 1;
l2 = 0.5;
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.1 1.5 0 1.5])
pause(0.05)
M(ct)= getframe(gcf);
ct=ct+1;
end
end
movie(M)
videofile = VideoWriter('forward_kinematics.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Explanation fo code :
for 2R robotic arm we assume that length of first link is l1= 1 meter and lenth of link 2 is l2= 0.5 meter.
angle of both link with horizontal axis is theta1 for first link and theta 2 for 2nd link.
we took linspace for equal divide angle from 0 to 90 in 10 parts for both theta1 and theta 2.
after that we use a for loop,
where in 1st line of for loop i=1:length(theta1) says that i will count from 1 to length of theta1 which is 10.
for every angle of arm one arm 2 have to rorate its full degree of freedom so we use another for loop in this for loop.
for that we use anothe theta which one is capitalized,
and the same for loop is repeated for arm 2 in 1st for loop.
now, we give values for x0,y0 which are the starting coordinates of arm 1 so x0 = 0 and y0=0,
for x1 and x2 which are the end coordinates of arm 1
x1 = l1*cosd(THETA1); for y1 same with sind
we use cosd for giving degree inputs.
arm 1 and arm 2 is connected so the end point coordinates become starting coordinates of link 2.
and for x2 and y2 which are the end coordinates of arm 2
we add l2*cosd(THETA2) to the x1 and same for the y2, l2*sind(THETA2) is added to y1
now we make the plot in same for loop by using plot command and give all coordinates.
then we give limits to the axis of the plot by using axis command for x axis -0.1 to 1.5 and for y axis 0 to 1.5
we use paus command which pauses the plot for 0.05 sec
now we save all plots in array called M by using getframe
now we take count ct and add 1 to ct so every time loop exicute so ct will increase by 1.
now we end both loop one by one.
so now for making animation video
we use movie command and give input M to the command.
for making vide file we use videoWriter and give file name and avi formate description. so our file is created now to write in the file first we ipen the file using open and then write filr using writeVideo anf givr input videofile and array M. then we close it using close command.
and now we run this program using f5 and it show us the plot and run amination plot after that we can see the avi file created in our folder which we can play using any video player.
anination video link -