Week 2- 2R Robotic Arm Challenge
Aim:- Write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm, as shown in the video.
Objective:- Create an animation file of the plot.
Here in this problem we used the forward kinematic concepts which deals with the length and angle of the Robotic arm to calculate the points to graph in 2D plot and then creat an animation of the robotic arm at different theta angles with help of MATLAB videowritting commands.
Error faced:-
The error I faced in the ploting command where I wrote all the x-axis points on one side which Matlab considered as a pair of (x,y) and I wrote all the y-axis points on another side which Matlab considered as a pair of (x,y) which was not correct way to write the ploting command. But that time I didn't capture the screen short.
Over coming the error:-
With the help of live sesion my teacher helped me to rectified the error by making the perfect x and y pair of the 2 separate links of the robot.
Output:-
https://drive.google.com/file/d/1_okB2wxvqL_63EjCbV8TGTF_FlxXcxRn/view?usp=sharing
-
clear all close all clc %defining link, theta 1 and 2 l1=1; l2=0.5; x0=0; y0=0; ct=1; %theta 1 and 2 are the angle made by the 2 links of the robot which are from 0-90 degrees. theta1= linspace(0,90,10); theta2= linspace(0,90,10); %we took nested for loop in this program the below for loop is a parent %loop which controls the theta1. for i= 1:length(theta1) t1= theta1(i); %the below for loop is a child loop / inner loop which controls the %theta2. for j= 1:length(theta2) t2= theta2(j); %these are the points to be plot these points are the function of %the robotic arm length and angle theta. x1= l1*cosd(t1); y1= l1*sind(t1); x2= x1+l2*cosd(t2); y2= y1+l2*sind(t2); %ploting graph plot([x0 x1],[y0 y1],[x1 x2], [y1 y2],'LineWidth', 3) axis([0 2 0 2]) title('2R Robotic arm working') xlabel('x-axis') ylabel('y-axis') legend('link 1','link 2') M(ct) = getframe(gcf); ct = ct+1; %ct is an updating value for each iteration of j. end end %movie command is used to creat movie of array (M). movie(M) %videofile is a variable. videofile = VideoWriter('robotic.AVI','Uncompressed AVI') open(videofile) writeVideo(videofile,M) close(videofile) Code explanation:-
- First we define the input parameters like link length, theta angle 1 & 2 , and the origan points.
- Then we took nested for loop the outer loop is to control the theta1 and the inner for loop is to control the theta2.
- Then we gave the statements to execute in the inner loop which are drived from the trignometry.
- Then we plot the calculated points with the help of plot command.
- The M(ct) =getframe(gcf) command is used for capturing the graph as a frameand then storing it on the M array with indexing (ct) in asequential order.
- we wrote the plot and M(ct) array inside the inner loop because we want to plot at each value of theta and the have to capture the each frame in order in M(ct) array.
-
Finally we gave the command to creat the movie with the help of (M) array by using the movie(M) command.The output we got is the animated clip of the working robotic arm.