Project

Python: Forward Kinematics of 2R Robotics Arm

AIM: Write a code in python to simulate forward kinematics of robotic arm.

OBJECTIVES:

  • Write a code in python to simulate forward kinematics of robotic arm by giving image of each position as output.
  • Stitch those images together to form an animation of robotic arm motion.

STEPS:

First the important libraries are imported into our code viz. matplotlib.pyplot (referred as plt), math, numpy (referred as np). Numpy provides: An array object of arbitrary homogeneous items; Fast mathematical operations over arrays; Linear Algebra, Fourier Transforms, Random Number Generation. Basically, we can use MATLAB commands in Python with the help of numpy package.

We define inputs for our code viz. length of first link = 1, link of second link = 0.5, Angle of first and second link. Here we use linspace command which is also used in MATLAB for creating an array within a range with equal interval. In this case our angle will range from 0 to 90 with 10 intervals.

Then we fix our plot at origin so that the animation looks perfect.

Then initiate a count and apply two loops for theta1 and theta2. Loop for theta 2 is placed inside the loop for theta1 as after evaluating all the plots for theta2 the angle of theta1 change by 10 and again the plots will be evaluated for all the values of theta2. The range for loops is provided. Here two formulas are used two define the position of link 1 and link 2 connector & the manipulator.

Then image file of plot is created with .png extension. The filename is set by count number to create a proper sequence which will ease the process of creating animation. Then we set-up all the plot points, x and y limits, enable grid, give plot a title. Finally, we save the plot as image and increment the count by 1.

CODE:

'''
Program for forward kinematics of 2R Robotics Arm
by Utkarsh Palwekar
'''
import matplotlib.pyplot as plt
import math
import numpy as np


#Inputs
l1 = 1 #Length of first link
l2 = 0.5 #Length of second link
theta1 = np.array(np.linspace(0,90,10)) #Angle of first link
theta2 = np.array(np.linspace(0,90,10)) #Angle of second link

#Fixing the Plot at Origin
x0 = 0 #Initial x-coordinate
y0 = 0 #Initial y-coordinate

#Setting up count
count = 1
for t1 in range(0,np.size(theta1)):
    for t2 in range(0,np.size(theta2)):
        #Co-ordinate of link 1 and link 2 connector
        x1 = l1*np.cos(math.radians((theta1[t1])))
        y1 = l1*np.sin(math.radians((theta1[t1])))
        #Co-ordinate of Manipulator
        x2 = x1+l2*np.cos(math.radians((theta2[t2])))
        y2 = y1+l2*np.sin(math.radians((theta2[t2])))

        #Creating plot and  saving it as .png image
        filename = str(count)+'.png'
        plt.figure()
        plt.plot([x0, x1], [y0, y1])
        plt.plot([x1, x2], [y1, y2])
        plt.xlim(0, 2) #Setting x-limits of plot
        plt.ylim(0, 2) #Setting y-limits of plot
        plt.grid() #Enabling grid on plot
        plt.title('Forward Kinematics of 2R Robotic Arm') # Giving plot a title
        plt.savefig(filename) #Saving plot as image
        count = count + 1 #Incrementing count by 1

OUTPUT/RESULT:

Animation of Forward Kinematics of 2R Robotic Arm

CONCLUSION:

Thus, a successfully animation of 2R robotic arm is created. This indicates the workspace required for the functioning of robot.

ERROR & SOLUTION:

Silly typo mistakes occur.

The output plot obtained was in different position for each value of theta. This problem was solved by fixing the plot at origin.

Faced difficulty in creating an array for theta values which was solved by importing numpy package/library and using linspace command.

The output image name was set according to count number to prevent confusion will creating an animation.

Only images were given as output. The animation was created by using an online tool to stitch all the images together to create an animation.

REFERENCES:

https://skill-lync.freshdesk.com/support/solutions/articles/43000511246-how-to-stitch-images-in-sequence-to-get-smooth-animation-

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.