2R Robotic Arm Simulator - Python
Problem Statement:
Write a python program for Simulation of 2R Robotic Arm Forwards Kinematics and creat animation of plot showing different position of Robotic Arms.
# Program for Forward Kinematics of 2R Robotic Arm
import math
import matplotlib.pyplot as plt
# Length of link 1 in mm
l1 = 120
# Length of link 2 in mm
l2 = 80
# Defining angular range of link1 & link2
theta_start = 0 # starting angle would be 0 degrees
theta_end = 90*(math.pi / 180) # end angle would be 90 degrees
# Defining no. of intervals between theta_start & theta_end
n_theta = 10
# Angle of rotation in Radians of link 1 with respect to Horizontal X axis(ranges from 0deg. to 90deg.)
theta1 = []
# Angle of rotation in Radians of link 2 with respect to Horizontal X axis(ranges from 0deg. to 90deg.)
theta2 = []
for i in range(0,n_theta): # for loop to calculate values of theta1 & theta2
tmp = theta_start + i*(theta_end - theta_start) / (n_theta - 1) #calculating value of interval in n_theta
theta1.append(tmp) # Incrementing theta1 with value equal to tmp(interval)
theta2.append(tmp) # Incrementing theta2 with value equal to tmp(interval)
# Initial co-ordinates of link 1 at fixed pivot end in X & Y axis will be 0
x0 = 0
y0 = 0
ct = 1
for t1 in theta1: # for loop for any value t1 in range of theta1 values
for t2 in theta2: # for loop for any value of t2 in range if theta2 values
x1 = l1 * math.cos(t1) # X co-ordinate of amother end of link 1 which is starting co-ordinate of link2
y1 = l1 * math.sin(t1) # Y co-ordinate of amother end of link 1 which is starting co-ordinate of link2
x2 = x1 + l2*math.cos(t2) # Final X co-ordinate of link 2
y2 = y1 + l2*math.sin(t2) # Final Y co-ordinate of link 2
filename = '%05.d.png' %ct # defining filename, so that plot created can be stitched sequentially to create animation
ct = ct + 1 # interval between two images
plt.figure() # To plot each figure in new frame so as to get clear idea about movement of Arm for different combination of theta1 & theta2
plt.plot([x0,x1],[y0,y1])
plt.plot([x1,x2],[y1,y2])
plt.xlim([0,200]) # limiting x axis upto 200mm
plt.ylim([0,200]) # limiting y axis upto 200mm
plt.savefig(filename)
Explanation:
Input:
Length of Link 1 = L1 = 120mm
Length of Link 2 = L2 = 80mm
Theta 1 = Angle in Radians of link 1 with respect to Horizontal X axis
Thata 2 = Angle in Radians of link 2 with respect to Horizontal X axis
x0, x1, x2 = X co-ordinates of links at node points 1, 2 & 3.
y0, y1, y2 = Y co-ordinates of links at node points 1, 2 & 3
1. Theta 1 varies from 0 to 90 degrees. For each value theta 1, Theta 2 also varies from 0 to 90
degrees. For plotting relative postions of links, 10 equal intervals are taken in between start and end
angle of links. For Loop is used to calculate theta1 and theta2 as per interval.
2. As a result of interval, we get 100 plots showing relative positions of link 1 & 2.
3. Values of co-ordinates of links, trigonometric relations are used.
x0 = 0 ; y0 = 0
x1= L1⋅cos(θ1) ; y1= L1⋅sin(θ1)
x2= x1 + L2⋅cos(θ2) ; y2= y1 + L2⋅sin(θ2)
4. As a result of executing program, we get 100 plots showing position of links as per varying values
of theta.
5. These results are stitched together by using Imagemagick to get animation of varying position of
links of Robotic Arm.
Result:
Above animation shows relative position of link 2(highlighted by yellow colour) with varying position of link 1(highlighted by blue colour).
Conclusion:
Using plots obtained as result of executing python program, we can calculate work space required of
movement of 2R Robotic Arm i.e. envelope of Robotic Arm.