Project

Kinematics of a 2R Robotic Arm Python

We code to Define the workspace of the Kinematic Motion of the 2R Robotic Arm.

Description: We basically define the workspace of the 2R Robotic Arm, by workspace we mean the possible locations that the robotic arm can resch, or in other words the total area that the robotic arm can cover when in motion is what is described in this code below,

We use the following formulae, to define the co-ordinates of the Robotic Arm:

x0 = 0;

y0 = 0; (This is the origin, or the point that defines the base of the robotic arm).

x1 = L1*cos (theta1)

y1 = L1*sin(theta1) (This is the next point in the robotic arm, it defines the end point of Robotic arm 1 and starting point of robotic arm 2)

x2 = x1 + L2*cos (theta2)

y2 = y1 + L2*sin(theta2) (This is the ending point of the robotic arm)

Using these formulae we code to define the workspace of the 2R Robotic Arm as given below:


The Explanation about the concepts are given as comments within the program.

# -*- coding: utf-8 -*-
\"\"\"
Created on Mon Jun 18 20:38:32 2018

@author: shabarish
\"\"\"
#Program to simulate the kinematics of a 2R Roboic Arm#
#Import Modules and Libraries#
import math
import matplotlib.pyplot as plt
import os
#Define the Inputs#
L1 = 2
L2 = 1
#Defining the robot base positions#
x0 = 0
y0 = 0
n_theta = 15                #The total number of divisions in angles#
theta_start = 0             #The Starting Angle#
theta_end = math.pi/2         #The Ending Angle#
theta1 = []                 #We create an empty array initially for theta1#
theta2 = []                 #We create an empty array initially for theta2#
for i in range(0, n_theta):
    tmp = theta_start + (i*(theta_end - theta_start)/(n_theta - 1)) #Creating an array of theta1 value and storing it in a variable tmp#
    theta1.append(tmp)
    theta2.append(tmp)
ct = 1                      #We set a counter#
#Creating Video Part 1#
def createFolder(directory):
    try:
        if not os.path.exists(directory):
            os.makedirs(directory)
    except OSError:
            print (\'Error: Creating directory. \' + directory)
createFolder(\'./Images/\')
#Creating the images for animation#
for t1 in theta1:
    for t2 in theta2:        
        #defining robot arm and manipulator positions#
        x1 = L1*math.cos(t1)
        y1 = L1*math.sin(t1)
        x2 = x1 + L2*math.cos(t2)
        y2 = x1 + L2*math.sin(t2)
        ct = ct+1
        seq_image = str(ct) + \'.png\'
        print(seq_image)
        plt.figure()    #Creates a New Figure During Every Iteration#
        plt.plot([x0, x1], [y0, y1], linewidth = 3, label = \'Link 1\')
        plt.plot([x1, x2], [y1, y2], linewidth = 2, label = \'Link 2\')
        plt.plot(x1, y1, \'ko\', markersize = 5, label = \'Link connector\')
        plt.title(\"Kinematics of a 2R Robotic Arm\")
        plt.legend(loc=\'best\')
        plt.xlim([-6,6])    #Defining Axis#
        plt.ylim([-6,6])    #Defining Axis#
        plt.xticks([]),plt.yticks([])
        plt.savefig(seq_image)

By using this Code, we can produce images or Frames of each step of the motion of the arm, by using imgflip.com, we could stictch these images and produce a gif file, which clearly depicts the motion of the 2R robotic arm.
The gif file is present in the below link.

https://drive.google.com/file/d/1X5J9Bs4Y5tNwl_BtVYOb6cA9OT1-EW3v/view?usp=sharing

All the images of the motion of the Robotic Arm are present in the link below.
https://drive.google.com/drive/folders/1DM92mYcx_Nr-lcXp4kIaO_6mbCGz9I5w

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.