2R Robotic Arm Challenge
AIM: To simulate the forward kinematics of a 2R Robotic Arm
GOVERNING EQUATIONS:
for First Arm:
`x1 = l1*cos(theta1)`
`y1 = l1 * sin(theta1)`
For Second Arm:
`x2 = x1 + l2*cos(theta2)`
`y2 = y1 + l2*sin(theta2)`
Where,
`(x1,y1) implies` end points of First Arm
`(x2,y2) implies` end points of Second Arm
`l1` is the length of First Arm in 'm'
`l2` is the length of Second Arm in 'm'
`theta1` is the angle made by First Arm with horizontal in degree
`theta2` is the angle made by Second Arm with horizontal in degree
OBJECTIVE:
1. To write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm.
2. Create an animation file of the plot
MATLAB CODE:
% Forward Kinematics of a 2R Robotics Manipulator
clear all
close all
clc
% Inputs
l1 = 1;
l2 = 0.7;
theta1 = linspace(0,90,30);
theta2 = linspace(0,90,30);
ct = 1;
for i = 1:length(theta1)
Theta1 = theta1(i);
for j= 1:length(theta2)
Theta2 = theta2(j);
% Coordinates
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 2 0 2]);
xlabel('x-axis');
ylabel('y-axis');
title('Forward Kinematics of a 2R Robotic Arm');
grid on;
pause(0.03)
M(ct) = getframe(gcf);
ct = ct +1;
end
end
%Animation Recording
movie(M)
videofile = VideoWriter('robotics.mp4', 'MPEG-4');
open(videofile);
writeVideo(videofile,M);
close(videofile);
1. linspace() is used to create an array of `theta1 and theta2` having equal intervals in its elements.
2. sind() and cosd() are used instead of sin() and cos() as `theta` values are in degree.
3. getframe(), captures the current axes as it appears on the screen as a movie frame.
4. movie(), plays the movie defined by a matrix whose columns are movie frames produced by getframe().
5. VideoWritter(), creates a video file.
6. open(), opens a specified file.
7. writeVideo(), writes video in the specified file.
8. close(), closes a specified file.
ERROR ENCOUNTERED:
This error is encountered because I did not initialize ct.
Steps were taken to overcome the error: Initialized ct = 1 at line number 12.
ANIMATION: