Forward Kinematics Of 2-R Robotic Arm In 3-D
AIM – To simulate the forward kinematics of a 2-R Robotic Arm in Matlab
Introduction -
Robotic Arm – It is a mechanical device which can easily be programmed to perform different tasks. It mainly consists of links which are connected by joints where the motor would be present .At the end an end-effector would be present , which will interact with the environment .
The links and the joints allow the arm to have , a rotational or translation displacement.
Governing Equations :
1.Above diagram shows the links and joints of the robotic arm.
2.”theta1” is the angle formed by the link 1 with base . “theta2” is the angle formed by link2 with a datum plane
3.The co-ordinates of base position of link 1 is “(x0,y0)” . The co-ordinates of the upper point of link1 is the base point of link2 which is – “(x1,y1)” .The top most point of the link2 is “(x2,y2)”.
4.let the length of link1 be l1 and the link2 length be l2.
5. value of the co-ordinates "(x0,y0)' -
x0 = 0
y0= 0
5 value of the co-ordinate “(x1,y1)” –
X1 = l1×cos(θ1)
Y1 =l1×sin(θ1)
6.value of the co-ordinate “(x2,y2)” -
X2 = x1 + l2×cos(θ2)
Y2= y1 + l2×sin(θ2)
7.Above equation will be used for simulation in matlab .
Objective –
- To simulate forward kinematics of simple 2R Robotic arm Matlab
- To plot the arms at different angles in graph.
- To create an animation video for the robotic arm simulation .
- To simulate simple code for 2R Robotic arm for re-tracing its path backwards in Matlab
- To represent the simulation using plot3 in 3-D space.
Code :
clear all
close all
clc
%inputs For the arms
l1=1; % setting the value of l1
l2= 0.5; % setting the value of l2
theta1 = 45 ; % setting the value of angles
theta2 = 30 ;
% co-ordinates
x0 =0;
y0= 0;
x1 = l1*cosd(theta1); % cosd()and sind() is used to calculate the angle using degree.
y1 = l1*sind(theta1);
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 -
1.The first three lines are used to clear all the previously present variables and also to clear the screen using "clc"
command .
2. In the next following lines the input for the code is taken .The value for "theta1" and "theta2" is initialized.
3.The values of the co-ordinates calculated using the equations mentioned above.
4.Using a line command both the links are represented .Also linewidth and color command is used to differentiate the links .The blue color line is link-1 and red color line is link-2 .
Conclusion -
- The graph can be plotted for different angles of "theta1" and "theta2" .
- A " FOR LOOP" can be used for the above code .
Improved Code 1 -
clear all
close all
clc
%inputs For the arms
l1=1; % setting the value of l1
l2= 0.5; % setting the value of l2
theta1 = linspace (0,90,10) ; % setting the value of angles using linspace
theta2 = linspace(0,60,10) ;
% co-ordinates
for i = 1: length (theta1)
THETA1 = theta1(i) ;
for j = 1: length(theta2)
THETA2 = theta2(j);
x0 = 0;
y0 = 0; %%base position os the arm
x1 = l1*cosd(THETA1); %% Position of the first link
y1 = l1*sind(THETA1);
x2 = x1+l2*cosd(THETA2); %% position of the second link
y2 = y1+l2*sind(THETA2);
% plotting
line([x0 x1] ,[y0 y1] , 'linewidth',3,'color','b');
line([x1 x2] ,[y1 y2] , 'linewidth',3,'color','r');
end
end
Explanation -
- In the following the changes are made . The "linspace" command is used to assign the values of "theta1" and "theta2" . The command has a starting value and an ending value also the interval which is required between ,the starting and ending values. For "theta1" - (0,90,10) - here the range is between 0 to 90 with a difference of 10. The value for "theta2" -(0,60,10) .
- A " NESTED FOR LOOP" is used at the start of main code . The loop is used to assign the different values of "theta1"and "theta2" specifically to "THETA1" and "THETA2" , which is used for code.
- The variables "i" and "j" are the loop counters which has a range till the length of "theta1"and"theta2".
- Whenever the loop gets completed the value of "i" and"j" will increase this will help to change the values of "THETA1" and "THETA2" .
- The different position of the links are calculated using the above equations where "THETA1" and "THETA2" is used to calculate as its value is keep on changing .
- At last "end" statements are used to close the "FOR LOOP" .
Conclusion -
- The variation of the angles have been seen.
- The use of line command give an output like the above which is not appropriate.All the position of the lines are shown .
- Simulation of the links is not seen which was the AIM .
Improved Code 2 -
clear all
close all
clc
%inputs For the arms
l1=1; %% all the necessary inputs for the code
l2= 0.5;
theta1 = linspace(0,180,20);
theta2 = linspace(0,30,10);
%Code for Forward kinematics
for i = 1: length (theta1)
THETA1 = theta1(i) ;
for j = 1: length(theta2)
THETA2 = theta2(j);
x0 = 0;
y0 = 0; %%base position of the arm
x1 = l1*cosd(THETA1); %% Position of the first link
y1 = l1*sind(THETA1);
x2 = x1+l2*cosd(THETA2); %% position of the second link
y2 = y1+l2*sind(THETA2);
% plotting Of the 2R robotic arm Using plot command
plot([x0 x1 ],[y0 y1 ], [x1 x2], [y1 y2] ,'linewidth',3 );
axis([-1.7 1.8 -1 1.8 ]);
xlabel('x-axis');
ylabel('y-axis');
pause(0.01); % To pause for a certain time to view the position of links
end
end
Explanation -
- In the above code the change which is made is the use of "plot" command is used to represent the links in graph. It is used to show the simulation in aproper manner
- An axis is defined , where the x co-ordinates and y co-ordinates are specified using the command "axis".
- The labels are given to the plot mentioning the x-axis and y-axis .
- A pause is used to represent the simulation with a pause of a specific time .
NOTE - The above picture is an image from the simulation .
Conclusion -
- The movie for the simulation needs to be created .
Improved Code 3 -
clear all
close all
clc
%inputs For the arms
l1=1;
l2= 0.5;
theta1 = linspace(0,180,20);
theta2 = linspace(0,30,10);
ct=1; % represents number of frames
%Code for Forward kinematics
for i = 1: length (theta1)
THETA1 = theta1(i) ;
for j = 1: length(theta2)
THETA2 = theta2(j);
x0 = 0;
y0 = 0; %%base position os the arm
x1 = l1*cosd(THETA1); %% Position of the first link
y1 = l1*sind(THETA1);
x2 = x1+l2*cosd(THETA2); %% position of the second link
y2 = y1+l2*sind(THETA2);
% plotting Of the 2R robotic arm Using plot command
plot([x0 x1 ],[y0 y1 ], [x1 x2], [y1 y2] ,'linewidth',3 );
axis([-1.7 1.8 -1 1.8 ]);
xlabel('x-axis');
ylabel('y-axis');
pause(0.01);
M(ct)=getframe; %getframe command helps to get each position picture
%seperately to a variable M
ct=ct+1;
end
end
mywriter = VideoWriter('FW_kinematics_animation') ; % creating a simple variable to help
% in vedio writing animation
open(mywriter); % to open the movie file
writeVideo(mywriter,M); % write to the file and storing the images
close(mywriter); % close the file when done .
Explanation -
- In the above code for making the video for the simulation the code has been improved.
- a variable "ct" is introduced at the begining of the loop with initial value as 1.
- The variable is used to get the different frames during the simulation using "getframe" command
- The frame at a position is saved to an array "M" which is then used to create vedieo.
- The value of"ct" is then incremented by 1 to move to the next frame .
- After closing the loop , a variable "mywriter" is used to acess the vedieo file made using the command "VideoWriter" .
- The file is then opwned and then the different frames are placed in it using "M" variable .
- After writing to file it is then closed .When the code runs at the end a "mywriter" variable would be formed and you will see the video file with the name given above with ".avi" extension.
Conclusion -
- The above code can be improved to show a simple code for Inverse kinematics of 2R robotic arm
- Instead of using plot command , a "plot3" command can be used to represent in 3-d.
Improved Code 4 -
clear all
close all
clc
%inputs For the arms
l1=0.5;
l2= 0.4;
l3= 0.01;
theta1 = linspace(0,360,10);
theta2 = linspace(-20,30,10);
ct=1;
%Code for Forward kinematics
for t_1 = 1:2 %% General loop to govern overall duration of programe
if t_1==2 %% condition for a simple inverse kinematics motion
theta1 = linspace(360,0,10);
theta2= linspace(30,-20,10);
end
for i = 1: length (theta1)
THETA1 = theta1(i) ;
for j = 1: length(theta2)
THETA2 = theta2(j);
x0 = 0;
y0 = 0; %%base position os the arm
z0= 0 ;
x1 = l1*cosd(THETA1); %% Position of the first link
y1 = l1*sind(THETA1);
z1 = 0.03;
x2 = x1+l2*cosd(THETA2); %% position of the second link
y2 = y1+l2*sind(THETA2);
z2 = z1+0.01 ;
% plotting Of the 2R robotic arm Using plot3 command
plot3([x0 x1 ],[y0 y1 ],[z0 z1 ],[ x1 x2 ],[ y1 y2 ],[ z1 z2],'linewidth',2);
axis([-1.7 1.8 -1 1.8 ]);
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
pause(0.03);
M(ct)=getframe(gcf); %getframe command helps to get each position picture
%seperately to a variable M
ct=ct+1; % incrementing the postion by 1.
end
end
end
mywriter = VideoWriter('FW_kinematics_animation_12 ') ; % creating a simple variable to
help in vedio writing
open(mywriter); % to open the movie file
writeVideo(mywriter,M); % write to the file and storing the images
close(mywriter); % close the file when done .
Explanation -
- In the above code a general "FOR LOOP" is used so as to govern both the forward and inverse kinematics .
- The loop count variable used is "t_1" whose initial value is 1 .
- The loop condition is " for t_1 = 1:2" , here the value of t_1 starts at 1 and when the loop end its value changes to 2 , thus the loop runs 2 times .
-
Also an "if " condition is used after the first loop. As the value of the theta would not be same during the inverse kinematics , it had to be changed . Thus an "if" statement is used to verify the condition when "t_1 =2" . Then the value of "theta1" and "theta2" would change respectively using "linspace " command with different conditions .
-
Also a "plot3" command is used . It is mainly used to show the links movement in three dimension .The points are written in one command itself .
-
Also with "plot3" command a "z" variable is introduced .Whose value is kept constant as it signifies the height in 3-d.
7. This was the error that was seen many times the command "VideoWriter" is case sensitive
NOTE - The above picture is an image from the simulation .
Conclusion -
- The 2R robotic arm is visualized in the 3-d space succesfully using matlab .
- The code can be modified for better simulation .
Video Link for Above code -
https://youtu.be/6DuBe9mQ8VY
Thank You .