Project

Forward Kinematics for a 2R Robotic Arm Simulator using Python

Forward Kinematics for a 2R Robotic Arm Simulator

AIM

  • Write a program in python to simulate the forward kinematics of a 2R Robotic Arm, as shown in the video.
  • Create an animation file of the plot.

CODE

import math
import matplotlib.pyplot as plt


#Inputs
#Length of the twp robotic arms
l1 = 1
l2 = 0.5

#Total number of angles
n_theta = 10

#Calculating values for theta1 and theta2
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 robot
x0 = 0
y0 = 0

ct = 1
for t1 in theta1:
	for t2 in theta2:
		#Link 1, Link2 coneector
		x1 = l1*math.cos(t1)
		y1 = l1*math.sin(t1)


		#Calculation of the coordinates of a manipulator
		x2 = x1 + l2*math.cos(t2)
		y2 = y1 + l2*math.sin(t2)

		filename = str(ct) + '.png'
		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)
		




 

Code explanation:

  1. First both the modules, that is, math and matplotlib.pyplot are imported.
  2. Then, inputs parameters including the length of the two robotic arms, the angle between them and the axes.
  3. Now, values for theta1 and theta2 are calculated and the constraints are provided as theta_start and theta_end.
  4. Next, for loop is used so that values are added to a function temp defined within the for loop.
  5. Now the base of the robot is defined with its coordinates. Link 1 and link2 connector coordinates are also defined as per the formula.
  6. Lastly, filename with .png extension is provided to the plot to be generated and plots are generated and finally, showed.

Output animation

Kindly find the gif file attached.

Result

The graphs were plotted as shown in the screenshot above and the gif was created using the terminal as shown in the video.

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.