Project

Stimulation of a 2R Robotic Arm by Forward Kinematics method using Python & MATLAB

OBJECTIVE:

To program and simulate Forward Kinematics of a 2R robotic arm using Python & MATLAB

THEORY

Kinematics

Kinematics is the science of motion. In a two-joint robotic arm, given the angles of the joints, the kinematics equations give the location of the tip of the arm. With the given desired coordinate, the problem reduces to finding the two angles involved.

  • The first angle is between the first arm and the base.
  • The second angle is between the first arm and the second arm.

Co-ordinates, length & angle for the linkages

To calculate the position of the base, link 1 and link 2, the following co-ordinaters have been determined along with length and angle of the linkage.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MATLAB~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MAIN PROGRAM:

clear all
close all
clc

%Forward Kinematics of a 2R Robotic arm
%Input Parameters
l1 = 2;
l2 = 1;

thetha1 = linspace(0,90,10);
thetha2 = linspace(0,90,15);

ct = 1;

%Using for loop for executing all combinations
for i = 1:length(thetha1)
    THETHA1 = thetha1(i);
    for j = 1:length(thetha2)
    THETHA2 = thetha2(j);

    %Coordinates at base
    x = 0;
    y = 0;

    %Coordinates at link 1
    x1 = l1*cosd(THETHA1);
    y1 = l1*sind(THETHA1);

    %Coordinates at link 2
    x2 = x1+l2*cosd(THETHA2);
    y2 = y1+l2*sind(THETHA2);

    %Plotting of Coordinates
    plot([x x1],[y y1],[x1 x2],[y1 y2],'linewidth',2);
    axis([-0.5 4 0 4]);
    pause(0.01)
    M(ct) = getframe(gcf);
    ct = ct + 1;
    end
end

%Creating of Animation
movie(M)
videofile = VideoWriter('forward_kinematics_of_Robotic_arm.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Explanation :

  • Matlab program is started with standard clear all, close all, clc commands for a new start.
  • The input parameters such as length of the linkage's has been given for the robotic arm and in order for the motion to be smooth linspace command has been generated for the thetha value varying from 0 to 90 degrees having 10 values for linkage 1 and 15 values for linkage 2.
  • Then the counter command ct is been intialized for value 1.
  • In order for the motion to occur repeatedly in a pattern for every angular motion for loop has been assigned for both the linkages (i.e) i & j respectively for the length of values in linspace command.
  • It has been assigned to the variable THETHA1 & THETHA2.
  • Now the values for x, y, x1, y1, x2, y2 coordinates has been assigned to the workspace.
  • The plot command is used to plot all the coordinates for animating along with line thickness of 2 and assigning the axes for a much clear visibilty of the graph.
  • So the video file is now saved with each individual frame in M.
  • end is used to terminate the for loop program.
  • Animation file is now done using the movie command, where the movie file is saved under avi command for individual frames.
  • open(videofile) and close(videofile) command is used to open & close the videofile.
  • Hitting F5 gives the output of the program and creates an animated video which is saved in the destination folder.

OUTPUT:

1)Code output in workspace

2)Plot output screenshot

Animation of the Robotic Arm Movement

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~PYTHON~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MAIN PROGRAM:

import math
import matplotlib.pyplot as plt

#Input Parameters
#Linkage in meters
l1 = 2
l2 = 1

#for determining 10 times angle of l1 & l1
n_theta = 10 
theta_start = 0
theta_end = math.pi/2

#creating empty arrays
theta1 = []
theta2 = []

#To calculate the value of theta1 & theta2
for i in range(0,n_theta):
	a = theta_start + i*(theta_end - theta_start)/(n_theta - 1)

	#to store the values of theta1 & theta2
	theta1.append(a)
	theta2.append(a)

#Counter value
ct = 1

#To generate image for the animation of arm movement
for THETA1 in theta1:
	for THETA2 in theta2:

		#Coordinates at base
		x = 0
		y = 0

		#Coordinates at link 1 & link 2 connection
		x1 = l1*math.cos(THETA1)
		y1 = l1*math.sin(THETA1)

		#Coordinates at Link 2 & Manipulator connection
		x2 = x1 + l2*math.cos(THETA2)
		y2 = y1 + l2*math.sin(THETA2)

		#To name the images in order accordingly by numbering
		filename = str(ct) + ' .png'
		ct = ct + 1

		#Plotting
		plt.figure()
		plt.title('Forward Kinematics of 2R Robotic Arm')
		plt.plot([x,x1],[y,y1])
		plt.plot([x1,x2],[y1,y2])
		plt.xlim([0,3.5])
		plt.ylim([0,3.5])
		plt.savefig(filename)

Explanation

  • import math module is imported which is needed to use the trignomentric functions ((i.e)in this program sine & cosine).
  • Now the plot library is imported using matlablib.pyplot as plt as such whenever plt command is used it refers to the plot for plotting various parameters.
  • Input parameters as been assigned for the length of the linkages as l1 & l2 for 2 & 1 meters respectively.
  • thetha value is determined for intializing the turn of angles for the arm with total value of 10 in between angles 0 to 90 degree given with n_theta, theta_start, theta_end. 'math' is used here to define pi value.
  • Empty arrays for theta1 & theta2 has been created to store values.
  • for loop is been called to calculate the values of theta1 & theta2 which is stored with 10 different equally spaced values between 0 to 90 degrees.
  • For every value of theta1 there is 10 values for theta2. This goes on for all the 10 values for theta1 which gives us the position of manipulator which is done by append command
  • Counter command ct is used to save the image to be generated.
  • This is now done by for loop which executes ct for new nested for THETA1 & THETA2.
  • Coordinaters at base, link 1 & 2 connector and link 2 & Manipulator connector is given.
  • To assign each of the 100 image different names, numbering is done by incrementing ct by 1 and saving it with str command.
  • For better viewability xlim & ylim command is used and the image has been plotted for varying postion of Robotic arm.

OUTPUT:

For creating gif from images:

  • Suitable version of imageMagik is downloaded and installed.
  • Open Command Prompt (windows key+R, type "cmd" and hit enter) in your system.
  • Navigate to the working directory that contains the output images. In my case it is D drive, so d: is given to change the drive.
  • Now the directory of the file in D drive is copied from the address and pasted with command cd.

  • To confirm that you have reached the target directory, use the 'dir' command to list all the contents of the directory.

  • The following commands are written in the terminal to create the animation file.
    magick *.png Animation.gif

  • The file is now saved as a gif in the same directory.

Animation of the Robotic Arm Movement

RESULT:

The animation of the 2R robotic arm has been executed with the given parameters using both Python and MATLAB.

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.