Forward Kinematics of a 2R Robotic Arm using Python
AIM:
- Create a animation file of the plot.
- Upload an animation on You tube and provide the You tube link for an animation in your report.
- paste the code using the "INSERT SAMPLE CODE" option and attach screenshots of the output.
INTRODUCTION:
- The Robotic Arm consists of 2 arms linked together of lengths l1 and l2.
- They both operate at an angle of theta 1 and theta 2.
- Where one end of the link 1 is fixed to the based and the other end of the link 1 is fixed to the link 2. The other end of the link 2 is fixed to the end manipulator.
- The angle of operation of the end manipulator determines the workspace
PROCEDURE:
- import the math and matplotlib.pyplot as plt command
- specify the input parameters such as length l1 and l2 and theta 1 and theta 2
- specify the range in the for loop
- theta 1 and theta 2 are calculated using a fomula that finds the difference between the end and start value of theta divided by 1 minus no of theta values and then multiplied to i of range
- the formula fo calculating theta is stored in a variable called tmp
- we can use append command to store the values in the the arrays called theta1 and theta 2
- now we use two different nested for loop that derives the value t1 and t2 from theta1 and theta2 fo every iteration and then is in the calculation of x1,y1 and x2,y2
- Since, x0 and y0 ae in orgin and lie at the base line they are (0,0). for x1 and y1, we have to use the formula x1 = l1 × cosd(theta1) , y1 = l1 × sind(theta1) and similary fo x2 and y2, x2 = x1 + l2 × cosd(theta2) , y2 = y2 + l2 × sind(theta2)
- now,we need to store the plot of each iteration in a desitnation so we use filename command to determine the file name for every iteration using a counter variable 'ct' within the str() command because ct is an integer and followed by '.png' for specifying the format of the image.
- By applying ct=ct+1 command we can increase the value of the counter variable for every iteration.
- plot the values and set the limits using plt.xlim() and plt.ylim() and set the limits as [0,2]
- This will control the range in which the graph is being executed
- Now when you run the program you will get several images of the plots. Store them in folder named forward_kinematics
- Now install cygwin64 and image magick
- open cygwin64 and change the curent folder by specifying the folder destination in the following manner: cd /cygdrive/c/ [destination name]
- After you enter the correct destination to the folder containing forward_kinematics
- Use ls command to open the files in that folder
- now use convert* output.gif command to create a gif image in that folder
- you can convert this gif file to a mp4 video file in any online website
CODE:
import math
import matplotlib.pyplot as plt
#Inputs
l1=1
l2=0.5
#Theta Value
n_theta= 10
theta_start=0
theta_end=math.pi/2
theta1=[]
theta2=[]
for i in range(0,n_theta):
tmp=(theta_start+i*(theta_end-theta_start)/(n_theta-1))
theta1.append(tmp)
theta2.append(tmp)
#Base of the robot
x0 = 0
y0= 0
ct=1
for t1 in theta1:
for t2 in theta2:
#Link1 , Link2 connector
x1= l1*math.cos(t1)
y1= l1*math.sin(t1)
#Cordinates 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)
OUTPUT:
This is one of the plotted image output. The entire video containing the animation of the robotic arm can be found in the link below :-
https://youtu.be/PEUD2CfU0Ok