2R Robotic Arm Challenge
2R Robotic Arm Challenge
AIM : Write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm.
OBJECTIVE :
Step-wise explanation of the code.
Create an animation file of the plot.
ROBOTIC ARM :
A robotic arm 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 kinetic chain. The terminus of the kinematic chain of the manipulator is called the end effector and it is analogous to the human hand.
INPUTS:
1. Length of links
length of link (l1) is l1 = 1
length of link (l2) is l2 = 0.5
2. Co-ordinates X and Y of link 1 & 2
(X0,Y0):
The starting point of the link 1 is attached to the base as the base is fixed, so the starting point of the link 1 cannot move as it is in fixed position so the value of 'X' and 'Y' will be '0'.
X0=0
Y0=0
(X1,Y1):
The ending point of the link 1 is the starting point of the link 2. The length of link 1 is l1 and the angle between the base and link 1 is represented by an angle `theta_1`.
`therefore` X1 and Y1 will be
X1=l1*cosd(`theta_1`)
Y1=l1*sind(`theta_1`)
(X2,Y2):
It is the ending point of link 2. The angle between link 1 and link 2 It is represented by an angle `theta_2` and length of link 2 is l2. The equation x and y will be
X2=X1+l2*cosd(`theta_2`)
Y2=Y1+l2*sind(`theta_2`)
CODE:
% Forward kinematics of a 2R Robotic Arm Manipulator
clear all
close all
clc
% Inputs
% Link lengths
l1 = 1; % Assuming a length of link 1 as 1m
l2 = 0.5; % Assuming a length of link 2 as 0.5m
% Angle between the links
theta1 = linspace(0,90,40); % Angle of traverse of first link
theta2 = linspace(0,90,40); % Angle of traverse of second link
ct=1;
for i = 1:length(theta1)
THETA1 = theta1(i)
for j = 1:length(theta2)
THETA2 = theta2(j)
X0 = 0; % x coordinate of the start point of link 1
Y0 = 0; % y coordinate of the start point of link 1
X1 = l1*cosd(THETA1); % x coordinate of the end point of link 1
Y1 = l1*sind(THETA1); % y coordinate of the end point of link 1
X2 = X1+l2*cosd(THETA2); % x coordinate of the end point of link 2
Y2 = Y1+l2*sind(THETA2); % y coordinate of the end point of link 2
% Plotting
plot([X0 X1], [Y0 Y1],[X1 X2], [Y1 Y2],'linewidth',3) % plotting the x and y axis coordinates
axis([-0.1 1.5 0 1.5]) % plotting the x and y axis coordinates
pause(0.03) % pause time for each frame
M(ct) = getframe(gcf); % for creating the animation
ct=ct+1; %frame of increment 1
end
end
movie(M)
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Step-wise explanation of the code:
1. Matlab code is started with clear all, close all, clc command
clear all : This command is used to clear all the data stored in the workspace.
close all : This command is used close all figures and close plots.
clc : This command clears the command window.
2. The input value for `theta_1` and `theta_2` is determined with a command linspace in this we create a degree of freedom from 0 to 90 with 10 values between the degree of freedom.
3. Then initialize the counter command using ct.
4. To move the robotic arm continuously we are using for loop.
for i = 1:length(theta1)
THETA1 = theta1(i)
for j = 1:length(theta2)
THETA2 = theta2(j)
The value of 'i' is going to change from 1 to length of `theta_1`and value of 'j' is going to change from 1 to length of `theta_2`. Here, as the values of 'i' & 'j' changes `theta_1` and `theta_2` are taken from original arrays and are assigned to variables 'THETA1' and 'THETA2' which contains only one value and not an array, loop counter variables are used to access particular value. For each value of `theta_1` i.e each position of link 1, link 2 can take multiple positions depends on `theta_2`.
5. Then enter the values of co-ordinates ([X0 X1], [Y0 Y1],[X1 X2], [Y1 Y2]).
6. Then plot the values of ([X0 X1], [Y0 Y1],[X1 X2], [Y1 Y2]) with the 'linewidth',3.
7. Then axis command is used for fixing axes - changing x-limits and y-limits.
8. Then use pause command to slow down the output.
9. Then we use getframe command to get individual frame and stored in variable M .
10. Then incrementing the counter value by 1.
11. The end command is used to end the for loops, this terminates the set of instructions.
12. using movie command we can create a movie with the help of individual frame.
13. Using the different individual frames a new uncompressed avi file is created using videofile command.
14. Then we are opening the videofile using the open command and closing the videofile using the close command.
ANIMATION :
ERRORS :
1. Case sensitive error
solution - replaced Movie with movie
2. Case sensitive and assigning variable
solution - writevideo replaced with writeVideo
Assigned the variable M with videofile - writeVideo(videofile,M)
3. Animation frames were not getting stored
solution - 'uncompressed AVI' corrected to 'Uncompressed AVI'