Week 2- 2R Robotic Arm Challenge
Programme:
%Forward kinematics of a 2r robotic arm
close all
clear all
clc
%inputs
l1=1;
l2=1.5;
theta1=linspace(0,90,4)
theta2=linspace(0,90,4)
ct=1;
for i=1:length(theta1)
THETA1=theta1(i);
for j=1:length(theta2)
THETA2=theta2(j);
x0=0;
y0=0;
x1=l1*cosd(THETA1);
y1=l1*sind(THETA1);
x2=x1+l2*cosd(THETA2);
y2=y1+l2*sind(THETA2);
%plotting
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2], 'linewidth',3)
axis([0 2.5 0 2.5])
pause(.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)
Aim:
To write a programme in MATLAB to simulate the forward kinematics of a 2R Robotic arm
Objectives:
- Write a programme to simulate the forward kinematics of a 2R robotic arm
- To plot the forward kinematics of 2R robotic arm
- To create an animation file of the plot
Code explanation:
Start programme with clear all, close all, clc. These commands used to removes data from the workspace, freeing up system memory, deletes the current figure or the specified figure, clears all input and output from the Command Window display, giving you a "clean screen respectivly.
Then next step is to define the input the values, length of link1 and link2 were defined as l1 and l2, both theta 1 and theta 2 were defined by using the 'linspace' command. The function of linspace command is to generate linerly space vector. So here linspace(0,90,4) generates linearly spaced vector having the values between and including 0t o 90.
The next step is to set up a counter. A counter is a variable that counts an event in a loop. A for loop was used for this project, a for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.Since theta 1 and theta 2 will contain multiple values of the angle , a new variable is needed inside the for loop for both x and y coordinates. THETA 1 and THETA 2 are added to the code, for THETA 2 the variable j was added instead of i as this will give THETA 1 its own respective angle.
Then the equations for calculating x1,y1,x2and y2 are added(x1=l1*cosd(THETA1),y2=l1+l2*sind(THETA2)
The next step is to plot the graph for all the x and y coordinates of the links l1&l2. The pause command was used for pausing the command for a certain time.The M command was used to capture the current frame every time the for loop runs. The final step of the loop is to add a +1 to the ct variable, this will increment the frame every time the for loop run. End command was used for the plot and for the for loop to stop both from running.
The final step of the code is to create a movie of the animation of the robotic arm, so the movie command was used.The VideoWriter command was used to assign the video format, for this project .avi was used as the format. The open command was used to open the video. writevideo command was used to write the video with plot frames. Finally, the close command was used to close the video file.
Output:

The errors faces while programming:
The error I had in the code was I typed videowriter instead of VideoWriter and writevideo instead of writeVideo. Then I noticed error in the command box and i realised that are case sentive key words, and corrected it.

Animation Video:
Here is the link for the video of the animation that I got for this project.
https://drive.google.com/file/d/1V78886TFklYRvfAe9xmtN19VX5lDelAD/view?usp=sharing