2R Robotic Arm Simulator using Python
Aim: To create a simulation of forward Kinematic motion of 2R robotic arm using python 3
Description:
- Kinematics: A Branch of mechanics that describe the motion of bodies without considering forces.
- Forward kinematics: The Forward kinematics of a robot is the position and orientation of manipulator from its joint values.
`θ1`: Angle made by link 1 with the base of the Robot
`θ2`: Angle made by link 2 with respect to X-Axis
'L1' : Length of link 1
'L2' : Length of link 2
Procedure:
- Position of link 1 considering the starting point is fixed to the origin with coordinates (x1=0, y1=0)
endpoints of link 1:
x1 = L1 * cos (θ1)
y1 = L1 * sin (θ1)
- Position of link 2 considering endpoints of link 1 as starting points of link 2 (x1, y1)
endpoints of link 2:
x2 = x1 + L2 * cos (θ2)
y2 = y1 + L2 * sin (θ2)
- For different values 'θ1' and 'θ2', the different simulations will be plotted. By saving simulation frames for different values of 'θ1' and 'θ2'. Stitching will be done on frames by using ImageMagick, simulation for 2R Robotic Arm will be created.
Program for the 2R Robotic Arm simulation as follows:
# Importing the math and plotting functions
import math
import matplotlib.pyplot as plt
# For a number of iterations of theta 1 and theta2
n_theta = 10
# To calculate Initial values for theta1 & theta2
theta_start = 0
theta_end = (math.pi)/2
# To store values for theta1 and theta2
theta1 = []
theta2 = []
for i in range(0,n_theta):
temp = theta_start + i*(theta_end- theta_start)/(n_theta - 1)
theta1.append(temp)
theta2.append(temp)
# inputs in meters :
l1 = 1
l2 = 0.5
# Base of Robot: Considering the initial positions as (0,0) for (x0,y0)
x0 = 0
y0 = 0
# Counter variable for image name count
ct = 1
# For loop for managing the values for t1 and t2 from list theta1 and theta2
for t1 in theta1:
for t2 in theta2:
# Co-ordinates for Link 1, Link 2 connector
x1= l1*math.cos(t1)
y1= l1*math.sin(t1)
# Co-ordinates of the manipulator
x2= x1 + l2*math.cos(t2)
y2= y1 + l2*math.sin(t2)
# For Output images from the counter with a unique filename
filename = str(ct) + '.png'
ct = ct + 1
# For Plotting the graph for a Forward kinematics
plt.figure()
plt.plot([x0,x1],[y0,y1])
plt.plot([x1,x2],[y1,y2])
# Limiting the Co-ordinates x & y
plt.xlim([0,2])
plt.ylim([0,2])
plt.savefig(filename)
Output Video on Youtube: https://youtu.be/K3Sf0mWbsWw