Project

Forward Kinematics of a Robotic Arm

%Program to simulate Forward Kinematics of a 2R Robotic Arm Manipulator

clear all
close all
clc

%Inputs
%Lengths of the arm
l1 = 1;
l2 = 0.5;
%Angles made by the robotic arm
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
ct = 1;

%Position of the arm calculation
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.5 1.5 0 1.5])
    grid on
    xlabel ('X Axis')
    ylabel('Y Axis')
    pause(0.05)
    M(ct) = getframe(gcf);
    ct = ct+1;


    end
end

%Animation video of the plot
movie(M)
videofile = VideoWriter('Robotic_arm_challenge.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Aim: To simulate the Forward Kinematics of a 2R Robotic Arm Manipulatior

Code Explanation:

To calculate the position of the robotic arms, we need the following inputs

length of arm 1 - l1

length of arm 2 - l2

the angle the arm 1 is making w.r.t the X-axis - `theta`1

the angle the arm 2 is making w.r.t the X-axis - `theta` 2

To calculate the positions, the inputs have been taken as follows

l1 = 1m

l2 = 0.5m

and the angles `theta`1 and `theta`2 are calculated by the linspace command, with equal intervals between 0 and 90 degrees`

created a varible ct = 1 to be able to count the no.of frames generated

by creating a for loop each for the value of the value of `theta`1 and `theta` 2, the value at that particular count is assigned ti the variable THETA1 and THETA2 respectively

the co-ordinates for the starting point will be (0,0). Hence x0 = 0 and y0 = 0

The coordinate x1 will be the length of the arm 1 times cos(THETA1). Similarly y1 will be the length of arm 1 times sin(THETA1)

The coordinate x2 will be calculated from the point x1 by adding the length of arm 2 times cos(THETA2). Similarly y2 will be calculated from y1 plus the length of arm 2 times sin(THETA2)

To plot these values, we use the plot command and give the input the values of [x0 x1],[y0 y1],[x1 x2], [y1 y2]

Set the x-axis values to range from -0.5 to 1.5 and the y-axis values to range from 0 to 1.5

Using the pause command, set the timing at 0.05 to obtain a smooth animation of the plot

Labes the axes using their respective commands, i.e xlabel and ylabel

Using the getframe command, the frame generated for each count is assigned to a variable M.

Once the variable is assigned, the count variable ct is updated.

To create an animation, we need create a file 'videofile' usiing the VideoWriter command and define the file name and the type of the file to be created.

We then open tthe file using the open(videofile) command and write data onto the file using hte writeVideo(videofile,M). We then close the file using the close(videofile) command.

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.