-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPS02-SpatialTransforms-06.py
More file actions
225 lines (184 loc) · 6.12 KB
/
PS02-SpatialTransforms-06.py
File metadata and controls
225 lines (184 loc) · 6.12 KB
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
import numpy as np
from scipy import linalg as la
import RoboticsLib as rob
###################################################
# set printing options for numpy
np.set_printoptions(precision=4, suppress=True)
###################################################
# select the demo
select = int(input('Which demo do you want to run? (1-5)\n'))
###################################################
# quaternion rotation 30 degrees around y-axis
#
if select == 1:
# init the figure and plot the Identy matrix as Cartesian frame in the origin
rfig = rob.Fig()
Fw = rob.HId()
rfig.plot_frame(Fw, size=2, label='Fw')
p = rob.Hvec()
p.set_xyz(2,3,4)
print('point p which is rotated \n', p)
rfig.plot_point(p, label='p')
qp = p.hv2q() # convert homog. point to quaterion
print(qp, '\n')
print('rotate 30 degrees around y-axis\n')
a0 = np.deg2rad(30)
v0 = rob.Hvec()
v0.set_xyz(0,1,0)
qr = rob.Quat()
qr.set_rot_axis_angle(v0, a0)
print('rotation quaternion qr\n', qr)
qpr = qr @ qp @ qr.conj()
print('rotated point p as quaternion qpr\n', qpr)
pr = qpr.q2hv()
rfig.plot_point(pr, label='pr')
print('rotated point p as homogeneous vector pr\n', pr)
###################################################
# quaternion rotation 30 degrees around (1,-1,3)
#
if select == 2:
# init the figure and plot the Identy matrix as Cartesian frame in the origin
rfig = rob.Fig()
Fw = rob.HId()
rfig.plot_frame(Fw, size=2, label='Fw')
p = rob.Hvec()
p.set_xyz(2,3,4)
print('point p which is rotated \n', p)
rfig.plot_point(p, label='p')
qp = p.hv2q()
print(qp, '\n')
print('rotate 30 degrees around (1,-1,3)\n')
a0 = np.deg2rad(30)
v0 = rob.Hvec()
v0.set_xyz(1,-1,3)
qr = rob.Quat()
qr.set_rot_axis_angle(v0, a0)
print('rotation quaternion qr\n', qr)
qpr = qr @ qp @ qr.conj()
print('rotated point p as quaternion qpr\n', qpr)
pr = qpr.q2hv()
rfig.plot_point(pr, label='pr')
print('rotated point p as homogeneous vector pr\n', pr)
###################################################
# quaternion rotation
# first by 30 degrees around y-axis
# then by 30 degrees around (1,-1,3)
#
if select == 3:
# init the figure and plot the Identy matrix as Cartesian frame in the origin
rfig = rob.Fig()
Fw = rob.HId()
rfig.plot_frame(Fw, size=2, label='Fw')
p = rob.Hvec()
p.set_xyz(2,3,4)
print('point p which is rotated \n', p)
rfig.plot_point(p, label='p')
qp = p.hv2q()
print(qp, '\n')
print('rotate 30 degrees around y-axis\n')
a1 = np.deg2rad(30)
v1 = rob.Hvec()
v1.set_xyz(0,1,0)
qr1 = rob.Quat()
qr1.set_rot_axis_angle(v1, a1)
print('rotation quaternion qr1\n', qr1)
print('rotate 30 degrees around (1,-1,3)\n')
a2 = np.deg2rad(30)
v2 = rob.Hvec()
v2.set_xyz(1,-1,3)
qr2 = rob.Quat()
qr2.set_rot_axis_angle(v2, a2)
print('rotation quaternion qr2\n', qr2)
qr3 = qr2 @ qr1
print('chained rotation qr3 by multiplying qr2 with qr1\n', qr3)
qpr = qr3 @ qp @ qr3.conj()
print('rotated point p as quaternion qpr\n', qpr)
pr = qpr.q2hv()
rfig.plot_point(pr, label='pr')
print('rotated point p as homogeneous vector pr\n', pr)
###################################################
# animated combined quaternion rotation
# of point (4, 5, 6)^T
# by 5 degrees around y-axis
# and 10 degrees around (1,-1,3)
# in each step
#
if select == 4:
# init the figure and plot the Identy matrix as Cartesian frame in the origin
rfig = rob.Fig()
Fw = rob.HId()
rfig.plot_frame(Fw, size=2, label='Fw')
p = rob.Hvec()
p.set_xyz(4,5,6)
print(p, '\n')
rfig.plot_point(p, label='p')
qp = p.hv2q()
print(qp, '\n')
print('rotate 5 degrees around y-axis\n')
a1 = np.deg2rad(5)
v1 = rob.Hvec()
v1.set_xyz(0,1,0)
qr1 = rob.Quat()
qr1.set_rot_axis_angle(v1, a1)
print('rotation quaternion qr1\n', qr1)
print('rotate 10 degrees around (1,-1,3)\n')
a2 = np.deg2rad(10)
v2 = rob.Hvec()
v2.set_xyz(1,-1,3)
qr2 = rob.Quat()
qr2.set_rot_axis_angle(v2, a2)
print('rotation quaternion qr2\n', qr2)
qr3 = qr2 @ qr1
print('chained rotation qr3 by multiplying qr2 with qr1\n', qr3)
for i in range(40):
# rfig.clear()
qp = qr3 @ qp @ qr3.conj()
print('rotated point p as quaternion qpr\n', qp)
pr = qp.q2hv()
rfig.plot_point(pr)
print('rotated point p as homogeneous vector pr\n', pr)
rfig.pause(0.1)
###################################################
# animated left-handed (LH) quaternion rotation (i.e., q_conj @ point @ q)
# of point (4, 5, 6)^T
# by 5 degrees around z-axis
# in each step
#
if select == 5:
# init the figure and plot the Identy matrix as Cartesian frame in the origin
rfig = rob.Fig()
Fw = rob.HId()
rfig.plot_frame(Fw, size=2, label='Fw')
p = rob.Hvec()
p.set_xyz(4,5,6)
print(p, '\n')
rfig.plot_point(p, label='p')
qp = p.hv2q()
print(qp, '\n')
print('rotate 5 degrees around y-axis\n')
a1 = np.deg2rad(5)
v1 = rob.Hvec()
v1.set_xyz(0,0,1)
qr1 = rob.Quat()
qr1.set_rot_axis_angle(v1, a1)
print('rotation quaternion qr1\n', qr1)
#print('rotate 10 degrees around (1,-1,3)\n')
#a2 = np.deg2rad(10)
#v2 = rob.Hvec()
#v2.set_xyz(1,-1,3)
#qr2 = rob.Quat()
#qr2.set_rot_axis_angle(v2, a2)
#print('rotation quaternion qr2\n', qr2)
#qr3 = qr2 @ qr1
#print('chained rotation qr3 by multiplying qr2 with qr1\n', qr3)
for i in range(40):
# rfig.clear()
qp = qr1.conj() @ qp @ qr1
print('rotated point p as quaternion qpr\n', qp)
pr = qp.q2hv()
rfig.plot_point(pr)
print('rotated point p as homogeneous vector pr\n', pr)
rfig.pause(0.1)
###################################################
# # close figure (works also from Console)
# rfig.close()