Week 2- 2R Robotic Arm Challenge
% This is a program to simulate the kinematics of a 2R Robotic arm
close all;
clear all;
clc;
% The 2R Robotics Arm has two arms (links) with two connectings joints
% The one end of an arm of the Robot assumed link1 is connected to a fixed base
% The other end of link1 is connected to an end of the second arm ( assumed link2)
% Input Data required
% The length of link1 is denoted as l1
% The length of link2 is denoted as l2
l1 = 1; % Assumed length
l2 = 0.5; % Assumed length
% The links are connectd together and rotate within a coordinate at the fixed base
%{
The arms will be rotating at varying angles from the horizontal, therefore
an array of angles is required for both arms (links)
%}
%{
To create an array of angles for theta1 and theta2, the linspace command shall be used
This command takes three arguments and create equally spaced values between
the first and second arguments. The number of values created equals the
third argument
%}
theta1 = linspace(0, 90, 10); % Assumed l1 makes an initial angle of 0 with the horizontal x-axis
theta2 = linspace(0, 90,10); % Assumed l2 makes an initial angle of 0 with the horizontal x-axis
%{
As the robot will rotate with varying angles of rotation, the use of
loops such as the for loop shall be utilized and a count initialized to
determined the number of rotations
%}
count = 1; % Initial rotation about the x-axis as the Robot assembly rotate several times
% Create a folder to save all plots that will be generated
Testing = pwd ; % current directory
RobotModel2 = 'RobotModel2' ; % Folder name to save all plots
folder = mkdir([Testing,filesep,RobotModel2]);
Testing = [Testing,filesep, RobotModel2];
for i = 1:length(theta1) % For each iteration of the loop, different angles equaling the length of theta1 are created
THETA1 = theta1(i); % Assign each angle created to a variable
%{
As the first arm rotates, the second arm rotates simultaneously,
hence there is need to create corresponding angles for the second
arm using another for loop with a different letter of iteration, in
this case j
%}
for j = 1:length(theta2) % For each iteration of the loop, different angles equaling the length of theta2 are created
THETA2 = theta2(j); % Assign each angle created to a variable
%{
Coordinates of rotation at the fixed base of the Robot (one end of link1)
Since the Robot Assembly is made of two arms, three positions shall
account for its rotation about the fixed base
%}
x0 = 0; % Initial assumed position of the fixed end of link1 about x-axis
y0 = 0; % Initial assumed position of the fixed end of link1 about y-axis
% From trigonometry, the second position of link1 is determined as follows
x1 = l1*cosd(THETA1);
y1 = l1*sind(THETA1);
% x1 and y1, the coordinates at the other end of link1 is the stating position of link 2
% From trigonometry, the coordinates at the second (ending position of link2 is determined as follows)
x2 = x1 + l2*cosd(THETA2);
y2 = y1 + l2*sind(THETA2);
%{
Plotting and Visualization
As each iteretaion of theta1 creates corresponding angles of theta2,
there is a need to utilize a counter variable to visualize each rotation
of link1 of the Robot to corresponding rotation of link2.
The number of plots equals the total count of the counter.
%}
figure(count);
plot([x0 x1], [y0 y1],[x1 x2], [y1 y2], 'LineWidth', 3);
axis([-0.1 1.5 0 1.5]);
temp=[Testing, filesep, fig',num2str(count),'.jpeg'];
saveas(gca,temp);
%{
Animation and Effects
As the plot may be rapid, it may be difficult to vitualize the animation of the plots of Robotic links.
Hence the use of the pause command to pause between each plot for better vitualization effect
is necessary
%}
pause(0.03);
% Movie Capture
% To capture the interior of the plots, the getframe command is used to capture each plot as a movie.
M(count) = getframe(gca);
count = count + 1;
end
end
% Video Creation
% The Videowriter command create a video file from an array or MATLAB Movie
movie(M)
v = VideoWriter('fkinematics1.avi', 'uncompressed AVI');
open(v)
writeVideo(v,M)
close(v)
%{
Google Links
Below are the google links to the video and plots
https://drive.google.com/file/d/1nbLAGsVkuAL7noMcyW4ZCb8K2txWNu65/view?usp=sharing
https://drive.google.com/drive/folders/1l_mlJo75lNr0BkoCfsP9H3hUh_RfC5q_?usp=sharing
%}