2R ROBOTIC ARM SIMULATOR USING PYTHON
1.AIM :
Simulate the forward kinematics of a 2R Robotic arm using Python
2.OBJECTIVES :
Simulate the working of a 2R robotic arm and know its workspace.
3. PROBLEM STATEMENT :
Write a python program to simulate the working of 2R robotic arm by calculating the position of arms varying with angle and thus define the workspace
4.GOVERNING EQUATIONS :
A 2R Robotic arm has 2 links and a manipulator as shown in figure:-
Link 1 is inclined be at an angle θ1 with the base and link 2 be at an angle θ2 with the base.
Let length of link 1 and link 2 be l1 and l2 respectively.
Coordinates of link joints are (x0,y0), (x1,y1) and (x2,y2).
Suppose link 1 is at origin so (x0,y0) will be (0,0), therefore using trignometry the other 2 coordinates will be
`"x1"= "l1 x"cos(theta1)`
`"y1"= "l1 x"sin(theta1)`
`"x2"= "x1"+"l2 x"cos(theta2)`
`"y2"= "y1"+"l2 x"sin(theta2)`
5.PYTHON PROGRAM :
import math
import matplotlib.pyplot as plt
#Inputs
#Length of arms
l1=1
l2=0.5
#no of angles
n_theta=10
theta_start=0
theta_end=math.pi/2
#initializing array for angle
theta1=[]
theta2=[]
#loop to calculate all the angles according to no of angles
for i in range(0,n_theta):
tmp = theta_start + i*(theta_end - theta_start)/(n_theta-1)
theta1.append(tmp)
theta2.append(tmp)
#base of the robot
x0=0
y0=0
ct=1
#calculating end coordinates of arm according to angle
for t1 in theta1:
for t2 in theta2:
#link 1, link 2 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))
#saving name for plot
filename=str(ct) + '.png'
ct=ct+1
#plotting link for the corresponding angle
plt.figure()
plt.plot([x0,x1],[y0,y1])
plt.plot([x1,x2],[y1,y2])
#Specifying limits of graph axis
plt.xlim([0,2])
plt.ylim([0,2])
#saving plot
plt.savefig(filename)
Explanation
- Input variables are initialized for length of both the links/arms, angles of both the links l1 and l2 (theta1 and theta2) which are divided in 10 equal parts from
0 to `pi/2`radians. - A counter variable 'ct' is initialized which increases with every loop execution and will be used for naming plots.
- Using for loop, position of end coordinates of link is calculated corresponding to every angle.
- After calculating position, robot arm is plotted and file is saved by giving every file a unique name using 'filename' variable, this variable uses the counter variable to give a unique name every time.
- All the plots saved are stiched and a gif animation is made.
5.RESULTS :
The animation made from the plots is given below
6.CONCLUSION :
The workspace of the robotic arm can be easily determined by plotting the position of arm at different angles and combining those plots into an animation. Detemining the workspace helps to know the area covered and it will make the designing process easier.