Program to simulate the Forward Kinematics of 2R Robotic Arm using Python
Program to simulate the Forward Kinematics of 2R Robotic Arm using Python
Forward kinematics means the application of the kinematic equation of a robot to compute the position of the end-effector i.e. end of the second link, considering the 2R manipulator from specified values for the joint parameters.
Aim: To find the position and orientation of the end effectors of the planer 2R robotic arm (manipulator) by knowing the fixed parameters of links and joint variable angles.
Data:
Length of link 1 = l1 = 1m
Length of link 2 = l2 = 0.5m
Angle of link 1 = [0,90] = theta1
Angle of link 2 = [0,90] = theta2
Link 1 and Link 2 can orient in any angle between 0 and 90 degrees including.
Solution:
1) First, fix the one end the link1 to the origin (0, 0).
2) Changing the theta1 and theta2 both ten times
3) For changing the angles of the link, we are using the ‘for’ loop every time the loop has executed the values of theta one, and theta 2 are stored in the respective array. Now, the theta 1 and theta 2 arrays are stored with the ten different equally spaced values between 0 and 90 degrees.
4) For every value of theta 1, there are 10 values of theta 2 as the orientation of the link 1, and link 2 are independent of each other.
5) Hence making use of ‘nested-for’ loop. For every 10 values of each theta 1 and theta 2, the ‘for’ loop gets executed and the position of the end-effector is obtained.
6) For saving the images of the different orientation of link 1 and link 2, we are making use of the counter.
7) Every time the ‘nested-for’ loop gets executed, the counter gets incremented by 1. Due to which the different image gets, saved.
8) As there are ten different values of theta 1 and theta 2, 100 images are saved.
9) For making the GIF of the 100 saved images, ImageMagick is used.
Program:
#program to simulate the forward kinematics of the 2R Robotic Arm
#by MEGHRAJ VAIDYA
import math
import matplotlib.pyplot as plt
# fixing the one end of the link1 to origin(0,0)
x0 = 0
y0 = 0
theta1 = [] #empty arrey
theta2 = [] #empty arrey
#input
#fixed parameters of link
l1 = 1 #in meter
l2 = 0.5 #in meter
theta_start = 0
theta_end = math.pi/2
#for changing the angles of link1 and link2 10 times each
n_theta = 10
for i in range(0,n_theta):
c = theta_start + i*(theta_end - theta_start)/(n_theta - 1)
theta1.append(c) #storing the values of theta1 and theta2
theta2.append(c)
ct=1 #counter for the images when the theta1 and theta2 changes
for t1 in theta1:
for t2 in theta2:
# link 1 link 2 connector
#instantenous position of the other (non-fixed) end of link1
x1 = l1*math.cos(t1)
y1 = l1*math.sin(t1)
# co_ordinates of manipulator
x2 = x1 + l2*math.cos(t2)
y2 = y1 + l2*math.sin(t2)
image = str(ct)+'.png'
#counter changes to account the another image
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(image)
For creating the GIF from the images
- Open the Command Prompt window by clicking on START>type in the search box ‘cmd.’
- For changing the directory using ‘cd’, then go for the location of the saved images folder.
- Then type in 'dir' to conform the directory and path of the folder
In the window, the details of the images will be written.
- For creating the GIF using the ImageMagick type ‘ magick *.png 2R.gif ’.
- The GIF file will be created in the folder where all images are saved.
- As YouTube does not support video in .gif format, it has been converted into the .avi format and then uploaded on YouTube.
YouTube video link - https://youtu.be/nrQ3fPB2pXw
References:
1) Wikipedia - Forward Kinematics
2) NPTEL Robotics course - https://nptel.ac.in/courses/112101098/15