Project

Forward Kinematics of a 2R Robotic Arm using Matlab

Aim of the project:-

To write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm

Introduction:-

Kinematics is the relationships between the positions, velocities and accelerations of the links of a manipulator.

The forward kinematics is when the kinematical data are known for the joint coordinates. The variables of the end-effector in a given Cartesian space are to be computed.

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.

A robotic joint provides relative motion between two links of the robot. Each joint, or axis, provides a certain degree-of-freedom (DOF) of motion. In most cases, only one degree-of-freedom is associated with each joint. Therefore the robot's complexity can be classified according to the total number of degrees-of-freedom they possess. Figure 1 shows the graphical representation and placement of frames/links and joints

Figure 1: Joint-link scheme for robot manipulator

The formula for calculating the link arm is shown below in figure 2

Figure 2: Formula for calculating the forward kinematics of a 2-link robotic arm

As seen in the formula to calculate the position for x2, x1 has to be calculated. Using trigonometry, x1 is defined as = length of the first link arm*cos(angle of the first link arm). The same process goes to finding y2. y1 is defined as = length of the first arm * sin (angle of the first link arm).

The code:-

% Forward Kinematics of a 2R robotic arm
clear all
close all 
clc


%Inputs
% length of first arm(m)
l1 = 1;
% length of second arm(m)
l2 = 0.5;
% angle of the first arm
theta1 = linspace(0,90,10);
% angle of the second arm
theta2 = linspace(0,90,10);


% initiate the counter value
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.1 1.5 0 1.5])
    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)

Figure 3: the Matlab code for the project

Code Explanation:-

Before writing the code, it's important to start with clear all, close all and clc commands since

Clear all - removes data from the workspace, freeing up system memory

Close all- deletes the current figure or the specified figure

clc - clears all input and output from the Command Window display, giving you a "clean screen

Next step for the code is to define the inputs and its values. Length of arm 1 and arm 2 were both defined. both theta 1 and theta 2 were defined by using the 'linspace' command. The function of the linspace generates linearly spaced vectors. So in the case of theta 1 and theta 2, linspace(0,90,10) means that Matlab will generate a row vector 90 of 10 points linearly spaced between and including 0 and 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 within the x-axis, 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 2 its own respective angle. The formula for calculating x1, y1, x2 and y2 are shown in figure 2.

The next step is to plot the graph for all the x and y coordinates with axis shown above in figure 3. A pause command was used for pausing the command for a certain time, for this project a pause of 0.03 s was used. 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.

Errors faced while programming:-

The first error I had in the code for this project is I accidentally typed 'Movie' instead of 'movie' since Matlab is a case sensitive program language, commands must be written with the same case. Figure 4 shows the command window message. Once I rewrote the command the code worked fine.

Cannot find an exact (case-sensitive) match for 'Movie'

The closest match is: movie in C:\Program Files\MATLAB\R2020a\toolbox\matlab\specgraph\movie.p


Error in forward_kinematics_with_for_loop (line 48)
Movie(M)

Figure 4: Command window error message

Animation Video:-

Here is the link for the video of the animation that I got for this project.

https://youtu.be/ZHyzfvy8gw0

References:-

  1. https://uk.rs-online.com/web/generalDisplay.html?id=ideas-and-advice/robotic-arms-guide
  2. http://dspace.calstate.edu/bitstream/handle/10211.3/194675/Simulation%20of%20forward%20and%20inverse%20kinematics%20of%20a%20robotic%20arm.pdf?sequence=1
  3. http://www.ece.northwestern.edu/local-apps/matlabhelp/techdoc/ref/linspace.html#:~:text=linspace%20(MATLAB%20Functions)&text=The%20linspace%20function%20generates%20linearly,and%20including%20a%20and%20b.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.