# 1-2-1 or X-Y-X rotation matrix not supported

5 views (last 30 days)
SE on 20 Dec 2019
Commented: SE on 2 Jan 2020
Hi all,
I want to convert from euler to quaternion with 1-2-1 (respectively X-Y-X) rotation.
However, Matlab is not supporting this, is there a way to go around this?

James Tursa on 20 Dec 2019
According to the doc here:
The angle2quat supports the 'XYX' sequence. What functions are you using?

SE on 20 Dec 2019
I get this error message:
Error using robotics.internal.validation.validateEulerSequence (line 36)
The given Euler sequence XYX is not supported. Try one of the supported sequences: ZYX,
ZYZ, XYZ.
Error in eul2quat (line 37)
seq = robotics.internal.validation.validateEulerSequence(varargin{:});
Error in calculations (line 20)
quaternions = eul2quat(angles, rotation);
James Tursa on 20 Dec 2019
Can you use the angle2quat( ) function from the Aerospace Toolbox instead? A bit strange that eul2quat( ) has this limitation when other functions in MATLAB do not. Maybe should submit an enhancement request to MathWorks for this oversight. The quat2eul( ) function also has this limitation.
Another option is to use a utility like SpinCalc by John Fuller that can be found in the FEX:
But if you use SpinCalc, be aware that it will return quaternions in Vector-Scalar order. Whereas the MATLAB toolbox functions work with quaternions in Scalar-Vector order. So you would need to manually adjust the result of SpinCalc to put it into the MATLAB convention. E.g.,
>> angdeg = [10,20,30];
qsv =
0.925416578398323 0.336824088833465 0.171010071662834 -0.030153689607046
>> qvs = SpinCalc('EA121toQ',angdeg)
qvs =
0.336824088833465 0.171010071662834 -0.030153689607046 0.925416578398323
>> [qvs(4),qvs(1:3)]
ans =
0.925416578398323 0.336824088833465 0.171010071662834 -0.030153689607046
>> SpinCalc('QtoEA121',qvs,1e-10,0)
ans =
10.000000000000002 19.999999999999993 30.000000000000004
SE on 2 Jan 2020
Can you use the angle2quat( ) function from the Aerospace Toolbox instead?
--> this actually did the job, thanks!

Matt J on 20 Dec 2019
Edited: Matt J on 20 Dec 2019
You could compose the rotation matrix and use rotm2quat.
function quat = xyx2quat(eul)
Rx = @(x) [1, 0, 0;...
0, cos(x), -sin(x);...
0, sin(x), cos(x) ];
Ry= @(x) [cos(x), 0, sin(x); 0 1 0; -sin(x), 0, cos(x) ];
rotm=Rx(eul(3))*Ry(eul(2))*Rx(eul(1));
quat = rotm2quat(rotm);
end

#### 1 Comment

James Tursa on 20 Dec 2019
There is a subtle difference here. In the MATLAB toolbox code and in the SpinCalc code, the assumption is that the angles represent the relationship between coordinate frames, whereas using the code above assumes the angles represent a vector rotation within the same coordinate frame. This difference results in the "opposite" sense of rotation. E.g., using the method above:
>> angles = [10,20,30];
>> rotm=Rx(angles(3))*Ry(angles(2))*Rx(angles(1));
>> quat = rotm2quat(rotm)
quat =
0.925416578398323 0.336824088833465 0.171010071662834 0.030153689607046
You can see that the last element has the opposite sign as the results using the angle2quat( ) and SpinCalc( ) functions. To get the same result as these functions, where the angles represent a rotation of the coordinate frames instead of a vector rotation, you need to reverse the order of the rotations with this method. E.g.,
>> rotm=Rx(angles(1))*Ry(angles(2))*Rx(angles(3));
>> quat = rotm2quat(rotm)
quat =
0.925416578398323 0.336824088833465 0.171010071662834 -0.030153689607046
Now you can see that the last element has the same sign as the result from angle2quat( ) and SpinCalc( ).

Jim Riggs on 20 Dec 2019
Edited: Jim Riggs on 20 Dec 2019
I recently cataloged all of the permutations of the 2 and 3 axis planar rotations.
It is easy to write your own functions.
See the attached paper for the method equations, the section on "Repeated Angle Rotations".
Here is an exerpt from the paper:
See the paper for the definition of "direct" and "inverse" rotations. It effects the interpretation of the signs of the angles.
Once you calculate the Direction Cosine Matrix, you can convert it to a quaternion or a rotation vector.