Simulation of the forward kinematics of a 2R Robotic Arm Using PYTHON.
Objective: Write a program in python to simulate the forward kinematics of a 2R Robotic Arm.
Procedure:
Theta 1 is at origin (0,0)
Theta 2 is at (x1,y1)
The desired location is at (x2,y2)
so equation comes out to be:
x2 = x1 + l2 cos (theta 2)
Y2 = Y1 + l2 sin (theta 2)
1. Open the sublime text file and first save the file with the extension .py.
2. Import some modules into the command window like:
- import matplotlib.pyplot as plt
This module helps us to plot the graph of the given result.- import math
This module helps us to use mathematical functions like pie, sine, cos, etc.- import numpy
This module helps us to do mathematical operations on an element of an array.
Whenever we want to define more than one value in a single variable we have to use an array and numpy.linspace is also the same thing it stores value in a range like this;
numpy.linspace (lower limit, upper limit, no. of division)
This works acc. to a formula:
Size of division = (upper limit-lower limit)/(no. of division-1)
3. Use an empty array for storing a value which we get from FOR loop, theta1 & theta2 will store 10 values.
4. Append is a command that will continuously append the value of a variable. It keeps on adding the values into an existing array.
so for that, we have to define an empty array for that variable which we are going to use append command. Temp is a variable in which we store values from append command.
5. use plt.plot() command to plot the graph of the given result and use plt.figure() command.
For proper animation we use .plt.xlim & plt.ylim command.
Coding:
import math
import matplotlib.pyplot as plt
import numpy
#Link length
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)
# base of the roots
x0 = 0
y0 = 0
ct=1
for t1 in theta1:
for t2 in theta2:
# link 1, link 2 connector
x1 = l1*math.cos(t1)
y1 = l1*math.sin(t1)
# coordinates if the manipulator
x2 = y1 + l2*math.cos(t2)
y2 = y1 + l2*math.sin(t2)
filename = 'test%05d.png' % ct
ct=ct+1
print(filename)
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)
Output small preview:
After receiving the output images combined them using Image magick to form GIF.
Steps:
Animation GIF: