All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
In this project, a simple code to create an animation of a 2R robotic arm will be created in MATLAB. A 2R robotic arm consists of a base, two arms and two rotational joints. Arm 1 is attached with the help of a rotational joint. Arm 1 makes an angle of θ1 with the horizontal axis and has a length of L1. The other…
Dushyanth Srinivasan
updated on 24 Jul 2022
In this project, a simple code to create an animation of a 2R robotic arm will be created in MATLAB.
A 2R robotic arm consists of a base, two arms and two rotational joints. Arm 1 is attached with the help of a rotational joint. Arm 1 makes an angle of θ1 with the horizontal axis and has a length of L1. The other end of arm 1 contains another rotational joint, which houses the end of arm 2. Arm 2 makes an angle of θ2 with the horizontal axis and has a length of L2. A simplified illustration is below [1]:
The fixed end of arm 1 has the coordinates x0, y0, the moving end of arm 1/rotating joint 2 has the coordinates x1, y1, while the furthest end of arm 2 has the coordinates x2, y2.
The goal of this project is to calculate x0, y0, x1, y1,x2,y2, and to create a pleasing animation of the area of effect of the robotic arm.
Using basic trignometry, it can be calculated:
x1=x0+L1⋅cosθ1
y1=y0+L1⋅sinθ1
x2=x1+L2⋅cosθ2
y2=y1+L2⋅sinθ2
Code (with explanation)
clear all
close all
clc
% input data
l1 = 1; % length of arm 1 (m)
l2 = 0.5; % length of arm 2 (m)
theta1 = linspace(0, 90, 31); % angles of rotation for arm 1, starting from 0 to 90 in increments of 3 degrees
theta2 = linspace(0, 90, 31); % angles of rotation for arm 2, starting from 0 to 90 in increments of 3 degrees
% base of arm 1 (origin)
x0 = 0; y0 = 0;
% counter variable to count number of frames for animation
ct = 1;
for i = 1:length(theta1)
% for loop to calculate end points of arm 1 for each angle of arm 1
x1 = x0 + l1 * cosd(theta1(i));
y1 = y0 + l1 * sind(theta1(i));
for j = 1:length(theta2)
% for loop to calculate end points of arm 2 for each angle of arm
% 2, based on arm 1's current end points
x2 = x1 + l2*cosd(theta2(j));
y2 = y1 + l2*sind(theta2(j));
% plotting the arms
figure(1)
% plotting arm 1 with red colour, and a line width of 3 in the
% prescribed axis
axis([-0.1 1.6 0 1.6])
plot([x0 x1],[y0 y1],'linewidth',3,'color','r')
hold on % to prevent erasure of previous plot
% plotting arm 2 with blue colour, and a line width of 3 in the
% prescribed axis
axis([-0.1 1.6 0 1.6])
plot([x1 x2],[y1 y2],'linewidth',3,'color','b')
% delay of 0.03 to create a smooth animation
pause (0.03)
% storing current figure frame into variable M at counter variable
% location
M(ct)= getframe(gcf);
% incrementing counter variable so M can store current figure frame
% for next iteration
ct=ct+1;
% clearing current figure so next iteration figure can be plotted
% on a clear figure
clf
end
end
% creating variable videofile with file name and file format
videofile = VideoWriter('foward_kinematics.avi','Uncompressed AVI');
% opening videofile, and writing figure frames for each iteration stored in
% variable M into videofile and closing videofile.
open(videofile)
writeVideo(videofile,M)
close(videofile)
Output
Animation: https://youtu.be/1y4YOaeHwR0
Errors Encountered
Steps taken to fix errors
Movie(M) was removed since the default MATLAB video player displayed the video at an abysmal frame rate, causing the video to play for a long time. Instead, the generated animation was viewed in a separate video player.
References
1. A Two-Link Robot Manipulator: Simulation and Control Design by Mahboub Baccouch and Stephen Dodds http://doi.org/10.35840/2631-5106/4128
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...
Project 2 - Rankine cycle Simulator
In this project, I will be writing code in MATLAB to simulate a Rankine Cycle for the given parameters. A Rankine Cycle is an ideal thermodynamic heat cycle where mechanical work is extracted from the working fluid as it passes between a heat source and heat sink. This cycle or its derivatives is used in steam engines…
04 Sep 2022 12:52 PM IST
Project 1 - Parsing NASA thermodynamic data
In this project, I will be parsing a data file prepared by NASA. The contents of the data file can be used to generated thermodynamic properties such as Specific Heat at Constant Pressure 'C_p' (J/(kg.K)), Enthalpy HorQ (J) and Entropy S (J/(kg.mol)) at various temperatures. The files will be parsed in MATLAB…
31 Aug 2022 01:07 PM IST
Week 5 - Genetic Algorithm
In this project, I will be generating a stalagmite function in MATLAB and find the global maxima of the function using Genetic Algorithm. A stalagmite function is a function which generates Stalactites, which are named after a natural phenomenon where rocks rise up from the floor of due to accumulation of droppings of…
29 Aug 2022 07:55 AM IST
Week 4.1 - Solving second order ODEs
In this project, I will be writing code in MATLAB to solve the motion of a simple pendulum. A simple pendulum motion's depends on Newton's Second Law. The equation which governs the motion of a simple pendulum is (with damping) d2θdt2+bmdθdt+gLsinθ=0 Where, θ is the angular displacement…
23 Aug 2022 08:06 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.