Project

2R Robotic Arm Simulator using python

AIM: To write a program in python to simulate the forward kinematics of a 2R Robotic Arm and also to animate the obtained plots.

Implementation

Here we want to simulate the motion of the robotic arm with 2 links of length 1m (l1) and 0.5m (l2) respectively making upto a maximum angle of 90 degrees independently. So to fing the positions of the arm at various angles we need to find the end co-ordinates of the links with respect to the origin in order to find the position.
Considering the fixed end as x0,y0 and end of the link 1 as x1,y1 and that of link 2 as x2,y2 respectively, where x0=0, y0=0;
x1=l1*cos(theta1), y1=l1*sin(theta1);
x2=x1+l2*cos(theta2), y2=y1+l2*sin(theta2).

Doing so and finding the values, we could find the position of the arm, but in the ccase of programming we have to add the values in a sequential manner in order to have the simulation to be near to the reality of the robotic arm working. So we use the for loop to initialize the evaluation of the co-ordinates at various angles by defining the start and end of the angle.

In coding part, we have considered to have about 10 angles for the evaluation so n=10. By giving the start and end of the angles we defined 2 empty arrays of theta1 and theta2 where the angles of values ranging between start and end angles will be stored into them and used for the evaluation by indexing. By using the for loop we have to calculate the value of the angles by using a formula `tmp=Start+i(end-Start)/(n-1);` where tmp is a temporary variable used to store the angle so found in that iteration of the loop. i is the loop indexinf value i.e. it keeps on increasing for every interation. Then the found tmp value is appended to the empty arrays theta1 and theta2 respectively. As there are 10 values each in both the arrays, we will have the possibility of 100 iterations to know the position of the arms i.e. for every angle of link 1, we observe 10 variations of link 2. By using the for loops the above mentioned co-ordinates are found and to plot the values for eact iteration and save it seperately can be done by using a counter variable as ct to count the nuber of iterations and also used to namae the plot in a sequencial order. The module of matplotlib.pyplot is used to plot the the values for each iteration in the loop where the syntax is followed to plot and then the plotted graph is saved using the command plt.savefig(filename) , where filename is given as a ct variable and saved as .png file.

The above is the complete explanantion of the following code.

import math
import matplotlib.pyplot as plt

n=10
theta_start =0
theta_end=math.pi/2

theta1=[]
theta2=[]
for i in range(0,n):
	#calculating the theta values using the interval gap 
	tmp= theta_start+i*(theta_end-theta_start)/(n-1)
	theta1.append(tmp)
	theta2.append(tmp)

l1=1
l2=0.5
#base of the robot
x0=0
y0=0

ct=1
for t1 in theta1:
	for t2 in theta2:
		#link 1, link 2 connector
		x1=l1*math.cos(t1)
		y1=l1*math.sin(t1)

		#coordiante of the manipulator
		x2=x1+l2*math.cos(t2)
		y2=y1+l2*math.sin(t2)
		
		filename=str(ct)+'.png'
		ct=ct+1
		
		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)

The outputs are saved in the file where the program is saved as individual .png files as shown below

The above screenshot is after running the code it states that it the code takes time as each plot is being saved and it is taking a lot of memory.

These .png files are converted into a gif file using the imgflip.com. We can also do it using the cygwin and imagmagik softwares together.

The following is the animated gif file of the outputs so obtained.

The following is the google drive link for the individual plots obtained.
https://drive.google.com/drive/folders/1_pb_OX3yCoN8_fyTU5KOLAXnVdhC8AHG?usp=sharing

Conclusion
The robotic arm simulator is generated using the python coding as well.

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.