Week 2- 2R Robotic Arm Challenge
CHALLENGE:4
- Writing a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm, as shown in the video.
-
%program in Matlab to simulate the forward kinematics of a 2R Robotic Arm clear all close all clc %given data l1 = 1.5; l2 = 2; 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',3) axis([-0.1 5 0 5]) pause(0.03) M(ct)=getframe(gcf); ct=ct+1; grid on end end movie(M) videofile= VideoWriter('forward_kinematics.avi','uncompressed AVI'); open(videofile) writeVideo(videofile,M) close - RESULT:
YouTube LINK:
- Step-wise explanation of my code:
- Here, first I'm giving comment "program in Matlab to simulate the forward kinematics of a 2R Robotic Arm" using the "%" command in Matlab.
- Then, input the given data by assigning suitable variables and here the we are using the "linspace" command to get the different values of theta1 and theta2 as per range mention in above code.
- After assigning the data, i'm using the "for loop" command for assigning different values to theta1 as in THETA1 and similarly for theta2 as in THETA2.
- In next, i'm input the coordinates for forward kinematic movement of 2R roboticarm.
- Then, using the "plot()" command plotting all the value of coordinates in graph.
- for better animation, i'm using the "ctr=1...ct=ct+1" command for contouring frame.
- for making animation video, i'm using,
"M(ct)=getframe(gcf);
ct=ct+1;
movie(M)
videofile= VideoWriter('forward_kinematics.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close" command.
- Here, the above code execute for creating the video(avi) file then with the use of "open(videofile)" command ,the video file open then using the command of "writeVideo(videofile,M)" above coding frame will be stored in video and using "close" command,i'm closing the video file.
- So, this was the explanation about the above program.