Challenge

Week 2- 2R Robotic Arm Challenge

Aim: writing a program to simulate the forward kinematics of a 2R Robotic Arm and uplaod it online.

Governing equations:

%equations for the first arm
x1=l1*cosd(THETA1);
y1=l1*sind(THETA1);
%equations for the second arm
x2=x1 + l2*cosd(THETA2);
y2=y1 + l2*sind(THETA2)
  • Objectives: To write equatons for two connected robotic arms moving Independant of each other .
  • defining the legth of the first arm and all the diffferent positions it will take in the 2d plot.
  • then defining the length and positions of the second arm with respect to the all the different positions of the first arm.
  • plotting all these values and animating it using movie command .

Body:

  • the programming language used is matlab.
  • close all 
    clear all 
    clc 
    
    l1=1;
    l2=0.7;
    theta1=linspace(0,90,10);
    
    theta2=linspace(0,90,10);
    
    
    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);
    
    
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',2);
        axis([0 2 0 2]);
        pause(0.003)
        M(ct)=getframe(gcf);
        ct=ct+1;
        
        end 
    end
        
     
    movie(M)
    videofile=VideoWriter('robotic_arm.avi','Uncompressed AVI');
    open(videofile)
    writeVideo(videofile,M)
    close(videofile)
        
     
    
    
    
    
    
  • first we define the length of both of the robotic arms to be 1 and 0.7 metres.
  • Then defining all the angles(theta 1 and theta2)which both of the robotic arms will make with the 2d axis using linespace command and that’s from 0 to 90 with a gap of every 10 degrees.
  • Declaring a variable ‘num_s’ that will increment with every iteration of theta1 and theta 2. This will basically help us in getting the frames for the video by numbering each frame with every iteration.
  • Using ‘for’ command to iterate between 10 positions of the first arm.
  • Defining a variable THETA1 that takes different values of theta1 depending on the number of the iteration the loop is in.
  • Using a nested for loop to iterate between 10 positions of the second arm.
  • Defining a variable THETA2 that takes different values of theta2 depending on the number of the iteration the loop is in.
  • Defining the starting point of the first arm to be the origin.
  • Writing equations using basic trigonometry to define the end points of the First arm (x1=l1*cosd(THETA1);)

(y1=l1*sind(THETA1);)

  • Writing equations using basic trigonometry to define the end points of the second arm ( x2=x1 + l2*cosd(THETA2);)

( y2=y1 + l2*sind(THETA2);)

  • Plotting the graph for the positions of the robotic arms. Arm one from( x0 to x1 ,y0 to y1) and the second arm from (x1 to x2 , y1 to y2).
  • The graph will get new values with every iteration of I and j and iterated values of Theta1 and Theta2.
  • Defining x axis from 0 to 2 and , y axis from 0 to 2
  • Using the getframe(gcf) command to get different frames to strore it in variable ‘M’.
  • ‘num_s’ defines the number of the frame thats getting saved.
  • num_s=num_s+1; increments the value of ‘num_s’ by one in every iteration.
  • Closing both the for loops.
  • Movie command creates a movies using all the values of variable M.
  • Line 42 command creates a videofile with the name robotic_arm and defines its format.
  • Line 43 opens the video file
  • Line 44 writes different frames into the videofile obtained from line 34.
  • Line 45 closes the video file.

And that ends the explanation of the given code.

The errors faced were -

  • Capitalising the alphabet ‘M’, the correct syntax is using a small ’m’.
  • Not using prober variable (using ‘frame’ instead of ‘ct’) not advisable as there are command like ‘getframe’, that uses the same word.
  • Using line command instead of plot command . Line command deletes all the previous values where as plot command is a high level function in which we can put the values for both the first arm and second arm together.

Conclusion: the animation was saved in the matoab folder and we can see all the positions of the first and the second arm in the plot and the video.

the commands can be modified to move the arm from 90 degrees to 0 degrees and vica versa for bothe arms

the limitations of this project is that cant show the arms moving independantly to each other in a random fashion.

video of robotic arm

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.