Week 2- 2R Robotic Arm Challenge
PROBLEM STATEMENT:
Write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm?
Answer:
Robotic Arm
Robotic arms are machines that are programmed to execute a specific task or job quickly, efficiently, and extremely accurately. Generally motor-driven, they’re most often used for the rapid, consistent performance of heavy and/or highly repetitive procedures over extended periods of time, and are especially valued in the industrial production, manufacturing, machining and assembly sectors.
A typical industrial robot arm includes a series of joints, articulations and manipulators that work together to closely resemble the motion and functionality of a human arm (at least from a purely mechanical perspective). A programmable robotic arm can be a complete machine in and of itself, or it can function as an individual robot part of a larger and more complex piece of equipment.
Forward kinematics refers to the use of the kinematic equations of a robot to compute the position of the end-effector from specified values for the joint parameters
In order to solve the above problem statement, we are going to use 2 arms of a robot which are named as link and then write a program for the movements of these arms.

STEP WISE EXPLANATION OF STATEMENTS USED IN THE MATLAB PROGRAM FOR ACHIEVING THE RESULT.
1.

The first step in building the program is to clear all the memory, which might have been present earlier.
CLEAR removes all variables from the current workspace, releasing them from system memory.
2. The next step is to provide the lengths of the 2 link used for robotic arm.

From the above statements we have initialised the lengths of link 1 as 1 and link 2 as 0.5.
3. Next step is to provide the theta values about an angle at which both the levers will rotate.

In the above statements we have assigned the name of theta 1 as 'theta1' and theta 2 as 'theta2'.
linspace function, it generated theta values from 0 to 90 degrees with an equal spacing of 10 degrees after each iteration.
The result of the above function would be:


4. Next step is to initialise a loop about which the functions will be repeated.

In the above the ct is the variable named counter which is assigned a value as 1 which represents and stores each frame.
THETA1 and THETA2 are the variables assigned to store the values of the theta values after each iteration of for loop.
The for loop is used to perform a function repeatidely until it reaches the end condition of the for loop statement.
Since we can see 2 for loops being used one after the other, this type of usage is known as nested for loop in programming laguage. In the nested for loop condition the for loop present inside will be executed till its conditions gets terminated and then again the first for loop will be iterated and then again all the conditions of nested for loop is executed. In nested for loop condition the function will get terminated till all the for loop conditions are terminated till the condition specified in each for loop function.
5. The main concept which will determine the length and position of the levers is written in the below statement.

In the first step one end of the lever 1 will be placed at (0,0) position in the graph. The other end of the lever 1 will be the end point of the lever whose lengths will be the maximum distance that would be displaced by the lever 1.
For lever 2, the starting point would the end point of lever 1 and the end point would be the x1 and y1 + the distance convered by lever in horizontal and vertical axis.
x1 = maximum distance covered by lever 1 in horizontal axis.
y1 = maximum distance covered by lever 1 in vertical axis.
x2 = maximum distance covered by lever 2 in horizontal axis.
y2 = maximum distance covered by lever 2 in verical axis.
In order to plot the lever on the grap, plot function is used in which linewidth is used to provide the width of the line on the graph.
Axis function is used to show the extreme ends of the graphs.
pause(n) - pauses execution for n seconds before continuing.
6. The next step is to create a video which would show the movements of the levers.

The above statements is used to create a variable M which would store the ct value which will contain the frames of each position of the lever.

A video is generated with the help of above statements, where movie is the function used which will store all the frames generated in above program.
Errors encountered while writting the above program.
1. 
The above error occured because in the program we can observe THETA2 variable is containing theta2(i) which means that the inside for loop containing j will not iterate as the value will still remain as i inside the for loop.
In order to solve the above error theta2(i) will be corrected as theta2(j) so that the iterations of j will store the theta 2 values.
2.

The above error due to wrong usage of brackets in the axis statement. It is corrected by using [] instad of {} brackets.
3.

The above error occured because the movie function is not typed correctly. Since Matlab is a case sensitive program it should be corrected as 'movie(M)'.
FINAL COMPLETE PROGRAM:
%2R ROBOTIC ARM PROGRAM
clear all
close all
clc
%length of robotic arms
l1 = 1;
l2 = 0.5;
%Intialing angles about which the lever will rotate.
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
ct = 1;
for i = 1: length(theta1)
THETA1 = theta1(i);
for j = 1: length(theta2)
THETA2 = theta2(j);
%starting point of lever 1
x0 = 0;
y0 = 0;
%end point of lever 1 and starting point of lever 2
x1 = l1*cosd(THETA1);
y1 = l1*sind(THETA1);
%end point of lever 2
x2 = x1 + l2*cosd(THETA2);
y2 = y1 + l2*sind(THETA2);
plot([x0 x1], [y0 y1], [x1 x2], [y1 y2], 'LineWidth', 3)
axis ([-0.1 1.5 0 2])
pause(0.03)
M(ct) = getframe(gcf);
ct = ct+1;
end
end
movie(M)
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
YOUTUBE LINK:
The below link shows the video where the movements of robotic arm is displayed on the graph.
CONCLUSION:
In the above activity following points are achieved.
1. Complete error free program is written which executes the forward kinematics of a 2R robotic arm.
2. A video file is generated which stores all the frames and then saved as a video file to display the result.