Simulation of Forward kinematics of a 2R Robotic Arm using Python
AIM
To simulate the forward kinematics of a 2R Robotic Arm.
OBJECTIVES
To simulate the forward kinematics of the 2R Robotic Arm and create an animation.
THEORY
Forward kinematics for a robot arm takes as input joint angles, and calculates the Cartesian position and orientation of the end effector.
This allows us to know about the reach of the arm within the workspace.
It is calculated by taking the co-ordinates of the starting and ending point of the links at different angles of the links with x-axis.
L1=Length of Link 1
L2=Length of Link 2
`theta_1`(theta1)=Angle made by Link 1 with x-axis
`theta_2`(theta2)=Angle made by Link 2 with x-axis
Points-
(X0,Y0)=starting point of Link 1
(X1,Y1)=ending point of Link 1 and starting point of Link 2
`X_1=L_1*costheta_1` ; `Y_1=L_1*sintheta_1`
(X2,Y2)=ending point of Link 2
`X_2=X_1+L_2*costheta_2` ; `Y_2=Y_1+L_2*sintheta_2`
INPUT DATA ASSUMED
Length of first link, L1=1 m
Length of second link, L2=0.5 m
theta1 and theta2 vary from 0 to 90 degree in 10 values
CODE
Programming Language used- PYTHON
#Program to simulate the forward kinematics of a 2R Robotic Arm
#Impoeting the required modules
import math
import matplotlib.pyplot as plt
import numpy as np
#Input parameters
l1=1#length of link 1
l2=0.5#length of ink 2
#Angle between link 1 and x-axis
theta1=np.linspace(0,(math.pi/2),10)
#Angle between link 2 and x-axis
theta2=np.linspace(0,(math.pi/2),10)
#Starting co-ordinates for link 1
x0=0
y0=0
#Calculating position of Robotic Arm and creating animation
ct=1
for t1 in theta1:
for t2 in theta2:
#Ending co-ordinates for link 1 and starting co-ordinates for link 2
x1=l1*math.cos(t1)
y1=l1*math.sin(t1)
#Ending co-ordinates for link 2
x2=x1+l2*math.cos(t2)
y2=y1+l2*math.sin(t2)
filename='robotic_arm%05d.png'%ct
ct=ct+1
#Plotting of forward kinematics
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)
PROCEDURE
- First the required modules(i.e math, matplotlib and numpy) are imported.
- Then the input parameters are declared.
- The values of theta1 and theta2 are input with the ‘linspace()’ command.
- The starting co-ordinate is set to (0,0).
- The counter variable are set to 1.
- Two for loops are used to calculate the required co-ordinates for the two links using the formulas for (x1,y1) and (x2,y2).
- Inside the loops the plots are saved and each time the counter is increased by 1.
- The saved plots are stitched together and made into an animation file using ImageMagick.
RESULTS
The animation can be viewed from the Google Drive link-
https://drive.google.com/file/d/1ef_eEpzGrxA-Vi9JPu-dGuPJXTBNikSU/view?usp=sharing
DISCUSSION
The ‘numpy’ module is used for ‘linspace()’ command. Similarly the ‘math’ and ‘matplotlib’ modules are used for trigonometric commands and plotting graphs respectively.
Two for loops are used to calculate different coordinates for (X1,Y1) and (X2,Y2) for different values of theta1 and theta2.
As (X1,Y1) depends on ‘theta1’ so one for loop changes this coordinate by changing the value of ‘theta1’ and for each (X1,Y1) corresponding (X2,Y2) co-ordinates are calculated varying the value of ‘theta2’ using the second for loop.
A counter is used to save all the iterations. The value of counter is set to 1 which increases by 1 for every iteration of loop.
‘xlim()’ and ‘ylim()’ commands are used to set the limits for x-axis and y-axis.For saving the images ‘savefig()’ is used
The images obtained from the code are used together to make an animation using ImageMagick.The output shows the Simulation of Forward Kinematics of the 2R Robotic Arm.
ERRORS ENCOUNTERED
There was a error due to calculation of co-ordinates as the input value was an array(theta1/theta2) instead of an element(t1/t2)
CONCLUSION
From the simulation we can see how the Robotic arm moves in the workspace.