Project

2R Robotic Arm Simulator

2R ROBOTIC ARM SIMULATOR

OBJECTIVE:

  • Write a program to simulate forward kinematics of a 2R Robotic Arm.

CODE:

  • Following is the code which I wrote down for simulating Forward Kinematics of a 2R Robotic Arm.
"""
Code by- Rishav Raj Sinha
Objective- WAP to simulate forward kinematics of a 2R Robotic Arm
"""

import math
import matplotlib.pyplot as plt

n_theta = 10

theta_start = 0
theta_end = math.pi/2

theta1 = []                                       #Array for Link 1 angles
theta2 = []                                       #Array for Link 2 angles

for i in range(0, n_theta):
	tmp = theta_start + i*(theta_end-theta_start)/(n_theta-1)
	theta1.append(tmp)                            #Angle of Link 1
	theta2.append(tmp)                            #Angle of Link 2

x0 = 0                                            #Value of x at Origin
y0 = 0                                            #Value of y at Origin
l1 = 1                                            #Length of 1st Link
l2 = 0.5                                          #Length of 2nd Link
ct = 1                                            #For keeping counts of the figures produced

for t1 in theta1:
	for t2 in theta2:

		#Defining of Link 1 points at (x1,y1)
		x1 = l1*math.cos(t1)
		y1 = l1*math.sin(t1)

        #Defining of Link 2 points at (x2,y2)
		x2 = x1 + l2*math.cos(t2)
		y2 = y1 + l2*math.sin(t2)

		filename = str(ct) + '.png'
		ct = ct + 1
		print(filename)

		#Output & Saving of figures
		plt.figure()
		plt.plot([x0,x1],[y0,y1])
		plt.plot([x1,x2],[y1,y2])
		plt.xlim([0,2])
		plt.ylim([0,2])
		plt.savefig(filename)

OUTPUT:

Explaination:- We have used 2 for loops t1 and t2 where t1 will perform n loops and t2 will perform n*(n-1) times loop. Such kind of loops are termed as Loop within Loop, where t1 is the main loop and t2 is the sub-loop. So with the assigned values we ran loop within loop to generate the values for two co-ordinates (x1,y1) and (x2,y2). A total of (n*n) such iterations were produced, n times of (x1, y1) and n*(n-1) times of (x2,y2). As we have assigned the value of 'n' to be 10, so t1 will be performing 10 loops thus resulting in 10 co-ordinates of (x1,y1) and in the same way t2 will be performing 10*(10-1)=90 loops resulting in the same number of co-ordinates for (x2,y2).

The 100 figures were saved in a folder which was then stitched using a software named ImageMagick to get a .gif file which is shown above.

LEARNING OUTCOME:

  • In this challenge, I learnt to write a code to develop forward kinematics of a 2R Robotics Arm and the method to stitch all the .png or any related formats into .gif format.

CONCLUSION:-

  • The figures were successfully generated after compilation of the program.
  • The figures were stitched to produce an animation of the 2R Robotics Arm.
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.