Project

Forward kinematics of 2-R Robotic Arm

Animation posted in youtbe and the link is here,https://youtu.be/_Lrs11bMwck

Here, as asked in question to simulate a Forward kinematics of 2R Robot Arm.

The code is as follows, there

  • Inputs were given firstly, lengths of two links, we consider them as l1 & l2 and made up values as 1 and 0.5 respectively
  • Coming to theta vales, we need to create a forward kinematics animation, so we cannot go for single values
  • We can use array to put couple of values rather than that, i used linspace command
  • This commands helps us creating random values of a given range and prescribed set of vales.
  • Examble, as shown in the code, theta1 = linspace(0,90,10);
  • here, 1st value is beginning value of range and 2nd one is ending value of range and last value is for set of values needed.
  • Nextly, we use for loop to make the values automatically without manual operations needed. As, value must differ i each and every case we use it to create a flow.
  • As shown in code, for i=1:length(thate1) here ':' means to a range from 1 to length of theta1. i.e, theta 1 has 10 values. which is from 1 to 10 numberically
  • We do it for theta2 also there use another variable j. concept is same. we equate theta1(i) and theta2(j) values to THETA1 & THETA2 respectively for setting up a variable.
  • Later, we focus on co-ordinates, as known to us origin (0,0) is 1st point of link1 so we name it as x0=0, y0=0. and 2nd end of link will be x1=l1*cosd(THETA1) and y1=l2*sind(THETA2). This is basic math to determine the other value of link when we have angle.
  • d in "sind(THETA2) & cosd(THETA1) is given because to convert the angle into degrees. Basically, matlab considers angle values in radians.
  • Now, 2nd link. The 1st end of link2 will be 2nd end of link1 i.e, x1 and y1 and 2nd end of link2 will be x2 and y2.
  • Here, values of x2 and y2 were taken as x2=x1+l2*cosd(THETA1) and y2=y1+l2*sind(THETA2).
  • The part l2*sind(THETA2) is same as done for link1's 2nd end. as it is 2nd link, we are adding length of 1st link too. therefore, it is y2=y1+l2*sind(THETA2).
  • Later, we use plot command to plot the links and provide animation. The command used for this "plot([x0,x1],[y0,y1], [x1,x2],[y1,y2],'linewidth',3);".
  • We just co-ordinate values firstly, later linewidth to set a width of the link and next value is the number prescribed for width of line, we made up it to 3.
  • Next,we set up a axis to plot the links animation in a prescribed portion of graph.
  • The command used is"axis([-0.1 1.5 0 1.5])". Here, 1st two values are of x-coordinates and next 2 values are of y-coordinates.
  • Next pause command is used to stop the plot so it is showing us the link action from initial stage to final stage in speed of prescibed seconds. we gave 0.03 seconds. The command used is pause(0.03).
  • The code we have done till this operation should be inside for loop. as it has to repeat the operation. end is used to end the loop.
  • Next to create a movie in matlab we use these commands.
  • movie(M)
    videofile=VideoWriter('forward.avi','uncompressed avi')
    open(videofile)
    writeVideo(videofile,M)
    close(videofile)
  • In the line, videofile=VideoWriter('forward.avi','uncompressed avi') we can set up the name and type of video is next which is uncompressed. remaing part is for operations of open, play and close.
  • This is how we code for forward kinematics of a 2-R robotics arm.
close all
clear all
clc

l1 = 1;
l2 = 0.5;

theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
ct=1;
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 = l1*sind(THETA1);

x2 = x1+l2*cosd(THETA2);
y2 = y1+l2*sind(THETA2);



plot([x0,x1],[y0,y1], [x1,x2],[y1,y2],'linewidth',3);
axis([-0.1 1.5 0 1.5])
pause(0.03)
M(ct)= getframe(gcf);

ct=ct+1;
    end
    
end


movie(M)
videofile=VideoWriter('forward.avi','uncompressed avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.