Challenge

Week 2- 2R Robotic Arm Challenge

A program to simulate the forward kinematics of a 2R Robotic Arm -

% Inputs 
l1 = 1;
l2 = 0.5;
theta1 = linspace(0,90,15);
theta2 = linspace(0,90,15);

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 = l2*sind(THETA2);
        x2 = x1+l2*cosd(THETA2);
        y2 = y1+l2*sind(THETA2);
        
        % Plotting
          plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'Linewidth',3)
          axis([-0.1 1.5 0 1.5])
          pause(0.08)
          M(ct)=getframe(gcf);
          ct=ct+1;
       
    end
end

movie(M)
videofile = VideoWriter('robotic_arm_motion.avi','uncompressed Avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)

 Result -   Animation file of the plot     https://youtu.be/pWutckgfd3g

Explanation - First we consider the length of 1st and 2nd robotic arm as l1 and l2 as 1 and 0.5 respectively. The angle each arm makes with x-axis is written using linspace command, and it is a matrix of 15 values between 0° to 90°. Then we are taking a for loop where i is a loop counter. length(theta1) represents the number of values in the theta1 array. THETA1 is a variable that takes one value only, The value of theta1(i) changes 15 times equally between 0° and 90°. THETA1 represents the angle of arm 1 in a given instant. As we know the angle of arm 2 to changes 15 times for each rotation of arm 1. so we uses another for loop to generate that. This time we take j as a loop counter. THETA2 represents the angle of arm 2 in a given instant. theta2(j) is an array of 15 values but THETA2 variable will store only one value at a time. After that we give the coordinates (x0,y0) (x1,y1) (x2,y2) of arm 1 and arm 2. Then we plot the code using plot command, we take linewidth as 3.  axis([-0.1 1.5 0 1.5]) represents the limits of x label and y label. We give pause(0.08) command to loop the code after  0.08 seconds. Then we are taking an array M of frame ct, initially we take the value of ct as 1 and for each loop the value of ct will increse by +1 value. Then we are using the getframe command for the array. getframe(gcf) each time takes the current frame running on the loop. After closing both the loops, we are generating a video file by using the command movie(M) by attaching all the frames of array M into a single file. fille name and format is generated by the command videofile = VideoWriter('robotic_arm_motion.avi','uncompressed Avi'); .

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.