Forward Kinematics of a 2R Robotic Arm using MATLAB
AIM: WAP in MATLAB to simulate forward kinematics of a 2R Robotic arm.
INTRODUCTION: 2R Robotic Arm basically consists of a base, two arms and two rotational joints. These links will have motion in them when they are programmed. Now link 1 will make an angle `theta_1` with respect to base, similarly the link 2 will make an angle `theta_2`.
OBJECTIVE: The primary intention of this program is to determine the workspace of the robotic arm. Workspace of the robotic arm is the points where the robotic arm can reach. Workspace identification plays a crucial role in determining the type of the robotic arm to be selected.
So we need to create a plot at various angular positions. For this we would require the initial position and final position of both the links 1 & 2.
For identifying these positions we would be using the following MATLAB program.
MATLAB Program
% WAP in MATLAB to simulate the forward kinematics of a 2R Robotic Arm.
clear all
close all
clc
%inputs
l1=1; %length of link 1 of the robotic arm
l2=0.5; %length of link 2 of the robotic arm
theta1=linspace(0,90,5); %theta1 is the angular position of link 1. It increases from 0 to 90 in 40 intervals
theta2=linspace(0,90,5); %theta2 is the angular position of link 2. It increases from 0 to 90 in 40 intervals
ct=1; %counter it is used for counting the frame number
for i=1:length(theta1) %for loop which is going to run for 5 iterations
THETA1=theta1(i); %The value of THETA1 is determined by the i counter value
for j=1:length(theta2) %for loop which is going to run for 5 iterations
THETA2=theta2(j); %The value of THETA2 is determined by the j counter value
x0=0; %initial position of the robotic arm is (0,0)
y0=0;
x1=l1*cosd(THETA1); %final position of link 1 in (x1,y1) is found
y1=l1*sind(THETA1);
x2=x1+l2*cosd(THETA2); %final position of link 2 in (x2,y2) is found
y2=y1+l2*sind(THETA2);
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)%this command is used to plot the graph of the final link positions of link1 and link2
axis([-0.1 1.5 0 1.5]) %The x-axis will have values from -0.1 to 1.5 and y-axis will have values from 0 to 1.5
pause(0.1) %there will be a pause of 0.1 second between two plots
M(ct)=getframe(gcf); %using getframe command the frames of the plot are captured and stored in the array M. gcf stands for get current figure.
ct=ct+1; % ct stands for counter and it gets incremented after each iteration
end
end
movie(M) %MATLAB creates a movie using the M array
videofile=VideoWriter('forward_kinematics_challenge.avi','Uncompressed AVI') %the name of the videofile and its type is assigned
open(videofile) %opens the videofile
writeVideo(videofile,M) %starts recording
close(videofile) %closes the videofile
I had faced the following errors in the code:
I had typed xo instead of x0.
x0 and y0 were not inside the for loop. I had placed them at the beginning of the program.
Line 28 there was an typo error it should be y2=y1+12*sind(THETA2).
Typo error in the line 36. It should be VideoWriter instead of Videowriter.
I followed a common approach for debugging all the errors. I carefully read the error message in the command window and did the required corrections.
Youtube Link: https://youtu.be/tHgP56MxzcU
CONCLUSION: Identified the workspace of the robotic arm.