Matlab program to simulate the forward kinematics of a 2R Robotic Arm
AIM : MATLAB program to simulate the forward kinematics of a 2R Robotic Arm.
BACKGROUND : A robotic arm (not robotic hand) is a type of mechanical arm, usually programmable, with similar functions to a human arm. The arm may be the sum total of the mechanism or may be part of a more complex robot. The links of such a manipulator are connected by joints allowing either rotational motion (such as in an articulated robot) or translational (linear) displacement. The links of the manipulator can be considered to form a kinematic chain. The terminus of the kinematic chain of the manipulator is called the end effector and it is analogous to the human hand.
The forward kinematics problem is characterized by determining the final position and orientation of a link (e.g., anatomical landmark on the hand) with knowledge of the joint variables. One can think of the forward kinematics (sometimes called direct kinematics) as a black box that contains the necessary calculations for accepting joint coordinates as input, and producing position and orientation parameters as output, for any of the segmental links in the chain.
Manipulator fig.
In our analysis, we are simulating a 2 link, 3 joints robotic arm with a manipulator at the end effect.
SOLUTION ALGORITHM
- The programming code will be written in MATLAB.
- The angles 1,2 and the lengths of the 2 links will be fed as the input.
- To simulate the movement of the end effector, the angles made by the links, namely, angle1 and angle2 will be varied over a range.
- The coordinates (x0,y0) of point 1 are taken at origin and hence (x0,y0)= (0,0).
- The coordinates of point 2 i.e. (x1,y1) are calculated using the formula,
x1= L1 * cosine(theta 1)
y1= L1 * sine(theta 1) ; theta1 = angle 1
- Similarly, the coordinates of point 3 i.e. (x2, y2) are calculated using the formula,
x2= x1 + L2 * cosine(theta 2)
y2= y1 + L2 * sine(theta 2) ; theta 2= angle 2
- After getting the coordinates the two links will be plotted on the same graph.
- To obtain a simulation, the angles theta1 and theta2 will be varied over a range.
- For each value of theta1, theta2 will be changed over the entire range, to obtain different positions of the manipulator at a particular angle theta1.
- The final output will be obtained in the form of an animation video clip.
MATLAB CODE :
%program to simulate forward kinematics of a 2R robotic arm
%input variables
%theta1 variation over a range of 0 to 90 degrees
theta1= linspace(0,90,10);
%theta2 variation over a range of 0 to 90 degrees
theta2= linspace(0,90,10);
%length of the two links
l1= 1;
l2= 0.5;
%a variable assignment to catch each frame of the plot
ct=1;
%coordinates of point 1
x0= 0;
y0=0;
%loop to calculate the coordinates x1, y1 and x2, y2 for various positions of the arm
%the counter variable will run from 1 to total no of divisions between 0 to
%90 degrees for theta1
for i= 1:length(theta1)
THETA1= theta1(i);
%the counter variable will run from 1 to total no of divisions between 0 to
%90 degrees for theta2
for j= 1:length(theta2)
THETA2= theta2(j);
%calculation of the coordinatesx1, y1 and x2, y2 for each location
x1= l1* cosd(THETA1);
y1= l1* sind(THETA1);
x2= x1 + l2*cosd(THETA2);
y2= y1 + l2*sind(THETA2);
%plotting the coordinates
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3,'color','b')
axis([-0.1 1.5 0 1.5])
%to create a pause between each frame
pause(0.005);
%varibale ct will capture each frame in the loop
M(ct)= getframe(gcf);
%increment of counter variable to capture next frame
ct= ct+1;
end
end
%to create an animation clip out of the plot
movie(M)
%creating a video file with the name kinematic simulator of avi format
videofile= VideoWriter('kinematic_simulator.avi', 'Uncompressed AVI');
%writing the content in the video file
open(videofile)
writeVideo(videofile,M)
close(videofile)
ERRORS:
- The first error occurred due to a typing mistake in the code which was rectified.
2. The second error occurred due to wrong location of increment of counter variable as the video file was unable to capture a frame. It was rectified by incrementing it after the frame was captured.
OUTPUT
The output is obtained in the form of an animated video clip which can be viewed from the youtube link below :
NOTE
- The angles theta1 and theta2 take 10 values each in the range of 0 to 90 degrees.
- The length of the links are taken as 1 m and 0.5 m respectively.
- The coordinates of point1 are taken at origin i.e. (0,0).