Week 2- 2R Robotic Arm Challenge
Animation file of my program for the forward kinematics of 2R robotic arm:
code for this animation is:
%Simulate the forward kinematics of a 2R robotic Arm
clear all
close all
clc
%inputs
theta1 = linspace(0,90,15);
theta2 = linspace(0,90,15);
l1 = 1;
l2 = 0.7;
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],'linewidth',5,'color','r')
line([x1 x2],[y1 y2],'LineWidth',5,'color','g')
axis([-0.1 2 0 2])
pause(0.03)
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)
in 2R robotic arm there is two links and two angles which is connected at base.
so for first link of robotic arm i have taken "l1" and this link is make angle with base called as "theta1".
and the second link called "l2" is connected at the second end of the first link and make angle with parallel to base is called as "theta2".
here i am taking different value of angle for first link (theta 1) 15 values between 0 to 90
and also same as second angle (theta 2) 15 values between 0 to 90
so this is done using linspace command.
theta1 = linspace(0,90,15)
so i get the values as a result:
0 6.4286 12.8571 19.2857 25.7143 32.1429 38.5714 45.0000 51.4286 57.8571 64.2857 70.7143 77.1429 83.5714 90.0000
also doing the same command for theta 2 and getting the same values.
now taking lenth of first link is (l1)=1 and for the second link is (l2)=0.7
now the first point of link 1 is base point (0,0) so for that the value of x0=0, and y0=0;
and the second point for the link 1 is said to be x1=l1*cos(theta1) and same as y1=l1*sin(theta1).
but here in matlab cos theta is show the value in radiun. so i have taken the cosd theta in the replace of cos theta. becase cosd theta is show the values in degree. also for different values of theta i have taken row matrix for angles.
i consider angles as THETA 1 and THETA 2.
now for second link first point is the same as the second point of first link. because both link is connected at that point.
the second point of the second link is consider as a
x2=x1+l2*cosd(THETA2) and y2=y1+l2*sind(THETA2)
now i have to go for plotting this all points on a graph using plot command;
here in plot command the values given for points and using linewidth command i have give the value 5 which shows the thickness of th line shown is figer is 5.
and i have use the color command for giving the different color for both line so i can distinguse both line different using "r" and "g" which means red and green color for both line.
using the axis command i am define the limit of x axis and y axis in plot to show the graph
using for command i am looping the values and so i get the different plots for different values of theta.
and giving pause commmand for giving animation pause at 0.03s. so when run the for loop it pause at 0.03s and then next plot will show. and the plot is simulate smootly animation.
ct = 1 for first plot. and each time its value is increased by one so ct = ct+1.
so everytime ct value is increased and next plot will be shown.
now for this animation making video
use the "movie" command and videofile command is used for giving the name of movie.
open comand is open the video file and videowrite command is write the video and after that using close command the video is close.
during all this programme running i have facing some issues like
i have write the command
plot([x0 x1],[y0 y1],'linewidth',5,color,'r')
which shows the error that unrecognize function.
so i understand that in replace of color write 'color' in colonoperator.
also i am writing line command for plotting the values. but this comaand shows all plot and animation is not looks very clear.
so then i am use the plot command for first 2 values and for other two values i am use the line command.
plot([x0 x1],[y0 y1],'linewidth',5,'color','r')
line([x1 x2],[y1 y2],'LineWidth',5,'color','g')
also
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',5)
shows the same result. but the color of line is random.