Simulating Forward Kinematics of a 2R Robotic Arm using Python program.
AIM:
Simulating Forward Kinematics of a 2R Robotic Arm using Python program.
OBJECTIVES:
- Write a program in Python to simulate the forward kinematics of a 2R Robotic Arm.
- Creating an animation from the plots obtained in the program.
- Understand the movement of an 2R Robotic in 2D workspace.
THEORY:
A robotic arm is a type of mechanical arm, usually programmable, with similar functions to a human arm; the arm may be the sum total of the mechanism or may be part of a more complex robot. The links of such a manipulator are connected by joints allowing either rotational motion or translational displacement. The links of the manipulator can be considered to form a kinematic chain. The terminus of the kinematic chain of the manipulator is called the end effector and it is analogous to the human hand.
Forward kinematics refers to the use of the kinematic equations of a robot to compute the position of the end-effector from specified values for the joint parameters.
Robotic arms used in various industrial applications such as drilling, welding, metal cutting, painting etc.
Here we have a 2R Robotic arm as shown in the figure:
The link-1 is attached to the base and end of link one is connected to link-2. And end of link-2 is connected to the end effector which performs the desired operation. In this project we will restrict the movement of the links in a 2D plane.
PROGRAM:
Programming language used is Python.
# Program for Forward Kinematics of 2R Robotic Arm
import math
import matplotlib.pyplot as plt
# Length of link 1
L1 = 1
# Length of link 2
L2 = 0.5
# Defining angular range of link1 & link2
theta_start = 0 # starting angle = 0 degrees
theta_end = 90*(math.pi / 180) # end angle = 90 degrees
# Defining number of intervals between theta_start & theta_end
n_theta = 10
# Angle traced in Radians by link 1 with respect to Horizontal X axis(ranges from 0 to 90 degrees)
theta1 = []
# Angle traced in Radians by link 2 with respect to Horizontal X axis(ranges from 0 to 90 degrees)
theta2 = []
# for loop to calculate values of theta1 & theta2
for i in range(0,n_theta):
angle_in_degrees = theta_start + i*(theta_end - theta_start) / (n_theta - 1) #calculating value of interval in n_theta
theta1.append(angle_in_degrees) # Incrementing theta1
theta2.append(angle_in_degrees) # Incrementing theta2
# Initial co-ordinates of link 1 at fixed pivot end in X & Y axis will be (0,0)
x0 = 0
y0 = 0
ct = 1
for THETA1 in theta1: # for loop for any value THETA1 in range of theta1 values
for THETA2 in theta2: # for loop for any value of THETA2 in range if theta2 values
# Calculating the coordinates of end point of link-1
x1 = L1 * math.cos(THETA1) # X co-ordinate of amother end of link 1 which is starting co-ordinate of link2
y1 = L1 * math.sin(THETA1) # Y co-ordinate of amother end of link 1 which is starting co-ordinate of link2
# Calculating the coordinates of end point of link-2
# End coordinates for link-1 are the start coordinates for link-2
x2 = x1 + L2*math.cos(THETA2) # Final X co-ordinate of link 2
y2 = y1 + L2*math.sin(THETA2) # Final Y co-ordinate of link 2
filename = '%05.d.png' %ct # defining filename, so that plot created can be stitched sequentially to create animation
ct = ct + 1 # interval between two images
plt.figure() # To plot each figure in new frame so as to get clear idea about movement of Arm for different combination of theta1 & theta2
plt.plot([x0,x1],[y0,y1])
plt.plot([x1,x2],[y1,y2])
plt.xlim([-0.5,1.8])
plt.ylim([0,1.8])
plt.title("Simulation of Forward Kinematics of a 2R Robotic arm using Python")
plt.savefig(filename)
EXPLANATION OF THE PROGRAM:
Length of Link 1 = L1
Length of Link 2 = L2
theta1 = Angle in Radians of link 1
thata2 = Angle in Radians of link 2
x0, x1, x2 = X co-ordinates of links at node points 1, 2 & 3.
y0, y1, y2 = Y co-ordinates of links at node points 1, 2 & 3
- Lengths of both the links are taken as input. Length of link-1 (L1) is taken as 1m and length of link-2 (L2) is taken as 0.5m.
- Angular range traced by both the links, is defined (which is 0-90 degrees). The angles traced are varied over a range to simulate the movement of the robotic arm.
- Using a for loop we allot theta1 and theta2, 10 values between 0 to 90 degrees.
- Base position of link-1 (`x_0,y_0`)is considered as origin (0,0).
- Then two for loops are used to iterate over the range of angles traced by both the links. For each particular angle of link-1 , link-2 is moved over its entire range , to obtain different positions of end effector at that angle of link-1.
- For a particular value of and , the end position of link-1 is calculated by these equations.
`x_1=x_0+L_1*cos(θ_1 )`
`y_1=y_0+L_1*sin(θ_1 )`
The end position of link-1 is also the start position of link two. The end position for link-2 is calculated by these equations.
`x_2=x_1+L_2*cos(θ_2 )`
`y_2=y_1+L_2*sin(θ_2 )`
- Then for each particular position of lin-1 and link-2 , a plot is made which is then captured frame by frame.
- Then we can use command line tool “Cygwin” and “imagemagick” program tool to combine all these positions to form an animation.
RESULTS:
Upon executing the given code, it will generate 100 plots due to iterating it for ten times for two different angles. The following plot is obtained as one of the outputs:
OUTPUT:
The output obtained in form of an animated video clip can be viewed by visiting the link mentioned below.
CONCLUSION:
Using plots obtained as result of executing python program, we can calculate work space required of
movement of 2R Robotic Arm.
A simulation of the forward kinematics of a 2R Robotic Arm in 2D workspace was created using Python program.