2
2
# 回転の座標変換
3
3
import numpy as np
4
4
import math
5
+
6
+
5
7
# 2D case
6
8
def Rz2D (theta ):
7
9
mat = np .array ([[math .cos (theta ), - math .sin (theta )], [math .sin (theta ), math .cos (theta )]])
@@ -11,60 +13,72 @@ def Rz2D(theta):
11
13
12
14
# 3D case
13
15
def Rx (theta ):
14
- mat = np .array ([[1 ,0 , 0 ],[0 ,math .cos (theta ),- math .sin (theta )],[0 ,math .sin (theta ),math .cos (theta )]])
16
+ mat = np .array ([[1 , 0 , 0 ], [0 , math .cos (theta ), - math .sin (theta )], [0 , math .sin (theta ), math .cos (theta )]])
15
17
return mat
16
18
19
+
17
20
def Ry (theta ):
18
- mat = np .array ([[math .cos (theta ),0 , math .sin (theta )],[0 ,1 , 0 ],[- math .sin (theta ),0 , math .cos (theta )]])
21
+ mat = np .array ([[math .cos (theta ), 0 , math .sin (theta )], [0 , 1 , 0 ], [- math .sin (theta ), 0 , math .cos (theta )]])
19
22
return mat
20
23
24
+
21
25
def Rz (theta ):
22
- mat = np .array ([[math .cos (theta ),- math .sin (theta ),0 ],[math .sin (theta ),math .cos (theta ),0 ],[0 ,0 , 1 ]])
26
+ mat = np .array ([[math .cos (theta ), - math .sin (theta ), 0 ], [math .sin (theta ), math .cos (theta ), 0 ], [0 , 0 , 1 ]])
23
27
return mat
24
28
29
+
25
30
def rpy2R (r , p , y ):
26
- r = np .dot (Ry (p ),Rx (r ))
27
- mat = np .dot (Rz (y ),r )
31
+ r = np .dot (Ry (p ), Rx (r ))
32
+ mat = np .dot (Rz (y ), r )
28
33
return mat
29
34
30
- def quaternion2R (q ):#(w,x,y,z)
31
- r = np .zeros ((3 ,3 ))
32
- r [0 ,0 ]= q [0 ]** 2 + q [1 ]** 2 - q [2 ]** 2 - q [3 ]** 2
33
- r [0 ,1 ]= 2 * (q [1 ]* q [2 ]- q [3 ]* q [0 ])
34
- r [0 ,2 ]= 2 * (q [3 ]* q [1 ]+ q [2 ]* q [0 ])
35
- r [1 ,0 ]= 2 * (q [1 ]* q [2 ]+ q [3 ]* q [0 ])
36
- r [1 ,1 ]= q [0 ]** 2 + q [2 ]** 2 - q [3 ]** 2 - q [1 ]** 2
37
- r [1 ,2 ]= 2 * (q [2 ]* q [3 ]- q [1 ]* q [0 ])
38
- r [2 ,0 ]= 2 * (q [3 ]* q [1 ]- q [2 ]* q [0 ])
39
- r [2 ,1 ]= 2 * (q [2 ]* q [3 ]+ q [1 ]* q [0 ])
40
- r [2 ,2 ]= q [0 ]** 2 + q [3 ]** 2 - q [1 ]** 2 - q [2 ]** 2
35
+
36
+ def quaternion2R (q ): # (w,x,y,z)
37
+ r = np .zeros ((3 , 3 ))
38
+ r [0 , 0 ] = q [0 ] ** 2 + q [1 ] ** 2 - q [2 ] ** 2 - q [3 ] ** 2
39
+ r [0 , 1 ] = 2 * (q [1 ] * q [2 ] - q [3 ] * q [0 ])
40
+ r [0 , 2 ] = 2 * (q [3 ] * q [1 ] + q [2 ] * q [0 ])
41
+ r [1 , 0 ] = 2 * (q [1 ] * q [2 ] + q [3 ] * q [0 ])
42
+ r [1 , 1 ] = q [0 ] ** 2 + q [2 ] ** 2 - q [3 ] ** 2 - q [1 ] ** 2
43
+ r [1 , 2 ] = 2 * (q [2 ] * q [3 ] - q [1 ] * q [0 ])
44
+ r [2 , 0 ] = 2 * (q [3 ] * q [1 ] - q [2 ] * q [0 ])
45
+ r [2 , 1 ] = 2 * (q [2 ] * q [3 ] + q [1 ] * q [0 ])
46
+ r [2 , 2 ] = q [0 ] ** 2 + q [3 ] ** 2 - q [1 ] ** 2 - q [2 ] ** 2
41
47
return r
42
48
49
+
43
50
def R2quaternion (r ):
44
51
q = []
45
- q [0 ] = math .sqrt (1 + r [0 ,0 ] + r [1 ,1 ] + r [2 ,2 ])
46
- q [1 ] = (r [2 ,1 ] - r [1 ,2 ])/ 4 / q [0 ]
47
- q [2 ] = (r [0 ,2 ] - r [2 ,0 ])/ 4 / q [0 ]
48
- q [3 ] = (r [1 ,0 ] - r [0 ,1 ])/ 4 / q [0 ]
52
+ q [0 ] = math .sqrt (1 + r [0 , 0 ] + r [1 , 1 ] + r [2 , 2 ])
53
+ q [1 ] = (r [2 , 1 ] - r [1 , 2 ]) / 4 / q [0 ]
54
+ q [2 ] = (r [0 , 2 ] - r [2 , 0 ]) / 4 / q [0 ]
55
+ q [3 ] = (r [1 , 0 ] - r [0 , 1 ]) / 4 / q [0 ]
49
56
return q
50
57
58
+
51
59
def quaternion2rpy (q ):
52
- roll = np .arctan2 (2.0 * (q [2 ]* q [3 ] + q [0 ]* q [1 ]), q [0 ]** 2 - q [1 ]** 2 - q [2 ]** 2 + q [3 ]** 2 )
53
- pitch = np .arcsin (2.0 * (q [0 ]* q [2 ] - q [1 ]* q [3 ]))
54
- yaw = np .arctan2 (2.0 * (q [1 ]* q [2 ] + q [0 ]* q [3 ]), q [0 ]** 2 + q [1 ]** 2 - q [2 ]** 2 - q [3 ]** 2 )
60
+ roll = np .arctan2 (2.0 * (q [2 ] * q [3 ] + q [0 ] * q [1 ]), q [0 ] ** 2 - q [1 ] ** 2 - q [2 ] ** 2 + q [3 ] ** 2 )
61
+ pitch = np .arcsin (2.0 * (q [0 ] * q [2 ] - q [1 ] * q [3 ]))
62
+ yaw = np .arctan2 (2.0 * (q [1 ] * q [2 ] + q [0 ] * q [3 ]), q [0 ] ** 2 + q [1 ] ** 2 - q [2 ] ** 2 - q [3 ] ** 2 )
55
63
return roll , pitch , yaw
56
64
65
+
57
66
def rpy2quaternion (roll , pitch , yaw ):
58
67
cosRoll = np .cos (roll / 2.0 )
59
68
sinRoll = np .sin (roll / 2.0 )
60
69
cosPitch = np .cos (pitch / 2.0 )
61
70
sinPitch = np .sin (pitch / 2.0 )
62
71
cosYaw = np .cos (yaw / 2.0 )
63
72
sinYaw = np .sin (yaw / 2.0 )
64
- q0 = cosRoll * cosPitch * cosYaw - sinRoll * sinPitch * sinYaw
65
- q1 = sinRoll * cosPitch * cosYaw + cosRoll * sinPitch * sinYaw
66
- q2 = cosRoll * sinPitch * cosYaw - sinRoll * cosPitch * sinYaw
67
- q3 = cosRoll * cosPitch * sinYaw + sinRoll * sinPitch * cosYaw
73
+ # ZYX オイラー角
74
+ q0 = cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw
75
+ q1 = sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw
76
+ q2 = cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw
77
+ q3 = cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw
78
+ # XYZオイラー角
79
+ # q0 = cosRoll * cosPitch * cosYaw - sinRoll * sinPitch * sinYaw
80
+ # q1 = sinRoll * cosPitch * cosYaw + cosRoll * sinPitch * sinYaw
81
+ # q2 = cosRoll * sinPitch * cosYaw - sinRoll * cosPitch * sinYaw
82
+ # q3 = cosRoll * cosPitch * sinYaw + sinRoll * sinPitch * cosYaw
68
83
q = np .array ([q0 , q1 , q2 , q3 ])
69
84
return q
70
-
0 commit comments