Challenge

Week 2- 2R Robotic Arm Challenge

clear all 
close all
clc
% inputs
l1=1;  % length of link 1
l2=0.5; % length of link 2
theta1= linspace(0,90,10); % angles that makes link 1
theta2= linspace(0,90,10); % angles that makes link 2
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);

        % plotting 
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2,],'linewidth',3)
        axis([-0.1 1.5 0 1.5])
        xlabel('x axis')
        ylabel('y axis')
        title('2R Robotic Arm')
        pause(0.03)
        M(ct)=getframe(gcf);
        ct=ct+1;
    end
end
movie(M)
videofile=VideoWriter('forward_kinematics.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

RESULT

Google drive link of 2R Robotic Arm

Explanation

Line 1:- clear all % To clear all the data in workspace.

Line 2:- close all % To close all the figure window.

Line 3:- clc % To clear all the data in command window.

Line 5:- l1= 1; % Input of length of link 1.

Line 6:- l2= 0.5; % Input of length of link 2.

Line 7:- theta1= linspace(0,90,10); % Angles that makes link 1.

Line 8:- theta2= linspace(0,90,10); % Angles that makes link 2.

Line 9:- ct= 1; % Input of variable ct.

Line 10:- for i= 1: length(theta1) % for loop condition with i varies from 1 to length of array theta1, that is 10.

Line 11:- THETA1= theta1(i); % It is array indices of theta1, that value assign to variable THETA1.

Line 12:- for j= 1: length(theta2) % It is a nested for loop, condition with j varies from 1 to length of array theta2, that is 10.

Line 13:- THETA2= theta2(j); % It is array indices of theta2, that value assign to variable THETA2.

Line 14:- x0= 0; % Input of x0.

Line 15:- y0= 0; % Input of y0.

Line 16:- x1= l1*cosd(THETA1); % Equation of x1.

Line 17:- y1= l1*sind(THETA1); % Equation of y1.

Line 18:- x2= x1+l2*cosd(THETA2); % Equation of x2.

Line 19:- y2= y1+l2*sind(THETA1); % Equation of y2.

Line 22:- plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3) %To plot the lines using cordinates with line width3.

Line 23:- axis([-0.1 1.5 0 1.5]) % It define axis length.

Line 24:- xlabel('xaxis') % It names xaxis to "xaxis".

Line 25:- ylabel('yaxis') % It names yaxis to "yaxis".

Line 26:- title('2R Robotic Arm') % It gives title of plot to "2R Robotic Arm".

Line 27:- pause(0.03) % It adjust the play time to 0.03 seconds.

Line 28:- M(ct)= getframe(gcf); % Capture movie frame to array M.

Line 29:- ct=ct+1; % Variable ct with increment value 1.

Line 30:- end % Termination of nested for loop.

Line 31:- end % Termination of for loop.

Line 32:- movie(M) % Play recorded movie frames.

Line 33:- videofile= VideoWriter('forward_kinematics.avi','uncompressed AVI'); % Create object to write video files.

Line 34:- open(videofile) % open file for writing video data.

Line 35:- writeVideo(videofile,M) % Write video data to file.

Line 36:- close(videofile) % Close file after writing video data.

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.