All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
OBJECTIVE: To simulate the forward kinematics of a 2R Robotic Arm using MATLAB INTRODUCTION: Robot kinematics is the study of the motion of robot manipulator where the position, velocity and acceleration of all the links are calculated without considering the forces that cause this motion. A manipulator is composed…
Nitesh Kumar Gautam
updated on 19 Mar 2021
OBJECTIVE:
To simulate the forward kinematics of a 2R Robotic Arm using MATLAB
INTRODUCTION:
Robot kinematics is the study of the motion of robot manipulator where the position, velocity and acceleration of all the links are calculated without considering the forces that cause this motion. A manipulator is composed of several links which are affixed to each other either by revolute or prismatic joints from the base frame through the end-effector. End-effector is the device which is attached to the manipulator that interacts with its environment to perform certain tasks . Forward kinematics is the method for determining the orientation and position of the end effector, given the joint angles and link lengths of the robot arm.
A 2R robotic arm consists of a base, two links which rotate in 2D palne and two joints where one joint is between base and link 1 and the other is between link 1 and link 2. The schematic of 2R robotic arm is given in the following figure
The lengths of links 1 and 2 are denoted by L1, L2 respectively and the angles of inclination of links 1 and 2 with respect to horizontal are denoted by θ1 and θ2 respectively.
Start point and end point of link 1 are considered as (X0, Y0) and (X1, Y1)
Start point and end point of link 2 are considered as (X1, Y1)and (X2, Y2)
INPUT DATA:
L1 = Length of link 1 = 1 m
L2 = Length of link 2 = 0.5 m
θ1 = Angle of inclination of link 1 varies from 0 to 90 degrees
θ2 = Angle of inclination of link 2 varies from 0 to 90 degrees
Base and link 1 joined at origin
X0 = 0
Y0 = 0
CALCULATION FORMULAE:
Coordinates of end point of link 1 and start point of link 2 (X1, Y1) are calculated using L1 and θ1
X1 = L1 * cos(θ1)
Y1 = L1 * sin(θ1)
Coordinates of end point of link 1 and start point of link 2 (X2, Y2) are calculated using X1, Y1, L2 and θ2
X2 = X1 + L2 * cos(θ2)
Y2 = Y1 + L2 * sin(θ2)
CODE:
% FORWARD KINEMAATICS OF A 2R ROBOTIC ARM MANIPULATOR
clear all
close all
clc
% inputs
% Lengths of link 1 and link 2
l1 = 1;
l2 = 0.5;
% Both link 1 and link 2 rotate from 0 to 90 degrees and the range divided in to
% certain number of divisions to get animated plot
theta1 = linspace(0,90,30);
theta2 = linspace(0,90,60);
% Adding a variable to indicate the number of frame in the loop
count = 1;
% First 'for' loop is to calculate link lengths at each angle(theta1) of link 1
for i = 1:length(theta1)
% Second 'for' loop is to calculate link lengths at each angle theta2 of link 2
% wrt to particular link1 angle(theta1(i))
for j = 1:length(theta2)
% Starting point coordinates of link 1
x0 = 0;
y0 = 0;
% Calculating the end coordintes of link 1 at particular angle theta1(i)
x1 = l1*cosd(theta1(i));
y1 = l1*sind(theta1(i));
% Calculating the end coordintes of link 2 at particular angle theta2(i)
% wrt to starting point
x2 = x1 + l2*cosd(theta2(j));
y2 = y1 + l2*sind(theta2(j));
% Plotting
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
axis([-0.1 1.6 -0.1 1.6])
pause(0.01)
% Storing the current frame into array M using getframe command
% gcf returns current figure handle
M(count) = getframe(gcf);
count = count+1;
end
end
% plays the movie frames in array M once & to play 'n' times use movie(M,n)
% movie(M)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%% CREATING VIDEO FILE %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Using VideoWriter object to create a video file from an array M
% Command for VideoWriter
% v = VideoWriter(filename,profile) or VideoWriter(filename)
% Different profiles for VideoWriter
% 'Archival' - Motion JPEG 2000 file with lossless compression
% 'Motion JPEG AVI' - AVI file using Motion JPEG encoding
% 'Motion JPEG 2000' - Motion JPEG 2000 file
% 'MPEG-4' - MPEG-4 file with H.264 encoding
% 'Uncompressed AVI' - Uncompressed AVI file with RGB24 video
% 'Indexed AVI' - Uncompressed AVI file with indexed video
% 'Grayscale AVI' - Uncompressed AVI file with grayscale video
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
v = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(v) % Open the video file 'v'
writeVideo(v,M) % Writes data from an array M to the video file 'v'
close(v) % Close the video file 'v'
CODE EXPLANATION:
1. Input variables like lengths of links are declared along with additional variable 'count' whose value is set to 1 ('count' is used to indicate number of frame in the 'for' loop).
2. In order to simulate the movement of links, the angle of inclination for both links is divided into certain 'N' number of divisions using linspace command.
theta1 = linspace(0,90,N);
theta2 = linspace(0,90,N);
3. For each angle of inclination(θ1) of link 1, link 2 will move from 0 to 90 degrees(θ2). Hence two loops are required. One loop is for every angle of theta1 and another loop, which is used inside the first loop, is for every angle of theta2.
4. Now under for loop, end coordinates of link 1 (X1, Y1) and link 2 (X2, Y2) are calculated. In order to get animated movie,
first plot command is used to plot the position of links and that plot frame is stored in to array M using getframe command
M(count) = getframe(gcf);
gcf returns the current frame handle and count varible is increased by one value
5. Both for loops are ended and syntax for creating a movie file using VideoWriter command is used.
Syntax for creating a movie using VideoWriter from an array M:
Command for VideoWriter
v = VideoWriter(filename,profile) or VideoWriter(filename)
open(v) % Open the video file 'v'
writeVideo(v,M) % Writes data from an array M to the video file 'v'
close(v) % Close the video file 'v'
Different profiles for VideoWriter
'Archival' - Motion JPEG 2000 file with lossless compression
'Motion JPEG AVI' - AVI file using Motion JPEG encoding
'Motion JPEG 2000' - Motion JPEG 2000 file
'MPEG-4' - MPEG-4 file with H.264 encoding
'Uncompressed AVI' - Uncompressed AVI file with RGB24 video
'Indexed AVI' - Uncompressed AVI file with indexed video
'Grayscale AVI' - Uncompressed AVI file with grayscale video
OUTPUT:
The final output animation can be seen in the following link
https://youtu.be/5jZKVhfgwDg
ERRORS:
ERROR 1: Cannot find exact (case sensitive) match for 'Videowriter'
Step to rectify: It is case sensitive function, so changed 'Videowriter' to 'VideoWriter'
ERROR 2: Unrecognised function or variable 'writevedio'
Step to rectify: It is case sensitive function, so changed 'writevideo' to 'writeVideo'
ERROR 3: Used first loop variable 'i' instead of second loop variable 'j' while calculating end coordinates of link 2 (X2, Y2)
The output animation incase of this error is given in the following link
https://youtu.be/aqYiLxjd1_M
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
RANKINE CYCLE SIMULATOR
OBJECTIVES: 1. To calculate the state points of the Rankine Cycle based on user inputs. 2. To plot corresponding T-s and h-s Diagrams. INTRODUCTION TO RANKINE CYCLE: The Rankine cycle or Rankine Vapor Cycle is the process widely used by power plants such as coal-fired power plants or nuclear…
12 Apr 2021 08:54 PM IST
PARSING NASA THERMODYNAMIC DATA
OBJECTIVES: 1. To write a function that extracts the 14 temperature co-efficients and calculate properties like molecular weight, enthalpy, entropy and specific heats for all the species in the given data file. 2. to plot the and save enthalpy, entropy and specific heats for the local temperature range spacific…
07 Apr 2021 10:23 AM IST
GENETIC ALGORITHM TO OPTIMISE THE STALAGMITE FUNCTION
OBJECTIVE: To optimise the stalagmite function and find the global maxima of the function. INTRODUCTION: Optimization is the process of making something better. It refers to finding the values of inputs in such a way that the 'best' output values are obtained. The definition of 'best' varies from problem…
01 Apr 2021 10:43 AM IST
CURVE FITTING USING MATLAB
OBJECTIVES: 1. To write codes for linear and cubic polynomial fit for given Cp data. 2. To plot the linear and cubic fit curves along with the raw data points. 3. To write a code to show splitwise method. 4. To measure the fitness characteristics for both the curves. CURVE FITTING: Curve fitting is one of the most…
28 Mar 2021 09:23 AM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.