02 Rotation Matrices
02 Rotation Matrices
Rotation Matrices
A problem we'll be looking at in two days is trying to determine where the tip of a robot is. To do this, we use
reference frames: an {X, Y, Z} axis tied to each link of the robot. As the robot moves, often times the angle of
each frame changes. Likewise, in order to go from one link to the next, a rotation and translation is required.
This lecture looks at rotation matrices.
In the {x, y, z} plane, you can rotate about any or all axis. For simplicity, we will look at three separate rotations:
Rotating about the X axis
Rotating about the Y axis, and
Rotating about the Z axis.
In addition, we will be referring to a point (P) relative to a specific reference frame. P1, for example, refers to
the {x, y, z} coordinate of point P relative to reference frame 1.
Solution:
Y1
P 1 = T 10 ⋅ P 0
x1 1 0 0 0 x0 Q
0 cos θ sin θ 0
y1 = y0 Y0
z1 0 −sin θ cos θ 0 z0
1 0 0 0 1 1
X0
X1
P 1 = T 10 ⋅ P 0
x1 cos θ 0 −sin θ 0 x0
0
y1 = 1 0 0 y0
Y0
z1 sin θ 0 cos θ 0 z0
Y1
1 0 0 0 1 1
X0
X1
P 1 = T 10 ⋅ P 0
x1 cos θ sin θ 0 0 x0
−sin θ cos θ 0 0
y1 = y0
Y1
z1 0 0 1 0 z0
1 0 0 0 1 1 Y0
X0
X1
1
2
P 0 =
3
1
Where is it at if you
Rotate 90 degrees about the X axis
Rotate 90 degrees about the X axis
Rotate 90 degrees about the X axis
Solution: For convenience, define c and s to be the sine and cosine of 90 degrees:
c = cos(90*pi/180);
s = sin(90*pi/180);
Rx = [1,0,0,0;0,c,s,0;0,-s,c,0;0,0,0,1];
Ry = [c,0,-s,0;0,1,0,0;s,0,c,0;0,0,0,1];
Rz = [c,s,0,0;-s,c,0,0;0,0,1,0;0,0,0,1];
P0 = [1;2;3;1]
Rx * P0
Ry * P0
Rz * P0
cos 20 0 0 sin 20 0 0 1
0 2
P 1 = T 10 ⋅ P 0 =
0 1 0
−sin 20 0 0 cos 20 0 0 3
0 0 0 1 1
cos 30 0 −sin 30 0 0 0
sin 30 0 cos 30 0 0 0
P 2 = T 21 ⋅ P 1 = ⋅ P1
0 0 1 0
0 0 0 1
or in MATLAB
c = cos(20*pi/180);
s = sin(20*pi/180);
Rx = [1,0,0,0;0,c,s,0;0,-s,c,0;0,0,0,1];
Ry = [c,0,-s,0;0,1,0,0;s,0,c,0;0,0,0,1];
Rz = [c,s,0,0;-s,c,0,0;0,0,1,0;0,0,0,1];
T10 = Ry;
c = cos(30*pi/180);
s = sin(30*pi/180);
Rx = [1,0,0,0;0,c,s,0;0,-s,c,0;0,0,0,1];
Ry = [c,0,-s,0;0,1,0,0;s,0,c,0;0,0,0,1];
Rz = [c,s,0,0;-s,c,0,0;0,0,1,0;0,0,0,1];
T21 = Rz;
P0 = [1;2;3;1]
P1 = T10 * P0
P2 = T21 * P1
1
2
3
1
The point relative to the first reference frame (rotated 20 degrees about the y axis)
P1 =
-0.0864
2.0000
3.1611
1.0000
The point relative to the second reference frame (finally rotated 30 degrees about the z axis)
P2 =
0.9252
1.7752
3.1611
1.0000
0.9252
In the rotated coordinate system, the point is located at P 2 = 1.7752 .
3.1611
Displaying a 3D Object
To illustrate the use of rotation matrices, let's draw an arrow and then
Rotate the camera about the X, Y, and Z axis, then
Rotate the arrow about the X, Y, and Z axis
Define the arrow by eight points:
x 0 0 0 0 0 0 0 0
Arrow = y = −0.5 0.5 0.5 1 0 −1 −0.5 −0.5
z 0 0 0.5 0.5 1 0.5 0.5 0
1.5
0.5
0
-1.5 -1 -0.5 0 0.5 1 1.5 Y
Since the screen is 2-dimensional, project the arrow on the YZ plane. An m-file to display a set of points (passed
in DATA) along with a transformation matrix is as follows:
function Display3D(DATA, T)
% scaling factor
s = T(4,4);
DATA = T*DATA;
T0 = T;
T0(1,4) = 0;
T0(2,4) = 0;
T0(3,4) = 0;
% transform
X0 = T0*X;
Y0 = T0*Y;
Z0 = T0*Z;
Origin = T0*O;
Tx = s*[0,1,0,0];
Ty = s*[0,0,1,0];
plot(Tx*DATA,Ty*DATA, 'b')
end
X = [0,0,0,0,0,0,0,0]';
Y = [-0.5,0.5,0.5,1,0,-1,-0.5,-0.5]';
Z = [0,0,0.5,0.5,1,0.5,0.5,0]';
ARROW = [X,Y,Z,0*X+1]'
0 0 0 0 0 0 0 0
-0.5000 0.5000 0.5000 1.0000 0 -1.0000 -0.5000 -0.5000
0 0 0.5000 0.5000 1.0000 0.5000 0.5000 0
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
T = eye(4,4);
Display3D(ARROW, T);
c = cos(5*pi/180);
s = sin(5*pi/180);
Rx = [1,0,0,0;0,c,s,0;0,-s,c,0;0,0,0,1];
T = eye(4,4);
for i=1:1000
T = Rx*T;
Display3D(ARROW, T);
pause(0.01);
end
Fixed Arrow, Camera Rotated 0, -30, and -60 degrees about the X axis
Note that the camera spins counter-clockwise (positive direction). This makes it look like the arrow is spinning
counter-clockwise
Ry = [c,0,-s,0;0,1,0,0;s,0,c,0;0,0,0,1];
0.9962 0 0.0872 0
0 1.0000 0 0
-0.0872 0 0.9962 0
0 0 0 1.0000
T = eye(4,4);
for i=1:1000
T = Ry*T;
Display3D(ARROW, T);
pause(0.01);
end
Fixed Arrow, Camera Rotated 0, 30, and 60 degrees about the Y axis
Rz = [c,-s,0,0;s,c,0,0;0,0,1,0;0,0,0,1]
0.9962 -0.0872 0 0
0.0872 0.9962 0 0
0 0 1.0000 0
0 0 0 1.0000
T = eye(4,4);
for i=1:1000
T = Tz*T;
Display3D(ARROW, T);
pause(0.01);
end
Fixed Arrow, Camera Rotated 0, 30, and 60 degrees about the Z axis
3D Perspective:
Move the camera 45 degrees about the Z axis then 25 degrees about the Y axis
c = cos(-25*pi/180);
s = sin(-25*pi/180);
Ry = [c,0,-s,0;0,1,0,0;s,0,c,0;0,0,0,1];
c = cos(-45*pi/180);
s = sin(-45*pi/180);
Rz = [c,-s,0,0;s,c,0,0;0,0,1,0;0,0,0,1]
Tdisp = Ry*Rz
Display3D(ARROW,Tdisp);
Viewing the arrow on the YX plane from an angle of Rotate Z 45 deg then Rotate Y 45 deg
T = eye(4,4);
for i=1:1000
T = T01*T;
Display3D(T*ARROW, Tdisp);
pause(0.01);
end
P1
Relative to the zero reference frame, the points are:
P 0 = T 01 P 1
P 0 = (T 10 ) −1 P 1
T = eye(4,4);
for i=1:1000
T = T01*T;
Display3D(T*ARROW, Tdisp);
pause(0.01);
end
T = eye(4,4);
for i=1:1000
T = T01*T;
Display3D(T*ARROW, Tdisp);
pause(0.01);
end