Project

Simulation of Robotic arm using MATLAB

  • Aim:- To simulate the forward kinematics of a 2R Robotic Arm.
  • Procedure/Explaination of the code:-

In this project, the kinematics of 2R robotic arm is carried out in order to determine the workspace of the robot.

1. Inputs:-

The length and the angle with repect to the positive x axis of both the links are specified and a counter value is initially set to 1 in order to save the frames for the movie.

The 1x10 matrix is used to store different values of `theta_1` and `theta_2`.

Length of arm 1 (`L_1 = 1 metre`)

Length of arm 2 (`L_2 = 0.5 metre`)

`theta_1 = 0,10,20,......,90`

`theta_2 = 0,10,20,......,90`

%Inputs

L1 = 1;                                 %Length of 1st arm    
L2 = 0.5;                               %Length of 2nd arm
theta1 = linspace(0,90,10);             %Angle of 1st arm wrt x-axis
theta2 = linspace(0,90,10);             %Angle of 2nd arm wrt x-axis

ct = 1;                                 %Set counter to 1 to save movie

2. Calculations:-

Point 1 of link 1 is `≡(0,0)` and point 2 of link 1 can be simply calculated by using

`x_1 = L_1 cos(theta_1)`

`y_1 = L_1 sin(theta_1)`

Similarly for link 2, the end point can be calculated by using

`x_2 = x_1+L_2 cos(theta_2)`

`y_2 = y_1+L_2 sin(theta_2)`

The link can then be plotted on the graph using plot command with linewidth of 3.

Now, for one position of link 1 , link 2 can have multiple positions,therefore, two 'for loops' are used and a variable 'M' captures the motion of links using 'getframe' command. Every time just before the execution of for loop, the value of counter(ct) is incremented by 1 and the process continues until it satisfies the specified condition.

ct = 1;

for i=1:length(theta1)
    THETA1 = theta1(i);
    
    for j=1:length(theta2)
    THETA2 = theta2(j);
    x0 = 0.1;
    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 2 0 2])
    pause(0.3)
    M(ct) = getframe(gcf)
    ct = ct+1;
    
    
    end
    
end

3. Movie creation:-

As soon as the value of i becomes 10. the for loop runs for the last time and the execution of for loop stops .The 'movie' command is then used to create a movie from the values obtained from the variable 'M'. The 'VideoWriter' command is used to create a new file in which the animations will be stored. The 'open' command then opens the video for writing and 'writeVideo' writes the values obtained from the variable 'M'. Finally, 'close' command closes the file after the writing is done.

movie(M)
video = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(video)
writeVideo(video,M)
close(video)
  • Errorr Faced/Steps taken to overcome the error:-

Error 1:- The 'movie' command was written incorrectly with M as lowercase m.

The error was rectified by correcting movie as Movie.

  • Screenshot of the error:-
  • Output:-

1. Plot:-

2. Output values

  • Animation Link:-

https://youtu.be/l5T7irV-Gv8

Entire Code:-

%Inputs

L1 = 1;                                 %Length of 1st arm    
L2 = 0.5;                               %Length of 2nd arm
theta1 = linspace(0,90,20);             %Angle of 1st arm wrt x-axis
theta2 = linspace(0,90,20);             %Angle of 2nd arm wrt x-axis

ct = 1;                                 %Set counter to 1 to save movie

for i=1:length(theta1)
    THETA1 = theta1(i); 
    
    for j=1:length(theta2)
    THETA2 = theta2(j);
    x0 = 0.1;                             
    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 2 0 2])
    pause(0.3)
    M(ct) = getframe(gcf)
    ct = ct+1;
    
    
    end
    
end

movie(M)
video = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(video)
writeVideo(video,M)
close(video)



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.