Challenge

Week 2- 2R Robotic Arm Challenge

% Program to show kinematics of a 2R Robotic Arm
clear
close all
clc

% Inputs
l1 = 1;    % length of first link
l2 = 0.5;  % length of second link

% using linspace command to create 10 linearly spaced data points from 0 to 90.
theta1 = linspace(0,90,10);      % angle made by link 1 with x-axis
theta2 = linspace(0,90,10);      % angle made by link 2 with x-axis

ct=1;   % initializing counter


for i = 1:length(theta1)     % length function returns the length of a vector which is 10 in this case.
    for j = 1:length(theta2)
        
        % coordinates for fixed end of link 1
        x0 = 0;
        y0 = 0;

        % coordinates for other end of link 1 which is connected to the link 2
        x1 = l1*cosd(theta1(i));  
        y1 = l1*sind(theta1(i));

        % coordinated for the manipulator end of link 2
        x2 = x1 + l2*cosd(theta2(j));
        y2 = y1 + l2*sind(theta2(j));

        % plotting
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
        axis([-0.1 1.5 0 1.5])      % setting scaling for the x-axis and y-axis
        pause(0.03)                  % using pause to create animation. 
        
        % creating a movie with getframe function
        M(ct) = getframe(gcf);        % getframe will return a movie frame. It will takes snapshots and store them in the variable M 
        ct=ct+1;                      % counter variablect will help changing the indices of the M array with each loop
    end
end

movie(M)  % play the movie  

% creating and saving the video file
videofile = VideoWriter('Challenge_Robotic_Arm.avi','Uncompressed AVI');  % this will create a file in which we can write the movie.
open(videofile)               % open the video file
writeVideo(videofile,M)       % This will write the movie data to the file
close(videofile)              % close the video file.





The objective of this program is to simulate the forward kinematics of a 2R Robotic Arm by creating an animation file of the plot.

The inputs needed for the program are

the lengths of the two links l1 and l2, coordinates of the both ends of the both links, angle made by l1 and l2 with x-axis i.e. theta1 and theta2.

In the begining of the code I have assigned the lengths of two links to variables l1 = 1 and l2 = 0.5

The angles made by link1 is theta1 and by link2 is theta 2 and the values are provided with the help of command linspace. linspace(0,90,10) this command will create 10 data points which are linearly spaced from 0 to 90. Therefore we have an array of 10 values for each angle theta1 and theta2. So there are two arrays theta1 and theta2 with 10 elements each.

Then I've initialized a variable ct for counter and set it's value to 1

Now for the creating the animation we will require two for loops which will calulate the coordinates for all the values of theta1 and theta2. For the first for loop the loop variable i is initialized (i = 1:length(theta1)). The range is provided by colon operator. I've also used length function which returns the value of length of an array or matrix. In this case it will return 10 as there are 10 values of theta. So first for loop will run from 1 to 10.

Now I've used another for loop with a loop variable j (j = 1:length(theta2)). Again I've used colon operator and length function to provide the range. This loop will also run 10 times.

Now when the code executes for i = 1 then it enter second for loop and executes j = 1:10 and rest of the commands inside second for loop will also be executed 10 times. Therefore, for each i value j will be executed from 1 to 10. So, in total the loop will run 100 times.

Now inside second for loop first I've provided inputs for the fixed end of link1 which is taken as origin (x0=0, y0=0).

Then the coordinates for other end which is joined end to end with the second link link. that means coordinates for second end of first link and first end of second link will be same. Coordinates are calculated by expressions x1 = l1*cosd(theta1(i)) and y1 = l1*sind(theta1(i)).

Functions cosd and sind are used because values of angles are provided in degrees.

I've use the loop variable (i) to provide the indices values for array theta1. so when the loop runs first time then, for i = 1, x1 = l1*cosd(theta(1)) and y1 = l1*sind(theta(1)). The value of (i) will keep changing every time the loop is executed and will calculate values for all the 10 angles in array theta1.

Now for manipulator end which the second end link 2, the coordinates are calculated by expressions x2 = x1 + l2*cosd(theta2(j)) and y2 = y1 + l2*sind(theta2(j)). This will work same as above. The loop variable (j) will provide the indices values for array theta2. And values for all the angles of array theta2 will be calculated similar to x1 and y1.

Now plot is created using plot command and entering all the x and y coordinates.

plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3). linewidth is used to increase or decrease the thickness of the plot lines.

axis([-0.1 1.5 0 1.5]) This command is used to set the scales for both the axes. x-axis will range from -0.1 to 1.5 and y will range from 0 to 1.5

then I've used pause(). This will pause the program for the specified time and the plot will seem like an animation. But this reduces the efficiency of a program. So to avoid this getframe function is used

M(ct) = getframe(gcf) In this command the movie frame is created by the function getframe is stored to the array M. ct provides the location for storing each frame. The value of ct will change with each iteration. ct = ct + 1 this will count the each iteration of loop. So, the value of ct will go from 1 to 100. So movie will be created by 100 frames of the plots.

Now both loops are stopped by using end command.

movie(M) command is used to run the movie.

videofile = VideoWriter('Challenge_Robotic_Arm.avi','Uncompressed AVI) this command will create file in which we can save the file. Here I've given the name Challenge_Robotic_Arm to the file and specified the format 'Uncompressed AVI'. Then this file is assigned to variable videofile.

open(videofile) This will open the file

writeVideo(videofile,M) This command will write the movie data in the array M to the videofile.

close(videofile) This will close the file

And now the animation is created and saved in the directory as Challenge_Robotic_Arm.

I had no errors while running the code. I've attached the screenshots of all the variables created by the execution of the program using whos command in command window and also attached a few screenshots of the plots while animation was running in the figure window.

The link for the youtube video of the animation:

https://youtu.be/HHWbgWk--fQ

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.