2R Robotic Arm Challenge
Aim: To simulate the forward kinematics of a 2R Robotic Arm Manipulator using basic MATLAB commands.
Process involved
Step 1: The lengths of the links were given as inputs, along with the angular orientations of the links. The start and end points of each link were provided taking the global coordinate system to be the starting point of link 1, taken to be `(x_0,y_0)` as (0,0). This point is fixed on the base of the robot.
Step 2: The endpoints of both links 1 and 2, taken as `(x_1,y_1)` and `(x_2,y_2)` respectively, were found using geometry and provided as the input for various angular positions of the two links, defined by `theta_1`and `theta_2` respectively. Since the animation was performed for various angular positions, the linspace command was used to generate various equally spaced angular positions between `0^@`to `90^@` for each link.
Step 3: In order to obtain a pattern of motion for each angular position, a for-loop was provided, giving the initial iteration as 1 and running it till the length of the `theta_1` array, i.e., 10. A similar for loop was initiated for the `theta_2` array, and this process would be repeated until all the possible combinations of angular positions defined in the two arrays are covered.
Step 4: The two links are then animated using a single plot command, providing a linewidth for better clarity of the animation in the process. Each frame of animation is then saved in a movie file M, and then converted into a video file format, with the video being updated with each frame as the code runs. The video file is finally saved as a .avi file for animation purposes, and thus, an animation showing all the possible angular orientations of the links is shown.
Common mistakes committed, errors obtained and how they are overcome
1. Using incorrect variable names in the code. For example, by typing the wrong name of the variable in the for loop, i.e., using lower case theta1 instead of upper case theta1, the animation results in incorrect link positions.
2. Using cos() function instead of cosd() function. The input for the angular positions must be given in degrees and not radians, or else the output that would be obtained would be incorrect.
3. Not defining the counter variable. The variable 'ct' used in the code has to be defined earlier, or else the code would show an error as follows:
4. Incorrect use of semi-colons or brackets. This is a common error which must be avoided at all costs, as it would quite unnecessary produce outputs for each stage of the code which does not have semi-colons. In the case of bracket errors, which are simply syntax errors, the syntax must be strictly adhered to.
5. The variable ct has to be incremented only after defining the initial frame, which can cause the programme to cause errors for some frames. This can be easily corrected by placing the line defining ct before the line which ensures that the frame is saved.
%Forward kinematics of a 2R robotic arm manipulator
clear all
close all
clc
%Inputs
l1 = 1;
l2 = 0.5;
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
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 = l2*sind(THETA1);
x2 = x1 + l2*cosd(THETA2);
y2 = x2 + l2*sind(THETA2);
%Plotting
plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',2)
axis([-0.1 2 0 1.5])
pause(0.1)
ct = 1;
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)