Week 2- 2R Robotic Arm Challenge
% Program for forward kinematics of a 2R robotic arm
clear all
close all
clc
%Input length of robotic arms
l1 = 1;
l2 = 0.5;
% Input angles made by robotic arm
theta1 = linspace(0,90,20);
theta2 = linspace(0,90,20);
ct = 1;
for i = 1:length(theta1) %for loop will run from 1 to the total value or length of array 'theta1'
THETA1 = theta1(i); % saving the value of array theta1(i) in variable THETA1 each time the loop runs
for j = 1:length(theta2)
THETA2 = theta2(j);
x0 = 0; %coordinates base of 1st robotic arm
y0 = 0;
x1 = l1*cosd(THETA1); %coordinates of end of 1st arm which is equal to starting coordinates of 2nd arm
y1 = l1*sind(THETA1);
x2 = x1 + l2*cosd(THETA2); %coordinates of end of 2nd arm
y2 = y1 + l2*sind(THETA2);
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2], 'linewidth', 3) %plot command for joining lines between the 3 coordinates
axis([-0.1 1.5 0 1.5]) % changing the origin to properly accomodate the robotic arms
pause(0.0001) % pausing execution to get all the frames' execution in same window
M(ct) = getframe(gcf) % M array getting each frame for video
ct = ct+1; % represents each frame
end
end
movie(M) %codes for saving the animation in a video file
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
In this program, the forward kinematics of a 2R robotic arm is animated by the help of MATLAB software. With the help of this program we can determine the movement of the robotic arms and the workspace of it which tells us the possible area in which the robotic arm can reach and complete the required task given to it.
1. Initially, we have to establish the required lenghts of the two robotic arms.
2. Then, we have to make an array of the angles made by the robotic arm relative to the datum line. Here, I have used 'linspace' command for the array generation. The arm's movement is from 0 to 90 degree which is divided into 20 parts for smoother movement in the animation.
3. Now, for plotting the different positions of the arms, we have to use the nested for loop because we want the trace of every point of second robotic arm for every position of 1st arm. So, for one loop of 1st 'for loop' the code will run multiple times as much as given in code which in this case is 20.
4. The initial for loop will run from 1 to the length or value of the array 'theta1' which in this case is 20. So the whole loop will run 200 times since the nested for loop is also running 20 times.
5. For plotting the graph of two robotic arms, we require 3 coordinates since the end coordinate of 1st arm will be same as the starting coordinate of second arm. For plotting the lines, I have used the 'plot' command.
6. To get compilation of the movement of the arms, we have to use the pause command. Since we have to save the animation also, so to accomplish that we have to save all the frames of the code execution in an array by using 'getframe' command.
7. In the end we can save the animation as a video in a file using 'videowriter' and 'writevideo' command.
The google drive link for the animation video is below - https://drive.google.com/file/d/1BxBFMXK1XSvVZ1LKawmU_JHG4l8muSpn/view?usp=sharing