2R Robotic Arm simulator in MATLAB
AIM :
To write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm.
Objective :
- To create a program for simulation of the forward kinematics of a 2R Robotic Arm.
- Create an animation file of the plot and save it.
CODE
%% Forward kinematics for 2R Robotic Manipulator with animation -
clear all
close all
clc
% Inputs -
l1 = 1;
l2 = 0.5;
thetha1= linspace(0,90,10);
thetha2= linspace (0,90,10);
ct=1; % counter
for i = 1:length(thetha1)
THETHA1=thetha1(i);
for j = 1:length(thetha2)
THETHA2=thetha2(j);
x0 = 0;
y0 = 0;
x1 = l1*cosd(THETHA1);
y1 = l1*sind(THETHA1);
x2 = x1+l2*cosd(THETHA2);
y2 = y1+l2*sind(THETHA2);
% Plotting -
plot([x0 x1], [y0 y1], [x1 x2], [y1 y2], 'linewidth',3)
axis([-0.1 1.5 0 1.5 ]) %Limiting the axis in plot
pause(0.02)
M(ct) = getframe(gcf); %Saving frames the figures of plot
ct = ct + 1;
end
end
movie(M) % Creating Movie
videofile = VideoWriter ('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Stepwise explaination of the code –
- First we are clearing the windows and history of any variable and closing the plots and figure if any exists by writing –
clear all
close all
clc
- Then we are giving the inputs i.e the required know values for the robotic arm manipulator like the link 1 and 2 length , angle theta , etc.
l1 = 1;
l2 = 0.5;
thetha1= linspace(0,90,10);
thetha2= linspace (0,90,10);
- Above linspace is used for varying values of theta from 0 to 90 deg with 10 size or intervals
- The below ct =1 shows the counter initial value as 1 for the input to initialize for the plot.
ct=1; % counter
- Now, we are using the for loop as we want to analyze the different positions of the robotic arm links with respect to different theta values from 0 to 90 deg. The loop goes on as from 1:length(thetha1) means from 1st value of thetha 1 to last value of theta 1 i.e 10th value as defined before for theta 1 that will be thetha 90. Same syntax goes for thetha 2 values. In between the for loop for thetha 2 we are also giving the co-ordinates x0,y0, and formula for x1,y1 ,x2,y2 derived from the forward kinematics of a Robotic arm figure.
for i = 1:length(thetha1)
THETHA1=thetha1(i);
for j = 1:length(thetha2)
THETHA2=thetha2(j);
x0 = 0;
y0 = 0;
x1 = l1*cosd(THETHA1);
y1 = l1*sind(THETHA1);
x2 = x1+l2*cosd(THETHA2);
y2 = y1+l2*sind(THETHA2);
- We are plotting all the co-ordinate values for robotic arm links in one figure by using plot command and give its axis limit as desired. Also, pause command is used to hold or stop the code for plot showing the figure for different robotic arm positions.
- Then we are using the get frame for the output plot figure and saving its counter in variable and saving the frames of plot and then closing the for loop for j and then end for i.
% Plotting –
plot([x0 x1], [y0 y1], [x1 x2], [y1 y2], 'linewidth',3)
axis([-0.1 1.5 0 1.5 ]) %Limiting the axis in plot
pause(0.02)
M(ct) = getframe(gcf); %Saving frames the figures of plot
ct = ct + 1;
end
end
movie(M) % Creating Movie
videofile = VideoWriter ('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
- In the above lines we are creating the movie by then its videofile by command VideoWriter (Filename,profile)
- Then opening that video file by using open command and then writeVideo for the videofile for Movie M as saved before.
Finally, closing the video by using the close command for the videofile.
Errors faced while programming –
- For creating the movie it doesen’t work it tells –
Error using movie ,
Movie contains uninitialized frames
- Undefined function 'write' for input arguments of type 'VideoWriter'.
Steps taken to overcome the errors –
- For error using movie we have defined M(ct) for movie frame at wrong place, so it will come before counter increment statement in for loop .
i.e pause(0.02)
M(ct) = getframe(gcf);
ct = ct + 1;
- For Undefined function 'write' we had used wrong syantax so we are writing it correct by using writeVideo(videofile,M) after open file command in next line.
i.e. open(videofile)
writeVideo(videofile,M)
close(videofile)
OUTPUTS OF PLOTS -
The above figure shows the plot which tells the position of the robotic arm of link 1 at an certain angle theta 1 and link 2 at different angle theta 2 which is less than 90 deg.
The above figure shows the final output the extreme last position of the Robotic Arm manipulator at an angle 90 deg for both the links 1 and 2 as we had defined theta 1 and theta 2 range from 0 to 90 deg. with 10 intervals or size of vector as the input in code for the different positions of arm.
Screenshots showing errors thrown in command window -
Animation VIDEO -