Challenge

Week 2- 2R Robotic Arm Challenge

Aim: To write a program in matlab to simulate the forward kinematics of a 2R Robotic arm and Create a movie from the plots obtained from the program.

Matlab Code

% program for 2R Robotic Arm Challange
clear all
close all
clc
%inputs
l1=1;
l2=0.5;
theta1=linspace(0,90,12);
theta2=linspace(0,90,12);
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);
%plotting
plot([x0,x1],[y0,y1] , [x1,x2],[y1,y2],'Linewidth',2)
axis([-0.1 2 0 1.5])
pause(0.01)
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)
Code Explanation
%inputs
l1=1;
l2=0.5;
theta1=linspace(0,90,12);
theta2=linspace(0,90,12);
Here,
  • THETA1 and THETA2 is the variable which contains 1 value. Thete1 and
    theta2 is the value from 0 to 90 degrees. Using for loop command to
    repeat the value of the theta1 and theta2 which has 40 values.
x1=l1*cosd(THETA1);
y1=l1*sind(THETA1);
x2=x1+l2*cosd(THETA2);
y2=y1+l2*sind(THETA2);
Here,
  • X1,y1 and x2,y2 are the starting point coordinates and endpoint coordinates
    of link1 respectively.
%plotting
plot([x0,x1],[y0,y1] , [x1,x2],[y1,y2],'Linewidth',2)
axis([-0.1 2 0 1.5])
pause(0.01)
M(ct)=getframe(gcf);
ct=ct+1;
end
end
Here,
  • Using plot command to plot the graph with the given statement.
  • Using axis command to set the x and y limits
  • Using pause command to set an interval at each position
  • Using an array M and adding the getframe (gcf) function to create an animation of all the parts.

Converting the animation plot of the above code:
Movie(M)
videofile = videowriter ('forward_kinematics.avi' , 'uncompressed AVI');
open(videofile)
writevideo(videofile,M)
close(videofile)
Here,

  • At the end of for loop matlab is going to make a movie with all the frames with movie (M) command
  • Creating the video with the command videofile
  • Opening the video file with open(videofile)command and then write the captured frame with the writevideo command and then close the
    video by close (videofile)command.

https://drive.google.com/file/d/1BBMTj41yFh4ns2eg0UQMJlFTcnRzod-l/view?usp=sharing

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.