Python program to simulate the forward kinematics of robotic arm.
AIM:
Python program to simulate the forward kinematics of 2R robotic arm.
OBJECTIVE:
To trace out the final position of the manipulator or end effectors of a 2R robotic arm by creating an animation.
PROJECT THEORY:
As kinematics refers to the analysis of motion without the consideration of forces. Forward kinematics means to find out the position of end efforts or manipulator of a robot with the help of kinematics equation of that particular robot, but for that geometry of the robot is required for mathematical calculations which in turn help to trace the position of manipulator.
PROJECT SETUP:
Figure: 2R robotic arm
Here, in this robotic arm there are 2 links, the one end of the link1 is attached to the base and other is to the one end of the link2, and the link2 is further connected to the manipulator.
Geometry:
The angle of lin1and link2 with the horizontal is θ1 and θ2 respectively.
Length of the link 1 = L1
Length of the link 2 = L2
The one end of the link1 (x0,y0) = (0,0)
Hence x1 = L1*cosd(θ1)
y1 = L1*sind(θ1)
so,
x2 = x1 + L2*cosd(θ2)
y2 = y1 + L2*sind(θ2)
so, by this way value of x2 and y2 can be calculated which tells the position of end efforts.
To creat the animation of the 2 links different value of angles is required during programming.
PROGRAMING LANGUAGE USED:
Python programming is used to make the video of robotic arm.
CODE TO PLOT ROBOT ARMS:
#program to plot the roboto arms
import math
import matplotlib.pyplot as plt
#inputs
theta1 = math.pi/3 #link1 angle with horizontal
theta2 = math.pi/6 #link2 angle with horizontal
L1 = 1 # length of link1
L2 = 0.5 # length of link2
# starting point position of link one
x0 = 0
y0 = 0
# end point of link1
x1 = L1*math.cos(theta1)
y1 = L1*math.sin(theta1)
# end point of link1 and starting point of link2 is same
# end point of link2
x2 = x1 + L2*math.cos(theta2)
y2 = y1 + L2*math.sin(theta2)
plt.plot([x0,x1],[y0,y1])
plt.plot([x1,x2],[y1,y2])
plt.xlim([0,2])
plt.ylim([0,2])
plt.show()
OUTPUT:
so, by changing the value of theta different arm position can be plotted.
CODE USED TO CREAT ANIMATION:
import math
import matplotlib.pyplot as plt
#inputs
#length of link 1
l1 = 1
#length of link 2
l2 = 0.5
n_theta = 10
# Start angle
theta_start = 0
#end angle
theta_end = math.pi/2
theta1 = []
theta2 = []
x0 = 0
y0 = 0
for i in range(0,n_theta):
temp = theta_start + i*(theta_end - theta_start)/(n_theta-1)
theta1.append(temp)
theta2.append(temp)
# assuming the frame
ct = 1
for t1 in theta1:
for t2 in theta2:
#position of connector
x1 = l1*math.cos(t1)
y1 = l1*math.sin(t1)
#position of manipulator
x2 = x1 + l2*math.cos(t2)
y2 = y1 + l2*math.sin(t2)
filename = 'test%05d.png' % ct
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(filename)
Explanation of the code:
- Import the math module for angle calculation and matplotlib module for genrating plots.
- length of the both links is defiend firtst.
- Different values of the theta angle is taken by n_theta variable and used it in for loop range.
- A formula is used to divide the value of theta 1 under for loop as shown in the program.
- The value of c_t shows the frame number which is taken as 1 and increased by one on every iteration of the loop.
- By using the for loop all the value of angle theta is used to plot the figure and second for loop is used to give multiple value of theta 2 for given values of theta 1.
- To creat the animation, image magick software is installed and then all the plot images, which were genrated by the program,get converted into gif by using the magick command in cmd window as shown below
ERROR FACED:
Fortunately no error were faced while executing the program.
OUTPUT:
The link of the animated video is below:
https://drive.google.com/file/d/1mR7WLwt1pyDCypmYyWlJDoGkoiY9mW69/view?usp=sharing