-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangle2dcm.m
261 lines (226 loc) · 11.5 KB
/
angle2dcm.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
function dcm = angle2dcm( r1, r2, r3, varargin )
% dcm = angle2dcm(r1,r2,r3,seq)
% builds euler angle rotation matrix
%function dcm = angle2dcm( r1, r2, r3, varargin )
% ANGLE2DCM Create direction cosine matrix from rotation angles.
% N = ANGLE2DCM( R1, R2, R3 ) calculates the direction cosine matrix, N,
% for a given set of rotation angles, R1, R2, R3. R1 is an M array of
% first rotation angles. R2 is an M array of second rotation angles. R3
% is an M array of third rotation angles. N returns an 3-by-3-by-M
% matrix containing M direction cosine matrices. Rotation angles are
% input in radians.
%
% N = ANGLE2DCM( R1, R2, R3, S ) calculates the direction cosine matrix,
% N, for a given set of rotation angles, R1, R2, R3, and a specified
% rotation sequence, S.
%
% The default rotation sequence is 'ZYX' where the order of rotation
% angles for the default rotation are R1 = Z Axis Rotation, R2 = Y Axis
% Rotation, and R3 = X Axis Rotation.
%
% All rotation sequences, S, are supported: 'ZYX', 'ZYZ', 'ZXY', 'ZXZ',
% 'YXZ', 'YXY', 'YZX', 'YZY', 'XYZ', 'XYX', 'XZY', and 'XZX'.
%
% Examples:
%
% Determine the direction cosine matrix from rotation angles:
% yaw = 0.7854;
% pitch = 0.1;
% roll = 0;
% dcm = angle2dcm( yaw, pitch, roll )
%
% Determine the direction cosine matrix from multiple rotation angles:
% yaw = [0.7854 0.5];
% pitch = [0.1 0.3];
% roll = [0 0.1];
% dcm = angle2dcm( pitch, roll, yaw, 'YXZ' )
%
% See also DCM2ANGLE, DCM2QUAT, QUAT2DCM, QUAT2ANGLE.
% Copyright 2000-2007 The MathWorks, Inc.
% $Revision: 1.1.6.5 $ $Date: 2007/08/15 17:16:07 $
narginchk(3, 4);
if any(~isreal(r1) || ~isnumeric(r1))
error('aero:angle2dcm:isnotreal1','First input element is not a real number.');
end
if any(~isreal(r2) || ~isnumeric(r2))
error('aero:angle2dcm:isnotreal2','Second input element is not a real number.');
end
if any(~isreal(r3) || ~isnumeric(r3))
error('aero:angle2dcm:isnotreal3','Third input element is not a real number.');
end
if (length(r1) ~= length(r2)) || (length(r1) ~= length(r3))
error('aero:angle2dcm:wrongdim','Rotations are not all arrays of M.');
end
if nargin == 3
type = 'zyx';
else
if ischar( varargin{1} )
type = varargin{1};
else
error('aero:angle2dcm:notchar','Rotation sequence is not a string.');
end
end
angles = [r1(:) r2(:) r3(:)];
dcm = zeros(3,3,size(angles,1));
cang = cos( angles );
sang = sin( angles );
switch lower( type )
case 'zyx'
% [ cy*cz, cy*sz, -sy]
% [ sy*sx*cz-sz*cx, sy*sx*sz+cz*cx, cy*sx]
% [ sy*cx*cz+sz*sx, sy*cx*sz-cz*sx, cy*cx]
dcm(1,1,:) = cang(:,2).*cang(:,1);
dcm(1,2,:) = cang(:,2).*sang(:,1);
dcm(1,3,:) = -sang(:,2);
dcm(2,1,:) = sang(:,3).*sang(:,2).*cang(:,1) - cang(:,3).*sang(:,1);
dcm(2,2,:) = sang(:,3).*sang(:,2).*sang(:,1) + cang(:,3).*cang(:,1);
dcm(2,3,:) = sang(:,3).*cang(:,2);
dcm(3,1,:) = cang(:,3).*sang(:,2).*cang(:,1) + sang(:,3).*sang(:,1);
dcm(3,2,:) = cang(:,3).*sang(:,2).*sang(:,1) - sang(:,3).*cang(:,1);
dcm(3,3,:) = cang(:,3).*cang(:,2);
case 'zyz'
% [ cz2*cy*cz-sz2*sz, cz2*cy*sz+sz2*cz, -cz2*sy]
% [ -sz2*cy*cz-cz2*sz, -sz2*cy*sz+cz2*cz, sz2*sy]
% [ sy*cz, sy*sz, cy]
dcm(1,1,:) = cang(:,1).*cang(:,3).*cang(:,2) - sang(:,1).*sang(:,3);
dcm(1,2,:) = sang(:,1).*cang(:,3).*cang(:,2) + cang(:,1).*sang(:,3);
dcm(1,3,:) = -sang(:,2).*cang(:,3);
dcm(2,1,:) = -cang(:,1).*cang(:,2).*sang(:,3) - sang(:,1).*cang(:,3);
dcm(2,2,:) = -sang(:,1).*cang(:,2).*sang(:,3) + cang(:,1).*cang(:,3);
dcm(2,3,:) = sang(:,2).*sang(:,3);
dcm(3,1,:) = cang(:,1).*sang(:,2);
dcm(3,2,:) = sang(:,1).*sang(:,2);
dcm(3,3,:) = cang(:,2);
case 'zxy'
% [ cy*cz-sy*sx*sz, cy*sz+sy*sx*cz, -sy*cx]
% [ -sz*cx, cz*cx, sx]
% [ sy*cz+cy*sx*sz, sy*sz-cy*sx*cz, cy*cx]
dcm(1,1,:) = cang(:,3).*cang(:,1) - sang(:,2).*sang(:,3).*sang(:,1);
dcm(1,2,:) = cang(:,3).*sang(:,1) + sang(:,2).*sang(:,3).*cang(:,1);
dcm(1,3,:) = -sang(:,3).*cang(:,2);
dcm(2,1,:) = -cang(:,2).*sang(:,1);
dcm(2,2,:) = cang(:,2).*cang(:,1);
dcm(2,3,:) = sang(:,2);
dcm(3,1,:) = sang(:,3).*cang(:,1) + sang(:,2).*cang(:,3).*sang(:,1);
dcm(3,2,:) = sang(:,3).*sang(:,1) - sang(:,2).*cang(:,3).*cang(:,1);
dcm(3,3,:) = cang(:,2).*cang(:,3);
case 'zxz'
% [ cz2*cz-sz2*cx*sz, cz2*sz+sz2*cx*cz, sz2*sx]
% [ -sz2*cz-cz2*cx*sz, -sz2*sz+cz2*cx*cz, cz2*sx]
% [ sz*sx, -cz*sx, cx]
dcm(1,1,:) = -sang(:,1).*cang(:,2).*sang(:,3) + cang(:,1).*cang(:,3);
dcm(1,2,:) = cang(:,1).*cang(:,2).*sang(:,3) + sang(:,1).*cang(:,3);
dcm(1,3,:) = sang(:,2).*sang(:,3);
dcm(2,1,:) = -sang(:,1).*cang(:,3).*cang(:,2) - cang(:,1).*sang(:,3);
dcm(2,2,:) = cang(:,1).*cang(:,3).*cang(:,2) - sang(:,1).*sang(:,3);
dcm(2,3,:) = sang(:,2).*cang(:,3);
dcm(3,1,:) = sang(:,1).*sang(:,2);
dcm(3,2,:) = -cang(:,1).*sang(:,2);
dcm(3,3,:) = cang(:,2);
case 'yxz'
% [ cy*cz+sy*sx*sz, sz*cx, -sy*cz+cy*sx*sz]
% [ -cy*sz+sy*sx*cz, cz*cx, sy*sz+cy*sx*cz]
% [ sy*cx, -sx, cy*cx]
dcm(1,1,:) = cang(:,1).*cang(:,3) + sang(:,2).*sang(:,1).*sang(:,3);
dcm(1,2,:) = cang(:,2).*sang(:,3);
dcm(1,3,:) = -sang(:,1).*cang(:,3) + sang(:,2).*cang(:,1).*sang(:,3);
dcm(2,1,:) = -cang(:,1).*sang(:,3) + sang(:,2).*sang(:,1).*cang(:,3);
dcm(2,2,:) = cang(:,2).*cang(:,3);
dcm(2,3,:) = sang(:,1).*sang(:,3) + sang(:,2).*cang(:,1).*cang(:,3);
dcm(3,1,:) = sang(:,1).*cang(:,2);
dcm(3,2,:) = -sang(:,2);
dcm(3,3,:) = cang(:,2).*cang(:,1);
case 'yxy'
% [ cy2*cy-sy2*cx*sy, sy2*sx, -cy2*sy-sy2*cx*cy]
% [ sy*sx, cx, cy*sx]
% [ sy2*cy+cy2*cx*sy, -cy2*sx, -sy2*sy+cy2*cx*cy]
dcm(1,1,:) = -sang(:,1).*cang(:,2).*sang(:,3) + cang(:,1).*cang(:,3);
dcm(1,2,:) = sang(:,2).*sang(:,3);
dcm(1,3,:) = -cang(:,1).*cang(:,2).*sang(:,3) - sang(:,1).*cang(:,3);
dcm(2,1,:) = sang(:,1).*sang(:,2);
dcm(2,2,:) = cang(:,2);
dcm(2,3,:) = cang(:,1).*sang(:,2);
dcm(3,1,:) = sang(:,1).*cang(:,3).*cang(:,2) + cang(:,1).*sang(:,3);
dcm(3,2,:) = -sang(:,2).*cang(:,3);
dcm(3,3,:) = cang(:,1).*cang(:,3).*cang(:,2) - sang(:,1).*sang(:,3);
case 'yzx'
% [ cy*cz, sz, -sy*cz]
% [ -sz*cx*cy+sy*sx, cz*cx, sy*cx*sz+cy*sx]
% [ cy*sx*sz+sy*cx, -cz*sx, -sy*sx*sz+cy*cx]
dcm(1,1,:) = cang(:,1).*cang(:,2);
dcm(1,2,:) = sang(:,2);
dcm(1,3,:) = -sang(:,1).*cang(:,2);
dcm(2,1,:) = -cang(:,3).*cang(:,1).*sang(:,2) + sang(:,3).*sang(:,1);
dcm(2,2,:) = cang(:,2).*cang(:,3);
dcm(2,3,:) = cang(:,3).*sang(:,1).*sang(:,2) + sang(:,3).*cang(:,1);
dcm(3,1,:) = sang(:,3).*cang(:,1).*sang(:,2) + cang(:,3).*sang(:,1);
dcm(3,2,:) = -sang(:,3).*cang(:,2);
dcm(3,3,:) = -sang(:,3).*sang(:,1).*sang(:,2) + cang(:,3).*cang(:,1);
case 'yzy'
% [ cy2*cz*cy-sy2*sy, cy2*sz, -cy2*cz*sy-sy2*cy]
% [ -cy*sz, cz, sy*sz]
% [ sy2*cz*cy+cy2*sy, sy2*sz, -sy2*cz*sy+cy2*cy]
dcm(1,1,:) = cang(:,1).*cang(:,3).*cang(:,2) - sang(:,1).*sang(:,3);
dcm(1,2,:) = sang(:,2).*cang(:,3);
dcm(1,3,:) = -sang(:,1).*cang(:,3).*cang(:,2) - cang(:,1).*sang(:,3);
dcm(2,1,:) = -cang(:,1).*sang(:,2);
dcm(2,2,:) = cang(:,2);
dcm(2,3,:) = sang(:,1).*sang(:,2);
dcm(3,1,:) = cang(:,1).*cang(:,2).*sang(:,3) + sang(:,1).*cang(:,3);
dcm(3,2,:) = sang(:,2).*sang(:,3);
dcm(3,3,:) = -sang(:,1).*cang(:,2).*sang(:,3) + cang(:,1).*cang(:,3);
case 'xyz'
% [ cy*cz, sz*cx+sy*sx*cz, sz*sx-sy*cx*cz]
% [ -cy*sz, cz*cx-sy*sx*sz, cz*sx+sy*cx*sz]
% [ sy, -cy*sx, cy*cx]
dcm(1,1,:) = cang(:,2).*cang(:,3);
dcm(1,2,:) = sang(:,1).*sang(:,2).*cang(:,3) + cang(:,1).*sang(:,3);
dcm(1,3,:) = -cang(:,1).*sang(:,2).*cang(:,3) + sang(:,1).*sang(:,3);
dcm(2,1,:) = -cang(:,2).*sang(:,3);
dcm(2,2,:) = -sang(:,1).*sang(:,2).*sang(:,3) + cang(:,1).*cang(:,3);
dcm(2,3,:) = cang(:,1).*sang(:,2).*sang(:,3) + sang(:,1).*cang(:,3);
dcm(3,1,:) = sang(:,2);
dcm(3,2,:) = -sang(:,1).*cang(:,2);
dcm(3,3,:) = cang(:,1).*cang(:,2);
case 'xyx'
% [ cy, sy*sx, -sy*cx]
% [ sx2*sy, cx2*cx-sx2*cy*sx, cx2*sx+sx2*cy*cx]
% [ cx2*sy, -sx2*cx-cx2*cy*sx, -sx2*sx+cx2*cy*cx]
dcm(1,1,:) = cang(:,2);
dcm(1,2,:) = sang(:,1).*sang(:,2);
dcm(1,3,:) = -cang(:,1).*sang(:,2);
dcm(2,1,:) = sang(:,2).*sang(:,3);
dcm(2,2,:) = -sang(:,1).*cang(:,2).*sang(:,3) + cang(:,1).*cang(:,3);
dcm(2,3,:) = cang(:,1).*cang(:,2).*sang(:,3) + sang(:,1).*cang(:,3);
dcm(3,1,:) = sang(:,2).*cang(:,3);
dcm(3,2,:) = -sang(:,1).*cang(:,3).*cang(:,2) - cang(:,1).*sang(:,3);
dcm(3,3,:) = cang(:,1).*cang(:,3).*cang(:,2) - sang(:,1).*sang(:,3);
case 'xzy'
% [ cy*cz, sz*cx*cy+sy*sx, cy*sx*sz-sy*cx]
% [ -sz, cz*cx, cz*sx]
% [ sy*cz, sy*cx*sz-cy*sx, sy*sx*sz+cy*cx]
dcm(1,1,:) = cang(:,3).*cang(:,2);
dcm(1,2,:) = cang(:,1).*cang(:,3).*sang(:,2) + sang(:,1).*sang(:,3);
dcm(1,3,:) = sang(:,1).*cang(:,3).*sang(:,2) - cang(:,1).*sang(:,3);
dcm(2,1,:) = -sang(:,2);
dcm(2,2,:) = cang(:,1).*cang(:,2);
dcm(2,3,:) = sang(:,1).*cang(:,2);
dcm(3,1,:) = sang(:,3).*cang(:,2);
dcm(3,2,:) = cang(:,1).*sang(:,2).*sang(:,3) - sang(:,1).*cang(:,3);
dcm(3,3,:) = sang(:,1).*sang(:,2).*sang(:,3) + cang(:,1).*cang(:,3);
case 'xzx'
% [ cz, sz*cx, sz*sx]
% [ -cx2*sz, cx2*cz*cx-sx2*sx, cx2*cz*sx+sx2*cx]
% [ sx2*sz, -sx2*cz*cx-cx2*sx, -sx2*cz*sx+cx2*cx]
dcm(1,1,:) = cang(:,2);
dcm(1,2,:) = cang(:,1).*sang(:,2);
dcm(1,3,:) = sang(:,1).*sang(:,2);
dcm(2,1,:) = -sang(:,2).*cang(:,3);
dcm(2,2,:) = cang(:,1).*cang(:,3).*cang(:,2) - sang(:,1).*sang(:,3);
dcm(2,3,:) = sang(:,1).*cang(:,3).*cang(:,2) + cang(:,1).*sang(:,3);
dcm(3,1,:) = sang(:,2).*sang(:,3);
dcm(3,2,:) = -cang(:,1).*cang(:,2).*sang(:,3) - sang(:,1).*cang(:,3);
dcm(3,3,:) = -sang(:,1).*cang(:,2).*sang(:,3) + cang(:,1).*cang(:,3);
otherwise
error('aero:angle2dcm:unknownrotation','Unknown rotation sequence, %s', type);
end