Project

2R Robotic Arm Simulator

Objective:

1.To write a python code to simulate the farward kinematics of a 2R Robotic Arm.

2. Create a animation plot showing the simulation output.

Code:

import math
import matplotlib.pyplot as plt

# length of two arms l1,l2 respectively
l1=0.5
l2=0.5


# defining start point
x0=0
y0=0


#defining intial and end anngles
n_theta=10
theta_start=0
theta_end=math.pi/2

theta1=[]
theta2=[]
ct =1


for i in range(0,n_theta):
	temp = theta_start + i*((theta_end- theta_start)/(n_theta-1))
	theta1.append(temp)
	theta2.append(temp)

#getting positions of each arm at various angles

for t1 in theta1:
	for t2 in theta2:
		x1= l1*math.cos(t1)
		y1= l1*math.sin(t1)

		x2= x1+l2*math.cos(t2)
		y2= y1+l2*math.sin(t2)
		ct=ct+1

		filename = str(ct) + '.png'

		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)

Explanation:

1. Import all the necessary modules such as math,matplotlib.

2. Define all the variables such as lengths of two arms, intial end ending angles.

3. Used for loop to get various values of angles between initial and end position.

4. Used for loop to get positions of each arm at every output angle from loop1.

5. Based on the positions of two arms, created a plot using matplotlib.

6. Finally using all the images genereated for each position, the animation file is generated.

Output:

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.