Challenge

Week 2- 2R Robotic Arm Challenge

Aim : Write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm

 Diagram of the 2R Robotic arm:

L1 = length of Link1 in meters

L2 = length of Link2 in meters

theta1 = angle1 = angle made by link1 with x axis

theta2 = angle2 = angle made by link2 with x axis

(x0,y0) = coordinates of link1 attached to support

(x1,y1) = coordinates of link1 where it attaches to link2 also its the starting coordinate of link2

(x2,y2) coordinates of where link2 ends

Main program :

clear all
close all
clc
%coordinates of arm1 attached to support
x0 = 0;
y0 = 0;
 %angle made by both arms with respect to x axis in degrees
theta1 = linspace(0,90,15);
theta2 = linspace(0,90,15); 
%length of arms in Meters
L1 = 1;
L2 = 0.5;

ct = 1;
for i=1:length(theta1)
    Theta1 = theta1(i)
    for j =1:length(theta2)
        Theta2 = theta2(j)
        
x1 = L1*cosd(Theta1)
y1 = L1*sind(Theta1)
x2 = x1 + L2*cosd(Theta2)
y2 = y1 + L2*sind(Theta2)
    
%plot

plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',4)
axis([0 1.6 0 1.6])
pause(0.03)
M(ct) = getframe(gcf) 
ct=ct+1;
    end
end

%Creating a animation

movie(M)
videofile = VideoWriter('forward_kinematics.avi','uncompressed AVI') %create videofile
open(videofile)         %opens videofile created titled forward_kinematics
writeVideo(videofile,M) %writes each frame captured in M into videofile
close(videofile)        %closes videofile after writing of frames is completed 

Explanation of Code :

  1. x0 and y0 indicates the coordinates of arm1 attached to support 
  2. we use linspace for theta1 and theta2 to input every possible angles from 0 to 90 degrees in steps of 15 degrees, for instance if the first angle is 0 next angle will 15.
  3. giving inputs to L1 and L2 that is arm lengths in meters
  4. ct is used to count each plot and later we use ct+1 to increment.
  5. We are using for loop to know the point where arm 2 ends for each angle arm 1 makes with x axis, so that we can plot every possible position robotic arm traces.
  6. getframe is used to get each frame of the plot, gcf indicates get current frame.
  7. every frame we get is stored in a array M(ct) and ct increases with every succefull run of for loop from 1 to length(theta1)
  8. after every frame is stored in M we use movie to create animation
  9. videowriter is used to give title to the movie and format with which it is saved in this example it saved as AVI and uncompressed AVI is used to save it as uncompressed.
  10. open videofile created titiled forward_kinematics by using open()
  11. writes each frame captured in M into videofile using writeVideo()
  12. close videoFile after writing is completed and now is saved as forward_kinematics.avi

Output :

Animation file link:

https://youtu.be/zsl7Xhaum_U

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.