Project

Forward Kinematic Simulation of a 2R Robotic Arm using Python

Forward Kinematic Simulation of a 2R Robotic Arm using Python

1.AIM:

  • To simulate forward kinematics of a 2R Robotic Arm using Python

2.GOVERNING EQUATIONS IF ANY:

- Trigonometry: sin(θ) and cos(θ) to find the base and height of the triangle shown in the above image.

- sin(θ) = `(Height)/(Hypoten use)`therefore, Height = sin(θ) * Hypotenuse = rsin(θ)

- Similarly, cos(θ) = `(Base)/(Hypoten use)`therefore, Base = cos(θ) * Hypotenuse = rcos(θ)

- Here, r is nothing but the radius of the circle.

3.OBJECTIVES OF THE PROJECT:

- Write a program in Python to simulate the forward kinematics of a 2R Robotic Arm.

- Create an animation file of the plot.

4.FLOW OF THE PROJECT:

1.Decide the total number of steps of angles of both links, L1 and L2.

2. Decide the link lengths of links L1 and L2.

3. Loop the increment of Link 2 for every increment of link 1, i.e create a nested loop.

4. Plot the graph and use pause function to animate the plot.

5. Save the animation as an ‘gif’ file.

5. CODE:

import math
import matplotlib.pyplot as plt
import numpy as np

a = math.pi/2                            #assigning value pi/2 to the variable a
theta_1 = np.linspace(0,a,10)            #Creating an evenly spaced array which will act as angle values
theta_2 = np.linspace(0,a,10)
len_1 = 5                                #Length of link 1 in metres
len_2 = 10                               #Length of link 2 in metres
ct = 1                                   #Counter value

for i in theta_1:
	x1 = len_1*math.cos(i)               #Calculating X coordinate of link 1 
	y1 = len_1*math.sin(i)               #Calculating Y coordinate of link 1 

	for j in theta_2:
		x2 = x1 + len_2*math.cos(j)      #Calculating X coordinate of link 2 and adding to X1 so that final coordinate we can get
		y2 = y1 + len_2*math.sin(j)      #Similar to above method
		filename = str(ct) + '.png'      #During every run of the for loop, we can get different names for plot saving. 
		                                 #e.g 1.png, 2.png, 3.png, 4.png and so on...
		ct = ct + 1

		plt.figure()                     #write to new figure for every iteration
		plt.plot([0,x1],[0,y1])          #Plot (0,0) and (x1,y1) which is link 1
		plt.plot([x1,x2],[y1,y2])        #Plot (x1,y1) and (x2,y2) which is link 2
		plt.xlim([-1,16])                #Set the range of X axis
		plt.ylim([-1,16])                #Set the range for Y axis
		plt.savefig(filename)            #Save the plot

6. CODE EXPLAINATION:

The Python code is very easy-to-understand code. It starts with:-

  1. Writing inputs of angles of both the links l1 and l2 which are theta1 and theta2 respectively which are divided in 10 equal parts from 0 radians to `pi/2` radians using linspace function.
  2. Next, the lengths of links l1 and l2 are fixed which in this case are 5m and 10m.
  3. We have used a variable ‘ct’ just for keeping a counter, i.e how many increments have been passed for both the links. This will be used later on to make animation files save easier.
  4. We have created a for-loop for incrementing the angle or the slope of the link l2. Thus, in the first 10 iterations, the link l2 would have travelled from 0 degree to 90 degrees i.e (`pi/2` radians), while link 1 is still at 0 degrees.
  5. But we need to also increment angle of link l1. Thus create a nested for loop to increment angle theta1 of link 1.
  6. So in all, we got nested for-loops where first loop increments angle theta1 of link 1 only and only after the next for-loop increments the angle theta2 of link 2, 10 times, i.e from 0 radians to `pi/2` radians.
  7. Plot the graph by putting coordinates (0,0) and so on in the same plot function to obtain both plots in a single plot itself.
  8. Use plot.figure() so that every iteration is plotted in new figure and not in same window.
  9. Save the figure using savefig function and using ct as counter to get new file name for every iteration.

7.ERRORS FACED:

  • All the iterations of link1 and link2 were being plotted in same figure window.

Fixed it by using plot.figure() function so that every plot gets written on a new figure.

8.CONCLUSION:

- We have successfully simulated the links of 2R Robotic arm, i.e in simple language, we have found out the reach of the robotic arm that is where the arm can reach and cannot reach.

- Here, the reach of the arm is not important, but the end effector installed on the end of link 2 should be considered.

- Link to the simulation video: https://www.youtube.com/watch?v=qUd_zZDQ33M

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.