Week 2- 2R Robotic Arm Challenge
%Forward kinematics of a 2R robotic arm- challenge
clear all
close all
clc
%factors required
%position of the strarting end of arm 1
x0 = 0;
y0 = 0;
%length of the arms
l1 = 5;
l2 = 2;
%angles of arm 1 and arm 2 wrt x axis
theta1 = linspace(1,70,20);
theta2 = linspace(1,45,20);
c = 1;
%calculating position of the end of arm 2 wrt change in position of arm 1
for i = 1:length(theta1)
t1 = theta1(i);
for j = 1:length(theta2)
t2 = theta2(j);
x1 = l1*cosd(t1);
y1 = l1*sind(t1);
x2 = x1 + l2*cosd(t2);
y2 = y1 + l2*sind(t2);
%plotting the reults
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',5);
pause(0.05);
M(c) = getframe(gcf);
c = c+1;
end
end
%creating a movie file
movie(M)
videofile = VideoWriter('2R_robotic_arm','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);

https://drive.google.com/file/d/1ak9uSEH4uj21kra6wAyOgHN8XVBsJ33k/view?usp=sharing
The Objective here is to find the position of a manipulator otherwise might be attached to the end of a 2 arm link and create an animation file to obtain the results,
We start by obtaining the factors necessary to calculate the positions.
There are 2 arms, of which the starting position of the first arm can be assumed as x0, y0, the ending position as x1,y1. for the arm 2, the starting position will be the ending position of arm 1 which is x1, y1 and the ending position will be x2, y2. let us assume some valve for the lengths of the arms as l1, l2. We know that we can calculate the position of the two arms by calculating the cos and sine values for the angles of the 2 links with respect to the x axis, which can be assumed as theta1 and theta2. Also, let us assume 20 values for theta1 between 1 to 70 degrees and 20 values for theta2 from 1 to 45 degrees.
Now, the position of the arm 2 depends upon the position of arm 1 and in order to calculate that, we need to find the position of arm 1. Now, we can include a nested for loop where the increment i can take the values of length of theta1 which is between 1-20, and returns us the value of theta1 corresponding to the particular i value. similarly for theta2, we can have an incremental operator j. We can store the corresponding theta values with respect to the respective incremental operators as t1 and t2.
Now, for each values of t1, the position of arm 1 is calculated. The end point in x axis is calculated as x1 = l1*cosd(t1) where cosd() is used to get the values in degrees, and y1 = l1*sind(t1).Similarly, the end points for the arm 2 is calculated as x2 = x1 + cosd(t2) corresponding to each values of t2, and y2 = y1 + sind(t2).
Now, to plot the results, we can use plot command and in a single plot command, we can plot both results and provide an appropriate pause time to adjust the timing of the plots. We can also add a counter c before starting the loop and increment it just before ending the loop so that we can obtain the corresponding values to the counter.
To obtain the values in the form of animation, we have to get the frames first, so a frame file named M(c) wrt to the counter is created and the fgrames are obtained using getframe. After closing the loop, we can obtain the animation file in the form of video or a movie file. First, we create a movie file named movie(M), assign a name for the movie file using videofile = videoWriter('..','..') , and open the file using open(videofile). Then we use wirteVideo(videofile, M); to write the animation into the created movie file and close the file using close(videofile) after writing the file. These videofiles will be saved in the particular location which can be accessed later.
The errors occured while writing the program were typo errors such as not providing quotes for linewidth and providing one for the value, using incorrect brackets for linspace command, using hold on command, and not including the movie file in write video command.