%完成选择一个空的坐标轴、、、、
% Create a 3-D plot
ax = axes('XLim', [-5 5], 'YLim', [-5 5], 'ZLim', [-5 5]);
xlabel(ax, 'X-axis');
ylabel(ax, 'Y-axis');
zlabel(ax, 'Z-axis');
% % Reverse the 2 axis directions to match the device coordinate system
%set(ax, 'Zdir', 'reverse');
%set(ax, 'Ydir', 'reverse');
grid on;
view(3);%看三维的视图
% Define the surface color
color = [0.3010 0.7450 0.9330];
%用caxa画图,然后读出坐标。这里用薄片,因此z轴坐标等于0,分别是每个点的x,y,z坐标
markdiy=[3 0 0;
1 1 0;
1 3 0;
-1 3 0;
-1 1 0;
-3 1 0;
-3 2 0;
-3.5 2 0;
-3.5 -2 0;
-3 -2 0;
-3 -1 0;
-1 -1 0;
-1 -3 0;
1 -3 0;
1 -1 0;
]
%构建薄片,分别是xi,yi,zi坐标markdiy(:,1),是markdiy的第一个数,就是x,坐标
p = patch(markdiy(:,1), markdiy(:,2),markdiy(:,3), 'red');
% 设置透明度
alpha(0.9)
%After the 3-D object is created, link the rotation matrix data acquired from the device to
%the plot by using hgtransform.
%将轴和3D物体的坐标系
tfObject = hgtransform('Parent', ax);
set(p, 'Parent', tfObject);
pause(5);
yaw=0;
pitch=0;
roll=0;
for i=1:10
%绕着z轴旋转
k=1;
yawtemp=30;
for k=1:10
yaw=yawtemp;
%生成旋转矩阵myRotationMatrix,3x3
q=quaternion([yaw pitch roll],'eulerd','zyx','frame');
myRotationMatrix=rotmat(q,'frame');
%生成要绑定的变换矩阵4x4
transformMatrix = eye(4);
%将myRotationMatrix放入到transformMatrix中,
%前面的上三角3x3矩阵是旋转矩阵
for i=1:3
for j=1:3
transformMatrix(i,j)=myRotationMatrix(i,j);
end
end
%将旋转矩阵用于物体,得到旋转
set(tfObject, 'Matrix', transformMatrix);
%变换下一次的角度
yawtemp=yawtemp+5;
drawnow
pause(0.2);
end
pause(5);
k=1;
pitchtemp=30;
for k=1:5
pitch=pitchtemp;
q=quaternion([yaw pitch roll],'eulerd','zyx','frame');
myRotationMatrix=rotmat(q,'frame');
transformMatrix = eye(4);
for i=1:3
for j=1:3
transformMatrix(i,j)=myRotationMatrix(i,j);
end
end
set(tfObject, 'Matrix', transformMatrix);
pitchtemp=pitchtemp+10;
drawnow
pause(0.5);
end
pause(3)
k=1;
rolltemp=30;
for k=1:5
roll=rolltemp;
q=quaternion([yaw pitch roll],'eulerd','zyx','frame');
myRotationMatrix=rotmat(q,'frame');
transformMatrix = eye(4);
for i=1:3
for j=1:3
transformMatrix(i,j)=myRotationMatrix(i,j);
end
end
set(tfObject, 'Matrix', transformMatrix);
rolltemp=rolltemp+10;
drawnow
pause(0.5);
end
pause(5);
end
2021-08-30matlab2019b,,旋转物体,旋转矩阵。下次结合IMU进行旋转
最新推荐文章于 2024-08-25 06:11:42 发布
该博客通过MATLAB实现了一个3D图形的创建与旋转。首先定义了一个3D坐标轴,然后绘制了一个红色的薄片,并设置了透明度。通过quaternion和rotmat函数,结合设备坐标系统,实现了对物体围绕三个轴的旋转效果,动态展示了旋转过程。
摘要由CSDN通过智能技术生成