Project

Robotic Arm Simulation In Python (2D)

2D Animation In Python:

A basic robotic arm has two essential parts: the main arm and the manipulator. The main arm is the backbone or the support and can rotate with the base and lean in or out based on the requirements. Thus, the arm defines the reach of the robot. The manipulator or grabber is the end part attached to the arm which serves the main purpose of the robot. The manipulator can be attached with a tool like a welder, a grabber, a cutter, cutting tool, punch, drill etc, depending upon the use of the robot.

(Source: https://bit.ly/2PcmPlI)

The motion of a robotic arm as shown in the above picture can be animated in Python using the coordinates of the base, the arm end, and the manipulator. The motion is then generated by varying the incremental angle (step) through which the arm can move.

Traditional 2D animation involves moving images, called frames, to create a sense of motion. In Python, we can create figures, which have a particular drawing in them, to develop the images for an animation. The drawings can be governed by some mathematical relation. Lastly, the figures, which serve as frames can be appended together to create a gif animation.

To create such an animation in Python, certain libraries/modules are required. The ones which I have used are:

1) math:

This module comes with Python 3 and is used to input various mathematical functions like trigonometric, exponential, logarithmic, floor, ceiling, factorial, modulus, hyperbolic and special. The module also includes constants like pi, e, inf, nan etc. In my program, I need the constant 'pi' and the sin and cosine functions.

2) matplotlib:

Matlplotlib.plot is a collection of command style functions that make matplotlib, a Python 2D plotting library, work like MATLAB. This module can plot a figure based on coordinates, add a legend, define figure axes, add a title, a marker etc.

3) Imageio:

Imageio is a Python library that provides an easy interface to read and write a wide range of image data, including animated images, volumetric data, and scientific formats. [source:http://imageio.github.io/].

The main functions from Imageio that are required to create the animation are:

a) imread(): reads the image (singly).

b) mimsave(): saves a series of images.

Additionally, the function 'mimread()', reads a series of images.

Program

Predefined terms.

Length of the arm = 1 unit.

Length of the manipulator = 0.5 units.

Angle increments = 10 degrees

Total angle change = 0-90 degrees.

To create the animation in Python, we can set the base of the arm at (0,0). The coordinates of the end of the arm can be calculated by the arm length and the trigonometric functions of the incremental angles with respect to the positive X-axis. Same is done with the manipulator. The manipulator end is fitted with a Diamond shape to represent a tool.

In the program, I have created ten angles for the arm and ten for the manipulator. Hence, there will be 10*10 = 100 images. The function 'mimsave' of the 'imageio' module pins all the images into a gif animation.

Code:

### Robotic Arm Simulator

#1 Importing Modules/libraries

import math as M
import matplotlib.pyplot as plt
import imageio as S

#2 Defining the Lengths of Arm and the Manipulator:

l1=1
l2 = 0.5


#Base Cordinates:

x0=0
y0=0


#3 Developing Angles:

start = 0   		#starting angle
inc = M.pi/18		#increments
numbs = 10			#no of angles


''' Here the start angle = 0 and with 10 deg increments we achieve the angle range of 0 - 90 degrees.'''


t1=[] #preallocating an empty array

#Creating the line-space for the angles

for i in range(numbs):
	nn= start + i*(inc)
	t1.append(nn)
''' append function is used to attach or concatenate objects in a single array. It creates a list/series of objects'''

t2=t1 #here, line-space for manipulator angles is the same as that of the arm



#4 Finding The Coordinates and Plotting

images=[] #preallocation

c = 1 #counter variable

for i in t1:
	for j in t2:

		#Arm end coordinates
		x1 = l1*M.cos(i)
		y1 = l1*M.sin(i)

		#manipulator end coordinates
		x2 = x1 + l2*M.cos(j)
		y2 = y1 + l2*M.sin(j)

		#Creating an array for image names
		filename ='C:/Users/Adnan/Desktop/Learn/Lync/Python/Programs/Robotic Arm Figures/' +str(c) + '.png'
		
	
		#Plotting the figures

		plt.figure()
		plt.plot([x0,x1], [y0,y1])		  #plots the arm
		plt.plot([x1,x2], [y1,y2])		  #plots the manipulator
		plt.plot([x2],[y2], marker="D")   #marker to represent a tool
		plt.legend(['Arm','Manupilator']) #legend to label the curves
		plt.axis((-.1,1.7,0,1.7))		  #setting the figure area
		plt.savefig(filename)             #saves the figures

		
		# Creating an Image list
		images.append(S.imread(filename))
	

		c = c+1 #counter set for the next iteration
		

#5 Creating gif Animation

S.mimsave('C:/Users/Adnan/Desktop/Learn/Lync/Python/Programs/Robotic Arm Figures/anime.gif',images)

'''mimsave saves the images captured from the 'images' variable into a gif animation'''

The gif created from the above code can be seen here. [link = https://giphy.com/gifs/simulation-python-robotic-arm-manipulator-6AdMMYEvjMvfuRk4MC].

AddOns:

1) Another method to create a line -space is to use the 'linspace()' function from the "numpy" module. Syntax: Numpy.linspace(start_num, end_num, Increment). Example:

#Creating A Line-Space

import Numpy

t = Numpy.linspace(0,90,10)
print(t)

Output = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

2) It is only necessary to create the images or frames for the animation. Appending, or creating a gif from the images can be done through various software or by online platforms like Giphy.

***END***

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.