Week 2- 2R Robotic Arm Challenge
Aim: To write a program in MATLAB to simulate the forward kinematics of a 2R robotic arm.
To create an animation file of the plot.
Program:
%Forward kinematics of a 2R robotic arm manipulator
clear all
close all
clc
%Inputs
l1 = 1;
l2 = 0.5;
theta1 = 45;
theta2 = 30;
%Base of the robot
x0 = 0;
y0 = 0;
%link1,link2 connector
x1 = l1*cosd(theta1);
y1 = l1*sind(theta1);
%Coordinates of the manipulator
x2 = x1+l2*cosd(theta2);
y2 = y1+l2*sind(theta2);
%Plotting
line([x0 x1],[y0,y1],'linewidth',3,'color','b')
line([x1 x2],[y1,y2],'linewidth',3,'color','r')
Explanation:
From the above code, the forward kinematics of a 2R robotic arm manipulator which has link1,link2 and a manipulator are considered.
- Inputs for the same is given as link 1 =1 and link 2 =0.5
- The angle with respect to the x-axis is represented in terms of theta 1 and theta 2 which is 45 and 30
- Coordinates for the same is set as x0=0 and y0=0 at the point x0,y0
- Similarly for the link 1 and link2 the coordinates are declared as (x1,y1) and (x2,y2)
- Connector of link 1 and link are written in terms of x1=l1*cosd(theta1) and same way for y1=l1*sind(theta1)
- Now connector and the manipulator in terms of the equation as follows x2 = x1+l2*cosd(theta2) and
y2 = y1+l2*sind(theta2) - Finally the plot for the same is obtained.
- In order to obtain the animation, ct is assigned with the value 1.
- Then the file for the same is stored in the M(ct)=getframe(gcf).\
- movie(M) is the command used to get animation in the folder.
- videofile = videowriter('forward_kinematics.avi','uncompressed AVI'); is the format that the plots get stored in the form of animation.
- Open,write and close the file and search for the animation in the desktop.
Error faced:
Error 1 faced was the usage of # instead of % displayed as undefined file type
Error 2 math.pi/4 was undefined
Steps taken to overcome the error:
Replaced the symbol as per the syntax defined
Replaced the math.pi/4 which is the python code to MATLAB cosd format which resulted in error free output with plot.
You tube-link:
Program:
clear all
close all
clc
%inputs
l1=1;
l2=0.5;
ct=1;
theta1 = linspace(0,90,40);
theta2 = linspace(0,90,40);
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.1 1.5 0 1.5])
pause(0.03)
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:
The animation for the 2R robotic arm manipulator is created and uploaded the same in the you tube.