Week 2- 2R Robotic Arm Challenge
AIM : To write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm.
PRE-REQUISITE KNOWLEDGE :
A 2R robotic arm has two links and two joints, each having 1 degree of freedom. So, the DOF of this robot is 2 DOF.
R stands for Revolution joint.
Example - Human Arm, where the elbow is the revolutionary joint.
MAIN CODE :
- Pre-define the length of the link 1 and link 2.
- Create two arrays of angles (01 and 02) from 0 to 90 degrees with 10 angles each using the linspace command.
- Pre-define the origin coordinates (x0, y0) = (0.0).
- Initiate the counter (ct = 1).
- Initialize the angles as theta 1= 0 and theta 2 = 0. These are the angles between link-1 and base; and link-2 and base, respectively.
- Initialize the for loop:
- Initiate i from 1 to the length of o1 matrix which is 10. So the values of i will change from 1 to 10 for every loop
- Calculate the co-ordinates of joint 2 (x1, y1) using the kinematic formulae: x1 = l1*cosd(Theta1) and y1 = l1*sind(Theta1) for every value of Theta
- Initiate j from 1 to the length of o2 matrix which is So the values of I will change from 1 to 10 for every loop iteration.
- Calculate the co-ordinates of joint 3 (x2. y2) using the kinematic formulae: x2 = l2*cosd(Theta2) and y2 = l2*sind(Theta2) for every value of Theta2.
- Plot the graph by inputting all the coordinates, (x0, x1), (y0, y1), (xl, x2), (y1, y2). In this manner so that all the plots appear in the same window rather than creating multiple plot
- Set time between the Lower the time, shorter will be the video.
- Feed the frames obtained into an array M(ct) using the following command: M(ct) = getframe(gcf); New images will be added to the consecutive positions in the array as the value of ct is incremented for every loop iteration.
- End the loop.
%forward kinematics for 2R robotic arm manipulator
clear all
close all
clc
%inputs
l1 = 2;
l2 = 1;
theta1=linspace(0,90,5);
theta2=linspace(0,90,5);
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 4 0 4])
pause(0.3)
m(ct) = getframe(gcf);
ct = ct+1;
end
end
movie(m)
videofile = VideoWriter('forward_kinematics_test.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,m)
close(videofile)
RESULT :
The program was executed.
https://youtu.be/0rKkEKhFKWI