-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFinal Program.py~
223 lines (174 loc) · 8.98 KB
/
Final Program.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Program of ECE 569 Project B
# Authors :- Debasmit Das, Tongyang Liu
# Input :- DH Parameters and Locus Parameters
# Output :-Drawing a Circle and Lines
from openravepy import * # This is imported for OpenRave Methods/Functions
import time # This is imported for invoking delay in the Program
import numpy as np # This is Matrix and Array Operations
from numpy import *
from sympy import * # This is imported for symbolic variables
from sympy import Matrix
from numpy import linalg # this is useful for Linear ALgebra Applications
p='/home/artlab/Desktop/ProjectA_Software_Liu_Das/DH.txt' # Change the Path of DH File as required
DH=numpy.genfromtxt(p) # Matrix of D-H Parameters and ranges read from a file
th=DH[:,0] # Separating the Joint Angles
thr=th*(np.pi/180) # Converting to Radians
al=DH[:,1] # Separating the Link Twist
alr=al*(np.pi/180) # Converting to Radians
a=DH[:,2] # Separating the Link Length
d=DH[:,3] # Separating the Joint Offset
r1=DH[:,4]*(np.pi/180) # Converting lower limit of range to radians
r2=DH[:,5]*(np.pi/180) # Converting upper limit of range to radians
# Here each joint angle which are the variables are stored
q=[]
for i in range(1,7):
q.append(Symbol('q'+repr(i))) # A Matrix of Symbolic Joint Variables is created
A=[[0.0],[0.0],[0.0],[0.0],[0.0],[0.0]];#Create a list of zero matrix This list will later fill up with symbolic matrices
# The Link transformation matrix is computed
for i in range(0,6):
A[i]=Matrix([[cos(q[i]),-cos(alr[i])*sin(q[i]),sin(alr[i])*sin(q[i]),a[i]*cos(q[i])],[sin(q[i]),cos(alr[i])*cos(q[i]),-sin(alr[i])*cos(q[i]),a[i]*sin(q[i])],[0,sin(alr[i]),cos(alr[i]),d[i]],[0,0,0,1]])
print A[i]
print "\n"
# Now the Geometric Inverse Kinmeatics Subroutine is written that is to be used for position control
def GeomIK(T,ARM,ELB,WRI,OHM):
px=T[0,3];py=T[1,3];pz=T[2,3];nx=T[0,0]; ny=T[1,0] ; nz=T[2,0] ; sx=T[0,1] ; sy=T[1,1] ; sz=T[2,1] ; ax=T[0,2] ; ay=T[1,2] ; az=T[2,2];
Px=px - d[5]*ax ; Py=py - d[5]*ay ; Pz=pz - d[5]*az ;
rg = (Px**2 + Py**2 - d[1]**2)**0.5 ; R1 = (Px**2 + Py**2)**0.5
Qg=np.array([0.0,0.0,0.0,0.0,0.0,0.0])
Qg[0]=atan2(-ARM*Py*rg - Px*d[1] , -ARM*Px*rg + Py*d[1])
R2=(Px**2 + Py**2 + Pz**2 - d[1]**2)**0.5
sina = -Pz/R2 ; cosa = -ARM*rg/R2 ; cosb=(a[1]**2 + R2**2 - d[3]**2 - a[2]**2)/(2*a[1]*R2) ; sinb = (1-cosb**2)**0.5
Qg[1]=atan2(sina*cosb + ARM*ELB*cosa*sinb , cosa*cosb - ARM*ELB*sina*sinb)
cosphi = (a[1]**2 + d[3]**2 + a[2]**2 - R2**2)/(2*a[1]*(d[3]**2 + a[2]**2)**0.5)
sinphi = ARM*ELB*(1 - cosphi**2)**0.5
sinbeta = d[3]/((d[3]**2 + a[2]**2)**0.5)
cosbeta = abs(a[2])/((d[3]**2 + a[2]**2))**0.5
Qg[2] = atan2(sinphi*cosbeta - cosphi*sinbeta, cosphi*cosbeta + sinphi*sinbeta)
M=WRI*OHM;
Qg[3] = atan2(M*(cos(Qg[0])*ay - sin(Qg[0])*ax) , M*(cos(Qg[0])*cos(Qg[1]+Qg[2])*ax + sin(Qg[0])*cos(Qg[1]+Qg[2])*ay - sin(Qg[1]+Qg[2])*az))
Qg[4] = atan2((cos(Qg[0])*cos(Qg[1]+Qg[2])*cos(Qg[3]) - sin(Qg[0])*sin(Qg[3]))*ax + (sin(Qg[0])*cos(Qg[1]+Qg[2])*cos(Qg[3]) + cos(Qg[0])*sin(Qg[3]))*ay - cos(Qg [3])*sin(Qg[1]+Qg[2])*az , cos(Qg[0])*sin(Qg[1]+Qg[2])*ax + sin(Qg[0])*sin(Qg[1]+Qg[2])*ay + cos(Qg[1]+Qg[2])*az)
Qg[5] = atan2((-sin(Qg[0])*cos(Qg[3]) - cos(Qg[0])*cos(Qg[1]+Qg[2])*sin(Qg[3]))*nx + (cos(Qg[0])*cos(Qg[3]) - sin(Qg[0])*cos(Qg[1]+Qg[2])*sin(Qg[3]))*ny + sin(Qg[1]+Qg[2])*sin(Qg[3])*nz, (-sin(Qg[0])*cos(Qg[3]) - cos(Qg[0])*cos(Qg[1]+Qg[2])*sin(Qg[3]))*sx + (cos(Qg[0])*cos(Qg[3]) - sin(Qg[0])*cos(Qg[1]+Qg[2])*sin(Qg[3]))*sy + sin(Qg[1]+Qg[2])*sin(Qg[3])*sz)
if(Qg[1]<=np.pi and Qg[1]>=np.pi/2):
Qg[1]=Qg[1]-2*np.pi
if(Qg[2]<=-np.pi/2 and Qg[2]>=-np.pi):
Qg[2]=Qg[2]+2*np.pi
return Qg
#Now Lets try finding the Jacobian
# First Let us try the cross-product method
P0=(A[0]*A[1]*A[2]*A[3]*A[4]*A[5])[0:3,3]
Z0=Matrix([[0],[0],[1]])
J0=Z0.cross(P0).transpose().col_join(Z0)
P1=(A[0]*A[1]*A[2]*A[3]*A[4]*A[5]-A[0])[0:3,3]
Z1=A[0][0:3,2]
J1=Z1.cross(P1).transpose().col_join(Z1)
P2=(A[0]*A[1]*A[2]*A[3]*A[4]*A[5]-A[0]*A[1])[0:3,3]
Z2=(A[0]*A[1])[0:3,2]
J2=Z2.cross(P2).transpose().col_join(Z2)
P3=(A[0]*A[1]*A[2]*A[3]*A[4]*A[5]-A[0]*A[1]*A[2])[0:3,3]
Z3=(A[0]*A[1]*A[2])[0:3,2]
J3=Z3.cross(P3).transpose().col_join(Z3)
P4=(A[0]*A[1]*A[2]*A[3]*A[4]*A[5]-A[0]*A[1]*A[2]*A[3])[0:3,3]
Z4=(A[0]*A[1]*A[2]*A[3])[0:3,2]
J4=Z4.cross(P4).transpose().col_join(Z4)
P5=(A[0]*A[1]*A[2]*A[3]*A[4]*A[5]-A[0]*A[1]*A[2]*A[3]*A[4])[0:3,3]
Z5=(A[0]*A[1]*A[2]*A[3]*A[4])[0:3,2]
J5=Z5.cross(P5).transpose().col_join(Z5)
Jv=J0.row_join(J1).row_join(J2).row_join(J3).row_join(J4).row_join(J5)
#Now, Let us try the Differential Translation and Rotation Approach
U0=A[0]*A[1]*A[2]*A[3]*A[4]*A[5]
J0=Matrix([[U0[0,3]*U0[1,0]-U0[1,3]*U0[0,0]],[U0[0,3]*U0[1,1]-U0[1,3]*U0[0,1]],[U0[0,3]*U0[1,2]-U0[1,3]*U0[0,2]],[U0[2,0]],[U0[2,1]],[U0[2,2]]])
U1=A[1]*A[2]*A[3]*A[4]*A[5]
J1=Matrix([[U1[0,3]*U1[1,0]-U1[1,3]*U1[0,0]],[U1[0,3]*U1[1,1]-U0[1,3]*U1[0,1]]J,[U1[0,3]*U1[1,2]-U1[1,3]*U1[0,2]],[U1[2,0]],[U1[2,1]],[U1[2,2]]])
U2=A[2]*A[3]*A[4]*A[5]
J2=Matrix([[U2[0,3]*U2[1,0]-U2[1,3]*U2[0,0]],[U2[0,3]*U2[1,1]-U2[1,3]*U2[0,1]],[U2[0,3]*U2[1,2]-U2[1,3]*U2[0,2]],[U2[2,0]],[U2[2,1]],[U2[2,2]]])
U3=A[3]*A[4]*A[5]
J3=Matrix([[U3[0,3]*U3[1,0]-U3[1,3]*U3[0,0]],[U3[0,3]*U3[1,1]-U3[1,3]*U3[0,1]],[U3[0,3]*U3[1,2]-U3[1,3]*U3[0,2]],[U3[2,0]],[U3[2,1]],[U3[2,2]]])
U4=A[4]*A[5]
J4=Matrix([[U4[0,3]*U4[1,0]-U4[1,3]*U4[0,0]],[U4[0,3]*U4[1,1]-U4[1,3]*U4[0,1]],[U4[0,3]*U4[1,2]-U4[1,3]*U4[0,2]],[U4[2,0]],[U4[2,1]],[U4[2,2]]])
U5=A[5]
J5=Matrix([[U5[0,3]*U5[1,0]-U5[1,3]*U5[0,0]],[U5[0,3]*U5[1,1]-U5[1,3]*U5[0,1]],[U5[0,3]*U5[1,2]-U5[1,3]*U5[0,2]],[U5[2,0]],[U5[2,1]],[U5[2,2]]])
Jd=J0.row_join(J1).row_join(J2).row_join(J3).row_join(J4).row_join(J5)
# The vector cross product sub-routine
def JacoV(QJ):
return np.array(Jv.subs(q[0],QJ[0]).subs(q[1],QJ[1]).subs(q[2],QJ[2]).subs(q[3],QJ[3]).subs(q[4],QJ[4]).subs(q[5],QJ[5]))
# Defining Camera Transformation
Tstart=np.array([[ 0.9983323 , 0.03651881, 0.04471023, -0.12004546],[-0.02196539, -0.47593777, 0.87920462, -4.18794775],[ 0.0533868 , -0.87872044, -0.47434189, 2.22462606],[ 0. , 0. , 0. , 1. ]])
Tcirc=np.array([[ 0.20604119, 0.33831506, -0.9181993 , 3.84545088],[ 0.97824604, -0.09434114, 0.18475504, -0.79177779],[-0.02411856, -0.93629198, -0.35039353, 1.40385485],[ 0. , 0. , 0. , 1. ]])
Tline=np.array([[ 0.02187936, 0.99890052, 0.04146134, 0.02088733],[ 0.98965908, -0.01575924, -0.14257124, 0.30318534],[-0.14176108, 0.04415196, -0.98891577, 2.93201566],[ 0. , 0. , 0. , 1. ]])
p='/home/artlab/Desktop/ProjectA_Software_Liu_Das/pumaarm569.dae' # Change the path here
env=Environment();
time.sleep(1);
env.SetViewer('qtcoin');
time.sleep(1)
env.Load(p); # Load the simple robot scene of PUMA 560
time.sleep(1)
env.GetViewer().SetCamera(Tstart)
robot = env.GetRobots()[0] # Get the first robot
Tz=np.array([[1,0,0,0],[0,1,0,0],[0,0,1,-1.37],[0,0,0,1]])
robot.SetTransform(Tz)
time.sleep(2)
#Drawing circle using Inverse Kinematics Method :
env.GetViewer().SetCamera(Tcirc)
time.sleep(2)
v=0.1
zline=0.434
aline=-1 ;bline=0.50;i=0;sam=0.005 # sam for sample
xline=0.41;
handles1=[]
while (xline<1):
T=np.array([[1,0,0,xline],[0,1,0,aline*xline+bline],[0,0,1,zline],[0,0,0,1]])
Q=GeomIK(T,-1,-1,1,1)
robot.SetDOFValues(Q)
handles1.append(env.plot3(robot.GetLinks()[5].GetTransform()[0:3,3],5))
time.sleep(sam/v)
xline=xline+ sam*cos(atan(aline))
# Drawing a Circle z^2 + y^2 = r^2 ##### Well Done
v=0.1
xcir=0.508; rcir=0.254; sam=0.005
handles=[]
ycir=0.0;
while (ycir<=rcir): # First Quadrant
T=np.array([[1,0,0,xcir],[0,1,0,ycir],[0,0,1,(rcir**2 - ycir**2)**0.5],[0,0,0,1]])
Q=GeomIK(T,-1,1,1,1)
robot.SetDOFValues(Q)
handles.append(env.plot3(robot.GetLinks()[6].GetTransform()[0:3,3],5))
ycir=ycir+sam;
time.sleep(sam/v);
ycir=rcir;
while (ycir>=0): # Second Quadrant
T=np.array([[1,0,0,xcir],[0,1,0,ycir],[0,0,1,-(rcir**2 - ycir**2)**0.5],[0,0,0,1]])
Q=GeomIK(T,-1,1,1,1)
robot.SetDOFValues(Q)
handles.append(env.plot3(robot.GetLinks()[6].GetTransform()[0:3,3],5))
ycir=ycir-sam;
time.sleep(sam/v);
ycir=0.0;
while (ycir>=-rcir): # Third Quadrant
T=np.array([[1,0,0,xcir],[0,1,0,ycir],[0,0,1,-(rcir**2 - ycir**2)**0.5],[0,0,0,1]])
Q=GeomIK(T,-1,1,1,1)
robot.SetDOFValues(Q)
handles.append(env.plot3(robot.GetLinks()[6].GetTransform()[0:3,3],5))
ycir=ycir-sam;
time.sleep(sam/v);
ycir=-rcir;
while (ycir<=0): # Fourth Quadrant
T=np.array([[1,0,0,xcir],[0,1,0,ycir],[0,0,1,(rcir**2 - ycir**2)**0.5],[0,0,0,1]])
Q=GeomIK(T,-1,1,1,1)
robot.SetDOFValues(Q)
handles.append(env.plot3(robot.GetLinks()[6].GetTransform()[0:3,3],5))
ycir=ycir+sam;
time.sleep(sam/v);
time.sleep(2)
# Drawing Line using inverse Kinematics Method
v=0.1
zline=0.434
aline=-1 ;bline=0.50;i=0;sam=0.005 # sam for sample
xline=0.41;
handles1=[]
while (xline<1):
T=np.array([[1,0,0,xline],[0,1,0,aline*xline+bline],[0,0,1,zline],[0,0,0,1]])
Q=GeomIK(T,-1,-1,1,1)
robot.SetDOFValues(Q)
handles1.append(env.plot3(robot.GetLinks()[5].GetTransform()[0:3,3],5))
time.sleep(sam/v)
xline=xline+ sam*cos(atan(aline))