Week 2- 2R Robotic Arm Challenge
Matlab to simulate the forward kinematics of a 2R Robotic Arm
Assumptions:
We will take a simple Robotic arm with two links for our programming. Let l1=1m & l2 = 0.5m be two links of
the arm which makes theta1 and theta2 angle with the horizontal axis respectively.
Co-ordinates of Link L1:
Starting point = [x0, y0] or [0, 0]
Ending point = [x1, y1] = [l1 cos(theta1), l1 sin(theta1)]
Accordingly,
Co-ordinates of Link l2:
Starting point = ending point of l1 = [x1, y1] = [l1 cos(theta1), l1 sin(theta1)]
Ending Point = [x2, y2] = [x1 + l2 cos(theta2), y1 + l2 sin(theta2)]
Now, to obtain motion of the robotic arms at different positions, we have to take different vaues of theta.
Coding:
% Program to depict the forward kinematics of a 2R Robotic Arm with animation
clear all
close all
clc
%Inputs
%Lengths of two arms in m
l1 = 1;
l2 = 0.5;
%Angle of both the links with the horizontal axis
theta1=linspace(0,90,15);
theta2=linspace(0,90,15);
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",4)
axis([-0.1 1.5 0 1.5])
pause(0.03)
title("Kinematics of 2R Robotic Arm")
xlabel("arm movement in x-axis")
ylabel("arm movement in y-axis")
grid on
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)
Output:
Explanation of the commands used:
Input Command:
First things need to consider is input which we have assumed and the boundries,
In program I have given the input values are
l1 = 1m
l2=0.5
theta1 = linspace(0,90,15);
theta1 = angle varies from 0 to 90 equally seperated by 15 values
Similarly,
theta2 = linspace(0,90,15);
theta2 = angle varies from 0 to 90 equally seperated by 15 values
For Loop Command:
'For Loop' is used to define a certain repeated output specific or desired number of times in MATLAB.
Plot Command:
It is used to plot the output or the result on the graph. In this command we define different axes with respect to the values or the variables used in the program.
Movie Command:
It used to make a small clip or the animation of the output with proper interval by using 'pause' command.
It basically provides clean and clear representation of multiple output frames in a single frame or video.