Project

Animation for Forward Kinematic of 2R robotic arm

Objective:

To create an animation representing the movement of the links i.e Forward Kinmetic of the 2R robotic arm describing its workspace with the help of MATLAB program.

Description:

We can plot the position of the kinematic link for a single position if a single value of angle made by link 1 and link 2 with the axis is mentioned. But, in order to create an animation we will have to capture the plot for different positions of links i.e different values of angle made by the links with the axis.

In order to create an animation we will need to define the following inputs,

Length of 1st link, L1 = 2m,

Length of 2nd link, L2 = 1m,

Angle made by 1st link with X-axis, theta1(variable) = 0° to 90° (varying with a step of 10°)

Angle made by 2nd link with X-axis, theta2(variable) = 0° to 90° (varying with a step of 10°)

The program for creating the animation can be written as follows,

% Program for creating animation for Forward kinematics of 2R robotic arm manipulator
clear all
close all
clc

% Inputs
% Length of kinematic arms(m)
L1 = 2;
L2 = 1;

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);
        
        x0  = 0;
        y0 = 0;
        
        x1 = L1*cosd(THETA1);
        y1 = L1*sind(THETA1);
        
        x2 = x1 + L2*cosd(THETA2);
        y2 = y1 + L2*sind(THETA2);
        
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
        axis ([-0.1 3.5 -0.1 3.5])
        pause(0.05)
        M(ct) = getframe(gcf);
        ct = ct+1;
    end
end

movie(M)
videofile = VideoWriter('Forward_kinematic.avi','Uncompressed avi')

open(videofile)
writeVideo(videofile,M)
close(videofile)

The steps for the program can be explained as follows,

1) Firstly the command for clearing the earlier defined value will be executed and then the input parameters are defined i.e "L1, L2, theta1 and theta2".

2)In order to create the animation we will require different postion of links, which mean different values of theta1 and theta2

3) The value of theta1 and theta2 are defined with command lindspace between 0° to 90°, the values for theta1 and theta 2 are as follows,

4) We need to use for loop in order to create an animation, hence defined the loop operator.

5) For the 1st for loop, we have defined the loop operator 'i' as,

i = 1:length(theta1)

THETA1 = theta1(i)

here, 'i' will have 10 values and also the THETA1 will have values varying from 0° to 90° with an increasing step of 10°. But for the animation to take place we will have to use 2nd for loop as we have 10 different position of link 2 for each position of link 1.

6) For the 2nd for loop, we have defined the loop operator 'j' as,

j = 1:length(theta2)

THETA2 = theta1(j)

here, 'j' will have 10 values and also the THETA2 will have values varying from 0° to 90° with an increasing step of 10°. So, there will be as many as 100 different positions of link 1 and 2 in the animation.

7) Now, we have to define the start and end coordinates of link 1 and link 2 in order to plot the postion of both the link.

8) After the coordinates are defined, plot command is used to plot the position of links.

9) Now, a variable M is defined to capture the postion frame by frame. Note that a counter variable 'ct' should be defined before the start of the for loop in order to capture animation frame by frame. Also, after the variable M has been defined, we have to increase the counter value by 1 to proceed to the next frame.

10) After ending both the for loop, we have defined the command to save the video file.

11) The link for animation of the above wrutten program is,

Hence the animation ahs been completed and we can observe the movements of kinematic links.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

While writing the program I encountered with one issue and that was I had not defined the counter 'ct' value due to which I was not able to capture the animation.

Following was the result,

The issue got rectified when I had defined the counter value and I was successfully able to capture the animation as desired.

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.