Forward Kinematics of a 2R Robotic arm
Aim: To write a program in MATLAB to simulate the forward kinematics of a 2R Robotic Arm.
Objective:
a) MATLAB program for simulating forward kinematics of a 2R robotic arm.
b) Creating and saving an animation file of the plot.
Description: A 2R robotic arm contains two links (l1 , l2) and two rotational joints. If we are able to figure out how a robot is going to move we can figure out the possible location the robot can reach.
Link1 is making an angle theta1 w.r.t x-axis and Link 2 is making an angle theta2 with w.r.t y-axis.
Calculation: If θ1 and θ2 are given, we can calculate the final position of the manipulator.
calculating the position of Link1 and Link2-
x2 = (x1 + L2cosθ2)
y2 = (y1 + L2sinθ2)
With the above co-ordinates, we have our solution set-
[x0 y0] , [x1 y1] , [x2 y2]
where,
x0 yo = Fixed point of link1
x1 y1 = End point of link1
x2 y2 = End point of link2
Inputs:
Length of the link1 (l1) = 1m
Length of the link2 (l2) = 0.5m
theta1 (θ1) and theta2 (θ2) varies from 0 to 90 degrees.
Code:
%Forward Kinematics of a 2R Robotic arm manipulator
clear all
close all
clc
%Inputs
l1= 1;
l2= 0.5;
theta1= linspace(0,90,40);
theta2= linspace(0,90,40);
ct=1;
for i= 1:length(theta1)
THETA1= theta1(i);
for j= 1:length(theta2)
THETA2= theta2(j);
x0= 0;
y0= 0;
x1= l1*cosd(THETA1);
y1= l1*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.01)
M(ct)= getframe(gcf);
ct=ct+1;
end
end
movie(M)
videofile= VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Explanation of the code:
clear all, close all, clc commands used to delete the variables created in the workspace and command window.
%Inputs
l1= 1;
l2= 0.5;
theta1= linspace(0,90,40);
theta2= linspace(0,90,40);
Here,
- l1 and l2 represents the length of Link1 and Link2
- linspace command used to create arrays of theta1 and theta2 from 0 to 90 degrees with 40 values between 0 to 90.
for i= 1:length(theta1)
THETA1= theta1(i);
for j= 1:length(theta2)
THETA2= theta2(j);
Here,
- THETA1 and THETA2 is variable which contains 1 value. theta1 and theta2 is the value from 0 to 90 degrees.
-
Using For loop command to repeat the value of theta1 and theta2 which has 40 values and it stores only one value. and the value of i and j is going to change from 1 to theta1 and theta2 value which is 40. And each time value changes it executes a set of commands that are present in for loop.
Ex: when i = 1, it takes the first value of theta1 and when i = 2, it takes the second value of theta1.
x1= l1*cosd(THETA1);
y1= l1*sind(THETA1);
x2= x1+l2*cosd(THETA2);
y2= y1+l2*sind(THETA2);
Here,
- x1, y1 and x2, y2 are the starting point coordinates and endpoint coordinates of link1 respectively.
%Plotting
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2], 'linewidth',3)
axis([-0.1 1.5 0 1.5])
pause(0.01)
M(ct)= getframe(gcf);
ct=ct+1;
end
end
- Using plot command to plot the graph with the given statement.
- Using axis command to set the x and y limits.
- Using pause command to set an interval at each position.
- Using an array M and adding the getframe(gcf) function to create (stitching) an animation of all the plots.
-
Using Counter command (ct=ct+1) to get a single position of link2 at a time and each and every time for loop is getting executed it is going to increment 1 time.
Converting the animation plot of the above code into a movie with .avi extension with the following code.
movie(M)
videofile= VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Here,
- At the end of for loop MATLAB is going to make a movie with all the frames with movie(M) command.
- Creating a video with the command videofile.
- Opening a video file with open(videofile) command and then write the captured frame with writevideo command and then closing the video by close(videofile) command.
Result: