Forward Kinematics of 2R robotic arm using Python
Abstract:
A 2R robotic arm contains two revolute pairs or two links. This simulation can figure out possible locations that the robotic arm. In the workspace, the arm rotates from 0° to 90°. For each position of link-1, there can be infinite positions of link-2 from 0° to 90°. Here, the python code is used to generate various positions of the robotic arm for different angles of links 1 and 2.
Steps Involved:
- The python libraries named math and math and mathplotlib has been imported to use the math and plot functions
- Lengths of the link were defined, where l1 and l2 are lengths of links 1 and 2 respectively. The number of `theta` values have been defined along with maximum and minimum values
- Using a for loop all the values of `theta1` and `theta2` were stored in an array using the append function
- Now the initial and final coordinates of both the links were defined. Two separate for loops have been written to extract individual values of `theta1 and theta2`
- As there are a total of 100 figures each of the file is saved with the number corresponding to it and saved in the folder using savefig function
PYTHON CODE:
Below is the python code to get the figures of different positions of the 2R robotic arm.
#Program to simulate forward kinematics of a 2R robotic arm
#Importing math library
import math
import matplotlib.pyplot as plt
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:
#End point of link 1/ Start point of link 2
x1 = l1*math.cos(t1)
y1 = l1*math.sin(t1)
# End point of link 2
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)