Skip to content

Commit db73b24

Browse files
committed
Fix corner cases for pitch angles, avoid divide by zero (or very small)
Note this in the symbolic code that generates these solutions
1 parent e3b0d5b commit db73b24

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

symbolic/rpygen.mlx

39 Bytes
Binary file not shown.

tr2rpy.m

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
% - Toolbox rel 8-9 has XYZ angle sequence as default.
2929
% - 'arm', 'vehicle', 'camera' are synonyms for 'xyz', 'zyx' and 'yxz'
3030
% respectively.
31+
% - these solutions are generated by symbolic/rpygen.mlx
3132
%
3233
% See also rpy2tr, tr2eul.
3334

@@ -87,7 +88,17 @@
8788
rpy(1) = -atan2(R(1,2), R(1,1));
8889
rpy(3) = -atan2(R(2,3), R(3,3));
8990

90-
rpy(2) = atan(R(1,3)*cos(rpy(1))/R(1,1));
91+
[~,k] = max(abs( [R(1,1) R(1,2) R(2,3) R(3,3)] ));
92+
switch k
93+
case 1
94+
rpy(2) = atan(R(1,3)*cos(rpy(1))/R(1,1));
95+
case 2
96+
rpy(2) = -atan(R(1,3)*sin(rpy(1))/R(1,2));
97+
case 3
98+
rpy(2) = -atan(R(1,3)*sin(rpy(3))/R(2,3));
99+
case 4
100+
rpy(2) = atan(R(1,3)*cos(rpy(3))/R(3,3));
101+
end
91102
end
92103

93104
case {'zyx', 'vehicle'}
@@ -106,8 +117,18 @@
106117
else
107118
rpy(1) = atan2(R(3,2), R(3,3)); % R
108119
rpy(3) = atan2(R(2,1), R(1,1)); % Y
109-
110-
rpy(2) = -atan(R(3,1)*cos(rpy(1))/R(3,3));
120+
121+
[~,k] = max(abs( [R(1,1) R(2,1) R(3,2) R(3,3)] ));
122+
switch k
123+
case 1
124+
rpy(2) = -atan(R(3,1)*cos(rpy(3))/R(1,1));
125+
case 2
126+
rpy(2) = -atan(R(3,1)*sin(rpy(3))/R(2,1));
127+
case 3
128+
rpy(2) = -atan(R(3,1)*sin(rpy(1))/R(3,2));
129+
case 4
130+
rpy(2) = -atan(R(3,1)*cos(rpy(1))/R(3,3));
131+
end
111132
end
112133

113134
case {'yxz', 'camera'}
@@ -127,7 +148,17 @@
127148
rpy(1) = atan2(R(2,1), R(2,2));
128149
rpy(3) = atan2(R(1,3), R(3,3));
129150

130-
rpy(2) = -atan(cos(rpy(1))*R(2,3)/R(2,2));
151+
[~,k] = max(abs( [R(2,1) R(2,2) R(1,3) R(3,3)] ));
152+
switch k
153+
case 1
154+
rpy(2) = -atan(R(2,3)*sin(rpy(1))/R(2,1));
155+
case 2
156+
rpy(2) = -atan(R(2,3)*cos(rpy(1))/R(2,2));
157+
case 3
158+
rpy(2) = -atan(R(2,3)*sin(rpy(3))/R(1,3));
159+
case 4
160+
rpy(2) = -atan(R(2,3)*cos(rpy(3))/R(3,3));
161+
end
131162
end
132163

133164
end

0 commit comments

Comments
 (0)