Week 2- 2R Robotic Arm Challenge
Program to simulate the forward kinematics of a 2R Robotics Arm :
clear all
close all
clc
% Input
L1 = 1.5;
L2 = 1;
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,20);
ct = 1
for i = 1:length(theta1);
THETA1 = theta1(i);
for j = 1:length(theta2);
THETA2 = theta2(j);
% Cordinates
x0 = 0;
y0 = 0;
x1 = L1*cosd(THETA1);
y1 = L1*sind(THETA1);
x2 = x1 + L2*cosd(THETA2);
y2 = y1 + L2*sind(THETA2);
% Ploting
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3);
axis([-0.1 3 0 3]);
pause(0.3);
M(ct)=getframe(gcf);
ct = ct+1
end
end
movie(M)
videofile = VideoWriter('forward_kinematics.avi','compressedfile.avi')
open('videofile')
VideoWriter(videofile,M)
close('videofile')
Explaination :
For writing the code to simulate the forward kinematic of 2R Robotics Arm the steps as follows
5
- first write the the inpute required initially ( i.e. L1 = length of 1st link, L2 = length of 2nd link , theta1 =angle of 1st link w.r.t. x-axis and theta2 = angle of 2nd link w.r.t. x-axis ,Here we need to take theta as an array as we need multiple position of theta for simulation)
- for making the theta as array i have used the command i.e. theta1 = linspace(0,90,5) and theta2=linspace(0,90,5): which meansd that the theta will is going to vary from 0 to 90 in 10 position in same increment order.
- In order to achive the repeatative mannar action we need to use for_loop : this command is used to do the repeatative action in program.
- here we have used two for_loops A) one for 1st link ,as it's going to takes multiple postion as theta1 increases , B) and another for 2nd link it's going to take multiple position as theta2 increases .
- In 1st for_loop , i have written the command i = 1:length(theta1); it's basically means that the (i) is going to changes from position 1 to 5 .both the theta will take same number of position .
- In both for_loop i have writen THETA1 = theta1(i) and THETA2=theta2(j) means each time theta1 and theta2 changes the new position will be THETA1 and THETA2 for how many times the theta1 and theta2 is going to change.
- Then after i have written the co-ordinates of points of 2 links i.e. x0 ,y0,x1,y1,x2,y2
- And running the code shown the wrong result in graph as shown in image name 4
- The graph is wrong manner because of theere was one error as shown in the below image name 3, t
- To overcome this errore i have written the cond(THETA1) instead of cosd(THETA1) and then after correcting the mistake the result shown in image name 5
- Then after to animate the result i have written the command plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
- Then taken the counter(ct) to visualize the animation which start from 1
- Give the axis limits by giving the Pause of 0.3 sec .
- To make it movie like animation , i have used the command M(ct) = getframe(gcf) ;Which basically take the every frame created by counter and make it into a movie like animation.
- But for this we need a .avi file where we need to dumb this all ,so i have made file by the name of movie(M)videofile = videowriter ('forward_kinematics.avi',' compressedfile.avi')
- The command open('videofile') : open the and read the code writthen the file name of " videofile"
- command : videowriter ( videofile,M) and
- close('videofile') :After reading and running the code the file will get closed
- For this result the animation is can be seen by clicking the link https://youtu.be/YOLKogzDoLs
- But animation was not smooth so in order to make smooth we need to change the values of thetas position's incrementas theta1 = linspace(0,90,10) and theta2 = linspace(0,90,20)
- Now we can see the resulted animation by cliking the link https://youtu.be/aGSqgtyGbhA