0

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?

manosbar
  • 318
  • 2
  • 3
  • 15

3 Answers3

1

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);
Matt
  • 12,848
  • 2
  • 31
  • 53
0

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
Steffen
  • 2,381
  • 4
  • 20
  • 33
  • Thank you for your reply. Yes, *coord* should be *coord_old* but the problem still appears. Please check my edit on the question for more details. – manosbar Jun 19 '15 at 13:29
  • It still doesn't work. I have x=[1;1;1;1;1], y=[2;2;2;2;2] and z=[3;3;3;3;3;3] and when ψ=90, φ=0 and θ=0, i don't get y=[1;1;1;1;1] and x=[-2;-2;-2;-2;-2]. By they way `coord_old = [x(i,1);y(i,1);z(i,1)]` – manosbar Jun 19 '15 at 14:04
0

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.

manosbar
  • 318
  • 2
  • 3
  • 15
  • 1
    Either use `sind` or `cosd` or manually convert them like I did in the example. I especially pointed this out because I knew it could have been a source of error as well. You can accept my answer as accepted solution if you think I answered your question well enough. – Matt Jun 19 '15 at 15:03
  • If someone answers your question you usually click the "grey checkbox" next to their answer so it turns green. You don't need any points to do that – andrew Jun 19 '15 at 17:17
  • [newbie] Thank you for the info! [/newbie] – manosbar Jun 20 '15 at 09:30
  • @manosbar: Steffen actually did not solve the whole problem and you accepted his answer. I mentioned the mistake in the rotation matrix as well! It would be nice to reconsider your decision. I even took time to generate a graphical illustration with the cube. – Matt Jun 22 '15 at 14:04
  • @Matt yes, I had marked it as correct because it helped me evaluate your correct solution! My fault! Hope everything's ok now, thanks once again! – manosbar Jun 23 '15 at 16:54