Animating the Forward Kinematic 2R-Robotic Arm Using Matlab
Aim:To create an Animation representing the 2R-Robotic Arm within its workspace.
Description:we can plot the two links L1,L2 by varying the angles with respect to their horizontal axis by manually.But to make an animation for a single L1 link angle and the relative motion for all L2 link angle , we have used a for loop with a loop Iterative i.e i,j.
The following parameters are used as an Inputs.
Length of the link1 = l1 = 1
Length of the link2 = l2 = 0.5
Angle made by the Link1 theta1(Array) = linspace(0,90,10)
Angle made by the Link2 theta2(Array) = linspace(0,90,10)
The Programm to create Animation For 2-R Robotic Arm is as follows,
% Program to create Animation for the 2-R Robotic Arm.
clear all
close all
clc
%INPUTS
l1 = 1;
l2 = 0.5;
theta1 = linspace(0,90,20);
theta2 = linspace(0,90,20);
ct = 1;
for i = 1:length(theta1);
THETA1 = theta1(i);
for j = 1:length(theta2);
THETA2 = theta2(j);
%coordinates
x0 = 0;
y0 = 0;
x1 = l1*cosd(THETA1);
y1 = l2*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.1 1.5 0 1.5])
pause(0.10)
m(ct)= getframe(gcf)
ct = ct+1
end
end
movie(m)
Videofile = VideoWriter('forward_kinematics_for_loop.avi','Uncompressed AVI');
open(Videofile)
writeVideo(Videofile,m)
close(Videofile)
The steps followed to create an Animation:
1. The first step is to clear and close all previous programm and commands used in matlab.
2. Defining the Input parameters
length of link1 = 1
length of link2 = 0.5
theta1(Array) = linspace(0,90,20)
Here theta1 is varying from 0 to 90 degrees, and total number of values are 20
theta2(Array) = linspace(0,90,20)
Here theta2 is varying from 0 to 90 degrees, and total number of values are 20
size(theta1)
ans =
1 20
>> size(theta2)
ans =
1 20
3. We need to use a for loop with a iterative i,j.
4.For the first for loop we will use loop iterative i
i = 1:length(theta1)
theta1 consists of 20 values ranging from 0 to 90 degrees
THETA1 = theta(i);
Here THETA1 is a variable stores only one value.To create animation i.e variation of first link for each change in second link angle ,will we use another for loop with a operator j.
5.
j= 1:length(theta2)
theta2 consists of 20 values ranging from 0 to 90 degrees
THETA2 = theta(j);
Here THETA2 is a variable stores only one value.
6.Now we have to define coordinates of link1,link2.
%coordinates
x0 = 0;
y0 = 0;
x1 = l1*cosd(THETA1);
y1 = l2*sind(THETA1);
x2 = x1+l2*cosd(THETA2);
y2 = y1+l2*sind(THETA2);
7. To Plot the link1,lin2 I have used a plot command .
%plotting
plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',3)
8.Now a variable 'm' is used to store the animation frame by frame.A counter is used before the for loop to capture animation frame by frame,and after the 'm' is defined the 'ct' should be Increased by +1 to capture next frame.
9. A videofile is created with an extension 'avi'.
The animation youtube link.