Forward Kinematics of a 2R Robotic arm using Python
Objective : To simulate the forward kinematics of a 2R Robotic Arm using Python.
Proedure :
Let,
- l1 and l2 be the length of link 1 and 2 respectively.
- theta1 and theta2 be the angle made by the links 1 and 2 w.r.t horizontal.
- theta1 and theta2 is assumed to take values from 0 to 90 degrees.
0≤theta1≤90 , 0≤theta2≤90
The following is the code for the 2R Robotic arm :
import math
import matplotlib.pyplot as plt
# INPUTS
l1 = 1
l2 = 0.5
n=10
ts = 0 # theta start
te = math.pi/2 # theta end
theta1 = []
theta2 = []
for i in range(0,n) :
tmp = ts + i*(te-ts)/(n-1)
theta1.append(tmp)
theta2.append(tmp)
x0 = 0
y0 = 0
ct=1 # counter variable used to create images with different filename !
for t1 in theta1 :
for t2 in theta2 :
x1 = l1*math.cos(t1)
y1 = l1*math.sin(t1)
x2 = x1+l2*math.cos(t2)
y2 = y1+l2*math.sin(t2)
filename = str(ct) + '.png'
ct = ct+1
plt.figure()
plt.xlim([0,2])
plt.ylim([0,2])
plt.plot([x0,x1],[y0,y1])
plt.plot([x1,x2],[y1,y2])
plt.savefig(filename)
- x0,y0 indicates the origin/starting points of arm/link 1.
- x1,y1 indicates the end points of arm/link 1.
- x2,y2 indicates the end points of arm/link 2.
FIGURE :
The above figure shows the position of the linkage at a given point of time for a particular value of theta .
" No errors were found during the execution of the code "
Video link :
For the better understanding of the simulation please refer to the video link provided below :