2R Robotic Arm Simulator
A program in python to simulate the forward kinematics of a 2R Robotic Arm
In this program we determine the workspace of a robotic arm with two links . The two links can have different constraints and lengths.
In the code
- First we give the input parameters like length of arms [l1,l2]
- The angle with which the arms can be rotated. We create 'for' loop and define the start and the end angles, after which a equation is solved to give the n angles between the start and the end angles the values are then stored in theta1 and theta2.
- Next we create 2 'for' loops one inside the other Inside the loop ,the first arm's one end is fixed at the origin with coordinates[ie,x0=0 &y0=0] other ends coordinates are calculated using the theta1 values and the arm length1.
- Similarly for arm2 initial coordinates are the final coordinates of arm1 other ends coordinates are calculated from the theta2 values and the arm length2 .
- Plot is made using the coordinate values and each plot is saved with different filenames inside the same for loops.
Program
#2R Robotic Arm Simulator
#importing the library functions
import math
import matplotlib.pyplot as plot
#inputs parameters
#length of arm 1
l1=1
#length of arm 2
l2=0.5
#defining the value of pi
pi=math.pi
#obtaining the range of theta values
ntheta=10
thetastart=0
thetaend=pi/2
theta1=[]
theta2=[]
for i in range(0,ntheta):
temp=thetastart+i*(thetaend-thetastart)/(ntheta-1)
theta1.append(temp)
theta2.append(temp)
#initials coordinates
x0=0
y0=0
#counters
ct=1
#plotting the figure and saving the files inside the loop
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
plot.figure()
plot.plot([x0,x1],[y0,y1])
plot.plot([x1,x2],[y1,y2])
plot.xlim([0,2])
plot.ylim([0,2])
plot.savefig(filename)
The animation created from the above results :click below