Python Program: 2R Robotic Arm Simulation
Aim: Write a Python code to simulate forward kinematics of a 2R robotic arm.
Objective:
- Simulate the movement of 2R robotic arm.
- Create animation of robotic arm's workspace.
Theory:
A robotic arm is a type of mechanical arm, usually programmable, with similar functions to a human arm. The arm may be the sum total of the mechanism or may be part of a more complex robot. The links of such a manipulator are connected by joints allowing either rotational or translation motion. The links of the manipulator can be considered to form a kinematic chain. The terminus of the kinematic chain of the manipulator is called the end effector and is analogous to the human hand.
A 2R robotic arm consists of two links (L1 & L2) and two rotational joints pivoted to the base s shoen in the following figure. Let `theta1` and `theta2` be the angle made by the links L1 and L2 respectively with the horizontal axis. Let (X0,Y0), (X1,Y1), (X2,Y2) be coordinates of the points as shown above.
Program:
Let's see this in the form of code.
#FORWARD KINEMATICS OF 2R ROBOTIC ARM
#Importing required libraries
from numpy import *
from matplotlib.pyplot import *
#Inputs
#Assuming lengh of link-1 as 3m and length of link-2 as 1.5m
l1 = 3
l2 = 1.5
#t1 and t2 are the angle of tranverse of 1st arm and 2nd arm respectively
t1 = linspace(0,pi/2,10)
t2 = linspace(0,pi/2,10)
x0 = 0
y0 = 0
ct = 1
#iteration for calculation of different positions
for i in range(10):
T1 = t1[i]
for j in range(10):
T2 = t2[j]
#First link
x1 = l1*cos(T1)
y1 = l1*sin(T1)
#Second link
x2 = x1 + l2*cos(T2)
y2 = y1 + l2*sin(T2)
#Filename
#The output jpg files were explicitly renamed so that it complies with cygwin
if(ct<10):
filename = 'robot00' + str(ct) + '.jpg'
if(ct>10):
if(ct<100):
filename = 'robot0' + str(ct) + '.jpg'
if(ct==100):
filename = 'robot' + str(ct) + '.jpg'
#Plotting
figure()
plot([x0,x1],[y0,y1],linewidth=2)
plot([x1,x2],[y1,y2],linewidth=2)
plot(x0,y0,"ko",linewidth=4)
plot(x1,y1,"ko",linewidth=4)
plot(x2,y2,"r*",linewidth=4)
xlim(-0.25,5)
ylim(-0.25,5)
title("2R Robotic Arm Workspace - Premkumar Iyer")
savefig(filename)
ct=ct+1
Working of Code:
1. First, we need to import the required libraries. For calculations (linspace and array calculation) we import numpy and as we need to embelish plots we use matplotlib.pyplot.
2. We need to define certain inputs. Firstly, we define the lengths of links. Then the angle of traverse is defined using linspace to create a vector array. The initial position is also defined. A loop counter too is placed whose significance would emerge in upcoming code sections.
3. We use for loop to calculate different positions to plot them. We call single angle values in the loop and it is clear that angles were already set in radians. By method of projections we calculate the position of links for different angles. Filename is defined in if statement to give a suitable name to the image files which would be saved later. The if statement works by comparing the values of loop counter.
4. plot command is used to embelish the required plot and limits are provided on axes to get consistent images. A suitable title is also provided.
5. The outputs are saved by using savefig command and loop counter is incremented by one.
Output:
As the ouptut cell/screen only throws run time warnings at each iterations, hereby the gif and video of the stitched plots are provided.
References:
- Python for Mechanical Engineers - Course provided by Skill Lync.
- Google Colab - Google LLC
- Anaconda - Anaconda Inc.
- JupyterLab - Project Jupyter
- ImageMagick - ImageMagick Studio LLC
- Supporting Material: Fundamentals of Robotics - DK Pratihar
- Supporting Material: 2R Robotic Arm Research Paper - ResearchGate
Materials: Python, code, notebook and plots can be accessed here