Challenge

Week 2- 2R Robotic Arm Challenge

1. AIM : A program to simulate the Forward Kinematics of a 2R Robotic Arm .

An example of the 2D plot is shown below. 

In the diagram above. 

1. Line 1 in shown in blue color which is mentioned as " l1 " in the equation(s). 

2. Line 2 is shown in red color , which is mentioned as " l2 " in the equation(s).

3. Theta1 is the angle of Line 1 ie " l1 ". Theta 1 will be variable.

4. Theta2 is the angle of Line 2 ie " l2 ". Theta 2 will be variable.

clear all
close all
clc

% Program to simulate forward kinematics of a 2R Robotic Arm
% Inputs
l1 = 2;
l2 = 1;
theta1 = linspace(0,90,10);
theta2 = linspace(0,120,20);

ct = 1;
for i = 1:length(theta1)
    THETA1 = theta1(i);
    for j = 1:length(theta2)
        THETA2 = theta2(j);
        
        %coordinates
        % line 1 coordinates x0,y0 and x1,y1
        % line 2 coordinates x1,y1 and x2,y2
        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',2)
        axis([-1 4 0 4])
        pause(0.05)
        M(ct) = getframe(gcf);
        ct = ct + 1;
        
    end
end

movie(M)
videofile = VideoWriter('2R_Robotic_Arm_Animation.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Description : The inputs provided in the code are :

1. length of the first and second line in metre. Angular range of line 1 and line 2. 

2.  linspace command is used for calculating the values of ' theta1 ' and ' theta2 ' in the given range. Here the range given for theta1 is from 0 to 90 with 10 values and the range for theta2 is from 0 to 120 with 20 values. The values of angles are in degrees.

Now, since we will have multiple values of theta1 and theta2 , hence we will need to repeat the program again and again using program code. For this puspose we have used ' for loop ' . The first line in the for loop indicates the length of theta1 from the range 1 to 10 ie the number of values for theta1 at which at which ' l1 ' will move from angle 0 to 90. so this process will be repeated 10 times. 

In order to plot the values of theta2 for every value of theta1 we again use a for loop within the for loop for theta1. Here the line again indicates the length of theta2 from the range of 0 to 120 with 20 values ie this process will be repeated with line ' l2 ' ,20 times for each and every value of theta1.

Next, we have to determine the coordinates for l1 and l2 . Coordinates for l1 is x0,y0 and x1,y1. Coordinates for l2 is x1,y1(since end point of l1 is start of l2) and x2,y2. The value of x1 and y1 is calculated using cosd and sind respectively , since the values of angles are in degrees.

Now we plot the values. 

please not : we have to enter the correct sequence in which the values of x and y will will be plotted. In the code the correct sequence is written ie  " plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',2) " . If we write the plot command " plot([x0 x1],[x1 x2],[y0 y1],[y1 y2],'linewidth',2) , we will get the incorrect plot as shown below.

The above plot is just to show the screenshot of part of the animation. The animation is still working but with the wrong visualisation.

Inorder to get a better view the axis command is given with proper coordinates ie axis([-1 4 0 4]).

The pause command is given here to pause between each frame ie pause(0.05). 

Please note : The number values of theta1 and theta2 and the pause duration will determine the smoothness in the animation. Less pause duration and higher values of theta1 and theta2 will give a much smoother animation. 

Converting the plots into a movie. 

The command  ' M(ct) = getframe(gcf) ' is used to save each and every frame including the first frame and subsequent frames. The number frames will depend on the number of values of theta1 and theta2. hence to create a movie all these frames have to be joined/stitched together. 

The ct command is the counter command and ct = ct +1 will increase the counter after each frame is saved and the process will go on till the desired condition is reached. 

To Convert into a movie file or .avi file , the following commands are :

movie(M)
videofile = VideoWriter('2R_Robotic_Arm_Animation.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
 
After the execution of the full program a .avi file is saved in the same folder as the program file folder. It is recommended to name the file based on the name of program. This .avi file is saved in an uncompressed form. 
Also, after executing the program ,the animated plot is shown in the output figure followed by the animation movie of the same plot.
 
Please not : There is an alternate way to create a animated movie .avi file.
Here we start with ct = 1 instead of 0. 
 
 
Here , After the executing the program , only the animated plot is shown . but the movie is saved in the .avi format and it is in the compressed form. 
 
Errors faced : 
1. Parenthesis error. 
 
 
The axis command was not closed with proper brackets. rectified the error by adding the brackets.
 
2. Only the lower arm l1 was moving but l2 was not moving at different angles and was left fixed at the start position of l2.
 
 
In the for loop command for theta2 the length of theta2 was not specified ie 'j = 1 'was written and not j = 1 :length (theta2). Hence only 1 value ie the first value was displayed with every value of theta1. After adding the range/length of theta2 the program executed the desired output. 
 
3. Incorrect commands given . For example ,
a) error (getframes) - correct (getframe)
b) error (openfile) - correct (open)
c) error (closefile) - correct (close)
d) error ( writevideo) - correct ( writeVideo) with a capital V
 
Conclusion : The link to the animation is mentioned below. 
 
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.