Clear Filters
Clear Filters

How can I rotate the following cube in real time?

4 views (last 30 days)
Hello to everyone,
I´m reading some data of an IMU in a microcontroller and I send "roll, pitch and "yaw" information from the angles to Matlab by serial port. I have created that multicolor cube in order to know better which part of the sensor is each face of the cube. I would like to create a function that receives roll, pitch and yaw angle values and then rotates the cube in real time.
patch([0, 4, 4, 0], [0, 0, 3, 3], [0, 0, 0, 0], 'blue') %Cara de la parte de abajo de la imu
patch([0, 4, 4, 0], [0, 0, 3, 3], [1, 1, 1, 1], 'red') %Cara de la parte de arriba
patch([0, 0, 0, 0], [0, 0, 3, 3], [0, 1, 1, 0], 'green') %Borde corto nº1 del sensor
patch([0, 4, 4, 0], [0, 0, 0, 0], [0, 0, 1, 1], 'yellow') %Borde largo nº1 del sensor
patch([4, 4, 4, 4], [0, 3, 3, 0], [0, 0, 1, 1], 'magenta') %Borde largo nº2 del sensor
patch([4, 4, 0, 0], [3, 3, 3, 3], [0, 1, 1, 0], 'cyan') %Borde corto nº2 del sensor
axis square
Thank you in advance,

Accepted Answer

KSSV
KSSV on 4 Apr 2019
X = [0, 4, 4, 0
0, 4, 4, 0
0, 0, 0, 0
0, 4, 4, 0
4, 4, 4, 4
4, 4, 0, 0] ;
Y = [0, 0, 3, 3
0, 0, 3, 3
0, 0, 3, 3
0, 0, 0, 0
0, 3, 3, 0
3, 3, 3, 3] ;
Z = [0, 0, 0, 0
1, 1, 1, 1
0, 1, 1, 0
0, 0, 1, 1
0, 0, 1, 1
0, 1, 1, 0] ;
C = {'blue' ;'red' ; 'green' ; 'yellow' ; 'magenta' ; 'cyan'};
figure
hold on
for i = 1:6
patch(X(i,:), Y(i,:), Z(i,:),C{i}) ;
end
axis square
view(3)
%% Rotate
th = linspace(0,2*pi) ;
for i = 1:length(th)
R = [1 0 0 ;0 cos(th(i)) -sin(th(i)) ; 0 sin(th(i)) cos(th(i))] ;
T = [X(:) Y(:) Z(:)]*R ;
Xi = reshape(T(:,1),[],4) ;
Yi = reshape(T(:,2),[],4) ;
Zi = reshape(T(:,3),[],4) ;
figure(1)
hold on
for j = 1:6
patch(Xi(j,:), Yi(j,:), Zi(j,:),C{j}) ;
end
axis square
view(3)
drawnow
hold off
clf
end
Can be improved a lot.
  1 Comment
Aitor Burdaspar
Aitor Burdaspar on 4 Apr 2019
Thank you very much for answering so quickly. Anyway, it doesn´t respond fine to the signal I send from the serial port. I have done this main program which reads the data from the serial port:
% I initialize the serial port where I will work. I delete previous data and declare the port and data transfer speed
delete(instrfind({'Port'}, {'COM3'}));
arduino = serial('COM3');
arduino.Baudrate = 9600;
fopen(arduino); %Open serial port
warning('off','MATLAB:serial:fscanf:unsuccesfulRead');
figure('Name', 'Serial communication: MATLAB + Arduino = CUBE 3D');
hold on
while 1
x = fscanf(arduino, '%f');
y = fscanf(arduino, '%f');
z = fscanf(arduino, '%f');
plotCube3Angles_2(x, y, z) %Function in order to rotate
drawnow limitrate %Create a 3d figure and plot in real time
clf %Clean figure and update it
end
Then, the "plotCube3Angles_2(x, y, z)" function is the following one:
function plotCube3Angles_2(roll, pitch, yaw)
%I define the vertex of the rectangle
vertex_matrix = [0 0 0
4 0 0
4 3 0
0 3 0
0 0 1
4 0 1
4 3 1
0 3 1];
%I define the faces of the rectangle
faces_matrix = [1 2 6 5
2 3 7 6
3 4 8 7
4 1 5 8
1 2 3 4
5 6 7 8];
subplot(1,3,1)
axis([0 4 0 3 0 1]); %axis limits
axis equal off; %serves to prevent Matlab from distorting the image to fit the window
cube = patch('Vertices',vertex_matrix,'Faces',faces_matrix,'FaceColor', 'blue'); %Create a 3d blue rectangle of the previous characteristics
rotate(cube, [1,0,0], roll); %rotate => 'object', 'rotation direction (of axis)', 'numeric value for rotation'
rotate(cube,[0,1,0], pitch);
rotate(cube, [0, 0, 1], yaw);
view(0,0); %view (0,0) places the camera completely horizontal so as not to disturb the perspective
end
In this way, it works fine. But, as you can see, in the way I create the rectangle, I can only choose the colour for the entire object. I would like to give different colours for each face.
It might be a silly thing, but I've tried it in different ways and I have not got it.
I would be grateful if someone answered me.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!