Simulation of the forward kinematics of a 2R Robotic Arm in MATLAB
Objective- To write a program in MATLAB to simulate the forward kinematics of a 2R Robotic Arm.
Governing equations- Robotic Arm has 2 degrees of freedom, so two coordinates need to be defined with respect to the origin.
Let, x0=0 y0=0, (x0,y0) is the origin.
L1= length of arm 1
L2= length of arm 2
Coordinates for arm1:
x1 = L1cosƟ1 , y1=sinƟ1
Coordinates for arm2:
x2= x1+L2cosƟ2
y2= y1 + L2sinƟ2
where Ɵ1 and Ɵ2 are angles as shown in the figure.
Figure1: Robotic arm
Code
clear all
close all
clc
%inputs
l1=1.0;
l2= 0.7;
theta1=linspace(0,135,5);
theta2=linspace(0,135,5);
ct=1; % for loop for values of theta
for i = 1:length(theta1)
THETA1 = theta1(i);
for j = 1:length(theta2)
THETA2 = theta2(j);
x0 = 0;% initial postion of the arms
y0 = 0;
%Equation for first link
x1 = l1*cosd(THETA1);
y1 = l1*sind(THETA1);
%Equation for second link
x2 = x1+l2*cosd(THETA2);
y2 = y1+l2*sind(THETA2);
%plotting
plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',4)
axis([0 2.0 0 2.0])
pause(0.08)
M(ct)=getframe(gcf);%collecting frames%
ct = ct+1;
end
end
%video generation
movie(M)
videofile= VideoWriter('robotics_Arm.avi','uncompressed avi');
open(videofile)
writeVideo(videofile,M)
Explanation of Code
- First of all, the length of the arms is fixed.
- Then angles Ɵ1 and Ɵ2 are defined with movement from 0o to 135o in intervals of 50 .
- For loop is used to compute the positions of arms at different angles.
- Results are plotted and then captured frame by frame using getframe.
- Then frames are converted into a video (AVI file).
Errors faced
- Basic typing errors.
- Forgot to put an apostrophe. (Figure 2)
Figure 2: error
Rectification
- Corrected the syntax.
Results
Output
Smooth animation plot
Conclusion-
- The objective is achieved using MATLAB 18.0.
- The orientation of arms captured successfully