Understanding the Forward Kinematics of a 2R Robotic Arm using Python Program
AIM: To understand the Forward Kinematics of a 2R Robotic Arm using Python Programming.
THEORY:
A Robotic arm is a type of mechanical arm which is programmable with similar functions of a human arm. The Robotic Arm used consists of links and joints. Each part of a machine which moves relative to another part is called as a kinematic link. The link used here in this robotic arm is a rigid link since there is no deformation occurring while transmission of motion. This robotic arm is said to have 2 arms, hence it has 2 Links in it. Thus, when 2 links or elements of a machine are in contact with each other, they are said to form 1 pair. Both the arms in the robot are said to have a Line Contact. It is because, the links are turning with respect to each other. The Robotic arm follows a Line Contact and it has a Higher Pair. Other examples of higher pairs are belt and rope drives, ball and roller bearings, cam and follower.
Diagramatic Representation of 2R Robotic Arm
In the given diagram, the base of the robotic is fixed and cannot be moved. Hence, It makes a hinge joint with Link1.The co-ordinates of Link1 with respect to the base is `(x0,y0)`. Link 1 makes an angle of `theta`1 with the base. The co-ordinates of end point of link1 is`(x1,y1)`and it is resolved into 2 components namely:
1. Horizontal component: `x1=x0+l1cdotcos(theta1)`
2. Vertical component: `y1=y0+l1cdot sin(theta1)`
The end of Link1 makes an angle of `theta2` with the base. The end of Link1 is the starting point of Link2.The co-ordinates of the point are `(x1,y1)`.The end point of Link2 is `(x2,y2)`.Similarly as Link1, Link2 is also resolved into 2 components namely:
1.Horizontal component: `x2=x1+l2cdotcos(theta2)`
2.Vertical component: `y2=y1+l2cdot sin(theta2)`
Problem Statement:
1.Write a program to understand the Forward Kinematics of a 2R Robotic Arm using Python Programming.
2.Plotting graphs for various range of values for Theta1 and Theta2 and converting it into an animation.
Methodology and Algorithm:
- For understanding the concept of forward kinematics, we have to understand the concepts such as 1.types of links 2.types of joints 3.Trigonometry 4.types of kinematic pair. The subject Theory of Machine plays a vital role in this project.
- For our program, we have assumed the length of Link1 and Link2 to be 1 meter and 0.5 meter respectively. An empty array of theta1 and theta2 are created so that the generated values for theta are appended in the array.
- In order to simulate the arm, we have used a range of values for theta1 and theta2 in order for the 2 links to move in each angle mentioned. The value of `theta` is in Radian. A range is being set for changing the value of `theta`. They are as follows: theta_start=0,theta_end=math.pi/2.A random variable n is assigned the number of values for theta. Using the for loop which ranges from (0,n-1) we can obtain values for `theta`.
- For obtaining various values for `theta`, we are using a temporary variable so that the variable can store the value of theta for each iteration which is done. Once the value is stored in the temporary variable, it is then appended to the empty array created.
- Now the empty arrays consist of various values for theta1 and theta2 respectively. Then a nested for loop is used in order to assign the values of theta1 and theta2 into their respective equations. The indentations of the for loops are very important for not getting an error.
- In order to save the file, the program was providing the output for each interaction. Since, the value of the iteration given is 10.We get about 100 output for both`theta1`and `theta2` respectively. This is not the output that is expected.
- For this query, we are using a counter variable which keeps on incrementing on each loop. Then the files are saved in the temporary variable named filename which accepts only one single Data type. Hence, we are converting the counter variable as a string using str(counter). and adding the file extension at the end as '.png'. Example 1.png,2.png...
- Upon doing this the graphs which were obtained as output were printed 100 times. In order to stop this, we are using the plt.figure () command. It is used so that the file gets over-written every time the iterations take place.
- Then the plot command is used to plot the graphs for the movement of the links of the robotic arm. Upon noticing the behaviour of the graphs, we found out that the x and y axis of the graphs kept on changing from time to time. For this to not happen, we are using the plt.xlim () and plt.ylim () function to limit the range of the x and y axis respectively.
- We use the plt.savefig () command to save our plots created in the respective folder. Upon opening the folder we can see 100 plots created for the Forward Kinematics of the 2R Robotic Arm.
Python Code:
"""Understanding the forward kinematics of a 2R Robotic arm using the python program"""
#Importing the required libraries
import math
import matplotlib.pyplot as plt
#Inputs
l1=1 #Length of link 1
l2=0.5 #Length of link 2
#Angle of the links respect to the base
theta1=[]
theta2=[]
#Co-ordinates of the base of the robot
x0=0
y0=0
#Number of values of Theta
n=10
#Accepting the values of theta_start and theta_end in radian
theta_start=0
theta_end=math.pi/2
#Using counter variable for saving the filename with .png extension
counter=1
#Using the for loop for adding values of theta in theta1
for i in range(0,n):
temp=theta_start+i*(theta_end-theta_start)/(n-1)
#Appending the value of temp in the empty array
theta1.append(temp)
theta2.append(temp)
#Using the Nested for loop for accepting various values of theta1 and theta2
for t1 in theta1:
for t2 in theta2:
#Co-ordinates of link1,link2 connector
x1=l1*math.cos(t1)
y1=l1*math.sin(t1)
#Co-ordinates of link2 and the end-manipulator
x2=x1+l2*math.cos(t2)
y2=y1+l2*math.sin(t2)
#Saving the file as a filename with each iteration
filename=str(counter) + '.png'
#Appending the counter variable for each iteration
counter=counter+1
#Plotting the graph
plt.figure() #function is used to re-write the file
plt.plot([x0,x1],[y0,y1])#Plotting the co-ordinates of link1
plt.plot([x1,x2],[y1,y2])#Plotting the co-odrinates of link2
plt.xlim([0,3])#This function is used to assign the limit of the x axis
plt.ylim([0,3])#This function is used to assign the limit of the y axis
plt.savefig(filename)#The file is saved as filename with .png extension
plt.show()#Fuction used to show the respective graphs plotted
Output Graphs:
Animation of the Plot:
We have obtained 100 plots for the understanding the forward kinematics of a 2R Robotic arm which is converted into a gif file for showcasing all the plots obtained.
Link to the animation of the plot:
Result:
From this report we created a program using python programming to understand the Forward Kinematics of a 2R Robotic Arm and we have plotted graphs for range of values for `theta1`and `theta2`.