-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram2.py~
310 lines (249 loc) · 11.6 KB
/
Program2.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Program of ECE 569 Project A
# Authors :- Debasmit Das, Tongyang Liu
# Input :- DH Parameters and Joint Ranges and XML File
# Output :- Link Transformation Matrices, OpenRave Visualization, Inverse Kinematic Solutions, Error, Indicators
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
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
# Now Each Link Transformation Matrix is stored as an element of a List
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
P=numpy.array([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]);#Create another blank Matrix
# 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"
## This Matrix belongs to sympy and not numpy.alright then I use yurs ok
#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)
# Building the vector cross product jacobian function ######### important
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]))
##### Like this ?? u there? im It will substitute the joint variables which will be the argument for this function. OK
#can you explain what QJ is?
# are u there
# QJ is array of joint angles
# ok, so basically this function is applying qj's value to QJ correct? yes
#ok so what I hav e to do
#ok
# think about the vector cross product
###Calling env.plot3 deletes the trajectory
p='/home/artlab/Desktop/ProjectA_Software_Liu_Das/pumaarm569.dae'
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)
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)
# Create a function for finding Geometric Inverse Kinematics
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
# Drawing the line y=ax+b using inverse kinematics ##### Good Done
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);
# Deploying Physics Engine
physics = RaveCreatePhysicsEngine(env,'ode')
env.SetPhysicsEngine(physics)
robot.GetLinks()[0].SetStatic(True)
# Now to Apply Jacobian Method to draw a circle. ######## To be fixed
# We will use the polar method :
rcir=0.254; xcir=0.808;v=0.1;w=v/rcir;
tH=0;
Tstart=np.array([[1,0,0,xcir],[0,1,0,rcir*sin(tH)],[0,0,1,rcir*cos(tH)],[0,0,0,1]])
Qstart=GeomIK(Tstart,-1,-1,1,1)
robot.SetDOFValues(Qstart)
handles=[];sam=0.01
while(tH<=2*np.pi):
vtask=np.array([[0],[rcir*w*cos(tH)],[-rcir*w*sin(tH)],[0],[0],[0]])
T=np.array([[1,0,0,xcir],[0,1,0,rcir*sin(tH)],[0,0,1,rcir*cos(tH)],[0,0,0,1]])
Q=GeomIK(T,-1,-np.sign(cos(tH)),1,1)
Qdot=np.dot(linalg.inv(JacoV(Q)),vtask)
robot.SetDOFVelocities(Qdot)
handles.append(env.plot3(robot.GetLinks()[6].GetTransform()[0:3,3],5))
time.sleep(sam/w)
tH=tH+sam;
robot.SetDOFVelocities([0,0,0,0,0,0])
# Now to Apply Jacobian Method to draw a Line.
v=0.1
zline=0.434
aline=-1 ;bline=0.56;i=0;sam=0.005 # sam for sample
xstart=0.41;
Tstart=np.array([[1,0,0,xstart],[0,1,0,aline*xstart + bline],[0,0,1,zline],[0,0,0,1]])
Qstart=GeomIK(Tstart,-1,-1,1,1)
robot.SetDOFValues(Qstart);xline=0.41;
handles=[]
while (xline<1):
xdot=v*cos(atan(aline))
ydot=v*sin(atan(aline))
vtask=np.array([[xdot],[ydot],[0],[0],[0],[0]])
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)
Qdot=np.dot(linalg.inv(JacoV(Q)),vtask)
robot.SetDOFVelocities(Qdot)
handles.append(env.plot3(robot.GetLinks()[5].GetTransform()[0:3,3],5))
time.sleep(sam)
xline=xline+v*sam*cos(atan(aline))
#Ass Saving jacobian Method to draw a circle Polar Method
rcir=0.254; xcir=0.508;v=0.1;w=v/rcir;
tH=0;
Tstart=np.array([[1,0,0,xcir],[0,1,0,rcir*sin(tH)],[0,0,1,rcir*cos(tH)],[0,0,0,1]])
Qstart=GeomIK(Tstart,-1,1,1,1)
robot.SetDOFValues(Qstart)
handles=[];sam=0.01
Qset=Qstart
while(tH<=2*np.pi):
vtask=np.array([[0],[rcir*w*cos(tH)],[-rcir*w*sin(tH)],[0],[0],[0]])
#T=np.array([[1,0,0,xcir],[0,1,0,rcir*sin(tH)],[0,0,1,rcir*cos(tH)],[0,0,0,1]])
#Q=GeomIK(T,-1,-np.sign(cos(tH)),1,1)
Qdot=np.dot(linalg.inv(JacoV(Qset)),vtask)
Qdotm1=np.vstack(Qdot[:, 0]).astype(np.float)
Qdotm2=Qdotm1.transpose()[0]
Qset=Qset + (Qdotm2*sam/w);
robot.SetDOFValues(Qset)
handles.append(env.plot3(robot.GetLinks()[6].GetTransform()[0:3,3],5))
time.sleep(sam/w)
tH=tH+sam;
#Ass Saving Jacobian Method to draw a line
v=0.1
zline=0.434
aline=-1 ;bline=0.56;i=0;sam=0.005 # sam for sample
xstart=0.41;
Tstart=np.array([[1,0,0,xstart],[0,1,0,aline*xstart + bline],[0,0,1,zline],[0,0,0,1]])
Qstart=GeomIK(Tstart,-1,-1,1,1)
robot.SetDOFValues(Qstart);xline=0.41;
handles=[];Qset=Qstart
while (xline<1):
xdot=v*cos(atan(aline))
ydot=v*sin(atan(aline))
vtask=np.array([[xdot],[ydot],[0],[0],[0],[0]])
#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)
Qdot=np.dot(linalg.inv(JacoV(Qset)),vtask)
Qdotm1=np.vstack(Qdot[:, 0]).astype(np.float)
Qdotm2=Qdotm1.transpose()[0]
Qset=Qset + (Qdotm2*sam);
robot.SetDOFValues(Qset)
handles.append(env.plot3(robot.GetLinks()[5].GetTransform()[0:3,3],5))
time.sleep(sam)
xline=xline+v*sam*cos(atan(aline))