-
Notifications
You must be signed in to change notification settings - Fork 117
/
RoboDK_API.vb
4739 lines (4320 loc) · 190 KB
/
RoboDK_API.vb
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
' 25/09/2017 (Frederic De Oliveira - www.rpc3d.com)
' Converted from C# to VB.Net
' Wrapped some methods from python API (RoboDK v3.2.6) // python code on non-wrapped method is commented for later conversion
' The RoboDK class allows interacting with RoboDK
' This library includes a robotics toolbox for C#, inspired from Peter Corke's Robotics Toolbox:
' http://petercorke.com/Robotics_Toolbox.html
'
' In this library: pose = transformation matrix = homogeneous matrix = 4x4 matrix
' Visit: http://www.j3d.org/matrix_faq/matrfaq_latest.html
' to better understand homogeneous matrix operations
'
' This library includes the mathematics to operate with homogeneous matrices for robotics.
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Net.Sockets ' for Socket
Imports Microsoft.Win32 ' for registry key
''' <summary>
''' Matrix class for robotics.
''' </summary>
Public Class Mat ' simple matrix class for homogeneous operations
Public rows As Integer
Public cols As Integer
Private _mat As Double(,)
' Class used for Matrix exceptions
Public Class MatException
Inherits Exception
Public Sub New(Message As String)
MyBase.New(Message)
End Sub
End Class
''' <summary>
''' Matrix class constructor for any size matrix
''' </summary>
''' <param name="Rows__1">dimension 1 size (rows)</param>
''' <param name="Cols__2">dimension 2 size (columns)</param>
Public Sub New(Rows__1 As Integer, Cols__2 As Integer)
' Matrix Class constructor
rows = Rows__1
cols = Cols__2
_mat = New Double(rows - 1, cols - 1) {}
End Sub
''' <summary>
''' Matrix class constructor for a 4x4 homogeneous matrix
''' </summary>
''' <param name="nx">Position [0,0]</param>
''' <param name="ox">Position [0,1]</param>
''' <param name="ax">Position [0,2]</param>
''' <param name="tx">Position [0,3]</param>
''' <param name="ny">Position [1,0]</param>
''' <param name="oy">Position [1,1]</param>
''' <param name="ay">Position [1,2]</param>
''' <param name="ty">Position [1,3]</param>
''' <param name="nz">Position [2,0]</param>
''' <param name="oz">Position [2,1]</param>
''' <param name="az">Position [2,2]</param>
''' <param name="tz">Position [2,3]</param>
Public Sub New(nx As Double, ox As Double, ax As Double, tx As Double, ny As Double, oy As Double, _
ay As Double, ty As Double, nz As Double, oz As Double, az As Double, tz As Double)
' Matrix Class constructor
rows = 4
cols = 4
_mat = New Double(rows - 1, cols - 1) {}
_mat(0, 0) = nx
_mat(1, 0) = ny
_mat(2, 0) = nz
_mat(0, 1) = ox
_mat(1, 1) = oy
_mat(2, 1) = oz
_mat(0, 2) = ax
_mat(1, 2) = ay
_mat(2, 2) = az
_mat(0, 3) = tx
_mat(1, 3) = ty
_mat(2, 3) = tz
_mat(3, 0) = 0.0
_mat(3, 1) = 0.0
_mat(3, 2) = 0.0
_mat(3, 3) = 1.0
End Sub
''' <summary>
''' Matrix class constructor for a 4x4 homogeneous matrix as a copy from another matrix
''' </summary>
Public Sub New(pose As Mat)
rows = pose.rows
cols = pose.cols
_mat = New Double(rows - 1, cols - 1) {}
For i As Integer = 0 To rows - 1
For j As Integer = 0 To cols - 1
_mat(i, j) = pose(i, j)
Next
Next
End Sub
''' <summary>
''' Matrix class constructor for a 4x1 vector [x,y,z,1]
''' </summary>
''' <param name="x">x coordinate</param>
''' <param name="y">y coordinate</param>
''' <param name="z">z coordinate</param>
Public Sub New(x As Double, y As Double, z As Double)
rows = 4
cols = 1
_mat = New Double(rows - 1, cols - 1) {}
_mat(0, 0) = x
_mat(1, 0) = y
_mat(2, 0) = z
_mat(3, 0) = 1.0
End Sub
'----------------------------------------------------
'-------- Generic matrix usage ---------------
''' <summary>
''' Return a translation matrix
''' | 1 0 0 X |
''' transl(X,Y,Z) = | 0 1 0 Y |
''' | 0 0 1 Z |
''' | 0 0 0 1 |
''' </summary>
''' <param name="x">translation along X (mm)</param>
''' <param name="y">translation along Y (mm)</param>
''' <param name="z">translation along Z (mm)</param>
''' <returns></returns>
Public Shared Function transl(x As Double, y As Double, z As Double) As Mat
Dim mat__1 As Mat = Mat.IdentityMatrix(4, 4)
mat__1.setPos(x, y, z)
Return mat__1
End Function
''' <summary>
''' Return a X-axis rotation matrix
''' | 1 0 0 0 |
''' rotx(rx) = | 0 cos(rx) -sin(rx) 0 |
''' | 0 sin(rx) cos(rx) 0 |
''' | 0 0 0 1 |
''' </summary>
''' <param name="rx">rotation around X axis (in radians)</param>
''' <returns></returns>
Public Shared Function rotx(rx As Double) As Mat
Dim cx As Double = Math.Cos(rx)
Dim sx As Double = Math.Sin(rx)
Return New Mat(1, 0, 0, 0, 0, cx, _
-sx, 0, 0, sx, cx, 0)
End Function
''' <summary>
''' Return a Y-axis rotation matrix
''' | cos(ry) 0 sin(ry) 0 |
''' roty(ry) = | 0 1 0 0 |
''' | -sin(ry) 0 cos(ry) 0 |
''' | 0 0 0 1 |
''' </summary>
''' <param name="ry">rotation around Y axis (in radians)</param>
''' <returns></returns>
Public Shared Function roty(ry As Double) As Mat
Dim cy As Double = Math.Cos(ry)
Dim sy As Double = Math.Sin(ry)
Return New Mat(cy, 0, sy, 0, 0, 1, _
0, 0, -sy, 0, cy, 0)
End Function
''' <summary>
''' Return a Z-axis rotation matrix
''' | cos(rz) -sin(rz) 0 0 |
''' rotz(rx) = | sin(rz) cos(rz) 0 0 |
''' | 0 0 1 0 |
''' | 0 0 0 1 |
''' </summary>
''' <param name="rz">rotation around Z axis (in radians)</param>
''' <returns></returns>
Public Shared Function rotz(rz As Double) As Mat
Dim cz As Double = Math.Cos(rz)
Dim sz As Double = Math.Sin(rz)
Return New Mat(cz, -sz, 0, 0, sz, cz, _
0, 0, 0, 0, 1, 0)
End Function
'----------------------------------------------------
'------ Pose to xyzrpw and xyzrpw to pose------------
''' <summary>
''' Calculates the equivalent position and euler angles ([x,y,z,r,p,w] vector) of the given pose
''' Note: transl(x,y,z)*rotz(w*pi/180)*roty(p*pi/180)*rotx(r*pi/180)
''' See also: FromXYZRPW()
''' </summary>
''' <returns>XYZWPR translation and rotation in mm and degrees</returns>
Public Function ToXYZRPW() As Double()
Dim xyzwpr As Double() = New Double(5) {}
Dim x As Double = _mat(0, 3)
Dim y As Double = _mat(1, 3)
Dim z As Double = _mat(2, 3)
Dim w As Double, p As Double, r As Double
If _mat(2, 0) > (1.0 - 0.000001) Then
p = -Math.PI * 0.5
r = 0
w = Math.Atan2(-_mat(1, 2), _mat(1, 1))
ElseIf _mat(2, 0) < -1.0 + 0.000001 Then
p = 0.5 * Math.PI
r = 0
w = Math.Atan2(_mat(1, 2), _mat(1, 1))
Else
p = Math.Atan2(-_mat(2, 0), Math.Sqrt(_mat(0, 0) * _mat(0, 0) + _mat(1, 0) * _mat(1, 0)))
w = Math.Atan2(_mat(1, 0), _mat(0, 0))
r = Math.Atan2(_mat(2, 1), _mat(2, 2))
End If
xyzwpr(0) = x
xyzwpr(1) = y
xyzwpr(2) = z
xyzwpr(3) = r * 180.0 / Math.PI
xyzwpr(4) = p * 180.0 / Math.PI
xyzwpr(5) = w * 180.0 / Math.PI
Return xyzwpr
End Function
''' <summary>
''' Calculates the pose from the position and euler angles ([x,y,z,r,p,w] vector)
''' The result is the same as calling: H = transl(x,y,z)*rotz(w*pi/180)*roty(p*pi/180)*rotx(r*pi/180)
''' </summary>
''' <param name="x"></param>
''' <param name="y"></param>
''' <param name="z"></param>
''' <param name="w"></param>
''' <param name="p"></param>
''' <param name="r"></param>
''' <returns>Homogeneous matrix (4x4)</returns>
Public Shared Function FromXYZRPW(x As Double, y As Double, z As Double, w As Double, p As Double, r As Double) As Mat
Dim a As Double = r * Math.PI / 180.0
Dim b As Double = p * Math.PI / 180.0
Dim c As Double = w * Math.PI / 180.0
Dim ca As Double = Math.Cos(a)
Dim sa As Double = Math.Sin(a)
Dim cb As Double = Math.Cos(b)
Dim sb As Double = Math.Sin(b)
Dim cc As Double = Math.Cos(c)
Dim sc As Double = Math.Sin(c)
Return New 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)
End Function
''' <summary>
''' Calculates the pose from the position and euler angles ([x,y,z,r,p,w] vector)
''' The result is the same as calling: H = transl(x,y,z)*rotz(w*pi/180)*roty(p*pi/180)*rotx(r*pi/180)
''' </summary>
''' <param name="xyzwpr"></param>
''' <returns>Homogeneous matrix (4x4)</returns>
Public Shared Function FromXYZRPW(xyzwpr As Double()) As Mat
If xyzwpr.Length < 6 Then
Return Nothing
End If
Return FromXYZRPW(xyzwpr(0), xyzwpr(1), xyzwpr(2), xyzwpr(3), xyzwpr(4), xyzwpr(5))
End Function
''' <summary>
''' 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*pi/180)*roty(ry*pi/180)*rotz(rz*pi/180)
''' </summary>
''' <param name="x"></param>
''' <param name="y"></param>
''' <param name="z"></param>
''' <param name="rx"></param>
''' <param name="ry"></param>
''' <param name="rz"></param>
''' <returns>Homogeneous matrix (4x4)</returns>
Public Shared Function FromTxyzRxyz(x As Double, y As Double, z As Double, rx As Double, ry As Double, rz As Double) As Mat
Dim a As Double = rx * Math.PI / 180.0
Dim b As Double = ry * Math.PI / 180.0
Dim c As Double = rz * Math.PI / 180.0
Dim crx As Double = Math.Cos(a)
Dim srx As Double = Math.Sin(a)
Dim cry As Double = Math.Cos(b)
Dim sry As Double = Math.Sin(b)
Dim crz As Double = Math.Cos(c)
Dim srz As Double = Math.Sin(c)
Return New 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)
End Function
''' <summary>
''' 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*pi/180)*roty(ry*pi/180)*rotz(rz*pi/180)
''' </summary>
''' <returns>Homogeneous matrix (4x4)</returns>
Public Shared Function FromTxyzRxyz(xyzwpr As Double()) As Mat
If xyzwpr.Length < 6 Then
Return Nothing
End If
Return FromTxyzRxyz(xyzwpr(0), xyzwpr(1), xyzwpr(2), xyzwpr(3), xyzwpr(4), xyzwpr(5))
End Function
''' <summary>
''' Calculates the equivalent position and euler angles ([x,y,z,rx,ry,rz] array) of a pose
''' Note: Pose = transl(x,y,z)*rotx(rx*pi/180)*roty(ry*pi/180)*rotz(rz*pi/180)
''' See also: FromTxyzRxyz()
''' </summary>
''' <returns>XYZWPR translation and rotation in mm and degrees</returns>
Public Function ToTxyzRxyz() As Double()
Dim xyzwpr As Double() = New Double(5) {}
Dim x As Double = _mat(0, 3)
Dim y As Double = _mat(1, 3)
Dim z As Double = _mat(2, 3)
Dim rx1 As Double = 0
Dim ry1 As Double = 0
Dim rz1 As Double = 0
Dim a As Double = _mat(0, 0)
Dim b As Double = _mat(0, 1)
Dim c As Double = _mat(0, 2)
Dim d As Double = _mat(1, 2)
Dim e As Double = _mat(2, 2)
If c = 1 Then
ry1 = 0.5 * Math.PI
rx1 = 0
rz1 = Math.Atan2(_mat(1, 0), _mat(1, 1))
ElseIf c = -1 Then
ry1 = -Math.PI / 2
rx1 = 0
rz1 = Math.Atan2(_mat(1, 0), _mat(1, 1))
Else
Dim sy As Double = c
Dim cy1 As Double = +Math.Sqrt(1 - sy * sy)
Dim sx1 As Double = -d / cy1
Dim cx1 As Double = e / cy1
Dim sz1 As Double = -b / cy1
Dim cz1 As Double = a / cy1
rx1 = Math.Atan2(sx1, cx1)
ry1 = Math.Atan2(sy, cy1)
rz1 = Math.Atan2(sz1, cz1)
End If
xyzwpr(0) = x
xyzwpr(1) = y
xyzwpr(2) = z
xyzwpr(3) = rx1 * 180.0 / Math.PI
xyzwpr(4) = ry1 * 180.0 / Math.PI
xyzwpr(5) = rz1 * 180.0 / Math.PI
Return xyzwpr
End Function
''' <summary>
''' Converts a pose (4x4 matrix) to a Staubli XYZWPR target
''' </summary>
''' <returns>XYZWPR translation and rotation in mm and degrees</returns>
Public Function ToStaubli() As Double()
'Converts a pose (4x4 matrix) to a Staubli XYZWPR target
Dim xyzwpr As Double() = ToTxyzRxyz() 'already in degrees and mm
'xyzwpr(3) *= 180.0 / Math.PI
'xyzwpr(4) *= 180.0 / Math.PI
'xyzwpr(5) *= 180.0 / Math.PI
Return xyzwpr
End Function
'def ToMotoman(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)
''' <summary>
''' Returns the quaternion of a pose (4x4 matrix)
''' </summary>
''' <param name="Ti"></param>
''' <returns></returns>
Private Shared Function PoseToQuaternion(Ti As Mat) As Double()
Dim q As Double() = New Double(3) {}
Dim a As Double = (Ti(0, 0))
Dim b As Double = (Ti(1, 1))
Dim c As Double = (Ti(2, 2))
Dim sign2 As Double = 1.0
Dim sign3 As Double = 1.0
Dim sign4 As Double = 1.0
If (Ti(2, 1) - Ti(1, 2)) < 0 Then
sign2 = -1
End If
If (Ti(0, 2) - Ti(2, 0)) < 0 Then
sign3 = -1
End If
If (Ti(1, 0) - Ti(0, 1)) < 0 Then
sign4 = -1
End If
q(0) = 0.5 * Math.Sqrt(Math.Max(a + b + c + 1, 0))
q(1) = 0.5 * sign2 * Math.Sqrt(Math.Max(a - b - c + 1, 0))
q(2) = 0.5 * sign3 * Math.Sqrt(Math.Max(-a + b - c + 1, 0))
q(3) = 0.5 * sign4 * Math.Sqrt(Math.Max(-a - b + c + 1, 0))
Return q
End Function
''' <summary>
''' Returns the pose (4x4 matrix) from quaternion data
''' </summary>
''' <param name="qin"></param>
''' <returns></returns>
Private Shared Function QuaternionToPose(qin As Double()) As Mat
Dim qnorm As Double = Math.Sqrt(qin(0) * qin(0) + qin(1) * qin(1) + qin(2) * qin(2) + qin(3) * qin(3))
Dim q As Double() = New Double(3) {}
q(0) = qin(0) / qnorm
q(1) = qin(1) / qnorm
q(2) = qin(2) / qnorm
q(3) = qin(3) / qnorm
Dim pose As New 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)
Return pose
End Function
''' <summary>
''' Converts a pose to an ABB target
''' </summary>
''' <param name="H"></param>
''' <returns></returns>
Private Shared Function PoseToABB(H As Mat) As Double()
Dim q As Double() = PoseToQuaternion(H)
Dim xyzq1234 As Double() = {H(0, 3), H(1, 3), H(2, 3), q(0), q(1), q(2), _
q(3)}
Return xyzq1234
End Function
''' <summary>
''' Calculates the equivalent position and euler angles ([x,y,z,r,p,w] vector) of the given pose in Universal Robots format
''' Note: The difference between ToUR and ToXYZWPR is that the first one uses radians for the orientation and the second one uses degres
''' Note: transl(x,y,z)*rotx(rx*pi/180)*roty(ry*pi/180)*rotz(rz*pi/180)
''' See also: FromXYZRPW()
''' </summary>
''' <returns>XYZWPR translation and rotation in mm and radians</returns>
Public Function PoseToUR() As Double()
Dim xyzwpr As Double() = New Double(5) {}
Dim x As Double = _mat(0, 3)
Dim y As Double = _mat(1, 3)
Dim z As Double = _mat(2, 3)
Dim angle As Double = Math.Acos(Math.Min(Math.Max((_mat(0, 0) + _mat(1, 1) + _mat(2, 2) - 1) / 2, -1), 1))
Dim rx As Double = _mat(2, 1) - _mat(1, 2)
Dim ry As Double = _mat(0, 2) - _mat(2, 0)
Dim rz As Double = _mat(1, 0) - _mat(0, 1)
If angle = 0 Then
rx = 0
ry = 0
rz = 0
Else
rx = rx * angle / (2 * Math.Sin(angle))
ry = ry * angle / (2 * Math.Sin(angle))
rz = rz * angle / (2 * Math.Sin(angle))
End If
xyzwpr(0) = x
xyzwpr(1) = y
xyzwpr(2) = z
xyzwpr(3) = rx
xyzwpr(4) = ry
xyzwpr(5) = rz
Return xyzwpr
End Function
''' <summary>
''' Calculates the pose from the position and euler angles ([x,y,z,r,p,w] vector)
''' Note: The difference between FromUR and FromXYZWPR is that the first one uses radians for the orientation and the second one uses degres
''' The result is the same as calling: H = transl(x,y,z)*rotx(rx)*roty(ry)*rotz(rz)
''' </summary>
''' <param name="xyzwpr">The position and euler angles array</param>
''' <returns>Homogeneous matrix (4x4)</returns>
Public Shared Function URToPose(xyzwpr As Double()) As Mat
Dim x As Double = xyzwpr(0)
Dim y As Double = xyzwpr(1)
Dim z As Double = xyzwpr(2)
Dim w As Double = xyzwpr(3)
Dim p As Double = xyzwpr(4)
Dim r As Double = xyzwpr(5)
Dim angle As Double = Math.Sqrt(w * w + p * p + r * r)
If angle < 0.000001 Then
Return Identity4x4()
End If
Dim c As Double = Math.Cos(angle)
Dim s As Double = Math.Sin(angle)
Dim ux As Double = w / angle
Dim uy As Double = p / angle
Dim uz As Double = r / angle
Return New Mat(ux * ux + c * (1 - ux * ux), ux * uy * (1 - c) - uz * s, ux * uz * (1 - c) + uy * s, x, ux * uy * (1 - c) + uz * s, uy * uy + (1 - uy * uy) * c, _
uy * uz * (1 - c) - ux * s, y, ux * uz * (1 - c) - uy * s, uy * uz * (1 - c) + ux * s, uz * uz + (1 - uz * uz) * c, z)
End Function
''' <summary>
''' Converts a matrix into a one-dimensional array of doubles
''' </summary>
''' <returns>one-dimensional array</returns>
Public Function ToDoubles() As Double()
Dim cnt As Integer = 0
Dim array As Double() = New Double(rows * cols - 1) {}
For j As Integer = 0 To cols - 1
For i As Integer = 0 To rows - 1
array(cnt) = _mat(i, j)
cnt = cnt + 1
Next
Next
Return array
End Function
''' <summary>
''' Check if the matrix is square
''' </summary>
Public Function IsSquare() As [Boolean]
Return (rows = cols)
End Function
Public Function Is4x4() As [Boolean]
If cols <> 4 OrElse rows <> 4 Then
Return False
End If
Return True
End Function
''' <summary>
''' Check if the matrix is homogeneous (4x4)
''' </summary>
Public Function IsHomogeneous() As [Boolean]
If Not Is4x4() Then
Return False
End If
Return True
'
' test = self[0:3,0:3];
' test = test*test.tr()
' test[0,0] = test[0,0] - 1.0
' test[1,1] = test[1,1] - 1.0
' test[2,2] = test[2,2] - 1.0
' zero = 0.0
' for x in range(3):
' for y in range(3):
' zero = zero + abs(test[x,y])
' if zero > 1e-4:
' return False
' return True
'
End Function
''' <summary>
''' Returns the inverse of a homogeneous matrix (4x4 matrix)
''' </summary>
''' <returns>Homogeneous matrix (4x4)</returns>
Public Function inv() As Mat
If Not IsHomogeneous() Then
Throw New MatException("Can't invert a non-homogeneous matrix")
End If
Dim xyz As Double() = Me.Pos()
Dim mat_xyz As New Mat(xyz(0), xyz(1), xyz(2))
Dim hinv As Mat = Me.Duplicate()
hinv.setPos(0, 0, 0)
hinv = hinv.Transpose()
Dim new_pos As Mat = rotate(hinv, mat_xyz)
hinv(0, 3) = -new_pos(0, 0)
hinv(1, 3) = -new_pos(1, 0)
hinv(2, 3) = -new_pos(2, 0)
Return hinv
End Function
''' <summary>
''' Rotate a vector given a matrix (rotation matrix or homogeneous matrix)
''' </summary>
''' <param name="pose">4x4 homogeneous matrix or 3x3 rotation matrix</param>
''' <param name="vector">4x1 or 3x1 vector</param>
''' <returns></returns>
Public Shared Function rotate(pose As Mat, vector As Mat) As Mat
If pose.cols < 3 OrElse pose.rows < 3 OrElse vector.rows < 3 Then
Throw New MatException("Invalid matrix size")
End If
Dim pose3x3 As Mat = pose.Duplicate()
Dim vector3 As Mat = vector.Duplicate()
pose3x3.rows = 3
pose3x3.cols = 3
vector3.rows = 3
Return pose3x3 * vector3
End Function
''' <summary>
''' Returns the XYZ position of the Homogeneous matrix
''' </summary>
''' <returns>XYZ position</returns>
Public Function Pos() As Double()
If Not Is4x4() Then
Return Nothing
End If
Dim xyz As Double() = New Double(2) {}
xyz(0) = _mat(0, 3)
xyz(1) = _mat(1, 3)
xyz(2) = _mat(2, 3)
Return xyz
End Function
''' <summary>
''' Sets the 4x4 position of the Homogeneous matrix
''' </summary>
''' <param name="xyz">XYZ position</param>
Public Sub setPos(xyz As Double())
If Not Is4x4() OrElse xyz.Length < 3 Then
Return
End If
_mat(0, 3) = xyz(0)
_mat(1, 3) = xyz(1)
_mat(2, 3) = xyz(2)
End Sub
''' <summary>
''' Sets the 4x4 position of the Homogeneous matrix
''' </summary>
''' <param name="x">X position</param>
''' <param name="y">Y position</param>
''' <param name="z">Z position</param>
Public Sub setPos(x As Double, y As Double, z As Double)
If Not Is4x4() Then
Return
End If
_mat(0, 3) = x
_mat(1, 3) = y
_mat(2, 3) = z
End Sub
Default Public Property Item(iRow As Integer, iCol As Integer) As Double
' Access this matrix as a 2D array
Get
Return _mat(iRow, iCol)
End Get
Set(value As Double)
_mat(iRow, iCol) = value
End Set
End Property
Public Function GetCol(k As Integer) As Mat
Dim m As New Mat(rows, 1)
For i As Integer = 0 To rows - 1
m(i, 0) = _mat(i, k)
Next
Return m
End Function
Public Sub SetCol(v As Mat, k As Integer)
For i As Integer = 0 To rows - 1
_mat(i, k) = v(i, 0)
Next
End Sub
Public Function Duplicate() As Mat
' Function returns the copy of this matrix
Dim matrix As New Mat(rows, cols)
For i As Integer = 0 To rows - 1
For j As Integer = 0 To cols - 1
matrix(i, j) = matrix(i, j)
Next
Next
Return matrix
End Function
Public Shared Function ZeroMatrix(iRows As Integer, iCols As Integer) As Mat
' Function generates the zero matrix
Dim matrix As New Mat(iRows, iCols)
For i As Integer = 0 To iRows - 1
For j As Integer = 0 To iCols - 1
matrix(i, j) = 0
Next
Next
Return matrix
End Function
Public Shared Function IdentityMatrix(iRows As Integer, iCols As Integer) As Mat
' Function generates the identity matrix
Dim matrix As Mat = ZeroMatrix(iRows, iCols)
For i As Integer = 0 To Math.Min(iRows, iCols) - 1
matrix(i, i) = 1
Next
Return matrix
End Function
''' <summary>
''' Returns an identity 4x4 matrix (homogeneous matrix)
''' </summary>
''' <returns></returns>
Public Shared Function Identity4x4() As Mat
Return Mat.IdentityMatrix(4, 4)
End Function
'
' public static Mat Parse(string ps) // Function parses the matrix from string
' {
' string s = NormalizeMatrixString(ps);
' string[] rows = Regex.Split(s, "\r\n");
' string[] nums = rows[0].Split(' ');
' Mat matrix = new Mat(rows.Length, nums.Length);
' try
' {
' for (int i = 0; i < rows.Length; i++)
' {
' nums = rows[i].Split(' ');
' for (int j = 0; j < nums.Length; j++) matrix[i, j] = double.Parse(nums[j]);
' }
' }
' catch (FormatException exc) { throw new MatException("Wrong input format!"); }
' return matrix;
' }
Public Overrides Function ToString() As String
' Function returns matrix as a string
Dim s As String = ""
For i As Integer = 0 To rows - 1
For j As Integer = 0 To cols - 1
s += [String].Format("{0,5:0.00}", _mat(i, j)) + " "
Next
s += vbCr & vbLf
Next
Return s
End Function
''' <summary>
''' Transpose a matrix
''' </summary>
''' <returns></returns>
Public Function Transpose() As Mat
Return Transpose(Me)
End Function
Public Shared Function Transpose(m As Mat) As Mat
' Matrix transpose, for any rectangular matrix
Dim t As New Mat(m.cols, m.rows)
For i As Integer = 0 To m.rows - 1
For j As Integer = 0 To m.cols - 1
t(j, i) = m(i, j)
Next
Next
Return t
End Function
Private Shared Sub SafeAplusBintoC(A As Mat, xa As Integer, ya As Integer, B As Mat, xb As Integer, yb As Integer, _
C As Mat, size As Integer)
For i As Integer = 0 To size - 1
' rows
For j As Integer = 0 To size - 1
' cols
C(i, j) = 0
If xa + j < A.cols AndAlso ya + i < A.rows Then
C(i, j) += A(ya + i, xa + j)
End If
If xb + j < B.cols AndAlso yb + i < B.rows Then
C(i, j) += B(yb + i, xb + j)
End If
Next
Next
End Sub
Private Shared Sub SafeAminusBintoC(A As Mat, xa As Integer, ya As Integer, B As Mat, xb As Integer, yb As Integer, _
C As Mat, size As Integer)
For i As Integer = 0 To size - 1
' rows
For j As Integer = 0 To size - 1
' cols
C(i, j) = 0
If xa + j < A.cols AndAlso ya + i < A.rows Then
C(i, j) += A(ya + i, xa + j)
End If
If xb + j < B.cols AndAlso yb + i < B.rows Then
C(i, j) -= B(yb + i, xb + j)
End If
Next
Next
End Sub
Private Shared Sub SafeACopytoC(A As Mat, xa As Integer, ya As Integer, C As Mat, size As Integer)
For i As Integer = 0 To size - 1
' rows
For j As Integer = 0 To size - 1
' cols
C(i, j) = 0
If xa + j < A.cols AndAlso ya + i < A.rows Then
C(i, j) += A(ya + i, xa + j)
End If
Next
Next
End Sub
Private Shared Sub AplusBintoC(A As Mat, xa As Integer, ya As Integer, B As Mat, xb As Integer, yb As Integer, _
C As Mat, size As Integer)
For i As Integer = 0 To size - 1
' rows
For j As Integer = 0 To size - 1
C(i, j) = A(ya + i, xa + j) + B(yb + i, xb + j)
Next
Next
End Sub
Private Shared Sub AminusBintoC(A As Mat, xa As Integer, ya As Integer, B As Mat, xb As Integer, yb As Integer, _
C As Mat, size As Integer)
For i As Integer = 0 To size - 1
' rows
For j As Integer = 0 To size - 1
C(i, j) = A(ya + i, xa + j) - B(yb + i, xb + j)
Next
Next
End Sub
Private Shared Sub ACopytoC(A As Mat, xa As Integer, ya As Integer, C As Mat, size As Integer)
For i As Integer = 0 To size - 1
' rows
For j As Integer = 0 To size - 1
C(i, j) = A(ya + i, xa + j)
Next
Next
End Sub
Private Shared Function StrassenMultiply(A As Mat, B As Mat) As Mat
' Smart matrix multiplication
If A.cols <> B.rows Then
Throw New MatException("Wrong dimension of matrix!")
End If
Dim R As Mat
Dim msize As Integer = Math.Max(Math.Max(A.rows, A.cols), Math.Max(B.rows, B.cols))
If msize < 32 Then
R = ZeroMatrix(A.rows, B.cols)
For i As Integer = 0 To R.rows - 1
For j As Integer = 0 To R.cols - 1
For k As Integer = 0 To A.cols - 1
R(i, j) += A(i, k) * B(k, j)
Next
Next
Next
Return R
End If
Dim size As Integer = 1
Dim n As Integer = 0
While msize > size
size *= 2
n += 1
End While
Dim h As Integer = size / 2
Dim mField As Mat(,) = New Mat(n - 1, 8) {}
'
' * 8x8, 8x8, 8x8, ...
' * 4x4, 4x4, 4x4, ...
' * 2x2, 2x2, 2x2, ...
' * . . .
'
Dim z As Integer
For i As Integer = 0 To n - 5
' rows
z = CInt(Math.Pow(2, n - i - 1))
For j As Integer = 0 To 8
mField(i, j) = New Mat(z, z)
Next
Next
SafeAplusBintoC(A, 0, 0, A, h, h, _
mField(0, 0), h)
SafeAplusBintoC(B, 0, 0, B, h, h, _
mField(0, 1), h)
StrassenMultiplyRun(mField(0, 0), mField(0, 1), mField(0, 1 + 1), 1, mField)
' (A11 + A22) * (B11 + B22);
SafeAplusBintoC(A, 0, h, A, h, h, _
mField(0, 0), h)
SafeACopytoC(B, 0, 0, mField(0, 1), h)
StrassenMultiplyRun(mField(0, 0), mField(0, 1), mField(0, 1 + 2), 1, mField)
' (A21 + A22) * B11;
SafeACopytoC(A, 0, 0, mField(0, 0), h)
SafeAminusBintoC(B, h, 0, B, h, h, _
mField(0, 1), h)
StrassenMultiplyRun(mField(0, 0), mField(0, 1), mField(0, 1 + 3), 1, mField)