/
t2x.m
415 lines (330 loc) · 10.2 KB
/
t2x.m
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
function x=t2x(T,str)
% x=t2x(T,str);
%
% Converts transformation matrix T between B and A coordinate
% frames into a generalized position vector x, which contains
% position and orientation vectors of B with respect to A.
% Orientation can be expressed with quaternions, euler angles
% (xyz or zxz convention), unit vector and rotation angle.
% Also both orientation and position can be expressed with
% Denavitt-Hartemberg parameters.
%
% ---------------------------------------------------------------------------
%
% The transformation matrix T between B and A coordinate
% frames is a 4 by 4 matrix such that:
% T(1:3,1:3) = Orientation matrix between B and A = unit vectors
% of x,y,z axes of B expressed in the A coordinates.
% T(1:3,4) = Origin of B expressed in A coordinates.
% T(4,1:3) = zeros(1,3)
% T(4,4) = 1
%
% ---------------------------------------------------------------------------
%
% The generalized position vector x contains the origin of B
% expressed in the A coordinates in the first four entries,
% and orientation of B with respect to A in the last four entries.
% In more detail, its shape depends on the value of str as
% specified below :
%
% ---------------------------------------------------------------------------
%
% str='van' : UNIT VECTOR AND ROTATION ANGLE
%
% [ Ox ] origin of the B coordinate frame
% x(1:4) = [ Oy ] with respect to A.
% [ Oz ]
% [ 1 ]
%
% [ Vx ] Vx,Vy,Vz = unit vector respect to A,
% x(5:8) = [ Vy ] which B is rotated about.
% [ Vz ]
% [ Th ] Th = angle which B is rotated (-pi,pi].
% ---------------------------------------------------------------------------
%
% str='qua' : UNIT QUATERNION
%
% [ Ox ] origin of the B coordinate frame
% x(1:4) = [ Oy ] with respect to A.
% [ Oz ]
% [ 1 ]
%
% [ q1 ] q1,q2,q3 = V*sin(Th/2)
% x(5:8) = [ q2 ] q0 = cos(Th/2) where :
% [ q3 ] V = unit vector respect to A, which B is
% [ q0 ] rotated about, Th = angle which B is rotated (-pi,pi].
% ---------------------------------------------------------------------------
%
% str='erp' : EULER-RODRIGUEZ PARAMETERS
%
% [ Ox ] origin of the B coordinate frame
% x(1:4) = [ Oy ] with respect to A.
% [ Oz ]
% [ 1 ]
%
% [ r1 ] r1,r2,r3 = V*tan(Th/2), where :
% x(5:8) = [ r2 ] V = unit vector with respect to A, which B is
% [ r3 ] rotated about.
% [ 0 ] Th = angle which B is rotated (-pi,pi) (<> pi).
% ---------------------------------------------------------------------------
%
% str='rpy' : ROLL, PITCH, YAW ANGLES (euler x-y-z convention)
%
% [ Ox ] origin of the B coordinate frame
% x(1:4) = [ Oy ] with respect to A.
% [ Oz ]
% [ 1 ]
%
% [ r ] r = roll angle ( fi (-pi,pi], about x, )
% x(5:8) = [ p ] p = pitch angle ( theta (-pi,pi], about y, <> +-pi/2)
% [ y ] y = yaw angle ( psi (-pi,pi], about z, )
% [ 0 ]
% ---------------------------------------------------------------------------
%
% str='rpm' : ROTATION, PRECESSION, MUTATION ANGLES (euler z-x-z convention)
%
% [ Ox ] origin of the B coordinate frame
% x(1:4) = [ Oy ] with respect to A.
% [ Oz ]
% [ 1 ]
%
% [ r ] r = rotation angle ( (-pi,pi] ,about z )
% x(5:8) = [ p ] p = precession angle ( (-pi,pi] ,about x , <> 0,pi )
% [ y ] y = mutation angle ( (-pi,pi] ,about z )
% [ 0 ]
% ---------------------------------------------------------------------------
%
% str='dht' : DENAVITT-HARTEMBERG PARAMETERS
%
% [ b ] [ a ] this four-parameter
% x(1:4) = [ d ] , x(5:8) = [ t ] , description does not involve
% [ 0 ] [ 0 ] a loss of information if and
% [ 0 ] [ 0 ] only if T has this shape:
%
% [ ct -st 0 b ] where :
% T = [ ca*st ca*ct -sa -d*sa ]
% [ sa*st sa*ct ca d*ca ] sa = sin(a), ca = cos(a)
% [ 0 0 0 1 ] st = sin(t), ct = cos(t)
% ---------------------------------------------------------------------------
%
% Example (see also x2t):
% x=[rand(3,1);1;rand(3,1);0];x-t2x(x2t(x,'rpm'),'rpm')
%
%
% Giampiero Campa 1/11/96
%
% ---------------------------------------------------------------------------
% UNIT VECTOR AND ROTATION ANGLE
if [ str=='van' size(T)==[4 4] ],
O=T(1:3,4);
R=T(1:3,1:3);
d=round(.5*(trace(R)-1)*1e12)/1e12;
if d==1,
v=[0 0 1]';
th=0;
elseif d==-1,
v0=sum(R'+eye(3,3))';
if v0 == 0
v0=R(:,2)+R(:,3)+[0 1 1]';
end;
if v0 == 0
v0=R(:,3)+[0 0 1]';
end;
v=v0/norm(v0);
th=pi;
else
sg=(vp([1 0 0]',R(:,1))+vp([0 1 0]',R(:,2))+vp([0 0 1]',R(:,3)));
if norm(sg) < 1e-12
disp(' ');
disp('T2x warning: det(R)<>1, unit vector assumed to be [0 0 1]''.');
disp(' ');
sg=[0 0 1]';
end
v=sg/norm(sg);
th=atan2(norm(sg)/2,d);
end
x=[O;1;v;th];
% ---------------------------------------------------------------------------
% UNIT QUATERNION
elseif [ str=='qua' size(T)==[4 4] ],
O=T(1:3,4);
R=T(1:3,1:3);
d=round(.5*(trace(R)-1)*1e12)/1e12;
if d==1,
v=[0 0 1]';
th=0;
elseif d==-1,
v0=sum(R'+eye(3,3))';
if v0 == 0
v0=R(:,2)+R(:,3)+[0 1 1]';
end;
if v0 == 0
v0=R(:,3)+[0 0 1]';
end;
v=v0/norm(v0);
th=pi;
else
sg=(vp([1 0 0]',R(:,1))+vp([0 1 0]',R(:,2))+vp([0 0 1]',R(:,3)));
if norm(sg) < 1e-12
disp(' ');
disp('T2x warning: det(R)<>1, unit vector assumed to be [0 0 1]''.');
disp(' ');
sg=[0 0 1]';
end
v=sg/norm(sg);
th=atan2(norm(sg)/2,d);
end
q=v*sin(th/2);
q0=cos(th/2);
x=[O;1;q;q0];
% ---------------------------------------------------------------------------
% EULER-RODRIGUEZ PARAMETERS
elseif [ str=='erp' size(T)==[4 4] ],
O=T(1:3,4);
R=T(1:3,1:3);
d=round(.5*(trace(R)-1)*1e12)/1e12;
if d==1,
v=[0 0 1]';
th=0;
elseif d==-1,
v0=sum(R'+eye(3,3))';
if v0 == 0
v0=R(:,2)+R(:,3)+[0 1 1]';
end;
if v0 == 0
v0=R(:,3)+[0 0 1]';
end;
v=v0/norm(v0);
th=pi;
else
sg=(vp([1 0 0]',R(:,1))+vp([0 1 0]',R(:,2))+vp([0 0 1]',R(:,3)));
if norm(sg) < 1e-12
disp(' ');
disp('T2x warning: det(R)<>1, unit vector assumed to be [0 0 1]''.');
disp(' ');
sg=[0 0 1]';
end
v=sg/norm(sg);
th=atan2(norm(sg)/2,d);
end
p=v*tan(th/2);
x=[O;1;p;0];
% ---------------------------------------------------------------------------
% ROLL, PITCH, YAW ANGLES (euler x-y-z convention)
elseif [ str=='rpy' size(T)==[4 4] ],
O=T(1:3,4);
R=T(1:3,1:3);
d=round([0 0 1]*R(:,1)*1e12)/1e12;
if d==1,
y=atan2([0 1 0]*R(:,2),[1 0 0]*R(:,2));
p=-pi/2;
r=-pi/2;
elseif d==-1
y=atan2([0 1 0]*R(:,2),[1 0 0]*R(:,2));
p=pi/2;
r=pi/2;
else
sg=vp([0 0 1]',R(:,1));
j2=sg/sqrt(sg'*sg);
k2=vp(R(:,1),j2);
r=atan2(k2'*R(:,2),j2'*R(:,2));
p=atan2(-[0 0 1]*R(:,1),[0 0 1]*k2);
y=atan2(-[1 0 0]*j2,[0 1 0]*j2);
end
y1=y+(1-sign(y)-sign(y)^2)*pi;
p1=p+(1-sign(p)-sign(p)^2)*pi;
r1=r+(1-sign(r)-sign(r)^2)*pi;
% takes smaller values of angles
if norm([y1 p1 r1]) < norm([y p r])
x=[O;1;r1;-p1;y1;0];
else
x=[O;1;r;p;y;0];
end
% ---------------------------------------------------------------------------
% ROTATION, PRECESSION, MUTATION ANGLES (euler z-x-z convention)
elseif [ str=='rpm' size(T)==[4 4] ],
O=T(1:3,4);
R=T(1:3,1:3);
d=round([0 0 1]*R(:,3)*1e12)/1e12;
if d==1,
m=0;
p=0;
r=atan2([0 1 0]*R(:,1),[1 0 0]*R(:,1));
elseif d==-1
m=0;
p=pi;
r=atan2([0 1 0]*R(:,1),[1 0 0]*R(:,1));
else
sg=vp([0 0 1]',R(:,3));
i2=sg/norm(sg);
j2=vp(R(:,3),i2);
m=atan2(j2'*R(:,1),i2'*R(:,1));
p=atan2([0 0 1]*j2,[0 0 1]*R(:,3));
r=atan2([0 1 0]*i2,[1 0 0]*i2);
end
r1=r+(1-sign(r)-sign(r)^2)*pi;
p1=p;
m1=m+(1-sign(m)-sign(m)^2)*pi;
% takes the smaller values of angles
if norm([r1 p1 m1]) < norm([r p m])
x=[O;1;r1;-p1;m1;0];
else
x=[O;1;r;p;m;0];
end
% ---------------------------------------------------------------------------
% DENAVITT-HARTEMBERG PARAMETERS
elseif [ str=='dht' size(T)==[4 4] ],
% b calculation
b=T(1,4);
% d calculation
if norm(T(3,3)) > norm(T(2,3))
d=T(3,4)/T(3,3);
else
d=T(2,4)/T(2,3);
end
% alfa and theta computation by mean of euler-zxz angles
R=T(1:3,1:3);
dl=round([0 0 1]*R(:,3)*1e12)/1e12;
if dl==1,
th=0;
alfa=0;
n=atan2([0 1 0]*R(:,1),[1 0 0]*R(:,1));
elseif dl==-1
th=0;
alfa=pi;
n=atan2([0 1 0]*R(:,1),[1 0 0]*R(:,1));
else
sg=vp([0 0 1]',R(:,3));
i2=sg/norm(sg);
j2=vp(R(:,3),i2);
th=atan2(j2'*R(:,1),i2'*R(:,1));
alfa=atan2([0 0 1]*j2,[0 0 1]*R(:,3));
n=atan2([0 1 0]*i2,[1 0 0]*i2);
end
th1=th+(1-sign(th)-sign(th)^2)*pi;
alfa1=alfa;
n1=n+(1-sign(n)-sign(n)^2)*pi;
% takes the smaller values of angles
if norm([th1 alfa1 n1]) < norm([th alfa n])
x=[b;d;0;0;-alfa1;th1;0;0];
else
x=[b;d;0;0;alfa;th;0;0];
end
% ---------------------------------------------------------------------------
% OTHER STRING
else
disp(' ');
disp(' x=T2x(T,str)');
disp(' where T is a 4 by 4 matrix (see help for details)');
disp(' and str can be : ''van'',''qua'',''erp'',''rpy'',''rpm'',''dht''. ');
disp(' ');
end
function z=vp(x,y)
% z=vp(x,y); z = 3d cross product of x and y
% vp(x) is the 3d cross product matrix : vp(x)*y=vp(x,y).
%
% by Giampiero Campa.
z=[ 0 -x(3) x(2);
x(3) 0 -x(1);
-x(2) x(1) 0 ];
if nargin>1, z=z*y; end