Multiple Rotation with Quaternions

7 views (last 30 days)
Tim Krzyzanowski
Tim Krzyzanowski on 23 Oct 2023
Commented: Tim Krzyzanowski on 25 Oct 2023
Hi,
i want to make a multiple rotation with Quaternions. In the first rotation i have a vector pointing to the Z-Axis. Here i wanted to have the option to make a intrinsic or extrinsic rotation. In the second rotation i wanted to rotate around the new or lets say "local" coordinate system. But also with 2 options. First option would be to turn once around z and then in the next step around the new y of the local, and in the second option it would be a rotation around the local z and local y. I think the code explains the problem the easiest way.
The first rotation works great and gives me the same results i had with rotation matrices.
In the second rotation the intrinsic rotation also works. But i dont know why a single rotation like:
quat_rot=quat_rot*quaternion([0,90,90],'eulerd','zyx','frame');
Does not work but at least it works like implemented in the bottom.
The main problem is the extrinsic rotation in the second else which starts at line 48. Here the vector_correct variable shows the expected result which vector_2 should be.
clc
clear all
x=[1,0,0];
y=[0,1,0];
z=[0,0,1];
center = [0; 0; 0];
type_of_rot1='extrinsic';
%type_of_rot1='extrinsic'
type_of_rot2='extrinsic';
%type_of_rot2='extrinsic'
rotation1=[90,90,90];
rotation2=[0,0,90];
rotation3=[0,90,0];
% Perform 1st rotation (vector is fixed to the Z-Axis of the lets say
% "Local' system. This is why i used at intrinsic the Mix of frame
% quaternion and rotatepoint function.
if strcmp(type_of_rot1, 'intrinsic')
% Rotate around global x then around y' and then around z'
quat_rot=quaternion(rotation1,'eulerd','XYZ','frame');
vector=round(rotatepoint(quat_rot,[x;y;z]),10);
else
% rotate around global x, global y and global z
quat_rot=quaternion(rotation1,'eulerd','XYZ','point');
vector=round(rotatepoint(quat_rot,[x;y;z]),10);
% get the same as frame rotation
eulerAnglesDegrees = eulerd(quat_rot,'XYZ','frame');
quat_rot=quaternion(eulerAnglesDegrees,'eulerd','XYZ','frame');
end
local.x_mag=round(rotatepoint(quat_rot,[1,0,0]),10)';
local.y_mag=round(rotatepoint(quat_rot,[0,1,0]),10)';
local.z_mag=round(rotatepoint(quat_rot,[0,0,1]),10)';
%Perform 2nd rotation
if strcmp(type_of_rot2, 'intrinsic')
%rotate around z' then around y''
quat_rot=quat_rot*quaternion(rotation2,'eulerd','xyz','frame')*quaternion(rotation3,'eulerd','xyz','frame');
vector_2=rotatepoint(quat_rot,[x;y;z]);
else
%% Here is the error part
%This is how it should work
vector_correct = fun_rot_n(vector'/norm(vector'), local.z_mag, center, rotation2(3));
vector_correct = fun_rot_n(vector_correct/norm(vector_correct), local.y_mag, center, rotation3(2));
%rotate around z' and around y'
quat_rot=quat_rot*quaternion(rotation2,'eulerd','xyz','point');
vector_2=round(rotatepoint(quat_rot,[x;y;z]),10);
end
function [out] = fun_rot_n(point, n, center_rotation, alpha)
%ROT_N rotation around a axi with the vektor[n1; n2; n3;]
out = [ n(1,1)^2*(1-cosd(alpha))+cosd(alpha), n(1,1)*n(2,1)*(1-cosd(alpha))-n(3,1)*sind(alpha), n(1,1)*n(3,1)*(1-cosd(alpha))+n(2,1)*sind(alpha); ...
n(2,1)*n(1,1)*(1-cosd(alpha))+n(3,1)*sind(alpha), n(2,1)^2*(1-cosd(alpha))+cosd(alpha), n(2,1)*n(3,1)*(1-cosd(alpha))-n(1,1)*sind(alpha); ...
n(3,1)*n(1,1)*(1-cosd(alpha))-n(2,1)*sind(alpha), n(3,1)*n(2,1)*(1-cosd(alpha))+n(1,1)*sind(alpha), n(3,1)^2*(1-cosd(alpha))+cosd(alpha)]*(point - center_rotation) + center_rotation;
end
  2 Comments
James Tursa
James Tursa on 25 Oct 2023
Edited: James Tursa on 25 Oct 2023
Please post the rotation matrix version of the code so we can compare what you are comparing to and make comments about what you expected not being the same.
If you don't want to use toolboxes then you would need to supply the Euler-To-Quaternion and Quaternion Multiply functionality yourself.
Tim Krzyzanowski
Tim Krzyzanowski on 25 Oct 2023
Thank you for your reply. I changed the code a little bit at the Top. Also did i solve 1 part of the problem but the last else is not working like it should but you can see now the problem solved with the rotation matrix vs the quaternions.

Sign in to comment.

Answers (0)

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!