Project

Python program to simulate the forward kinematics of a 2R Robotic Arm.

AIM:

To simulate the forward kinematic of a 2R robotic arm

THEORY:

Forward Kinematics uses the kinematic equations to determine the positions of the end effectors. Consider a 2R robotic arm as shown in Figure 1. There are two links whose lengths are l1 and l2. In order to simulate we need the values of the joints. The inclination of the link 1 with the base is given by θ1(theta1) and that of link 2 is given by θ2(theta2).

Figure 1

SOLUTION:

Step1: Calculate the position of link 1

Link 1 has two points-

1.The base of the robot coordinate given by (x0,y0) which is the origin.

2. The endpoint is connected to link 2. Coordinate is given by

(x1,y1)= (l1*cos(theta1),l1*sin(theta1)).

Step 2: Calculate the position of link 2

Link 2 has two points-

1.The endpoint of link 1 which is the first point of link 2. Coordinate is the same as that of the endpoint of link 1:

(x1,y1)= (l1*cos(theta1),l1*sin(theta1)).

2.The endpoint of link 2. Coordinate is given by

(x2,y2)=(x1+l2*cos(theta2),y1+l2*sin(theta2))

PROGRAM:

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):
	temp=theta_start+i*(theta_end-theta_start)/(n_theta-1)
	theta1.append(temp)
	theta2.append(temp)

#Base of the robot
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)

		#coordinates 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)


  

The required modules are imported and the variables are declared. Then the number of values of θ1 and θ2 are declared as 10. The 10 angles are to be appended in an array. The starting value is 0 degree and the ending value is 90 degree. Then 10 values within these intervals are appended into the respective array using a for loop. Now the positions of the joints for each link are found out using the formulas mentioned above. With these positions, the graph is plotted using matplotlib.pyplot module. Each plotted file is given a different name so that the plots won't get overwritten and they are saved as a different file using figure(). The axes are also scaled using xlim() and ylim(). The plots are saved using savefig().

ANIMATION

A sample plot is given below

Hence the simulation of the forward kinematics of a 2R robotic arm was done using python.

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.