Week 3.2 - 2R Robotic Arm Challenge
The program is about the positioning of the arms of a 2R Robotic Manipulator.
The link lengths are assumed and for each values of theta1 and theta2 the positions of the links are calculated and with the help of a for loop, its animated to a video.
The main challenge faced in the program was to animate the link position. Firstly the command used was line command and then changed to plot command. Then animation was created using pause command which was ineffective and then later changed to getframe() and movie() to create the animation.
The code is given below:
%Animation of Robotic Manuipulator
clear all
close all
clc
theta1=linspace(0,90,10); % creating an array for the theta 1 value
theta2=linspace(0,90,10); % creating an array for the theta 2 value
l1=1; % assuming the length of link 1 as 1 meter
l2=0.5; % assuming the leAngth of link 2 as 0.5 meter
ct=1; % counter intialization
for i=1:length(theta1) % Loop 1 intialization
THETA1=theta1(i); % extracting each value to a new variable for calculations
for j=1:length(theta2) % Loop 2 intialization
THETA2=theta2(j); % extracting each value to a new variable for calculations
%coordinates
x0=0; % intialization of x0 as 0
y0=0; % intialization of y0 as 0
x1=l1*cosd(THETA1); % calculating the x-coordinate of link 1
y1=l1*sind(THETA1); % calculating the y-coordinate of link 1
x2=x1+(l2*cosd(THETA2)); % calculating the x-coordinate of link 2
y2=y1+(l2*sind(THETA2)); % calculating the x-coordinate of link 2
%plotting
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3) % Plot command
axis([-0.1 1.5 0 1.5]) %fixing the axis
pause(0.03) %giving appropriate pause to generate the video
M(ct)=getframe(gcf); % getframe() captures the current frame as it appears on the screen as a movie frame.
ct=ct+1;
end
end
movie(M) %creating a movie using the frames obtained in the for loop.
videofile=VideoWriter('Animation.avi','Uncompressed AVI'); %a videofile is created
open (videofile) %opening the videofile
writeVideo(videofile,M) %writing the captured frames to the videofile
close (videofile) %closing the videofile