Week 2- 2R Robotic Arm Challenge
%Forward Kinematics of a 2r robotic arm manupulator with animation
clear all;
close all;
clc
%Inputs
%Angles of the Robotic arms
theta1 = linspace(0,90,10);
theta2 = linspace(-90,90,20);
theta1a = linspace(90,180,10);
theta2a = linspace(90,270,20);
%I am making the arms move on either side of the x and y axis. So the Robotic arm has larger workspace
%Length of the robotic arms
l1 = 1;
l2 = 0.8;
%How to approach the problem, as we can see we have multiple angles for both links.
%so we cannot write individual eq for the position of each links, which is
%cumbersome and will consume a lot of time, so here we make use of the
%'for' loop to solve that issue.
%Another thing to remember is if we use 'for' loop then each time the value
%stored in 'i' is a single value, rather than a matrix.
%eg: If we use i=1:10, then it give 10 values straight in a matrix form. If
%we type size(i) we get 1 row and 10 column. But in for loop each time it
%goes through we get the numbers 1, 2, 3, 4 individually and so on.
%Giving counter
ct=1;
%We need a combination of 10 times 10
for i = 1:length(theta1)
T1=theta1(i);
for j = 1:length(theta2)
T2=theta2(j);
x0 = 0; %Starting point for link 1
y0 = 0;
x1 = l1*cosd(T1); %Ending point for link 1, but as link 2 is pivoted at the same point we can say that link 2's starting point is also the same
y1 = l1*sind(T1);
x2 = x1+l2*cosd(T2); %Ending point for link 2
y2 = y1+l2*sind(T2);
%Plotting
plot([x0 x1], [y0 y1], [x1 x2], [y1 y2], 'linewidth', 5)
axis([-2 2 -2 2]) %Fixing the axis
pause(0.08) %Pauses the execution for 0.08s, so by this we can see the effect of animation
M(ct)=getframe(gcf); %Stores each frame in M as ct gets incremented
ct=ct+1; %ct increments in as for loop repeats itself
end
end
%This is the 'for' loop to change the direction or the working axis of the
%robotic arm. This increases the workspace accessability of the robotic arm
%from 90 degree to 180 degree.
for k = 1:length(theta1a)
T3=theta1a(k);
for l = 1:length(theta2a)
T4=theta2a(l);
x0 = 0; %Starting point for link 1
y0 = 0;
x1a = l1*cosd(T3); %Ending point for link 1, but as link 2 is pivoted at the same point we can say that link 2's starting point is also the same
y1a = l1*sind(T3);
x2a = x1a+l2*cosd(T4); %Ending point for link 2
y2a = y1a+l2*sind(T4);
%Plotting
plot([x0 x1a], [y0 y1a], [x1a x2a], [y1a y2a], 'linewidth', 5)
axis([-2 2 -2 2])%Fixing the axis
pause(0.08) %Pauses the execution for 0.08s, so by this we can see the effect of animation
M(ct)=getframe(gcf); %Stores each frame in M as ct gets incremented
ct=ct+1; %ct increments in as for loop repeats itself
end
end
movie(M)
Videofile = VideoWriter('RoboArm_Animation_2R_180.avi', 'uncompressed AVI');
open(Videofile) %This is the 1st operation. to open the video file
writeVideo(Videofile,M) %We are going to write the frames(Which is stored in M) into this video file
close(Videofile)
%Errors faced
%While I tried to get it to work for 180 degree, I 1st faced an error in
%link 2, which instead of moving from 90 to 270 degree, it was moving in
%the opoosite direction i.e., from -90 to 90.
%To tackle this I had to create another 2 variables and create a seperate
%linspace for that (2nd set)
%While I tried to insert 'for' loop within the 'for' loop of 1st one, then
%I had too many problems there as the 2nd set of linspace we repeated
%several times for each link 2 movement of 1st set.
%To counter this I had to create another 'for' loop seperately to execute
%the 2nd set of degrees.
%While saving the movie I tried with 2 variable, which gave me an error.
%then I thought that as M(ct) only stores a single variable as it
%increments, therefore there is no need for a second variable to store the
%movie. As the entire animation can be stored within the same variabel M.