Why does my 3 axes system coordinate orientation change x with y values?

123 views Asked by At

I am using Matlab and Euler Angles in order to reorient a 3axes coordinate system. Specifically,

Rz = [cos(ψ) sin(ψ) 0;-sin(ψ) cos(ψ) 0;0 0 1];
Ry = [cos(φ) 0 -sin(φ);0 1 0;sin(φ) 0 cos(φ)];
Rx = [1 0 0;0 cos(θ) -sin(θ);0 sin(θ) cos(θ)];
Rtotal = Rz*Ry*Rz

Then I loop through my old system coordinates (x,y,z) and make a vector coord_old. Then I get the reoriented system with (xn,yn,zn)

for i=1:size(num,1)
    coord_old = [x(i,1);y(i,1);z(i,1)];
    coord_new = Rtotal*coord_old;
    xn(i,1) = coord_new(1,1);
    yn(i,1) = coord_new(2,1);
    zn(i,1) = coord_new(3,1);
end

My issue is that when θ,φ,ψ≃0 then x->-y and y->x and when θ,φ≃0 and ψ=90 then x and y will not rotate! That means that when x,y should rotate they don't and when they shouldn't rotate they stay as they were!

--EDIT-- For example, when ψ=20.0871, φ=0.0580 and θ=0.0088 I get these results

See that x->y and y->x while z doesn't change at all

See that x->-y and y->x while z doesn't change at all! Any thoughts?

3

There are 3 answers

0
Matt On BEST ANSWER

Ok, I see two main problems here:

  1. Rtotal = Rz*Ry*Rz is probably not what you want since Rz is multiplied twice. I think you mean Rtotal = Rz*Ry*Rx.
  2. Your rotation matrix seems to be incorrect. Check this Wikipedia artice to get the correct signs.

Here a corrected rotation matrix:

Rz = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0;      0 0 1];
Ry = [cos(phi) 0 sin(phi);  0 1 0;                    -sin(phi) 0 cos(phi)];
Rx = [1 0 0;                0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Rtotal = Rz*Ry*Rx;

With this matrix I get the correct results:

x=1; y=2; z=3;
psi=0; phi=0; theta=0;
[xn,yn,zn] >> 1 2 3

x=1; y=2; z=3;
psi=90/180*pi; phi=0; theta=0;
[xn,yn,zn] >> -2 1 3

And here a full graphical example of a cube in 3d-space:

% Create cube (not in origin)
DVert = [0 0 0; 0 1 0; 1 1 0; 1 0 0 ; ... 
         0 0 1; 0 1 1; 1 1 1; 1 0 1];     
DSide = [1 2 3 4; 2 6 7 3; 4 3 7 8; ...
         1 5 8 4; 1 2 6 5; 5 6 7 8];
DCol  = [0 0 1; 0 0.33 1; 0 0.66 1; ...
         0 1 0.33; 0 1 0.66; 0 1 1];

% Rotation angles
psi   = 20  /180*pi;    % Z
phi   = 45  /180*pi;    % Y
theta = 0   /180*pi;    % X

% Rotation matrix
Rz = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0;      0 0 1];
Ry = [cos(phi) 0 sin(phi);  0 1 0;                    -sin(phi) 0 cos(phi)];
Rx = [1 0 0;                0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Rtotal = Rz*Ry*Rz;

% Apply rotation
DVertNew = Rtotal * DVert';

% Plot cubes
figure;
patch('Faces',DSide,'Vertices',DVert,'FaceColor','flat','FaceVertexCData',DCol); 
patch('Faces',DSide,'Vertices',DVertNew','FaceColor','flat','FaceVertexCData',DCol); 

% Customize view
grid on;
axis equal;
view(30,30);
2
Steffen On

When I use your code and insert 0 for all angles, I get Rtotal:

Rtotal = 
1 0 0
0 1 0
0 0 1

This is the identity matrix and will not change your values.

You have an error in your matrix multiplication. I think you should multiply: Rtotal*coord_old. I think you are missing the _old. depending on what is in you coordvariable, this may be the bug.

When I run:

for i=1:size(1,1)
    coord_old = [1;2;3];
    coord_new = Rtotal*coord_old;
    xn(i,1) = coord_new(1,1);
    yn(i,1) = coord_new(2,1);
    zn(i,1) = coord_new(3,1);
end

I get the correct result:

coord_new = 
1
2
3
5
manosbar On

Thank you both @Steffen and @Matt. Unfortunately, my reputation is not high enough to vote Up your answers!

The problem was not with Rtotal as @Matt correctly stated. It should be as it was Rz*Ry*Rx. However, both your ideas helped me test my code with simple examples (5 sets of coordinates and right hand rule), and realize where my (amateur) mistake was.

I had forgotten I had erased parts of codes where I was expressing my angles to degrees... I should be using sind & cosd instead of sin and cos.