-
Notifications
You must be signed in to change notification settings - Fork 36
/
robodk.py
1483 lines (1296 loc) · 50.3 KB
/
robodk.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2017 - RoboDK Software S.L. - http://www.robodk.com/
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# --------------------------------------------
# --------------- DESCRIPTION ----------------
#
# This is a robotics toolbox for RoboDK robot post processors and RoboDK API for Python
# This toolbox includes a simple matrix class for pose transofmrations (Mat)
# This toolbox has been inspired from Peter Corke's Robotics Toolbox:
# http://petercorke.com/wordpress/toolboxes/robotics-toolbox
#
# In this document: pose = transformation matrix = homogeneous matrix = 4x4 matrix
# Visit the Matrix and Quaternions FAQ for more information about pose/homogeneous transformations
# http://www.j3d.org/matrix_faq/matrfaq_latest.html
#
# More information about RoboDK post processors here:
# http://www.robodk.com/help#PostProcessor
#
# More information about the RoboDK API for Python here:
# http://www.robodk.com/doc/PythonAPI/index.html
# --------------------------------------------
import math
import operator
import sys
import unittest
import time
#----------------------------------------------------
#-------- Generic file usage ---------------
import os.path
import time
def searchfiles(pattern='C:\\RoboDK\\Library\\*.rdk'):
"""List the files in a directory with a given extension"""
import glob
return glob.glob(pattern)
def CurrentFile(file = __file__):
"""Returns the current Python file being executed"""
return os.path.realpath(file)
def getFileDir(filepath):
"""Returns the directory of a file path"""
return os.path.dirname(filepath)
def getBaseName(filepath):
"""Returns the file name and extension of a file path"""
return os.path.basename(filepath)
def getFileName(filepath):
"""Returns the file name (with no extension) of a file path"""
return os.path.splitext(os.path.basename(filepath))[0]
def DateModified(filepath, stringformat=False):
"""Returns the time that a file was modified"""
time_in_s = os.path.getmtime(filepath)
if stringformat:
return time.ctime(time_in_s)
else:
return time_in_s
def DateCreated(filepath, stringformat=False):
"""Returns the time that a file was modified"""
time_in_s = os.path.getctime(filepath)
if stringformat:
return time.ctime(time_in_s)
else:
return time_in_s
def DirExists(folder):
"""Returns true if the folder exists"""
return os.path.isdir(folder)
def FileExists(file):
"""Returns true if the file exists"""
return os.path.exists(file)
#----------------------------------------------------
#-------- Generic math usage ---------------
pi = math.pi
def pause(seconds):
"""Pause in seconds
:param pause: time in seconds
:type pause: float"""
time.sleep(seconds)
def atan2(y,x):
"""Returns angle of a 2D coordinate in the XY plane"""
return math.atan2(y,x)
def sqrt(value):
"""Returns the square root of a value"""
return math.sqrt(value)
def sin(value):
"""Returns the sinus of an angle in radians"""
return math.sin(value)
def cos(value):
"""Returns the cosinus of an angle in radians"""
return math.cos(value)
def asin(value):
"""Returns the arc sinus in radians"""
return math.asin(value)
def acos(value):
"""Returns the arc cosinus in radians"""
return math.acos(value)
def name_2_id(str_name_id):
"""Returns the number of a numbered object. For example: "Frame 3" returns 3"""
words = str_name_id.split()
number = words.pop()
if number.isdigit():
return float(number)
return -1
#----------------------------------------------------
#-------- Generic matrix usage ---------------
def rotx(rx):
r"""Returns a rotation matrix around the X axis
.. math::
R_x(\theta) = \begin{bmatrix} 1 & 0 & 0 & 0 \\
0 & c_\theta & -s_\theta & 0 \\
0 & s_\theta & c_\theta & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
:param rx: rotation around X axis in radians
:type rx: float"""
ct = math.cos(rx)
st = math.sin(rx)
return Mat([[1,0,0,0],[0,ct,-st,0],[0,st,ct,0],[0,0,0,1]])
def roty(ry):
r"""Returns a rotation matrix around the Y axis
.. math::
R_y(\theta) = \begin{bmatrix} c_\theta & 0 & s_\theta & 0 \\
0 & 1 & 0 & 0 \\
-s_\theta & 0 & c_\theta & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
:param ry: rotation around Y axis in radians
:type ry: float"""
ct = math.cos(ry)
st = math.sin(ry)
return Mat([[ct,0,st,0],[0,1,0,0],[-st,0,ct,0],[0,0,0,1]])
def rotz(rz):
r"""Returns a rotation matrix around the Z axis
.. math::
R_x(\theta) = \begin{bmatrix} c_\theta & -s_\theta & 0 & 0 \\
s_\theta & c_\theta & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
:param ry: rotation around Y axis in radians
:type ry: float"""
ct = math.cos(rz)
st = math.sin(rz)
return Mat([[ct,-st,0,0],[st,ct,0,0],[0,0,1,0],[0,0,0,1]])
def transl(tx,ty=None,tz=None):
r"""Returns a translation matrix
.. math::
T(t_x, t_y, t_z) = \begin{bmatrix} 0 & 0 & 0 & t_x \\
0 & 0 & 0 & t_y \\
0 & 0 & 0 & t_z \\
0 & 0 & 0 & 1
\end{bmatrix}
:param tx: translation along the X axis
:type tx: float
:param ty: translation along the Y axis
:type ty: float
:param tz: translation along the Z axis
:type tz: float
"""
if ty is None:
xx = tx[0]
yy = tx[1]
zz = tx[2]
else:
xx = tx
yy = ty
zz = tz
return Mat([[1,0,0,xx],[0,1,0,yy],[0,0,1,zz],[0,0,0,1]])
def RelTool(target_pose, x, y, z, rx=0,ry=0,rz=0):
"""Calculates a relative target with respect to the tool coordinates. This procedure has exactly the same behavior as ABB's RelTool instruction.
X,Y,Z are in mm, W,P,R are in degrees."""
if type(target_pose) != Mat:
target_pose = target_pose.Pose()
new_target = target_pose*transl(x,y,z)*rotx(rx*pi/180)*roty(ry*pi/180)*rotz(rz*pi/180)
return new_target
def Offset(target_pose, x, y, z, rx=0,ry=0,rz=0):
"""Calculates a relative target with respect to the reference frame coordinates.
X,Y,Z are in mm, W,P,R are in degrees."""
if type(target_pose) != Mat:
# item object assumed:
target_pose = target_pose.Pose()
if not target_pose.isHomogeneous():
raise Exception(MatrixError, "Pose matrix is not homogeneous!")
new_target = transl(x,y,z)*rotx(rx*pi/180.0)*roty(ry*pi/180.0)*rotz(rz*pi/180.0)*target_pose
return new_target
def point_Zaxis_2_pose(point, zaxis, yaxis_hint1=[0,0,1], yaxis_hint2=[0,1,1]):
"""Returns a pose given the origin as a point, a Z axis and a preferred orientation for the Y axis"""
pose = eye(4)
pose.setPos(point)
pose.setVZ(zaxis)
yaprox = yaxis_hint1
if angle3(zaxis, yaprox) < 2*pi/180:
yaprox = yaxis_hint2
xaxis = normalize3(cross(yaprox, zaxis))
yaxis = cross(zaxis, xaxis)
pose.setVX(xaxis)
pose.setVY(yaxis)
return pose
def eye(size=4):
r"""Return the identity matrix
.. math::
T(t_x, t_y, t_z) = \begin{bmatrix} 1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
:param size: square matrix size (4 by default)
:type size: int"""
return Mat([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]])
def size(matrix,dim=None):
"""Returns the size of a matrix (m,n).
Dim can be set to 0 to return m (rows) or 1 to return n (columns)
:param matrix: pose
:type matrix: :class:`.Mat`
:param dim: dimension
:type dim: int
"""
return matrix.size(dim)
def tr(matrix):
"""Returns the transpose of the matrix
:param matrix: pose
:type matrix: :class:`.Mat`"""
return matrix.tr()
def invH(matrix):
"""Returns the inverse of a homogeneous matrix
:param matrix: pose
:type matrix: :class:`.Mat`"""
return matrix.invH()
def catV(mat1, mat2):
"""Concatenate 2 matrices (vertical concatenation)"""
return mat1.catV(mat2)
def catH(mat1, mat2):
"""Concatenate 2 matrices (horizontal concatenation)"""
return mat1.catH(mat2)
def tic():
"""Start a stopwatch timer"""
import time
global TICTOC_START_TIME
TICTOC_START_TIME = time.time()
def toc():
"""Read the stopwatch timer"""
import time
if 'TICTOC_START_TIME' in globals():
elapsed = time.time() - TICTOC_START_TIME
#print("Elapsed time is " + str(elapsed) + " seconds.")
return elapsed
else:
print("Toc: start time not set")
return -1
def LoadList(strfile, separator=','):
"""Load data from a CSV file or a TXT file to a list of numbers"""
import csv
# Read all CSV data:
csvdata = []
with open(strfile) as csvfile:
csvread = csv.reader(csvfile, delimiter=separator, quotechar='|')
for row in csvread:
row_nums = [float(i) for i in row]
csvdata.append(row_nums)
return csvdata
def LoadMat(strfile, separator=','):
"""Load data from a CSV file or a TXT file to a :class:`.Mat` Matrix"""
return Mat(LoadList(strfile,separator))
#----------------------------------------------------
#------ Pose to xyzrpw and xyzrpw to pose------------
def pose_2_xyzrpw(H):
"""Calculates the equivalent position and euler angles ([x,y,z,r,p,w] array) of the given pose according to the following operation:
Note: transl(x,y,z)*rotz(w*pi/180)*roty(p*pi/180)*rotx(r*pi/180)
See also: xyzrpw_2_pose()
:param H: pose
:type H: :class:`.Mat`"""
x = H[0,3]
y = H[1,3]
z = H[2,3]
if (H[2,0] > (1.0 - 1e-6)):
p = -pi/2
r = 0
w = math.atan2(-H[1,2],H[1,1])
elif H[2,0] < -1.0 + 1e-6:
p = pi/2
r = 0
w = math.atan2(H[1,2],H[1,1])
else:
p = math.atan2(-H[2,0],sqrt(H[0,0]*H[0,0]+H[1,0]*H[1,0]))
w = math.atan2(H[1,0],H[0,0])
r = math.atan2(H[2,1],H[2,2])
return [x, y, z, r*180/pi, p*180/pi, w*180/pi]
def xyzrpw_2_pose(xyzrpw):
"""Calculates the pose from the position and euler angles ([x,y,z,r,p,w] array)
The result is the same as calling: H = transl(x,y,z)*rotz(w*pi/180)*roty(p*pi/180)*rotx(r*pi/180)
See also: pose_2_xyzrpw()"""
[x,y,z,r,p,w] = xyzrpw
a = r*pi/180
b = p*pi/180
c = w*pi/180
ca = math.cos(a)
sa = math.sin(a)
cb = math.cos(b)
sb = math.sin(b)
cc = math.cos(c)
sc = math.sin(c)
H = Mat([[cb*cc, cc*sa*sb - ca*sc, sa*sc + ca*cc*sb, x],[cb*sc, ca*cc + sa*sb*sc, ca*sb*sc - cc*sa, y],[-sb, cb*sa, ca*cb, z],[0,0,0,1]])
return H
def TxyzRxyz_2_Pose(xyzrpw):
"""Calculates the pose from the position and euler angles ([x,y,z,rx,ry,rz] array)
The result is the same as calling: H = transl(x,y,z)*rotx(rx)*roty(ry)*rotz(rz)"""
[x,y,z,rx,ry,rz] = xyzrpw
srx = math.sin(rx);
crx = math.cos(rx);
sry = math.sin(ry);
cry = math.cos(ry);
srz = math.sin(rz);
crz = math.cos(rz);
H = Mat([[ cry*crz, -cry*srz, sry, x],[crx*srz + crz*srx*sry, crx*crz - srx*sry*srz, -cry*srx, y],[srx*srz - crx*crz*sry, crz*srx + crx*sry*srz, crx*cry, z],[0,0,0,1]])
return H
def Pose_2_TxyzRxyz(H):
"""Converts a pose to a 6-value target as [x,y,z,rx,ry,rz]:
H = transl(x,y,z)*rotx(rx)*roty(ry)*rotz(rz).
:param H: pose
:type H: :class:`.Mat`"""
x = H[0,3]
y = H[1,3]
z = H[2,3]
a = H[0,0]
b = H[0,1]
c = H[0,2]
d = H[1,2]
e = H[2,2]
if c > (1.0 - 1e-6):
ry1 = pi/2
rx1 = 0
rz1 = atan2(H[1,0],H[1,1])
elif c < (-1.0 + 1e-6):
ry1 = -pi/2
rx1 = 0
rz1 = atan2(H[1,0],H[1,1])
else:
sy = c
cy1 = +sqrt(1-sy*sy)
sx1 = -d/cy1
cx1 = e/cy1
sz1 = -b/cy1
cz1 =a/cy1
rx1 = atan2(sx1,cx1)
ry1 = atan2(sy,cy1)
rz1 = atan2(sz1,cz1)
return [x, y, z, rx1, ry1, rz1]
def Pose_2_Staubli(H):
"""Converts a pose (4x4 matrix) to a Staubli XYZWPR target
:param H: pose
:type H: :class:`.Mat`"""
xyzwpr = Pose_2_TxyzRxyz(H)
xyzwpr[3] = xyzwpr[3]*180.0/pi
xyzwpr[4] = xyzwpr[4]*180.0/pi
xyzwpr[5] = xyzwpr[5]*180.0/pi
return xyzwpr
def Pose_2_Motoman(H):
"""Converts a pose (4x4 matrix) to a Motoman XYZWPR target
:param H: pose
:type H: :class:`.Mat`"""
xyzwpr = pose_2_xyzrpw(H)
return xyzwpr
def Pose_2_Fanuc(H):
"""Converts a pose (4x4 matrix) to a Fanuc XYZWPR target
:param H: pose
:type H: :class:`.Mat`"""
xyzwpr = pose_2_xyzrpw(H)
return xyzwpr
def Motoman_2_Pose(xyzwpr):
"""Converts a Motoman target to a pose (4x4 matrix)"""
return xyzrpw_2_pose(xyzwpr)
def Pose_2_KUKA(H):
"""Converts a pose (4x4 matrix) to a Kuka target
:param H: pose
:type H: :class:`.Mat`"""
x = H[0,3]
y = H[1,3]
z = H[2,3]
if (H[2,0]) > (1.0 - 1e-6):
p = -pi/2
r = 0
w = atan2(-H[1,2],H[1,1])
elif (H[2,0]) < (-1.0 + 1e-6):
p = pi/2
r = 0
w = atan2(H[1,2],H[1,1])
else:
p = atan2(-H[2,0],sqrt(H[0,0]*H[0,0]+H[1,0]*H[1,0]))
w = atan2(H[1,0],H[0,0])
r = atan2(H[2,1],H[2,2])
return [x, y, z, w*180/pi, p*180/pi, r*180/pi]
def KUKA_2_Pose(xyzrpw):
"""Converts a KUKA XYZABC target to a pose (4x4 matrix)"""
[x,y,z,r,p,w] = xyzrpw
a = r*math.pi/180.0
b = p*math.pi/180.0
c = w*math.pi/180.0
ca = math.cos(a)
sa = math.sin(a)
cb = math.cos(b)
sb = math.sin(b)
cc = math.cos(c)
sc = math.sin(c)
return Mat([[cb*ca, ca*sc*sb - cc*sa, sc*sa + cc*ca*sb, x],[cb*sa, cc*ca + sc*sb*sa, cc*sb*sa - ca*sc, y],[-sb, cb*sc, cc*cb, z],[0.0,0.0,0.0,1.0]])
def Adept_2_Pose(xyzrpw):
"""Converts an Adept XYZRPW target to a pose (4x4 matrix)"""
[x,y,z,r,p,w] = xyzrpw
a = r*math.pi/180.0
b = p*math.pi/180.0
c = w*math.pi/180.0
ca = math.cos(a)
sa = math.sin(a)
cb = math.cos(b)
sb = math.sin(b)
cc = math.cos(c)
sc = math.sin(c)
return Mat([[ca*cb*cc - sa*sc, - cc*sa - ca*cb*sc, ca*sb, x],[ca*sc + cb*cc*sa, ca*cc - cb*sa*sc, sa*sb, y],[-cc*sb, sb*sc, cb, z],[0.0,0.0,0.0,1.0]])
def Pose_2_Adept(H):
"""Converts a pose to an Adept target
:param H: pose
:type H: :class:`.Mat`"""
x = H[0,3]
y = H[1,3]
z = H[2,3]
if H[2,2] > (1.0 - 1e-6):
r = 0
p = 0
w = atan2(H[1,0],H[0,0])
elif H[2,2] < (-1.0 + 1e-6):
r = 0
p = pi
w = atan2(H[1,0],H[1,1])
else:
cb=H[2,2]
sb=+sqrt(1-cb*cb)
sc=H[2,1]/sb
cc=-H[2,0]/sb
sa=H[1,2]/sb
ca=H[0,2]/sb
r = atan2(sa,ca)
p = atan2(sb,cb)
w = atan2(sc,cc)
return [x, y, z, r*180/pi, p*180/pi, w*180/pi]
def Comau_2_Pose(xyzrpw):
"""Converts a Comau XYZRPW target to a pose (4x4 matrix)"""
return Adept_2_Pose(xyzrpw)
def Pose_2_Comau(H):
"""Converts a pose to a Comau target
:param H: pose
:type H: :class:`.Mat`"""
return Pose_2_Adept(H)
def Pose_2_Nachi(pose):
"""Converts a pose to a Nachi XYZRPW target
:param pose: pose
:type pose: :class:`.Mat`"""
[x,y,z,r,p,w] = pose_2_xyzrpw(pose)
return [x,y,z,w,p,r]
def Nachi_2_Pose(xyzwpr):
"""Converts a Nachi XYZRPW target to a pose (4x4 matrix)"""
return Fanuc_2_Pose(xyzwpr)
def pose_2_quaternion(Ti):
"""Returns the quaternion orientation vector of a pose (4x4 matrix)
:param Ti: pose
:type Ti: :class:`.Mat`"""
a=(Ti[0,0])
b=(Ti[1,1])
c=(Ti[2,2])
sign2=1
sign3=1
sign4=1
if (Ti[2,1]-Ti[1,2])<0:
sign2=-1;
if (Ti[0,2]-Ti[2,0])<0:
sign3=-1;
if (Ti[1,0]-Ti[0,1])<0:
sign4=-1
q1=sqrt(max(a+b+c+1,0))/2
q2=sign2*sqrt(max(a-b-c+1,0))/2
q3=sign3*sqrt(max(-a+b-c+1,0))/2
q4=sign4*sqrt(max(-a-b+c+1,0))/2
return [q1, q2, q3, q4]
def quaternion_2_pose(qin):
"""Returns the pose orientation matrix (4x4 matrix) from a quaternion orientation vector"""
qnorm = sqrt(qin[0]*qin[0]+qin[1]*qin[1]+qin[2]*qin[2]+qin[3]*qin[3])
q = qin
q[0] = q[0]/qnorm
q[1] = q[1]/qnorm
q[2] = q[2]/qnorm
q[3] = q[3]/qnorm
pose = Mat([[ 1 - 2*q[2]*q[2] - 2*q[3]*q[3] , 2*q[1]*q[2] - 2*q[3]*q[0] , 2*q[1]*q[3] + 2*q[2]*q[0] , 0],
[2*q[1]*q[2] + 2*q[3]*q[0] , 1 - 2*q[1]*q[1] - 2*q[3]*q[3] , 2*q[2]*q[3] - 2*q[1]*q[0] , 0],
[2*q[1]*q[3] - 2*q[2]*q[0] , 2*q[2]*q[3] + 2*q[1]*q[0] , 1 - 2*q[1]*q[1] - 2*q[2]*q[2], 0],
[0 , 0 , 0 , 1]])
return pose
def Pose_2_ABB(H):
"""Converts a pose to an ABB target
:param H: pose
:type H: :class:`.Mat`"""
q = pose_2_quaternion(H)
return [H[0,3],H[1,3],H[2,3],q[0],q[1],q[2],q[3]]
def print_pose_ABB(pose):
"""Displays an ABB RAPID target
:param pose: pose
:type pose: :class:`.Mat`"""
q = pose_2_quaternion(pose)
print('[[%.3f,%.3f,%.3f],[%.6f,%.6f,%.6f,%.6f]]'%(pose[0,3],pose[1,3],pose[2,3],q[0],q[1],q[2],q[3]))
def Pose_2_UR(pose):
"""Calculate the p[x,y,z,rx,ry,rz] position for a pose target"""
def saturate_1(value):
return min(max(value,-1.0),1.0)
angle = acos( saturate_1((pose[0,0]+pose[1,1]+pose[2,2]-1)/2) )
rxyz = [pose[2,1]-pose[1,2], pose[0,2]-pose[2,0], pose[1,0]-pose[0,1]]
if angle == 0:
rxyz = [0,0,0]
else:
rxyz = normalize3(rxyz)
rxyz = mult3(rxyz, angle)
return [pose[0,3], pose[1,3], pose[2,3], rxyz[0], rxyz[1], rxyz[2]]
def UR_2_Pose(xyzwpr):
"""Calculate the pose target given a p[x,y,z,rx,ry,rz] cartesian target"""
x,y,z,w,p,r = xyzwpr
wpr = [w,p,r]
angle = norm(wpr)
cosang = cos(0.5*angle)
if angle == 0.0:
q234 = [0.0,0.0,0.0]
else:
ratio = sin(0.5*angle)/angle
q234 = mult3(wpr, ratio)
q1234 = [cosang, q234[0], q234[1], q234[2]]
pose = quaternion_2_pose(q1234)
pose.setPos([x,y,z])
return pose
#----------------------------------------------------
#-------- ROBOT MODEL (D-H and D-H M) ---------------
def dh(rz,tx=None,tz=None,rx=None):
"""Returns the Denavit-Hartenberg 4x4 matrix for a robot link.
calling dh(rz,tx,tz,rx) is the same as using rotz(rz)*transl(tx,0,tx)*rotx(rx)
calling dh(rz,tx,tz,rx) is the same as calling dh([rz,tx,tz,rx])
"""
if tx is None: [rz,tx,tz,rx] = rz
crx = math.cos(rx)
srx = math.sin(rx)
crz = math.cos(rz)
srz = math.sin(rz)
return Mat( [[crz, -srz*crx, srz*srx, tx*crz],
[srz, crz*crx, -crz*srx, tx*srz],
[ 0, srx, crx, tz],
[ 0, 0, 0, 1]]);
def dhm(rx, tx=None, tz=None, rz=None):
"""Returns the Denavit-Hartenberg Modified 4x4 matrix for a robot link (Craig 1986).
calling dhm(rx,tx,tz,rz) is the same as using rotx(rx)*transl(tx,0,tx)*rotz(rz)
calling dhm(rx,tx,tz,rz) is the same as calling dhm([rx,tx,tz,rz])
"""
if tx is None: [rx,tx,tz,rz] = rx
crx = math.cos(rx)
srx = math.sin(rx)
crz = math.cos(rz)
srz = math.sin(rz)
return Mat([[crz, -srz, 0, tx],
[crx*srz, crx*crz, -srx, -tz*srx],
[srx*srz, crz*srx, crx, tz*crx],
[ 0, 0, 0, 1]]);
def joints_2_angles(jin, type):
"""Converts the robot joints into angles between links depending on the type of the robot."""
jout = jin
if type == 2:
jout[2] = -jin[1] - jin[2]
jout[3] = -jin[3]
jout[4] = -jin[4]
jout[5] = -jin[5]
elif type == 3:
jout[2] = -jin[2]
jout[3] = -jin[3]
jout[4] = -jin[4]
jout[5] = -jin[5]
elif type == 4:
jout[2] = +jin[1] + jin[2]
return jout
def angles_2_joints(jin, type):
"""Converts the angles between links into the robot joint space depending on the type of the robot."""
jout = jin
if type == 2:
jout[2] = -jin[1] - jin[2]
jout[3] = -jin[3]
jout[4] = -jin[4]
jout[5] = -jin[5]
elif type == 3:
jout[2] = -jin[2]
jout[3] = -jin[3]
jout[4] = -jin[4]
jout[5] = -jin[5]
return jout
#----------------------------------------------------
#-------- Useful geometric tools ---------------
def norm(p):
"""Returns the norm of a 3D vector"""
return sqrt(p[0]*p[0] + p[1]*p[1] + p[2]*p[2])
def normalize3(a):
"""Returns the unitary vector"""
norminv = 1.0/norm(a)
return [a[0]*norminv,a[1]*norminv,a[2]*norminv]
def cross(a, b):
"""Returns the cross product of two 3D vectors"""
c = [a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0]]
return c
def dot(a,b):
"""Returns the dot product of two 3D vectors"""
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]
def angle3(a,b):
"""Returns the angle in radians of two 3D vectors"""
return acos(dot(normalize3(a),normalize3(b)))
def pose_angle(pose):
"""Returns the angle in radians of a 4x4 matrix pose
:param pose: pose
:type pose: :class:`.Mat`"""
cos_ang = (pose[0,0]+pose[1,1]+pose[2,2]-1)/2
cos_ang = min(max(cos_ang,-1),1)
return acos(cos_ang)
def pose_angle_between(pose1, pose2):
"""Returns the angle in radians between two poses (4x4 matrix pose)"""
return pose_angle(invH(pose1)*pose2)
def mult3(v,d):
"""Multiplies a 3D vector to a scalar"""
return [v[0]*d, v[1]*d, v[2]*d]
def subs3(a,b):
"""Subtracts two 3D vectors c=a-b"""
return [a[0]-b[0],a[1]-b[1],a[2]-b[2]]
def add3(a,b):
"""Adds two 3D vectors c=a+b"""
return [a[0]+b[0],a[1]+b[1],a[2]+b[2]]
def distance(a,b):
"""Calculates the distance between two points"""
return norm(subs3(a,b))
def intersect_line_2_plane(pline,vline,pplane,vplane):
"""Calculates the intersection betweeen a line and a plane"""
D = -dot(vplane,pplane)
k = -(D+dot(vplane,pline))/dot(vplane,vline)
p = add3(pline,mult3(vline,k))
return p
def proj_pt_2_plane(point,planepoint,planeABC):
"""Projects a point to a plane"""
return intersect_line_2_plane(point,planeABC,planepoint,planeABC);
def proj_pt_2_line(point, paxe, vaxe):
"""Projects a point to a line"""
vpaxe2point = subs3(point,paxe)
dist = dot(vaxe,vpaxe2point)/dot(vaxe,vaxe)
return add3(paxe,mult3(vaxe,dist))
def fitPlane(points):
"""Best fits a plane to a cloud of points"""
import numpy as np
XYZ = np.array(points)
[rows,cols] = XYZ.shape
# Set up constraint equations of the form AB = 0,
# where B is a column vector of the plane coefficients
# in the form b(1)*X + b(2)*Y +b(3)*Z + b(4) = 0.
p = (np.ones((rows,1)))
AB = np.hstack([XYZ,p])
[u, d, v] = np.linalg.svd(AB,0)
B = v[3,:] # Solution is last column of v.
nn = np.linalg.norm(B[0:3])
B = B / nn
pplane = [0,0,-(B[3]/B[2])]
vplane = B[0:3].tolist()
return pplane, vplane
#----------------------------------------------------
#-------- Mat matrix class ---------------
class MatrixError(Exception):
""" An exception class for Matrix """
pass
class Mat(object):
"""A pose matrix object. A pose is a 4x4 matrix that represents the position and orientation of one reference frame with respect to another one, in the 3D space.
This type is meant to be used for robotics."""
def __init__(self, rows=None, ncols=None):
if ncols is None:
if rows is None:
m = 4
n = 4
self.rows = [[0]*n for x in range(m)]
else:
if isinstance(rows,Mat):
rows = rows.copy().rows
m = len(rows)
transpose = 0
if not isinstance(rows[0],list):
rows = [rows]
transpose = 1
n = len(rows[0])
if any([len(row) != n for row in rows[1:]]):# Validity check
raise Exception(MatrixError, "inconsistent row length")
self.rows = rows
if transpose:
self.rows = [list(item) for item in zip(*self.rows)]
else:
m = rows
n = ncols
self.rows = [[0]*n for x in range(m)]
def copy(self):
sz = self.size()
newmat = Mat(sz[0],sz[1])
for i in range(sz[0]):
for j in range(sz[1]):
newmat[i,j] = self[i,j]
return newmat
def __getitem__(self, idx):
if isinstance(idx,int):#integer A[1]
return tr(Mat(self.rows[idx]))
elif isinstance(idx,slice):#one slice: A[1:3]
return Mat(self.rows[idx])
else:#two slices: A[1:3,1:3]
idx1 = idx[0]
idx2 = idx[1]
if isinstance(idx1,int) and isinstance(idx2,int):
return self.rows[idx1][idx2]
matsize =self.size();
if isinstance(idx1,slice):
indices1 = idx1.indices(matsize[0])
rg1 = range(*indices1)
else: #is int
rg1 = range(idx1,idx1+1)
if isinstance(idx2,slice):
indices2 = idx2.indices(matsize[1])
rg2 = range(*indices2)
else: #is int
rg2 = range(idx2,idx2+1)
#newm = int(abs((rg1.stop-rg1.start)/rg1.step))
#newn = int(abs((rg2.stop-rg2.start)/rg2.step))
newm = rg1
newn = rg2
newmat = Mat(len(newm),len(newn))
cm = 0
for i in rg1:
cn = 0
for j in rg2:
newmat.rows[cm][cn] = self.rows[i][j]
cn = cn + 1
cm = cm + 1
return newmat
def __setitem__(self, idx, item):
if isinstance(item,float) or isinstance(item,int):
item = Mat([[item]])
elif isinstance(item, list):
item = Mat(item)
matsize = self.size();
if isinstance(idx,int):#integer A[1]
idx1 = idx
idx2 = 0
#raise Exception(MatrixError, "Cannot set item. Use [i,:] instead.")
#self.rows[idx] = item
elif isinstance(idx,slice):#one slice: A[1:3]
# raise Exception(MatrixError, "Cannot set item. Use [a:b,:] instead.")
idx1 = idx
idx2 = 0
else:
idx1 = idx[0]
idx2 = idx[1]
# at this point we have two slices: example A[1:3,1:3]
if isinstance(idx1,slice):
indices1 = idx1.indices(matsize[0])
rg1 = range(*indices1)
else: #is int
rg1 = range(idx1,idx1+1)
if isinstance(idx2,slice):
indices2 = idx2.indices(matsize[1])
rg2 = range(*indices2)
else: #is int
rg2 = range(idx2,idx2+1)
#newm = int(abs((rg1.stop-rg1.start)/rg1.step))
#newn = int(abs((rg2.stop-rg2.start)/rg2.step))
newm = rg1
newn = rg2
itmsz = item.size();
if len(newm) != itmsz[0] or len(newn) != itmsz[1]:
raise Exception(MatrixError, "Submatrix indices does not match the new matrix sizes",itmsz[0],"x",itmsz[1],"<-",newm,"x",newn)
#newmat = Mat(newm,newn)
cm = 0
for i in rg1:
cn = 0
for j in rg2:
self.rows[i][j] = item.rows[cm][cn]
cn = cn + 1
cm = cm + 1
def __str__(self):
#s='\n [ '.join([(', '.join([str(item) for item in row])+' ],') for row in self.rows])
s='\n [ '.join([(', '.join(['%.3f'%item for item in row])+' ],') for row in self.rows])
return '[[ ' + s[:-1] + ']\n'
def __repr__(self):
s=str(self)
rank = str(self.size())
rep="Matrix: %s\n%s" % (rank,s)
return rep
def tr(self):
"""Returns the transpose of the matrix"""
mat = Mat([list(item) for item in zip(*self.rows)])
return mat
def size(self,dim=None):
"""Returns the size of a matrix (m,n).
Dim can be set to 0 to return m (rows) or 1 to return n (columns)"""
m = len(self.rows)
n = len(self.rows[0])
if dim is None:
return (m, n)
elif dim==0:
return m
elif dim==1:
return n
else:
raise Exception(MatrixError, "Invalid dimension!")
def catV(self,mat2):
"""Concatenate with another matrix (vertical concatenation)"""
if not isinstance(mat2, Mat):
raise Exception(MatrixError, "Concatenation must be performed with 2 matrices")
sz1 = self.size()
sz2 = mat2.size()
if sz1[1] != sz2[1]:
raise Exception(MatrixError, "Horizontal size of matrices does not match")
newmat = Mat(sz1[0]+sz2[0],sz1[1])
newmat[0:sz1[0],:] = self
newmat[sz1[0]:,:] = mat2
return newmat
def catH(self,mat2):
"""Concatenate with another matrix (horizontal concatenation)"""
if not isinstance(mat2, Mat):
raise Exception(MatrixError, "Concatenation must be performed with 2 matrices")
sz1 = self.size()
sz2 = mat2.size()
if sz1[0] != sz2[0]:
raise Exception(MatrixError, "Horizontal size of matrices does not match")
newmat = Mat(sz1[0],sz1[1]+sz2[1])
newmat[:,:sz1[1]] = self
newmat[:,sz1[1]:] = mat2
return newmat
def __eq__(self, mat):
"""Test equality"""
return (mat.rows == self.rows)
def __add__(self, mat):
"""Add a matrix to this matrix and
return the new matrix. It doesn't modify
the current matrix"""
if isinstance(mat,int) or isinstance(mat,float):
m, n = self.size()
result = Mat(m, n)
for x in range(m):
for y in range(n):
result.rows[x][y] = self.rows[x][y] + mat
return result
sz = self.size()
m = sz[0]
n = sz[1]
ret = Mat(m,n)
if sz != mat.size():
raise Exception(MatrixError, "Trying to add matrixes of varying size!")
for x in range(m):
row = [sum(item) for item in zip(self.rows[x], mat.rows[x])]
ret.rows[x] = row