2 Arm Forward Movement using Python
Introduction
A robotic arm movement is shown by two arms of length `l1` and `l2`. The position of the arms can be given with the help of 4 variables i.e. lengths and angle with horizontal of both arms. The animation of moving arms is done using a code on the Python language.
Movement of Arms
There are two arms represented as lines connecting points `(x0, y0)`, `(x1, y1)` and `(x1, y1)`, `(x2, y2)`. One end of arm `1` is fixed and taken `(0, 0)`. Both the arms move from the right end with an increase in angle from `0°` to `90°`. For every angle of arm `1`, the arm `2` moves from `0°` to `90°`. Arm `1` and arm `2` are shown in blue and red respectively.
Code
- The length of the arms is taken input by the user in this case `1` and `1`.
- Three variables are created of `10` (no. of intermediate values), `0°` and `90°`. Using these variables theta1 and theta2 are assigned using a `"for"` loop.
- Now making `2` for loops one inside the other for the movement of each arm.
- The number of iterations of each `"for"` loop is equal to the length of both angle vectors and total iterations is equal to their product.
- Arm `1` is plotted using two points `(x0, y0)` and `(x1, y1)`. `(x1, y1)` is calculated using cos and sin of theta1 (angle of arm `1` with horizontal).
- Arm `2` is plotted using two points `(x1, y1)` and `(x2, y2)`. `(x2, y2)` is calculated using cos and sin of theta2 (angle of arm 2 with horizontal) and adding `(x1, y1)`.
- The value of `x0, x1, x2, y0, y1`, and `y2` is assigned at every iteration.
- The plot is then stored in the directory using the savefig function.
- The filename is stored using variable `cn` which is incremented at every iteration.
# Forward Kinematics
# Two-arm motion
import math
import matplotlib.pyplot as plt
#Inputs
l1 = 1
l2 = 1
n_theta = 10
theta_start = 0
theta_end = math.pi/2
theta1 = []
theta2 = []
for i in range(0,n_theta):
tmp = theta_start + i*(theta_end - theta_start)/(n_theta - 1)
theta1.append(tmp)
theta2.append(tmp)
# Base of the Robot
x0 = 0
y0 = 0
ct = 1
for t1 in theta1:
for t2 in theta2:
# Link 1, Link 2 connector
x1 = l1*math.cos(t1)
y1 = l1*math.sin(t1)
# Coordinates of the manipulator
x2 = x1 + l2*math.cos(t2)
y2 = y1 + l2*math.sin(t2)
filename = str(ct) + '.png'
ct = ct + 1
# Plotting
plt.figure()
plt.plot([x0,x1],[y0,y1])
plt.plot([x1,x2],[y1,y2])
plt.xlim([0,2.1])
plt.ylim([0,2.1])
plt.savefig(filename)
Output
Youtube Link -