How can I do the same simulation as this (with euler angles), but using quaternions?
5 views (last 30 days)
Show older comments
I´m using BNO055 IMU, and I have read all the data (euler angles, quaternions...). This data is send by serial from the micro to Matlab. I have done a short program in order to show the orientation of a cube in 3D, and it rotates depending on the obtained Euler angle values in real time. This is the main program:
%% Initialize the serial port I will work with... Remove previous data and define the COM and baud rate
delete(instrfind({'Port'}, {'COM3'}));
arduino = serial('COM3');
arduino.Baudrate = 9600;
%%puerto_serial = serial('COM3', 'BaudRate', 9600, 'Terminator', 'CR/LF');
fopen(arduino); %%Open serial port
warning('off','MATLAB:serial:fscanf:unsuccesfulRead');
%figure('Name', 'Serial communication: MATLAB + Arduino = CUBE 3D');
while 1
x = fscanf(arduino, '%f');
y = fscanf(arduino, '%f');
z = fscanf(arduino, '%f');
%quat(1) = fscanf(arduino, '%f'); %w
%quat(2) = fscanf(arduino, '%f'); %x
%quat(3) = fscanf(arduino, '%f'); %y
%quat(4) = fscanf(arduino, '%f'); %z
plotCube3Angles_2(x, y, z)
drawnow limitrate
clf %%Clean image for updating it
end
And this is the function for creating, rotating and ploting the cube with Euler angles.
function plotCube3Angles_2(roll, pitch, yaw)
Dim_X = 4; %size of the cube in X
Dim_Y = 3; %size of the cube in Y
Dim_Z = 1; %size in Z axes
vertex_matrix = [0 0 0;
1 0 0;
1 1 0;
0 1 0;
0 0 1;
1 0 1;
1 1 1;
0 1 1];
% 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];
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];
CubeCenter_Coord = [Dim_X/2 Dim_Y/2 Dim_Z/2];
%origin = CubeCenter;
origin = [0 0 0]; %offset
CubeParameters = [vertex_matrix(:,1)*Dim_X+origin(1),vertex_matrix(:,2)*Dim_Y+origin(2),vertex_matrix(:,3)*Dim_Z+origin(3)];
axis equal;
cube = patch('Vertices',CubeParameters,'Faces',faces_matrix,'FaceColor', 'blue');
rotate(cube, [1,0,0], roll); %rotate cube, axis direction, angle value
rotate(cube,[0,1,0], pitch);
rotate(cube, [0, 0, 1], -yaw);
view(3); %3D
end
This works fine. Anyway, euler angles have some singularities and it is better to use quaternions for avoiding that issues on the rotating. I have seen there is a function called "rotateframe" that can rotate a frame with the quaternion... but I have not achieved anything. Does anyone know how should I do?
Thanks,
2 Comments
James Tursa
on 8 Apr 2019
Is there a problem with the Euler Angles that are in the data set? Or are you just trying to learn how to use quaternions? Even if the orientation is a singular one with respect to the Euler Angle set you are using, as long as the sender is doing the calculations correctly you should still be able to use them to plot the cube orientation.
Answers (0)
See Also
Categories
Find more on Quaternion Math in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!