Challenge

Week 2- 2R Robotic Arm Challenge

close all;
clear all;
clc;


l1 = 0.3;
l2 = 0.2;
THETA1 = linspace(0,90,10);
THETA2 = linspace(0,360,25);

counter = 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);
        
        
        
        plot([x0 x1], [y0 y1],[x1 x2], [y1 y2], 'LineWidth',3)
        axis([-0.5 1 -0.5 1])
        pause(0.05)
        M(counter) = getframe(gcf); 
        counter = counter + 1;

    end
end

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


Movie clip video link: https://www.youtube.com/watch?v=rHCG-FSIips

The code is made in such a way that link 1 can be rotated for 0 to 90 deg & link 2 can be rotated by 360 deg

Code explanation

1) Clear all: Clear all the variables in the current workspace

2) Close all: Closes all the graphs, if anyone is opened

3) Clc: clears the command window.

4) The length of link 1 is taken as 0.3 & length of link 2 is taken as 0.2.

5) The THETA1 is considered as 10 equally spaced values between 0 & 90. Whereas, THETA2 is taken as 25 equally spaced values between 0 & 360.

6) A variable is created as a "counter "& value is assigned "1" to keep a count of many figures that will be made during the program execution.

7) Focusing on an angle (theta): A "for " loop is used to keep a track of No. of values in THETA1 & assigned to theta1. For each value of theta1, No. of values of THETA2 is considered & assigned to theta2.

8) Focusing on the position: The initial coordinates are:

(x0, y0) = (0,0)

(x1,y1)= (l1*cosd(theta1), l1*sind(theta1))

(x2,y2)= (x1 + l2*cosd(theta2), y1 + l2*sind(theta2))
9) The plot command is used to plot the line.
The X & Y coordinates of arm l1 is ((x0,x1), (y0,y1)).
The X & Y coordinates of arm l2 is ((x1,x2), (y1,y2)).
The line width is taken as 3.
10) The range of axis is defined as -0.5 to 1 for x-axis & y-axis both.
11) Each figure will have a pause of 0.05 sec on the screen.
12) M(counter) is a variable that captures the image data of the figure that will appear during the program execution.
13) Figure counter will increment by 1 every time
the for loop runs.
14) A video file is created of .AVI extension & opened & the captured image data stored in variable "M" is written in the file.
The file name is given as, robo_try_13.
ERROR FACED
The error shows "Value not a numeric scalar". It is because the linewidth is given as a string & not as a numeric value (as 3 is in quotation mark"").
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.