2 Arm Robotic Simulator using Python
AIM of the Project:
To write a program to simulate the behaviour of a 2 arm Robotic Simulator using Python.
Code of the Project:
"""
Two Arm Robotic Simuator
"""
import math
import matplotlib.pyplot as plt
l1 = 1
l2 = 0.5
theta1 = []
theta2 = []
x0 = 0
y0 = 0
n_theta = 10
theta_start = 0
theta_end = math.pi/2
for i in range(0,n_theta):
tmp = theta_start + i*(theta_end-theta_start)/(n_theta-1)
theta1.append(tmp)
theta2.append(tmp)
ct = 1
for t1 in theta1:
for t2 in theta2:
x1 = l1*math.cos(t1)
y1 = l1*math.sin(t1)
x2 = x1 + l2*math.cos(t2)
y2 = y1 + l2*math.sin(t2)
filename = str(ct) + '.png'
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:
"""
Two Arm Robotic Simuator
"""
#Comments to show the heading of the program
import math
#Importing math library of Python
import matplotlib.pyplot as plt
#Importing matlplotlib.pyplot as plt to make python work as Matlab. matplotlib.pyplot is a collection of
functions used for various activities such generating figures, generating plots, etc.
l1 = 1
#Defining the variable l1 to represent the length of link1 and initializing with 1.
l2 = 0.5
#Defining the variable l2 to represent the length of link2 and initializing with 0.5.
theta1 = []
#Declaring an array theta1 as an empty array to store the values of the angular freedom of link1.
theta2 = []
#Declaring an array theta2 as an empty array to store the values of the angular freedom of link2.
x0 = 0
#Defining the variable x0 to represent the x-coordinate of the starting point of link1 and initializing
with 0.
y0 = 0
#Defining the variable y0 to represent the y-coordinate of the starting point of link1 and initializing
with 0.
n_theta = 10
#Defining the variable n_theta to represent the number of values of the angles of link1 and link2.
theta_start = 0
#Defining the variable theta_start to represent the angle at the initial position of link1 and link2.
theta_end = math.pi/2
#Defining the variable theta_start to represent the angle at the extreme position of link1 and link2.
for i in range(0,n_theta):
#Defining a for loop using the variable 'i' having values from 0 to n_theta.
tmp = theta_start + i*(theta_end-theta_start)/(n_theta-1)
#Defining a variable tmp to store the values of the angles of link1 and link2 as per the above equation.
theta1.append(tmp)
#Using the append command to store the succesive values of tmp in the array theta1.
theta2.append(tmp)
#Using the append command to store the succesive values of tmp in the array theta2.
ct = 1
#Defining a counter variable 'ct' and initializing with 1.
for t1 in theta1:
#Starting a for loop using the variable t1 to access the values of theta1.
for t2 in theta2:
#Starting a for loop using the variable t2 to access the values of theta2.
x1 = l1*math.cos(t1)
#Calculating the value of the x-coordinate of the end point of link1 and storing in the variable x1.
y1 = l1*math.sin(t1)
#Calculating the value of the y-coordinate of the end point of link1 and storing in the variable y1.
x2 = x1 + l2*math.cos(t2)
#Calculating the value of the x-coordinate of the end point of link2 and storing in the variable x2.
y2 = y1 + l2*math.sin(t2)
#Calculating the value of the y-coordinate of the end point of link2 and storing in the variable y2.
filename = str(ct) + '.png'
#Defining the file name for all the images using the counter variable ct.
ct = ct + 1
#Incrementing the value of ct by 1.
plt.figure()
#Declaring the figure to plot the data.
plt.plot([x0,x1],[y0,y1])
#Plotting the line to represent link1 using its coordinates.
plt.plot([x1,x2],[y1,y2])
#Plotting the line to represent link2 using its coordinates.
plt.xlim([0,2])
#Defining the limits of x-axis in the plot.
plt.ylim([0,2])
#Defining the limits of y-axis in the plot.
plt.savefig(filename)
#Saving the figure by the name stored in the variable filename.
Link of the animation:
Output: