Challenge

Week 2- 2R Robotic Arm Challenge

A program and a animation video in Matlab to simulate the forward kinematics of a 2R Robotic Arm.

% A program in Matlab to simulate the forward kinematics of a 2R Robotic Arm.
% Input
% Length of 1st link in meters(m).
l1 = 5;
% Length of 2nd link in meters(m).
l2 = 3;
% Angle of 1st link with Positive X axis in degrees.
theta1 = linspace(0,95,10);
% Angle of 2nd link with Positive X axis in degrees.
theta2 = linspace(0,95,40);
ct = 1
for i = 1:length(theta1)
    THETA1 = theta1(i);
   for j = 1:length(theta2)
    THETA2 = theta2(j);

% We are considering the hinge point of link 1 at origin.
% Therefore the co-ordinates of fix point of 1st link will be
x0 = 0;
y0 = 0;
% Second point co-ordinates of 1st link will be
x1 = l1*cosd(THETA1); %(length of 1st link multiply by cosine theta1). 
y1 = l1*sind(THETA1); %(length of 1st link multiply by sine theta1).
% First point of 2nd link and second point of 1st link arre same and their co-ordinates is represented by (x1,y1).
% Second point co-ordinates of 2nd link will be
x2 = x1+l2*cosd(THETA2); %(x1 plus length of 2nd link multiply by cosine theta2).
y2 = y1+l2*sind(THETA2); %(y1 plus length of 2nd link multiply by sine theta2).
% Now let us Plot the robotic arm Movement.
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',2.5)
axis([-1 8 -1 8])
pause(0.03)
M(ct) = getframe(gcf);
ct =ct+1;
   end
end
%let us generate a movie of Robotic arm movement.
movie(M)
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

Explanation of code

  • We have got the Inputs (Length and angle of link from +ve X axis i.e. theta) of our robotic arm which determines the Position of our two links.
  • Since the length of link will be constant, so the factor which will change the position of links will be theta. So we have the value of theta at different positions and it is represented in an array via linspace command.
  • We will apply a For loop for two different thetas i.e theta1 & theta.
  • Applied for loop for i = 1:length(theta1)
    THETA1 = theta1(i) Similarly for theta2 it can be done.
  • We have applied the for loop to take the discrete values of theta instead of an array.
  • We have considered that first point of Link 1 is hinged and it is placed at origin. Therefore, (x0,y0) will be (0,0).
  • Co-ordinated of second point of 1st link will be calculated sine and cosine formula. Therefore, co-ordinates will be
  • x1 = l1*cosd(THETA1) & y1 = l1*sind(THETA1).
  • Co-ordinate of second point of 1st link and first point of 2nd link will be same as they are connected.
  • Co-ordinate of second point of 2nd link will be x2 = x1+l2*cosd(THETA2) & y2 = y1+l2*sind(THETA2).
  • We have Plot the robotic arm Movement with the plot command.
  • Put the command to generate frames for animation of robotic arm.
  • Then applied the command to create & and save the video file.
  • The animation video was saved in provided directory.

The errors faced while programming

  1. Dont put small letters theta in place of capital letters THETA while putting in co-ordinates and for loop.

2. Don't put x and y co-ordinates together while plotting the links graphs.

Rectification

1. Keep your self clear between capital letters THETA and small letters theta while putting in co-ordinates and for loop.

2. While plotting make sure place the same co-ordinates together i.e. x co-ordinates together and y co-ordinates together instead of x and y co-ordinates together.

Animation video

Please find the Drive link of Animation video

https://drive.google.com/file/d/1Hw89FqmqKiuQ92dVmoDMllcDOnEyYmmd/view?usp=sharing

https://drive.google.com/file/d/1Hw89FqmqKiuQ92dVmoDMllcDOnEyYmmd/view?usp=sharing

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.