Week 2- 2R Robotic Arm Challenge
Write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm, as shown in the video.
- Create an animation file of the plot.
- Upload an animation on You tube and provide the You tube link for an animation in your report.
- You can also provide the google drive link.Note: Make sure that you provide the access to the video, while pasting the google drive link.
answer:
- The code is provided bellow:
clear all
close all
clc
% angle of the first link:
theta1= linspace(0,90,10);
% angle of the second link:
theta2= linspace (0,90,10);
% defining variables:
l1= 2;
l2= 1;
% counter value:
ct= 1;
for i= 1: length(theta1)
A1= theta1(i);
for j= 1: length(theta2)
A2=theta2(j);
x0=0;
y0=0;
x1 = l1*cosd(A1);
y1 = l1*sind(A1);
x2= x1+ l2* cosd(A2);
y2= y1+ l2* sind(A2);
plot ([x0 x1],[y0 y1],[x1 x2],[y1 y2],'LineWidth',3,'Color','r')
axis ([0 4 0 4]);
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);
After defining the inputs and their values, l1 and l2 were both defined .
theta1 and theta2 were defined by using linspace command. Counter value (ct) was also defined.
Nextly, for loop was used to allow me repeat control structure over in a specific number of times. While theta1 and theta2 have multiple angle values, I added new variables to the code which are A1 and A2. In addition, the formula of calculating x1,y1,x2 and y2 were defined.
Moreover, I used plot command to plot a graph for all x and y coordinates with axis.
'M' command was used to capture the current frame every time the for loop runs and added +1 to ct to increment the frame every time the for loop run.
Lastly, 'end' command was used to stop the plot and for loop from running.
The final step is to create a movie of the animation of the robotic arm. for that I used 'movie' command
'videowriter' command was used to assign video format, AVI was used as a format.
'open' command was used to open file for writing video data
'close' command was used to close the file after writing video data.
while writing the code some errors occured, and screenshots of those error are provided in the attached file:
Error-1: in plot command there was miswriting of 'linewdth' which resulted in invalid use of operator. This error has been resolved by re-writing it correctly.
Error: A1 and A2 variables were missing which resulted in wrong animation of the robotic arm. This error has been solved by defining new variables A1 and A2 to the code.
Error-2: mistyping of 'videofile'. this error has been resolved by re-writing it correctly.
NOTE: the animation of robotic arm link is provided bellow: