Project

SIMULATION OF FORWARD KINEMATICS OF A 2R ROBOTIC ARM USING PYTHON

AIM: To Write a program in python to simulate the forward kinematics of a 2R Robotic Arm.

OBJECTIVE:

  • To write a program to create motion of both the arms.
  • To create plots of different positions of the arms.
  • To create an animation of the plots.

INTRODUCTION:

A 2R robotic arm is a type of mechanical arm with similar functions to a human arm. The image of the arm is as shown below.

Image for 2R robotic arm

In this we have two links connected by joints and these links allow the motion of the arm.

The robotic arms functions based on the program that is applied to it .so here we are going to make use of python to write the program.

INPUT PARAMETERS:

So the inputs that we are going to use in the program are as follows.

  • l1=length of link 1
  • l2=length of link 2
  • theta 1=Angle between link 1 and base
  • theta 2=Angle between link 2 and base
  • (X0,y0)= starting point of link 1
  • (x1,y1)=end points of link 1and starting points for link 2
  • (x2,y2)=end points for link2.

BODY OF THE CONTENT:

We will use Python as the programming language.

CODE:

# Forward Kinematics of a 2R robotic arm
import math
import matplotlib.pyplot as plt

#inputs
l1=1
l2=0.5

n_theta=10
theta_start=0
theta_end=math.pi/2
theta1=[]
theta2=[]

for i in range (0,n_theta):
	tmp=theta_start+i*(theta_end-theta_start)/(n_theta-1)
	theta1.append(tmp)
	theta2.append(tmp)

x0=0
y0=0

ct=1
for t1 in theta1:
	for t2 in theta2:
		#link1,link2 connector
		x1=l1*math.cos(t1)
		y1=l1*math.sin(t1)

		#co-ordinates of manipulator
		x2=x1+l2*math.cos(t2)
		y2=y1+l2*math.sin(t2)

		filename= str(ct)+ '.png'
		ct=ct+1
		plt.figure()
		plt.plot([x0,x1],[y0,y1])
		plt.plot([x1,x2],[y1,y2])

		plt.xlim([0,2])
		plt.ylim([0,2])

		plt.savefig(filename)

EXPLANATION:

  • We first start with importing the math module and the plotting library.
  • Then we define the required inputs.
  • Further we define n_theta equal to 10 so as to run the for loop and get different angle for theta1 and theta2
  • Then we define the starting angle and the ending angle which helps to define the motion of links i.e form which angle they start and upto which angle they make the motion.
  • Then we define empty arrays for storing values for theta1 and theta2.
  • For changing the angles of the link we are using the for loop, every time the loop gets executed the values of theta1 and theta2 gets stored in the respective array.At the end theta1 and theta2 array's are stored with ten different equally spaced values between 0 and 90 degrees.
  • For each value of theta1 there are 10 values of theta2 as the orientation of the link1 and link 2 are independent of each other.
  • Hence making use of nested for loop.For every 10 values of each theta1and theta2, the for loop gets executed and the position of the end effector is obtained.
  • For saving the images of the different orientation of link 1 and link 2we are making use of counter.
  • Every time the nested for loop gets executed ,the counter gets incremented by 1,due to which the different images gets saved.
  • As there are ten different values of theta1 and theta2 , 100 images get saved.
  • For making the Gif of the 100 saved images we make use of imageMagick and then we convert the GIF to animation.
  • For making the GIF we first open the cmd and then we give the command to move to the directory where the 100 images are saved and then once the directory is located we use "magick .*png 2R.gif" to create the gif and save it.The Gif gets automatically saved in the folder where the images are saved.

Output:

The output was in a form of video and the link for which is being given below

https://www.youtube.com/watch?v=wRPRL0j_34k

Conclusion:

  • So using the python program we were able to get an animation of the different positions of the robotic arm.
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.