2R Robotic Arm Simulator
2D ROBOTIC ARM SIMULATION
Objective: To write a program in python to simulate the forward kinematics of a 2R Robotic Arm.
Robo arm consists of two links l1 and l2. l1 has a lenght of 1meter and l2 is 0.5meter. One end of the l1 is connected to the base and other end is connected to the l2. Other end of the l2 is connected to manupulator. l1 is at an angle of theta1 and l2 at theta2 to base.
l1,l2 and manupulator has there respective starting co-ordinates as follows
l1 = [x0,y0]
l2 = [x1,y1]
manupulator = [x2,y2]
Inorder to create the robotic arm movement we have to vary the theta1 and theta2 values. However, before changing the values of the positions of the links to be noted.
x0 = 0
y0 = 0
x1 = l1*cos(theta1)
y1 = l1*sin(theta1)
x2 = x1+ l2*cos(theta2)
y2 = y1+ l2*sin(theta2)
Now using the positions of arms we can write the python program.
import math
import matplotlib.pyplot as plt
#inputs
# lenght of the arms
l1 = 1
l2 = 0.5
ntheta=10
thetastart=0
thetaend=math.pi/2
theta1=[]
theta2=theta1
for i in range(0,ntheta):
theta1.append(i*(thetaend)/(ntheta-1))
# base of the robot
x0 = 0
y0 = 0
ct = 1
for t1 in theta1:
for t2 in theta2:
x1 = l1*math.cos(t1)
y1 = l1*math.sin(t1)
# manuoulator location
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)
'Math' is imported to use 'sin' and 'cos' functions. whereas 'mathplotlib' is imported for plotting of the results.
Theta1 and theta2 has an array of 10 values between 0 and 90 degrees.
As per the program the for one variation of theta1, there are 10 variations of theta2. we have almost 100 combinations of arms movement.
Few of the arm movements plots are as follows to understand the links positioning.
In this plot, Blue line is l1 and it is at its intial position and orange line is l2 and it has moved away from its initial position,
In this plot, l1 has moved away form its initial position so does the l2.
When the program runs, it creates filenames that are random which makes creation of animation by stiching the plots become difficult. So a string 'ct' is created which give filenames to the plot in a seqence.
Before creating the animation we have to make a .gif using all the plots that are generated from the program. Imagemagick is used to convert all the plots into .gif file.
Once the .gif file is created then it is converted into a video format using a random tool available on internet.
Animation of the 2D robotic arm.
In this animation we can see a blue line and a orange line both of them represent link1 and link2 i.e..l1 and l2.