Challenge

2R Robotic Arm Challenge

CODE:

% Forward kinematics of a 2R robotic arm manipluator
clear all
close all
clc


l1 = 1.5; % length of link1
l2 = 1; %length of link2
theta1 = linspace(0,90,25); %angle of 1st rotational joint with x axis
theta2 = linspace(0,90,25); %angle of 2nd rotational joint with x axis
ct = 1; % for frame counter

for i=1:length(theta1)
    THETA1 = theta1(i); % new variable assigned the value of theta1
    for j=1:length(theta2)
        THETA2 = theta2(j); % new variable assigned the value of theta2
        x0=0; %origin of arm
        y0=0; %origin of arm
        x1=l1*cosd(THETA1);%x coordinate of end of link1/start of link2 
        y1=l1*sind(THETA1);%y coordinate of end of link1/start of link2
        x2=x1+l2*cosd(THETA2); %x coordinate of end of link2
        y2=y1+l2*sind(THETA2); %y coordinate of end of link2
        
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth', 2)
        axis ([-1 3 0 3])
        pause(0.01) %pauses the plot from one to nest and so on by 0.01s intervals
        M(ct) = getframe(gcf); % captures figures as movieframes
        ct=ct+1; % moves the counter by 1 so that nest frame can be captured(repeats till loop ends)
    end
end

movie(M) %plays the movie frames
videofile = VideoWriter('fwdkine.avi','Uncompressed AVI'); %name and type of video
open(videofile) %ones video file
writeVideo(videofile,M) %writes the movie m
close(videofile) %closes the video file

CODE EXPLAINATION:

  • We fix the values of length of link 1 and link 2.
  • Using linspace, 15 values of theta1 and theta 2 are created.
  • Using nested for loops, for every value of thetha 1 and all corresponding values of theta 2, the position coordinates of link 1's end and link 2's ends are calculated.
  • At the same time, the values are plotted and using pause () command, it can be seen as an animation.
  • A new array is created and all the plots are stored as frames in the array for creating a movie.
  • Nested for loop is ended.
  • The frames are played using movie().
  • A videofile is created with name and type specified.
  • The movie is written in that file and it is then closed.

Error Faced :

A typing error in getframe(gfc) which was writted as getframe(gcf) resulted in an error as shown below. I had taken down the same command in a notebook and was able quickly realize why i was getting the error and rectify it. No other major errors were encountered.

OUTPUT VIDEO youtube link:

https://www.youtube.com/watch?v=vnnedVwqVg4

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.