2R Robotic Arm Simulation using MATLAB
R Robotic Arm - Forward Kinematic Motion
Here 2R stands for "Two Rotational" Joints for an two link Kinematic Chain.
Let us consider,
length of link 1, l1=1 unit
length of link 2, l2=0.5 unit
To find the workspace/final position of the above defined Robot, we need to find the positions of link1 and link2 coordinates and positions ie; `theta1 and theta2`
`(x0,y0)=(0,0)` {1}
{2}
{3}
{fig 1}
The simulation code is performed via OCTAVE and enclosed below,
CODE:
% forward kinematics of 2R robotic arm manipulator
clear all
close all
clc
% inputs
l1=1;
l2=0.5;
theta1=linspace(0,70,10);
theta2=linspace(0,110,10);
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 1.5 0 1.5])
pause(0.03)
ct=ct+1;
end
end
CODE EXPLANATION
The matlab code begins with the clear all, close all, clc to clear the other computed data in the command window as well in workspace to avoid overwriting or manipulating the required values.
%inputs
(l1 &l2) : The Robtic arm which considered here in this problem has two links whose length are taken as 1 & 0.5 respectively.
(theta1 & theta2) :Since these are connected through the Rotational Joints, they can be constrained with an angle, which in my case the lower arm (link1) is limited to 70 degree and upper arm (link2) to 110 degrees wrt. Horizontal plane
linspace is used to define the number of actions to be performed within the limits of defined angle theta, also the simulation smoothness is proportional to linspace value.
ct stands for counter, which here in this program it refers to the 1st iteration of inputs ct=1,
ct=ct+1 at the end supports the next set of iterations untill the last to simulate.
The for loop has defined to support the iterations ie; for every value of theta1 and theta2, there exists a new function THETA1 and THETA2 respectively
Now, THETA1 & THETA2 are new set of values (10 each defined in l"inspace") in the defined limits of 0degree-70degree and 0degree-110degree
for every value of THETA for loop performs simulation and ct provides the source of next iteration upto last value.
%coordinates
The coordinates for the lower arm/link1 is considered as initial position followed by which the coordinates for the @Joint and the @End is calculated as shown in (fig1.)
The Trignometric functions by default takes the inputs as 'radians', to change it to degrees 'cosd' &'sind' are used.
%plotting
The plot command helps to plot the simulated value in a 2D graph,
The 1st two set of values in plot command defines the link1, whereas the next 2 set of values defines the link2,
"linewidth" is used to assign the thickness (3) to the links,
axis is used to define the limiting values on both axis of the graph to have a simulation well within the frame, [Xa Xb Ya Yb]
The pause command is used to have a interval between frames (0.03s)
the for loop is terminated using end command.
The program is executed and no errors were found
The values of inputs, coordinates and math are obtained and as follows
But the plot command in the above program has taken the "linewidth" only for the link2, whereas the link1 value has been taken by default (refer fig. below)
This was fixed by editing the plot comand as follows,
CODE CORRECTION
plot([x0 x1],[y0 y1],'linewidth',3,[x1 x2],[y1 y2],'linewidth',3)
This program was also executed for shorter values in "linspace" command and using "sprintf" & "saveas" in OCTAVE, different frames were obtained which were later stiched together to get an .gif file.
...
...continued
%plotting
plot([x0 x1],[y0 y1],'linewidth',3,[x1 x2],[y1 y2],'linewidth',3)
axis([-0.1 1.5 0 1.5])
pause(0.3)
text=sprintf('test%d.png',ct);
saveas(gca,text);
ct=ct+1;
end
end
The .gif file of compiled frames is enclosed below