Week 2- 2R Robotic Arm Challenge
Title:- Program to simulate the forward kinematics of a 2R mechanism & create a animation.
Aim :- to simulate the forward kinematics of a 2R mechanism and to create a animmation to study the workspace of 2R mechanism.
Code :-
% Title:- Program to simulate the forward kinematics of a 2R mechanism ,
% create a animation.
% Here we are considering a 2R mechanism.
clear all
close all
clc
% input variables.
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,90);
%theta01 & theta02 is a variable we will use these variables to take value
%from variable we created i.e.theta1 & theta2
l1 = 3; %length for 1st link
l2 = 1.5; %length for 2nd link
% starting point for link no .01
x0 = 0;
y0 = 0;
% x1 & y1 is the ending point for link 1.
% x1 & y1 is the starting point for link 2.
% x2 & y2 is the ending point for link 2.
p=1;
for i = 1:length(theta1)
theta01 = theta1(i);
for j = 1:length(theta2)
theta02 = theta2(j);
x1 = l1*cosd(theta01);
y1 = l1*sind(theta01);
x2 = x1 + l2*cosd(theta02);
y2 = y1 + l2*sind(theta02);
plot([ x0 x1],[y0 y1],[x1 x2],[y1 y2])
axis([-1 5 0 5])
M(p)= getframe(gcf); % getframe is used to get the frame which stored in a variable name M(p) until it converted into a video
p= p+1; % provides a successive increment for the frames.
end
end
movie(M)
videofile = VideoWriter('robotic_two_r.avi', 'Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
ty = sprintf('Thank You')
Result :- The Result we get is an "avi" file which is the animation of 2R mechanism, by analysing that we can find the workspace that a 2R mechanism can access.
Youtube Link :-https://youtu.be/lWCCcCjmfHs
Explanation :- Here we are considering a 2R mechanism.
1. input variables
- starting variables of link no.1
x = 0
y = 0
- ending variables of link no.1
x1 = 0
y1 = 0
- length of link 1 = 3
- starting variables of link no.2
x1 = 0
y1 = 0
- ending variables of link no.1
x2 = 0
y2 = 0
- length of link 1 = 1.5
for loops(having starting and ending valve are complusory, which perform a specified task with varing variable value )
1.to vary the theta1 from 0 to 90 which return 10 values.
2.to vary the theta2 from 0 to 90 which return 90 values.
variable theta01 takes the value from theta1, using for loop
ex :- for i = 1:length(theta1)
theta01 = theta1(i)
variable theta02 takes the value from theta2, using for loop
ex :- for j = 1:length(theta2)
theta02 = theta2(j)
after calculating the values for x1, x2, y1, & y2 we plot the curve.
x1 = l1*cosd(theta01);
y1 = l1*sind(theta01);
x2 = x1 + l2*cosd(theta02);
y2 = y1 + l2*sind(theta02);
to plot
plot([x0 x1], [y0 y1], [x1 x2], [y1 y2])
for ploting 2 links starting and ending point for each link is mandatory, so we have to write the plot command in such a way that the starting value and ending value in square brackets for link1 and link2 is seprated by a comma, which plot link 1 and link 2 with two different colour.
2nd option
plot([x0 x1 x2], [y0 y1 y2]), also plot the link 1 and link 2 but with same colour which makes difficult to understand link1 and link 2
we need to capture the frames of the graph, for that we use getframe(gcf) this command capture the frame and save that to a variable name M, initial frame p =1 as a starting frame everytime the program run we add 1 in the staring value of p, p=p+1.
p=p+1 should be used after the frame capturing command, otherwise movie command will not produce animation.
To convert the frame into movie we use movie(M) command
here is the basic way to
1. name a file, videofile = VideoWriter('file_name.avi','Uncompressed AVI')
2. open the file, open(videofile)
3. write the file, writeVideo(vediofile,M) this is to write the frame output that stored in variable name M in the videofile,
4. close the file close(videofile)
Error
1. i wrote videowriter instead of VideoWriter, i overcome the error by correcting the format.
2. i made a spelling mistake of video, i use vedio instead if video.