Week 2- 2R Robotic Arm Challenge
Matlab program to simulate the forward kinematics of a 2R Robotic Arm
The length of the two arms are taken respectively
l1 = 1m
l2 = 0.5m
the workspace angles are taken between 0 and 90 degrees for each arms
I will present the code now
clear all
close all
clc
%kinematics of robotic arm with for loop
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 2 0 2])
M(ct) = getframe(gcf);
ct=ct+1;
end
end
movie(M)
videofile = VideoWriter('robotiC_arm_with_loops_2.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
clear all - used for clearing the variables in workspace
close all - used fo closing the existing plots , graphs
clc - clears the command window
Lengths are takesn as 1m and 0.5m for L1 and L2 respectively
Angle range ;
The theta1 has a range of value ,angles from 0 to 90 degree which is taken by linspace command ranging 10 degrees between them for 10 values
same way theta2 also taken
theta1 values are the angles of arm 1 1 L1
theta2 values are the angles of arm 2 L2
here FOR loops are used in this program to allocate the angle values of the arms for each case
as
theta1 = linspace(0,90,10) gives 10 values namely
theta1 = [0 10 20 30.....90] - 10 values
the FOR loops are used
FOR i = 1:length(theta1)
THETA1 = theta1(i)
here i repeats 10 times by assigning value for THETA1. These values are taken from theta1 which is from the array [ 0 10 20 30 40 50 60 70 80 90] so THETA1 (variable) has a value of angles from 0 to 90 degrees. This corresponds to arm 1
same way for the arm 2 FOR loops are programmed, but here as the arm 2 corresponds to every action of arm 1 again a FOR loop is given inside the loop itself . so that the arm 2 which will have 10 values of angle correspomds to 10 values of angle already given to arm 1 .
So that totally there will be 10*10 output position of the Robotic arms.
THE PROBLEM;
Initially the coordinates are set to origin i.e x = 0 and y = 0
the arm 1 would be determined by giving coordinate values of x and y with the help of trigonometry
so x1 = L1cosd(THETA1)
y1 = L1sind(THETA1) are given
same way arm 2 coordinates are determined
arm 2 starts after arm 1
x2 = x1 + L2cosd(THETA2)
y2 = y1 + L2sind(THETA2) are given
THETA1 and THETA2 are the variables here assigning angle values for arm 1 and arm 2 respectively.
PLOTTING;
plot([x0 x1],[y0 y1], [x1 x2], [y1 y2]) gives us the plot for the coordinates x0 x1 ,y0 y1, and x1 x2, y1 y2
the figures of the plot are stored in a variable called M i.e M(ct)
M(ct) = getframe(gcf) this is given that the figures ,plots, frames that is being generated must be captured and asiigned to the variable M
then the whole code is incremented
These all codes comes under FOR loop as the variables mentioned above depends on the angular value of arm 1 and arm 2 andd corresponds to it.
VIDEOFILE;
The plots(totally 100 images) which is inside the variable M is commanded to form a video by giving a command video(M)
The video file must be in AVI form so
videofile = VideoWriter('robotic_arms_with_loops_2.avi','Uncompressed AVI')
is given which means that ,this videofile contains video in AVI format so a file is initially created and the file is opened using the command
open( videofile)
and the video is played by the command
writeVideo(videofile,M) which consists the video M in AVI file format.
close(videofile)
ends the video
here is my link to youtube video of robotic arm with 2 loops.