Matlab Robotics Toolbox by Peter Corke Examples
Matlab Robotics Toolbox by Peter Corke Examples
Homework Set: 02
Ozan Gülbaş
1. Use MATLAB Robotics Toolbox and write functions for Theta (degree) = 25, 45,
65, 82:
(a) ROTX(theta). This function will give a 4x4 homogenous transformation matrix
that will rotate “theta” about the X-axis.
function output_matrix = ROTX(theta)
output_matrix = trotx(theta, "deg");
end
(b) ROTY(theta). This function will give a 4x4 homogenous transformation matrix
that will rotate “theta” about the Y-axis
function output_matrix = ROTY(theta)
output_matrix = troty(theta, "deg");
end
(c) ROTZ(theta). This function will give a 4x4 homogenous transformation matrix
that will rotate “theta” about the Z-axis
function output_matrix = ROTZ(theta)
output_matrix = trotz(theta, "deg");
end
(d) TRANS(x,y,z). This function will give a 4x4 homogenous transformation ma-
trix that will translate “x” units over the X-axis, “y” units over the Y-axis, and
“z” units over the Z-axis
function output_matrix = TRANS(x, y, z)
output_matrix = transl(x, y, z);
end
.
The code which displays ROTX,ROTY and ROTZ for given Thetas:
CENG483 Homework Set: 02 Ozan Gülbaş
for i = 1 : length(thetas)
theta = thetas(i);
rot_matrix_X = ROTX(theta);
rot_matrix_Y = ROTY(theta);
rot_matrix_Z = ROTZ(theta);
fprintf("\n=======================\n")
end
=======================
2
CENG483 Homework Set: 02 Ozan Gülbaş
0 0.7071 0.7071 0
0 0 0 1.0000
=======================
=======================
3
CENG483 Homework Set: 02 Ozan Gülbaş
=======================
Output:
Resulting matrix N:
0.5000 0 0.8660 2.0000
0 1.0000 0 3.0000
-0.8660 0 0.5000 -2.0000
0 0 0 1.0000
(b) Find a final transformation rotates a frame O 60 degrees over its translated Y
axis and then translates the rotated frame (2,-2, 2).
Code:
O = ROTY(60);
O_trans = O * TRANS(2, -2, 2);
fprintf("Resulting matrix O:\n")
disp(O_trans)
Output:
4
CENG483 Homework Set: 02 Ozan Gülbaş
Resulting matrix O:
0.5000 0 0.8660 2.7321
0 1.0000 0 -2.0000
-0.8660 0 0.5000 -0.7321
0 0 0 1.0000
5
CENG483 Homework Set: 02 Ozan Gülbaş
3. Create a 2D rotation matrix. Visualize the rotation using trplot2. Use it to trans-
form a vector. Invert it and multiply it by the original matrix; what is the result?
Reverse the order of multiplication; what is the result? What is the determinant of
the matrix and its inverse?
Code:
Output:
Result 1:
6
CENG483 Homework Set: 02 Ozan Gülbaş
1.0000 0.0000
0 1.0000
Result 2:
1.0000 0.0000
0 1.0000
Determinant of T:
1
Determinant of T inverse:
1
7
CENG483 Homework Set: 02 Ozan Gülbaş
% Transform a vector
V = [1; 1; 1];
V_trans = R_y * V;
fprintf("Transform of vector V with R_y:\n")
disp(V_trans)
Output:
8
CENG483 Homework Set: 02 Ozan Gülbaş
1.0000
-0.6476
Result 1:
1.0000 0 -0.0000
0 1.0000 0
0 0 1.0000
Result 2:
1.0000 0 -0.0000
0 1.0000 0
0 0 1.0000
Determinant of R_y:
1
9
CENG483 Homework Set: 02 Ozan Gülbaş
base_pose = roty(0);
% First sequence
rot1 = ROTX(90);
rot2 = rot1 * ROTY(90);
% Second sequence
rot3 = ROTY(90);
rot4 = rot3 * ROTX(90);
figure(3)
subplot(2, 3, 1)
trplot(base_pose)
xaxis(-0.2, 1.2);yaxis(-0.2, 1.2); zlim([-0.2, 1.2])
view(10, 10)
subplot(2, 3, 2)
trplot(rot1)
xaxis(-0.2, 1.2);yaxis(-1.2, 0.2); zlim([-0.2, 1.2])
view(10, 10)
subplot(2, 3, 3)
trplot(rot2)
xaxis(-0.2, 1.2);yaxis(-0.2, 1.2); zlim([-0.2, 1.2])
view(10, 10)
subplot(2, 3, 4)
trplot(base_pose)
xaxis(-0.2, 1.2);yaxis(-0.2, 1.2); zlim([-0.2, 1.2])
view(10, 10)
subplot(2, 3, 5)
trplot(rot3)
xaxis(-0.2, 1.2);yaxis(-0.2, 1.2); zlim([-1.2, 0.2])
view(10, 10)
subplot(2, 3, 6)
trplot(rot4)
xaxis(-0.2, 1.2);yaxis(-1.2, 0.2); zlim([-1.2, 0.2])
view(10, 10)
10
CENG483 Homework Set: 02 Ozan Gülbaş
11
CENG483 Homework Set: 02 Ozan Gülbaş
6. For the 3-dimensional rotation about the vector [2, 3, 4] by 0.5 rad compute an SO(3)
rotation matrix using: the matrix exponential functions expm and trexp, Ro-
drigues’ rotation formula (code this yourself ), and the Toolbox function angvec2tr.
Compute the equivalent unit quaternion.
First, the Rodrigues’s forluma implemented like this:
skew_matrix = skew(V) ;
Code:
% Define vector
V = [2; 3; 4];
V_normalized = V / norm(V);
12
CENG483 Homework Set: 02 Ozan Gülbaş
Output:
s: 0.9689
v: [0.0919 0.1378 0.1838]
13